Instructions to use sentence-transformers/static-retrieval-mrl-en-v1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use sentence-transformers/static-retrieval-mrl-en-v1 with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("sentence-transformers/static-retrieval-mrl-en-v1") sentences = [ "What was the Office of Foods in charge of?", "This area, stretching northward from the centrally located Great Hall of State, is believed to have been the site of the Office of Foods. This office stocked foods other than the rice that was paid as tax, and was in charge of providing meals for state banquets and rituals held in the palace.", "In 2002, Barclay Records, then as part of Universal Music France, released a digitally remastered version of the original vinyl in CD and in 10\" (25 cm) vinyl record (LP), under the same name, as part of a compilation containing re-releases of all of Dalida's studio albums recorded under the Barclay label. The album was again re-released in 2005.", "Kevin Jon Davies is a British television and video director primarily associated with documentaries and spin-off videos associated with \"Doctor Who\", \"The Hitchhiker's Guide to the Galaxy\" and \"Blake's 7\". He also worked on the BAFTA award-winning animation sequences of the 1981 \"Hitchhiker's Guide\" television adaptation." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Notebooks
- Google Colab
- Kaggle
How can i use this with Transformers
I just get this:
Unrecognized model in tomaarsen/static-retrieval-mrl-en-v1. Should have a model_type key in its config.json, or contain one of the following strings in its name: ...
This model is not transformers compatible, I'm afraid. You can only run it with sentence transformers.
- Tom Aarsen
I saw that it was possible with transformers.js: https://github.com/huggingface/transformers.js/issues/1160
but i don't know how to put the model type in python transformers.
I wanna try late chunking since the sequence length is infinite, but I couldn't really figure out how to do it with sentence-transformers
model = SentenceTransformer(
"tomaarsen/static-retrieval-mrl-en-v1",
backend="onnx",
device="cpu"
)
tokens = model.tokenize(sentences)
print(tokens)
print(model.encode("".join(sentences), output_value="token_embeddings", convert_to_tensor=True))
when i try something like this get
for token_emb, attention in zip(out_features[output_value], out_features["attention_mask"]):
~~~~~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'token_embeddings'
so i guess token embeddings are not supported by this model?
Ah, understandable. Indeed, the StaticEmbedding module that is used in this model (as seen in modules.json) uses an EmbeddingBag, which grabs token embeddings and then applies mean pooling. This EmbeddingBag is equivalent to Embedding plus torch.mean, but a bit faster to run.
If you want to try late interaction with it, then I would recommend copying the StaticEmbedding into a new module locally, changing the EmbeddingBag to Embedding, updating the forward so that the features["token_embeddings"] = self.embedding(...), cloning this model, and updating the modules.json to e.g.:
[
{
"idx": 0,
"name": "0",
"path": "",
"type": "my_custom_static_embedding_script.TokenStaticEmbedding"
},
{
"idx": 1,
"name": "1",
"path": "1_Pooling",
"type": "sentence_transformers.models.Pooling"
}
]
- Tom Aarsen
Hey thanks for answering. I am kinda new to this so I am having some problems.
I copied the StaticEmbedding to a new module locally, renamed the class and changed the EmbeddingBag to embedding and updated the forward function to this:
def forward(self, features: dict[str, torch.Tensor], **kwargs) -> dict[str, torch.Tensor]:
features["token_embeddings"] = self.embedding(features["input_ids"])
return features
But I am having problems with the last part. I put the model.safetensors in my dir with tokenizer and populated modules.json with what you sent and created a directory: "1_Pooling" with this config
{
"word_embedding_dimension": 1024,
"pooling_mode_cls_token": false,
"pooling_mode_mean_tokens": true,
"pooling_mode_max_tokens": false,
"pooling_mode_mean_sqrt_len_tokens": false
}
And when i run:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer(
"./",
backend="onnx",
device="cpu"
)
sentences = [
"hello how are you",
"whats up with you"
]
tokens = model.tokenize(sentences)
print(tokens)
print(model.encode("".join(sentences), output_value="token_embeddings", convert_to_tensor=True))
the tokenize works but the encoding throws this:
Traceback (most recent call last):
File "/redacted/stuff/main.py", line 16, in <module>
print(model.encode("".join(sentences), output_value="token_embeddings", convert_to_tensor=True))
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/redacted/.venv/lib/python3.13/site-packages/torch/utils/_contextlib.py", line 120, in decorate_context
return func(*args, **kwargs)
File "/redacted/.venv/lib/python3.13/site-packages/sentence_transformers/SentenceTransformer.py", line 1051, in encode
out_features = self.forward(features, **kwargs)
File "/redacted/.venv/lib/python3.13/site-packages/sentence_transformers/SentenceTransformer.py", line 1132, in forward
input = module(input, **module_kwargs)
File "/redacted/.venv/lib/python3.13/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/redacted/.venv/lib/python3.13/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl
return forward_call(*args, **kwargs)
File "/redacted/.venv/lib/python3.13/site-packages/sentence_transformers/models/Pooling.py", line 239, in forward
output_vector = torch.cat(output_vectors, 1)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
Sorry I quite dont understand what I am doing