Skip to content

Fix index misalignment in char_wb ngram padding - #8429

Open
adityaanikam wants to merge 5 commits into
NVIDIA:mainfrom
adityaanikam:tfidf-char-wb-ngram-fix-8416
Open

Fix index misalignment in char_wb ngram padding#8429
adityaanikam wants to merge 5 commits into
NVIDIA:mainfrom
adityaanikam:tfidf-char-wb-ngram-fix-8416

Conversation

@adityaanikam

Copy link
Copy Markdown

Fixes #8416

Root cause

get_char_ngrams() pads each token with self.delimiter on both sides before generating character ngrams:

padding = Series(self.delimiter).repeat(len(tokens))

@adityaanikam
adityaanikam requested a review from a team as a code owner July 31, 2026 15:20
@adityaanikam
adityaanikam requested a review from jcrist July 31, 2026 15:20
@copy-pr-bot

copy-pr-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Cython / Python Cython or Python issue label Jul 31, 2026
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: d6b4779b-19d0-486d-ad39-04458c74c1c7

📥 Commits

Reviewing files that changed from the base of the PR and between ed13db7 and 1394360.

📒 Files selected for processing (2)
  • python/cuml/cuml/feature_extraction/_vectorizers.py
  • python/cuml/tests/test_text_feature_extraction.py

Included review availability: Your plan provides up to 12 included reviews per hour; 10 remain after this review.


📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added get_feature_names_out() for retrieving vectorizer feature names in a NumPy array.
    • Updated text vectorizer behavior to align with the current feature-name API.
  • Bug Fixes

    • Fixed character n-gram extraction to preserve correct positional alignment when constructing padded tokens.
    • Improved consistency of generated character features for inputs with non-sequential indexes.
    • Ensured character-boundary n-grams match expected feature names and results across supported n-gram lengths.
  • Deprecations

    • Existing get_feature_names() calls now issue a future deprecation warning.

Walkthrough

CountVectorizer now supports get_feature_names_out() and preserves deprecated API behavior through a warning mixin. Character n-gram padding resets indexes before concatenation. Tests cover current feature-name access and char_wb TF-IDF output against scikit-learn.

Changes

Vectorizer API and character n-gram behavior

Layer / File(s) Summary
Feature-name API compatibility
python/cuml/cuml/feature_extraction/_vectorizers.py, python/cuml/tests/test_text_feature_extraction.py
CountVectorizer now provides get_feature_names_out() and inherits deprecated API support. Tests use NumPy array comparisons and verify FutureWarning behavior.
Character n-gram index alignment
python/cuml/cuml/feature_extraction/_vectorizers.py
get_char_ngrams resets token and padding indexes before both concatenation operations.
Regression validation
python/cuml/tests/test_text_feature_extraction.py
Tests compare char_wb TF-IDF matrices and feature names with scikit-learn for n-gram lengths 2 through 6.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 13943

This localized fix adjusts character n-gram padding alignment and includes focused test changes; no actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: jcrist

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary fix for char_wb n-gram index misalignment.
Description check ✅ Passed The description explains the linked bug and identifies the character n-gram padding root cause.
Linked Issues check ✅ Passed The code resets padding indexes before concatenation and updates tests, addressing issue #8416 requirements.
Out of Scope Changes check ✅ Passed The API and test updates support the vectorizer fix and current feature-name API without unrelated changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
python/cuml/cuml/feature_extraction/_vectorizers.py (1)

216-224: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add an enabled char_wb Tfidf regression test.

In python/cuml/tests/test_text_feature_extraction.py, test TfidfVectorizer.fit_transform(analyzer="char_wb", ngram_range=(2, 6)). Compare the sparse output and feature names with scikit-learn using get_feature_names_out().

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@python/cuml/cuml/feature_extraction/_vectorizers.py` around lines 216 - 224,
Add an enabled regression test in test_text_feature_extraction.py covering
TfidfVectorizer with analyzer="char_wb" and ngram_range=(2, 6). Fit and
transform the same input with cuML and scikit-learn, then compare sparse outputs
and feature names using get_feature_names_out().
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@python/cuml/cuml/feature_extraction/_vectorizers.py`:
- Around line 216-224: Add an enabled regression test in
test_text_feature_extraction.py covering TfidfVectorizer with analyzer="char_wb"
and ngram_range=(2, 6). Fit and transform the same input with cuML and
scikit-learn, then compare sparse outputs and feature names using
get_feature_names_out().

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 80ca255e-7d3d-41f0-ab51-594df4f04c33

📥 Commits

Reviewing files that changed from the base of the PR and between 74f5ffa and 3edfa37.

📒 Files selected for processing (1)
  • python/cuml/cuml/feature_extraction/_vectorizers.py

@csadorf

csadorf commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

@divyegala Can you review this, please?

@divyegala divyegala left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! Could you please add a test?

@adityaanikam
adityaanikam requested a review from a team as a code owner August 19, 2026 09:00
@adityaanikam

adityaanikam commented Aug 19, 2026

Copy link
Copy Markdown
Author

Added test_tfidf_vectorizer_char_wb_ngrams — compares cuML's TfidfVectorizer(analyzer="char_wb", ngram_range=(2, 6)) against scikit-learn's on the existing DOCS fixture (13 documents, mixed lengths, includes empty docs), checking both the output matrix and feature names via get_feature_names_out(). That corpus mix is what actually exercises the bug, since the misalignment only showed up once documents had varying token counts. Also rebased onto latest main to pick up the get_feature_names_out() API change from #8480.
This also needs bug + non-breaking labels for the Label Checker I don't have permission to add them myself.

@divyegala divyegala added bug Something isn't working non-breaking Non-breaking change labels Aug 19, 2026

@divyegala divyegala left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, LGTM!

@divyegala

Copy link
Copy Markdown
Contributor

/ok to test 36f9500

@divyegala

Copy link
Copy Markdown
Contributor

/merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Cython / Python Cython or Python issue non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] TfidfVectorizer with char_wb analyzer and ngrams fails

5 participants