Skip to content

Commit 9e4c057

Browse files
authored
perf(embeddings): replace sentence-transformers with model2vec (#156)
1 parent 95d42dd commit 9e4c057

File tree

9 files changed

+29882
-108
lines changed

9 files changed

+29882
-108
lines changed

mostlyai/qa/assets/__init__.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,15 @@ def load_tokenizer():
4040

4141
def load_embedder():
4242
"""
43-
Load the embedder model.
44-
Can deal with read-only cache folder by attempting to download the model if it is not locally available.
45-
Users can set MOSTLY_HF_HOME environment variable to override the default cache folder.
43+
Load the embedder model from local files.
44+
The model files are included in the package to avoid downloading at runtime.
4645
4746
Note that this method can take significant time to load the model. Thus, it is recommended to call this method once and reuse the returned object.
4847
"""
49-
from sentence_transformers import SentenceTransformer
50-
51-
model_name = "sentence-transformers/all-MiniLM-L6-v2"
52-
cache_folder = os.getenv("MOSTLY_HF_HOME")
53-
try:
54-
# First try loading from local cache
55-
return SentenceTransformer(model_name_or_path=model_name, cache_folder=cache_folder, local_files_only=True)
56-
except Exception:
57-
# If not found in cache, attempt downloading
58-
return SentenceTransformer(model_name_or_path=model_name, cache_folder=cache_folder, local_files_only=False)
48+
from model2vec import StaticModel
49+
50+
model_path = _MODULE_DIR / "embedders" / "potion-base-8M"
51+
return StaticModel.from_pretrained(model_path)
5952

6053

6154
__all__ = ["load_embedder"]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
library_name: model2vec
3+
license: mit
4+
model_name: potion-base-8M
5+
tags:
6+
- embeddings
7+
- static-embeddings
8+
- sentence-transformers
9+
---
10+
11+
# potion-base-8M Model Card
12+
13+
This [Model2Vec](https://github.com/MinishLab/model2vec) model is a distilled version of a Sentence Transformer. It uses static embeddings, allowing text embeddings to be computed orders of magnitude faster on both GPU and CPU. It is designed for applications where computational resources are limited or where real-time performance is critical. Model2Vec models are the smallest, fastest, and most performant static embedders available. The distilled models are up to 50 times smaller and 500 times faster than traditional Sentence Transformers.
14+
15+
16+
## Installation
17+
18+
Install model2vec using pip:
19+
```
20+
pip install model2vec
21+
```
22+
23+
## Usage
24+
25+
### Using Model2Vec
26+
27+
The [Model2Vec library](https://github.com/MinishLab/model2vec) is the fastest and most lightweight way to run Model2Vec models.
28+
29+
Load this model using the `from_pretrained` method:
30+
```python
31+
from model2vec import StaticModel
32+
33+
# Load a pretrained Model2Vec model
34+
model = StaticModel.from_pretrained("potion-base-8M")
35+
36+
# Compute text embeddings
37+
embeddings = model.encode(["Example sentence"])
38+
```
39+
40+
### Using Sentence Transformers
41+
42+
You can also use the [Sentence Transformers library](https://github.com/UKPLab/sentence-transformers) to load and use the model:
43+
44+
```python
45+
from sentence_transformers import SentenceTransformer
46+
47+
# Load a pretrained Sentence Transformer model
48+
model = SentenceTransformer("potion-base-8M")
49+
50+
# Compute text embeddings
51+
embeddings = model.encode(["Example sentence"])
52+
```
53+
54+
### Distilling a Model2Vec model
55+
56+
You can distill a Model2Vec model from a Sentence Transformer model using the `distill` method. First, install the `distill` extra with `pip install model2vec[distill]`. Then, run the following code:
57+
58+
```python
59+
from model2vec.distill import distill
60+
61+
# Distill a Sentence Transformer model, in this case the BAAI/bge-base-en-v1.5 model
62+
m2v_model = distill(model_name="BAAI/bge-base-en-v1.5", pca_dims=256)
63+
64+
# Save the model
65+
m2v_model.save_pretrained("m2v_model")
66+
```
67+
68+
## How it works
69+
70+
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. Best of all, you don't need any data to distill a model using Model2Vec.
71+
72+
It works by passing a vocabulary through a sentence transformer model, then reducing the dimensionality of the resulting embeddings using PCA, and finally weighting the embeddings using [SIF weighting](https://openreview.net/pdf?id=SyK00v5xx). During inference, we simply take the mean of all token embeddings occurring in a sentence.
73+
74+
## Additional Resources
75+
76+
- [Model2Vec Repo](https://github.com/MinishLab/model2vec)
77+
- [Model2Vec Base Models](https://huggingface.co/collections/minishlab/model2vec-base-models-66fd9dd9b7c3b3c0f25ca90e)
78+
- [Model2Vec Results](https://github.com/MinishLab/model2vec/tree/main/results)
79+
- [Model2Vec Tutorials](https://github.com/MinishLab/model2vec/tree/main/tutorials)
80+
- [Website](https://minishlab.github.io/)
81+
82+
83+
## Library Authors
84+
85+
Model2Vec was developed by the [Minish Lab](https://github.com/MinishLab) team consisting of [Stephan Tulkens](https://github.com/stephantul) and [Thomas van Dongen](https://github.com/Pringled).
86+
87+
## Citation
88+
89+
Please cite the [Model2Vec repository](https://github.com/MinishLab/model2vec) if you use this model in your work.
90+
```
91+
@software{minishlab2024model2vec,
92+
authors = {Stephan Tulkens and Thomas van Dongen},
93+
title = {Model2Vec: Fast State-of-the-Art Static Embeddings},
94+
year = {2024},
95+
url = {https://github.com/MinishLab/model2vec}
96+
}
97+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"model_type": "model2vec",
3+
"architectures": [
4+
"StaticModel"
5+
],
6+
"tokenizer_name": "baai/bge-base-en-v1.5",
7+
"apply_pca": 256,
8+
"apply_zipf": true,
9+
"hidden_dim": 256,
10+
"seq_length": 1000000,
11+
"normalize": true
12+
}
28.8 MB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"idx": 0,
4+
"name": "0",
5+
"path": ".",
6+
"type": "sentence_transformers.models.StaticEmbedding"
7+
},
8+
{
9+
"idx": 1,
10+
"name": "1",
11+
"path": "1_Normalize",
12+
"type": "sentence_transformers.models.Normalize"
13+
}
14+
]

0 commit comments

Comments
 (0)