Skip to main content

Fast State-of-the-Art Static Embeddings

Project description

Model2Vec logo

Fast State-of-the-Art Static Embeddings

Model2Vec is a technique to turn any sentence transformer into a really small static model, reducing model size by a factor up to 50 and making the models up to 500 times faster, with a small drop in performance. Our best model is the most performant static embedding model in the world. See our results here, or dive in to see how it works.

Quickstart

Install the lightweight base package with:

pip install model2vec

You can start using Model2Vec by loading one of our flagship models from the HuggingFace hub. These models are pre-trained and ready to use. The following code snippet shows how to load a model and make embeddings, which you can use for any task, such as text classification, retrieval, clustering, or building a RAG system:

from model2vec import StaticModel

# Load a model from the HuggingFace hub (in this case the potion-base-8M model)
model = StaticModel.from_pretrained("minishlab/potion-base-8M")

# Make embeddings
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])

# Make sequences of token embeddings
token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It's a secret to everybody."])

Instead of using one of our models, you can also distill your own Model2Vec model from a Sentence Transformer model. First, install the distillation extras with:

pip install model2vec[distill]

Then, you can distill a model in ~30 seconds on a CPU with the following code snippet:

from model2vec.distill import distill

# Distill a Sentence Transformer model, in this case the BAAI/bge-base-en-v1.5 model
m2v_model = distill(model_name="BAAI/bge-base-en-v1.5", pca_dims=256)

# Save the model
m2v_model.save_pretrained("m2v_model")

After distillation, you can also fine-tune your own classification models on top of the distilled model, or on a pre-trained model. First, make sure you install the training extras with:

pip install model2vec[training]

Then, you can fine-tune a model as follows:

import numpy as np
from datasets import load_dataset
from model2vec.train import StaticModelForClassification

# Initialize a classifier from a pre-trained model
classifier = StaticModelForClassification.from_pretrained(model_name="minishlab/potion-base-32M")

# Load a dataset
ds = load_dataset("setfit/subj")

# Train the classifier on text (X) and labels (y)
classifier.fit(ds["train"]["text"], ds["train"]["label"])

# Evaluate the classifier
predictions = classifier.predict(ds["test"]["text"])
accuracy = np.mean(np.array(predictions) == np.array(ds["test"]["label"])) * 100

For advanced usage, please refer to our usage documentation.

Updates & Announcements

Main Features

  • State-of-the-Art Performance: Model2Vec models outperform any other static embeddings (such as GLoVe and BPEmb) by a large margin, as can be seen in our results.
  • Small: Model2Vec reduces the size of a Sentence Transformer model by a factor of up to 50. Our best model is just ~30 MB on disk, and our smallest model just ~8 MB (making it the smallest model on MTEB!).
  • Lightweight Dependencies: the base package's only major dependency is numpy.
  • Lightning-fast Inference: up to 500 times faster on CPU than the original model.
  • Fast, Dataset-free Distillation: distill your own model in 30 seconds on a CPU, without a dataset.
  • Fine-tuning: fine-tune your own classification models on top of Model2Vec models.
  • Integrated in many popular libraries: Model2Vec is integrated direclty into popular libraries such as Sentence Transformers and LangChain. For more information, see our integrations documentation.
  • Tightly integrated with HuggingFace hub: easily share and load models from the HuggingFace hub, using the familiar from_pretrained and push_to_hub. Our own models can be found here.

What is Model2Vec?

Model2vec creates a small, fast, and powerful model that outperforms other static embedding models by a large margin on all tasks we could find, while being much faster to create than traditional static embedding models such as GloVe. Like BPEmb, it can create subword embeddings, but with much better performance. Distillation doesn't need any data, just a vocabulary and a model.

The core idea is to forward pass a vocabulary through a sentence transformer model, creating static embeddings for the indiviudal tokens. After this, there are a number of post-processing steps we do that results in our best models. For a more extensive deepdive, please refer to the following resources:

  • Our initial Model2Vec blog post. Note that, while this post gives a good overview of the core idea, we've made a number of substantial improvements since then.
  • Our Tokenlearn blog post. This post describes the Tokenlearn method we used to train our potion models.
  • Our official documentation. This document provides a high-level overview of how Model2Vec works.

Documentation

Our official documentation can be found here. This includes:

Model List

We provide a number of models that can be used out of the box. These models are available on the HuggingFace hub and can be loaded using the from_pretrained method. The models are listed below.

Model Language Sentence Transformer Params Task
potion-base-32M English bge-base-en-v1.5 32.3M General
potion-base-8M English bge-base-en-v1.5 7.5M General
potion-base-4M English bge-base-en-v1.5 3.7M General
potion-base-2M English bge-base-en-v1.5 1.8M General
potion-retrieval-32M English bge-base-en-v1.5 32.3M Retrieval
M2V_multilingual_output Multilingual LaBSE 471M General

Results

We have performed extensive experiments to evaluate the performance of Model2Vec models. The results are documented in the results folder. The results are presented in the following sections:

License

MIT

Citing

If you use Model2Vec in your research, please cite the following:

@software{minishlab2024model2vec,
  authors = {Stephan Tulkens and Thomas van Dongen},
  title = {Model2Vec: Fast State-of-the-Art Static Embeddings},
  year = {2024},
  url = {https://github.com/MinishLab/model2vec}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

model2vec-0.4.0.tar.gz (2.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

model2vec-0.4.0-py3-none-any.whl (38.6 kB view details)

Uploaded Python 3

File details

Details for the file model2vec-0.4.0.tar.gz.

File metadata

  • Download URL: model2vec-0.4.0.tar.gz
  • Upload date:
  • Size: 2.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for model2vec-0.4.0.tar.gz
Algorithm Hash digest
SHA256 48d4a3da040499b0090f736eb8f22ea0fdd35b67462d81d789c70004423adbae
MD5 d8cc2bc22e050e7a62c4b7aad3569ff0
BLAKE2b-256 83e23fb7bd8c612f71ad3abded92e7401f97f1e71427d3a68a3fb85f39394b17

See more details on using hashes here.

File details

Details for the file model2vec-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: model2vec-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 38.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for model2vec-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df30685a55841c61c6638e4f329648e76b148507bd778801d7bfcd6b970a4f2f
MD5 bb3dff38d1a33f821f6a8fd22a33ea15
BLAKE2b-256 937d39ff093c4e45303a06e3c5825c6144cbd21f18a1393a154bbf93232b0f1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page