Skip to content

Fix TfIdfVectorizer weight indexing semantics - #31649

Open
apsonawane wants to merge 1 commit into
mainfrom
msrc/cpu-tfidfvectorizer-weight-index-fix
Open

Fix TfIdfVectorizer weight indexing semantics#31649
apsonawane wants to merge 1 commit into
mainfrom
msrc/cpu-tfidfvectorizer-weight-index-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request refactors the TfIdfVectorizer implementation to improve the handling of n-gram index mapping, especially for cases where n-gram indexes are not sequential or are permuted. The changes also update the weighting callback signature to provide more context, and add new tests to ensure correct behavior for permuted n-gram indexes.

Refactoring and API Changes

  • Updated the signature of the fn_weight callback in TfIdfVectorizer::ComputeImpl to take both the n-gram id and output index, allowing for more flexible and accurate mapping between n-gram ids and output vector indices.
  • Modified all usages of fn_weight in ComputeImpl to pass both the n-gram id and output index, ensuring the callback has the necessary information to apply weights correctly. [1] [2]
  • Updated the construction of fn_weight in the main Compute method to match the new signature, and to use the correct values for output assignment based on n-gram id and output index.

Testing Improvements

  • Added new unit tests (Int64_IDFWeights_PermutedNgramIndexes and Int64_TFIDFWeights_PermutedNgramIndexes) to verify correct behavior when n-gram indexes are permuted, ensuring the output vector is populated at the correct index regardless of n-gram id order.

Use pool-position index for weights and ngram_indexes value for output coordinate, preventing out-of-range span access and aligning with ONNX TfIdfVectorizer semantics.

Add regressions for permuted ngram_indexes in IDF/TFIDF modes.

Copilot AI 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.

Pull request overview

This PR fixes TfIdfVectorizer’s weighting/indexing behavior when ngram_indexes is permuted (i.e., n-gram “pool order” does not match output vector positions) by splitting the notions of “which weight to use” vs “where to write in the output vector”. It also adds unit tests covering permuted index scenarios for IDF/TFIDF modes.

Changes:

  • Refactored ComputeImpl’s weighting callback to accept both a weight/pool index and an output index, and updated call sites accordingly.
  • Updated TF/IDF/TFIDF weighting lambdas to index weights by n-gram/pool position while writing to the permuted output index.
  • Added new CPU unit tests validating correct outputs when ngram_indexes is permuted.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/core/providers/cpu/nn/tfidfvectorizer.cc Refactors weighting callback usage to correctly decouple weight indexing from output indexing when ngram_indexes is permuted.
onnxruntime/test/providers/cpu/nn/tfidfvectorizer_test.cc Adds regression tests for permuted ngram_indexes in IDF and TFIDF modes.

Comment on lines 260 to +262
void TfIdfVectorizer::ComputeImpl(const void* x_data_raw, size_t elem_size, ptrdiff_t row_num, size_t row_size,
bool is_input_string, gsl::span<float> output_data,
std::function<void(size_t, gsl::span<float>&)>& fn_weight) const {
std::function<void(size_t, size_t, gsl::span<float>&)>& fn_weight) const {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants