OPENNLP-1836: Fix input encoding in SentenceVectorsDL#1072
Open
krickert wants to merge 2 commits into
Open
Conversation
SentenceVectorsDL sent an all-zero attention_mask and all-one token_type_ids, so the model attended to nothing. Use the standard single-segment BERT encoding (mask=1, types=0), consistent with DocumentCategorizerDL. Also close OnnxTensor/Result resources, replace the NPE on a vocabulary miss with a descriptive exception, add a unit test for the encoding, and update the eval test expectations (verified against the same MiniLM ONNX export). Vectors produced by the previous encoding are not comparable with the corrected output.
There was a problem hiding this comment.
Pull request overview
Fixes SentenceVectorsDL’s ONNX input encoding so sentence-transformer models receive standard single-segment BERT inputs (attention mask = 1 for real tokens, token type ids = 0), aligning behavior with other DL components and updating expected eval outputs accordingly.
Changes:
- Corrects SentenceVectorsDL token encoding (mask/types) and improves vocabulary-miss handling with a descriptive exception.
- Prevents native-memory leaks by closing ONNX tensors and
OrtSession.Result. - Adds unit tests for tokenization/encoding and updates SentenceVectorsDLEval pinned vector expectations.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| opennlp-eval-tests/src/test/java/opennlp/dl/vectors/SentenceVectorsDLEval.java | Updates pinned expected vector values for the corrected encoding. |
| opennlp-core/opennlp-ml/opennlp-dl/src/test/java/opennlp/dl/vectors/SentenceVectorsDLTest.java | Adds unit tests validating single-segment BERT encoding and vocabulary/UNK behavior. |
| opennlp-core/opennlp-ml/opennlp-dl/src/main/java/opennlp/dl/vectors/SentenceVectorsDL.java | Fixes mask/types encoding, closes ONNX resources, and improves vocab-mismatch error reporting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Contributor
Author
|
Ran copilot against this. It didn't do a bad job because it only said that the expected vs actual were reversed. |
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.
See https://issues.apache.org/jira/browse/OPENNLP-1836
SentenceVectorsDL sent an all-zero attention_mask and all-one token_type_ids to the ONNX model, so the encoder attended to nothing. This fixes the encoding to the standard single-segment BERT convention (mask=1, types=0), consistent with DocumentCategorizerDL, and additionally:
Eval values were verified empirically: the unfixed code reproduces the previously pinned values exactly against the public sentence-transformers/all-MiniLM-L6-v2 ONNX export, and the corrected encoding produces the new pinned values (dimension 384).
Note: this is a behavioral fix - vectors persisted from the old encoding are not comparable with the corrected output and should be re-embedded.