Fix TfIdfVectorizer weight indexing semantics - #31649
Open
apsonawane wants to merge 1 commit into
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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_indexesis 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request refactors the
TfIdfVectorizerimplementation 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
fn_weightcallback inTfIdfVectorizer::ComputeImplto take both the n-gram id and output index, allowing for more flexible and accurate mapping between n-gram ids and output vector indices.fn_weightinComputeImplto pass both the n-gram id and output index, ensuring the callback has the necessary information to apply weights correctly. [1] [2]fn_weightin the mainComputemethod to match the new signature, and to use the correct values for output assignment based on n-gram id and output index.Testing Improvements
Int64_IDFWeights_PermutedNgramIndexesandInt64_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.