Repository navigation
Conversation
StaticEmbedding embeds every token (max_seq_length is inf) and already disables the tokenizer's padding, but kept any truncation the tokenizer was loaded with. Tokenizers from e.g. sentence-transformers/all-MiniLM-L6-v2 truncate to 128 tokens via their tokenizer.json, so longer texts silently lost their tail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Closes #4366
StaticEmbedding embeds every token (its max_seq_length is inf) and already calls no_padding() on its tokenizer, but it kept any truncation the tokenizer was loaded with. Tokenizers of e.g. sentence-transformers/all-MiniLM-L6-v2 truncate to 128 tokens via their tokenizer.json, so a StaticEmbedding built from them silently averaged only the first 128 tokens of each text. See the issue for a reproducer: two different texts that share their first 128 tokens get a similarity of 1.0000 on main and 0.5973 with this change.
Change: call self.tokenizer.no_truncation() next to the existing self.tokenizer.no_padding() in StaticEmbedding.init. This covers both a tokenizers.Tokenizer and a PreTrainedTokenizerFast (whose backend is already copied since #4253), and both new modules and loading, since load goes through init.
Backwards compatibility: models whose saved tokenizer.json has no truncation, which includes the published static models I checked (static-retrieval-mrl-en-v1, static-similarity-mrl-multilingual-v1, minishlab/potion-base-8M), produce the same outputs. A StaticEmbedding that was saved with a truncating tokenizer now embeds all tokens of texts longer than that limit, which is the behavior max_seq_length == inf describes.
Tests: new test_tokenize_ignores_tokenizer_truncation in tests/sentence_transformer/modules/test_static_embedding.py, parametrized over a tokenizers.Tokenizer and a PreTrainedTokenizerFast with truncation enabled. Both cases fail on main and pass with this change.
Run on macOS (CPU), Python 3.12, torch 2.15.0, on main at 9240eeb. Found through code analysis, not in actual use.
Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.
🤖 Generated with Claude Code