Repository navigation
Description
StaticEmbedding reports max_seq_length as inf and disables padding on its tokenizer, but it keeps any truncation the tokenizer was loaded with. The tokenizer.json files of several popular models enable truncation, for example sentence-transformers/all-MiniLM-L6-v2, sentence-transformers/all-mpnet-base-v2 and sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 all truncate to 128 tokens. A StaticEmbedding built from one of these tokenizers only averages the first 128 tokens of every text, during training and inference, without any warning.
Affected workflow: training a static embedding model with the tokenizer of an existing model, as in the StaticEmbedding docstring example (Tokenizer.from_pretrained with a model id), or passing transformer.tokenizer from a Transformer module, e.g. inside a Router. Both a tokenizers.Tokenizer and a transformers PreTrainedTokenizerFast keep the truncation from tokenizer.json.
Reproducer:
import copy
import torch
from tokenizers import Tokenizer
from sentence_transformers import SentenceTransformer
from sentence_transformers.sentence_transformer.modules import StaticEmbedding
tokenizer = Tokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
print("tokenizer.json truncation:", tokenizer.truncation)
torch.manual_seed(0)
static_embedding = StaticEmbedding(tokenizer, embedding_dim=64)
model = SentenceTransformer(modules=[static_embedding], device="cpu")
print("model.max_seq_length:", model.max_seq_length)
head = " ".join(["the cat sat on the mat"] * 30) # 180 tokens
texts = [head, head + " " + " ".join(["quantum chromodynamics"] * 50)]
untruncated = copy.deepcopy(tokenizer)
untruncated.no_truncation()
print("tokens in text: ", [len(untruncated.encode(t, add_special_tokens=False).ids) for t in texts])
print("tokens embedded: ", [static_embedding.preprocess([t])["input_ids"].numel() for t in texts])
embeddings = model.encode(texts, convert_to_tensor=True)
print(f"similarity of the two different texts: {model.similarity(embeddings[0], embeddings[1]).item():.4f}")Output on main (9240eeb):
tokenizer.json truncation: {'max_length': 128, 'stride': 0, 'strategy': 'longest_first', 'direction': 'right'}
model.max_seq_length: inf
tokens in text: [180, 480]
tokens embedded: [128, 128]
similarity of the two different texts: 1.0000
Expected: all 180 and 480 tokens are embedded, matching max_seq_length == inf, so the two different texts get different embeddings.
Cause: StaticEmbedding.init calls self.tokenizer.no_padding() but not self.tokenizer.no_truncation(), so preprocess (encode_batch) applies the truncation stored in the tokenizer. #4253 made StaticEmbedding copy the backend of a transformers tokenizer so that later tokenizer calls can't add padding or truncation, but truncation that is already present when the tokenizer is loaded still applies.
The published static models I checked (sentence-transformers/static-retrieval-mrl-en-v1, sentence-transformers/static-similarity-mrl-multilingual-v1, minishlab/potion-base-8M) have no truncation in their tokenizer.json, so they are not affected.
I found this through code analysis while reading the StaticEmbedding tokenizer handling after #4253, not in actual use; the snippet above reproduces it.
Versions: sentence-transformers main (6.4.0.dev0, 9240eeb), tokenizers 0.23.3, transformers 5.22.2, torch 2.15.0 CPU, Python 3.12, macOS.
I have a fix with a regression test ready and am opening a PR for it.
Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.
Activity