Skip to content

Disable tokenizer truncation in StaticEmbedding - #4367

Open
JoeyTan21 wants to merge 1 commit into
huggingface:mainfrom
JoeyTan21:fix/static-embedding-no-truncation
Open

JoeyTan21 wants to merge 1 commit into
huggingface:mainfrom
JoeyTan21:fix/static-embedding-no-truncation

Conversation

@JoeyTan21

Copy link
Copy Markdown
Contributor

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.

python -m pytest tests/sentence_transformer/modules/test_static_embedding.py tests/base/modules/test_router.py -q
  103 passed, 4 skipped
python -m pytest tests/sentence_transformer/test_model.py tests/base/modules/test_dense.py tests/sentence_transformer/losses/test_merged_forward.py tests/base/test_model.py -q -k "static or Static"
  5 passed, 3 skipped
pre-commit run --files sentence_transformers/sentence_transformer/modules/static_embedding.py tests/sentence_transformer/modules/test_static_embedding.py
  ruff check, ruff format, typos: Passed

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

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

StaticEmbedding silently truncates inputs to the max_length in the tokenizer's tokenizer.json

1 participant