model2vec 0.11.0

Model2Vec 0.11.0 includes Rust-based inference via model2vec-rs. This means that tokenization, embedding lookup, pooling and normalization are now all much faster. Your code doesn’t need to change; the embeddings are the same as in 0.10.0.
While this is the main selling point, this release also adds a way to create a model from a tokenizer, makes distillation produce float32 models by default, and fixes a few numerical issues with float16.
Benchmarks
0.10.0 vs 0.11.0
We encoded the first 1,000 articles of English Wikipedia (the 20231101.en dump) with blink-8M, truncated to 512 tokens, using the default settings of encode. Each version ran three times for at least two seconds, and we report the best run. All runs were on an Apple M3 Pro. The chart at the top of this post shows the results.
Compared to other models
The table below compares blink-8M to two transformer models and to GloVe. All models encode the same 1,000 Wikipedia articles on CPU. blink-8M is truncated to 512 tokens, like bge-base-en-v1.5. The sentence-transformers models are run with the default settings of encode in the sentence-transformers library.
| Model | Parameters | Avg MTEB | Articles per second | blink-8M is faster by |
|---|---|---|---|---|
| blink-8M | 7.7M | 55.35 | 218,289 | — |
| all-MiniLM-L6-v2 | 23M | 59.03 | 100 | 2,174× |
| bge-base-en-v1.5 | 109M | 65.14 | 11 | 20,363× |
| GloVe 6B 300d | 120M | 45.82 | 481 | 454× |
blink-8M reaches ~94% of the average MTEB score of all-MiniLM-L6-v2, while encoding over 2,000 times as many articles per second.
Rust-backed inference
The first time you call encode, a Python StaticModel creates a model2vec_rs.StaticModel from its embeddings and tokenizer, which is reused on every call. While this adds about 20ms of overhead during loading, it’s worth it.
from model2vec import StaticModel
model = StaticModel.from_pretrained("minishlab/blink-8M")embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])Read-only properties
model.embedding, model.weights and model.token_mapping are now read-only. When the dtypes match, the Python model and the Rust backend share memory, so the embeddings are only stored once. Changing them in place raises an error. If you actually want to change them, assign a new array. The backend is rebuilt on the next encode:
model.embedding *= 10 # ValueError: read-onlymodel.embedding = model.embedding * 10 # Fine, the backend is rebuilt on the next encodeThe same goes for the tokenizer. If you change model.tokenizer in place, assign it again (model.tokenizer = model.tokenizer) for encode to use the changes. This is unfortunately not easy to make read-only, but changing a tokenizer in place should hopefully be a rare occurrence.
Which dtypes to use
The backend works directly with float32 and int8 embeddings. Models in other dtypes still work, but they are cast to float32 when the backend is built. This means that a float16 model incurs the cost of an extra float32 copy of its embeddings. For float16 models, we recommend loading them as int8. This leads to no loss in precision, and is also a bit faster.
model = StaticModel.from_pretrained("path/to/float16-model", quantize_to="int8")Removed encoding arguments
The Rust backend fully handles batching and parallelism. As a result, the following arguments no longer do anything:
batch_sizeshow_progress_baruse_multiprocessingmultiprocessing_threshold
StaticModel.encode, StaticModel.encode_as_sequence, and StaticModelPipeline.predict, predict_proba and evaluate still accept them as keyword arguments, but they raise a DeprecationWarning. You can remove them from your code.
Creating a model from a tokenizer
We introduced two new functions to create a StaticModel with random embeddings. This is useful as a starting point for training a model from scratch.
random_init: creates a random model from a model name.init_from_tokenizer: creates a random model from an in-memory tokenizer.
from model2vec.distill import random_init
model = random_init("BAAI/bge-base-en-v1.5", dim=256)The tokenizer and vocabulary go through the same preprocessing as in distill. Each token gets an embedding drawn from a standard normal distribution, weighted by the SIF coefficient. These models have pooling set to the new PoolingMode.RANDOM in their config.
Distillation
-
Distilled models are now
float32by default. In previous versions,distillanddistill_from_modeldefaulted toquantize_to="float16".float32is a dtype the Rust backend uses natively. As a result, distilled models are twice as large on disk as before. To get smaller models, passquantize_to="int8". -
vocabulary_quantizationwas removed fromdistillanddistill_from_model. You can still quantize the vocabulary when loading a model:model = StaticModel.from_pretrained("path/to/model", vocabulary_quantization=1000) -
Fix: tokens that are not in the original vocabulary are now decoded before they are encoded with the original tokenizer. Previously, they could be encoded with the wrong form when using a BPE tokenizer.
Training
out_dimis assigned lazily.out_dimargument was removed fromStaticModelForClassificationandStaticModelForSimilarity. In previous versions, we allowed assignment, but immediately overwrote it. A classifier that hasn’t been fit yet has no head, sopredictandto_pipelineraise an error beforefit.
Other fixes
float16normalization underflow: normalizingfloat16embeddings could underflow. Normalization now happens infloat32, in bothencodeand the ONNX export.- Vocabulary quantization of
float16models: the norms are now computed in at leastfloat32, and rows with a zero norm no longer cause division by zero.
Breaking changes
vocabulary_quantizationis no longer an argument ofdistillanddistill_from_model.out_dimis no longer an argument ofStaticModelForClassificationandStaticModelForSimilarity.StaticModelPipeline.predict,predict_probaandevaluatenow only accept keyword arguments afterX(andy). Passingbatch_sizeorshow_progress_barpositionally raises aTypeError.- Distilled models default to
float32instead offloat16. model.embedding,model.weightsandmodel.token_mappingare read-only.- In the
state_dictof a trainable model, the head’s parameters are now under_head.*instead ofhead.*.
Dependencies
- New:
model2vec-rs>=0.5.1,<0.6.0. It includes prebuilt wheels for Linux, macOS and Windows, on both x86_64 and ARM. tokenizersnow needs to be at least0.22.joblibwas removed.
New models
The README and results now feature our new blink models. blink-8M reaches an average MTEB score of 55.35, which is 93.77% of the performance of all-MiniLM-L6-v2. It also outperforms our older retrieval-specific models on retrieval. blink-multilingual-64M reaches 99.10% of the performance of LaBSE, with half the parameters of potion-multilingual-128M. See the results for all numbers.