Skip to content

model2vec 0.11.0

model2vec 0.11.0 encodes 218,289 articles per second, 35× faster than the 6,196 of 0.10.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.

ModelParametersAvg MTEBArticles per secondblink-8M is faster by
blink-8M7.7M55.35218,289—
all-MiniLM-L6-v223M59.031002,174×
bge-base-en-v1.5109M65.141120,363×
GloVe 6B 300d120M45.82481454×

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-only
model.embedding = model.embedding * 10 # Fine, the backend is rebuilt on the next encode

The 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_size
  • show_progress_bar
  • use_multiprocessing
  • multiprocessing_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 float32 by default. In previous versions, distill and distill_from_model defaulted to quantize_to="float16". float32 is a dtype the Rust backend uses natively. As a result, distilled models are twice as large on disk as before. To get smaller models, pass quantize_to="int8".

  • vocabulary_quantization was removed from distill and distill_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_dim is assigned lazily. out_dim argument was removed from StaticModelForClassification and StaticModelForSimilarity. In previous versions, we allowed assignment, but immediately overwrote it. A classifier that hasn’t been fit yet has no head, so predict and to_pipeline raise an error before fit.

Other fixes

  • float16 normalization underflow: normalizing float16 embeddings could underflow. Normalization now happens in float32, in both encode and the ONNX export.
  • Vocabulary quantization of float16 models: the norms are now computed in at least float32, and rows with a zero norm no longer cause division by zero.

Breaking changes

  • vocabulary_quantization is no longer an argument of distill and distill_from_model.
  • out_dim is no longer an argument of StaticModelForClassification and StaticModelForSimilarity.
  • StaticModelPipeline.predict, predict_proba and evaluate now only accept keyword arguments after X (and y). Passing batch_size or show_progress_bar positionally raises a TypeError.
  • Distilled models default to float32 instead of float16.
  • model.embedding, model.weights and model.token_mapping are read-only.
  • In the state_dict of a trainable model, the head’s parameters are now under _head.* instead of head.*.

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.
  • tokenizers now needs to be at least 0.22.
  • joblib was 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.