StaticEmbedding ignores model2vec `mapping`/`weights` tensors, so e.g. `minishlab/potion-code-16M` produces wrong embeddings

Author: JoeyTan21Created Oct 1, 2026Updated Oct 1, 2026

model2vec models can store two tensors next to embeddings in model.safetensors: a per-token mapping into the embedding rows, and per-token weights. model2vec averages embeddings[mapping[id]] * weights[id]. StaticEmbedding.load, StaticEmbedding.from_model2vec and StaticEmbedding.from_distillation only read embeddings and drop the other two.

minishlab/potion-code-16M has all three tensors, and its modules.json loads it through StaticEmbedding. Its embeddings from SentenceTransformer(...) don't match model2vec's:

python
# pip install sentence-transformers model2vec
import numpy as np
from model2vec import StaticModel

from sentence_transformers import SentenceTransformer

model_id = "minishlab/potion-code-16M"
texts = ["def add(a, b): return a + b", "SELECT * FROM users WHERE id = 1"]

static_model = StaticModel.from_pretrained(model_id)
# Encode one text at a time: this model's tokenizer.json enables padding, so model2vec's batched encode averages pads in
reference = np.stack([static_model.encode(text) for text in texts])
st_model = SentenceTransformer(model_id, device="cpu")  # modules.json: StaticEmbedding + Normalize
embeddings = st_model.encode(texts)

print("cosine(model2vec, sentence-transformers):", np.round((reference * embeddings).sum(axis=1), 4))
print("max abs diff:", float(np.abs(reference - embeddings).max()))
assert np.allclose(embeddings, reference, atol=1e-5)
cosine(model2vec, sentence-transformers): [0.6376 0.9264]
max abs diff: 0.17389284515745068
AssertionError

When a model is distilled with vocabulary_quantization, embeddings has fewer rows than the vocabulary, so encode fails with an index-out-of-range error.

Suggested fix: fold the mapping and weights into one row per token id (embeddings[mapping] * weights[:, None]) when loading. Mean pooling over those rows gives exactly model2vec's output (max abs diff ~4e-8 on the repro above). I have a small PR with tests ready.

Environment: sentence-transformers main (6.2.0.dev0), model2vec 0.9.0, torch 2.14, Python 3.12, CPU.

Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.

Source: huggingface/sentence-transformers