Repository navigation
Conversation
StaticEmbedding used the backend tokenizer of a PreTrainedTokenizerFast directly. Calling the transformers tokenizer sets the padding and truncation of that backend tokenizer, so after a call such as tokenizer(texts, padding=True), for example from a Transformer module in the same Router, StaticEmbedding averaged padding tokens into the embeddings of the shorter texts, and after a call with truncation, it truncated its inputs. Copy the backend tokenizer instead.
Member
|
Hello! The copy makes sense. Sharing the backend can change StaticEmbedding's behavior during training, while saving and reloading creates independent tokenizers. Keeping them separate from construction avoids that discrepancy. So, I think this is probably better than not copying.
|
This was referenced Nov 12, 2026
Open
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.
When
StaticEmbeddingis created with aPreTrainedTokenizerFast, it uses that tokenizer's backendtokenizers.Tokenizer(tokenizer._tokenizer) directly. Calling the transformers tokenizer sets the padding and truncation of this backend tokenizer, so after a call such astokenizer(texts, padding=True),StaticEmbedding.preprocess()pads the texts of a batch to the longest one, andnn.EmbeddingBagaverages the padding tokens into the embeddings of the shorter texts. After a call withtruncation=True, max_length=..., it truncates its inputs to that length.This also happens without any direct tokenizer call when another module uses the same tokenizer, for example in a
Routerthat encodes queries with aStaticEmbeddingand documents with aTransformer:On
main, this prints a value well below 1 for the shorter query, for exampletensor([0.2308, 1.0000])(it depends on the random embedding weights): once documents have been encoded, the query embeddings change. With this change, it printstensor([1.0000, 1.0000]).This change copies the backend tokenizer, so that
StaticEmbeddinghas its own and keeps tokenizing without padding or truncation. Atokenizers.Tokenizerthat is passed directly is used as before.I found this through code analysis, not in actual use. I searched the issues and pull requests about
StaticEmbeddingand its tokenizer and didn't find this. The closest is #3925, which fixed a tokenizer that was shared betweenWordEmbeddingsinstances.Tests
New
test_tokenize_after_transformers_tokenizer_callintests/sentence_transformer/modules/test_static_embedding.py: it tokenizes two texts with aStaticEmbeddingcreated from aPreTrainedTokenizerFast, before and after calling that tokenizer withpadding=True, and withtruncation=True, max_length=4. Both cases fail onmain(the inputs have padding tokens, or are truncated) and pass with this change.pytest tests/sentence_transformer/modules/test_static_embedding.py(HF_HUB_OFFLINE=1): 20 passed, 3 skipped and 1 failed onmain; 22 passed, 3 skipped and the same 1 failed with this change. The failure istest_loading_model2vec, which downloads a model.pytest tests/sentence_transformer/modules tests/base/modules tests/sparse_encoder/modules(HF_HUB_OFFLINE=1): 593 passed onmainand 595 with this change, with the same 19 failures and 2390 errors in both runs, from tests that need models that aren't in my local cache.tensor([1.0000, 1.0000])with this change.ruff check,ruff format --checkandtyposon the changed files: passed.Versions: Python 3.12.13, transformers 5.17.0, tokenizers 0.23.2, torch 2.14.0.
AI assistance: Claude Code helped me find this bug and write the fix and the test.