model2vec 0.2.4__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {model2vec-0.2.4 → model2vec-0.3.0}/.github/workflows/ci.yaml +9 -3
- {model2vec-0.2.4 → model2vec-0.3.0}/.gitignore +0 -1
- {model2vec-0.2.4 → model2vec-0.3.0}/Makefile +3 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/PKG-INFO +176 -121
- {model2vec-0.2.4 → model2vec-0.3.0}/README.md +167 -112
- model2vec-0.3.0/assets/images/logo_v2.png +0 -0
- model2vec-0.3.0/assets/images/model2vec_model_diagram_transparant_dark.png +0 -0
- model2vec-0.3.0/assets/images/model2vec_model_diagram_transparant_light.png +0 -0
- model2vec-0.3.0/model2vec/__init__.py +4 -0
- model2vec-0.3.0/model2vec/distill/__init__.py +10 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec/distill/distillation.py +13 -3
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec/distill/inference.py +24 -13
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec/distill/tokenizer.py +39 -35
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec/distill/utils.py +2 -0
- model2vec-0.2.4/model2vec/utils.py → model2vec-0.3.0/model2vec/hf_utils.py +5 -22
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec/model.py +88 -76
- model2vec-0.3.0/model2vec/utils.py +86 -0
- model2vec-0.3.0/model2vec/version.py +2 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec.egg-info/PKG-INFO +176 -121
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec.egg-info/SOURCES.txt +8 -2
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec.egg-info/requires.txt +6 -6
- {model2vec-0.2.4 → model2vec-0.3.0}/pyproject.toml +9 -11
- model2vec-0.3.0/results/README.md +88 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/tests/conftest.py +5 -2
- {model2vec-0.2.4 → model2vec-0.3.0}/tests/test_distillation.py +4 -2
- {model2vec-0.2.4 → model2vec-0.3.0}/tests/test_model.py +28 -16
- model2vec-0.3.0/tests/test_utils.py +121 -0
- model2vec-0.3.0/tutorials/README.md +15 -0
- model2vec-0.3.0/tutorials/semantic_deduplication.ipynb +552 -0
- model2vec-0.3.0/uv.lock +1588 -0
- model2vec-0.2.4/model2vec/__init__.py +0 -4
- model2vec-0.2.4/model2vec/distill/__init__.py +0 -3
- model2vec-0.2.4/model2vec/distill/__main__.py +0 -53
- model2vec-0.2.4/tests/test_utils.py +0 -52
- model2vec-0.2.4/tutorials/README.md +0 -14
- model2vec-0.2.4/uv.lock +0 -1412
- {model2vec-0.2.4 → model2vec-0.3.0}/.pre-commit-config.yaml +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/LICENSE +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/assets/images/logo.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/assets/images/model2vec_model_diagram.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/assets/images/sentences_per_second_vs_average_score.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/assets/images/speed_vs_accuracy.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/assets/images/speed_vs_accuracy_v2.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/assets/images/speed_vs_accuracy_v3.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/assets/images/tutorial_ezlo.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec/model_card_template.md +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec.egg-info/dependency_links.txt +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/model2vec.egg-info/top_level.txt +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/setup.cfg +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/tests/__init__.py +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/tests/data/test_tokenizer/special_tokens_map.json +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/tests/data/test_tokenizer/tokenizer.json +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/tests/data/test_tokenizer/tokenizer_config.json +0 -0
- {model2vec-0.2.4 → model2vec-0.3.0}/tutorials/recipe_search.ipynb +0 -0
|
@@ -10,7 +10,14 @@ jobs:
|
|
|
10
10
|
strategy:
|
|
11
11
|
matrix:
|
|
12
12
|
os: ["ubuntu-latest", "windows-latest"]
|
|
13
|
-
python-version: ["3.10"]
|
|
13
|
+
python-version: ["3.9", "3.10", "3.11", "3.12"]
|
|
14
|
+
exclude:
|
|
15
|
+
- os: windows-latest
|
|
16
|
+
python-version: "3.9"
|
|
17
|
+
- os: windows-latest
|
|
18
|
+
python-version: "3.11"
|
|
19
|
+
- os: windows-latest
|
|
20
|
+
python-version: "3.12"
|
|
14
21
|
fail-fast: false
|
|
15
22
|
|
|
16
23
|
steps:
|
|
@@ -42,8 +49,7 @@ jobs:
|
|
|
42
49
|
|
|
43
50
|
# Install dependencies using uv pip
|
|
44
51
|
- name: Install dependencies
|
|
45
|
-
run: make install
|
|
46
|
-
# run: uv pip install -e ".[pytest]"
|
|
52
|
+
run: make install-no-pre-commit
|
|
47
53
|
|
|
48
54
|
# Run tests with coverage
|
|
49
55
|
- name: Run tests under coverage
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: model2vec
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: Distill a Small
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Distill a Small Static Model from any Sentence Transformer
|
|
5
5
|
Author-email: Stéphan Tulkens <stephantul@gmail.com>, Thomas van Dongen <thomas123@live.nl>
|
|
6
6
|
License: MIT License
|
|
7
7
|
|
|
@@ -35,22 +35,18 @@ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
|
35
35
|
Classifier: Topic :: Software Development :: Libraries
|
|
36
36
|
Classifier: License :: OSI Approved :: MIT License
|
|
37
37
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
38
39
|
Classifier: Programming Language :: Python :: 3.10
|
|
39
40
|
Classifier: Programming Language :: Python :: 3.11
|
|
40
41
|
Classifier: Programming Language :: Python :: 3.12
|
|
41
42
|
Classifier: Natural Language :: English
|
|
42
|
-
Requires-Python: >=3.
|
|
43
|
+
Requires-Python: >=3.9
|
|
43
44
|
Description-Content-Type: text/markdown
|
|
44
45
|
License-File: LICENSE
|
|
45
|
-
Requires-Dist: click
|
|
46
46
|
Requires-Dist: numpy
|
|
47
47
|
Requires-Dist: rich
|
|
48
48
|
Requires-Dist: tqdm
|
|
49
|
-
Requires-Dist:
|
|
50
|
-
Requires-Dist: transformers
|
|
51
|
-
Requires-Dist: torch
|
|
52
|
-
Requires-Dist: tokenizers
|
|
53
|
-
Requires-Dist: scikit-learn
|
|
49
|
+
Requires-Dist: tokenizers>=0.20
|
|
54
50
|
Requires-Dist: setuptools
|
|
55
51
|
Provides-Extra: dev
|
|
56
52
|
Requires-Dist: black; extra == "dev"
|
|
@@ -60,24 +56,29 @@ Requires-Dist: pre-commit; extra == "dev"
|
|
|
60
56
|
Requires-Dist: pytest; extra == "dev"
|
|
61
57
|
Requires-Dist: pytest-coverage; extra == "dev"
|
|
62
58
|
Requires-Dist: ruff; extra == "dev"
|
|
59
|
+
Provides-Extra: distill
|
|
60
|
+
Requires-Dist: torch; extra == "distill"
|
|
61
|
+
Requires-Dist: transformers; extra == "distill"
|
|
62
|
+
Requires-Dist: scikit-learn; extra == "distill"
|
|
63
63
|
|
|
64
64
|
|
|
65
65
|
<div align="center">
|
|
66
66
|
<picture>
|
|
67
|
-
<img width="
|
|
67
|
+
<img width="35%" alt="Model2Vec logo" src="assets/images/logo_v2.png">
|
|
68
68
|
</picture>
|
|
69
69
|
</a>
|
|
70
70
|
</div>
|
|
71
71
|
|
|
72
72
|
<div align="center">
|
|
73
|
-
<h2>Distill a Small
|
|
73
|
+
<h2>Distill a Small Static Model from any Sentence Transformer</h2>
|
|
74
74
|
</div>
|
|
75
75
|
|
|
76
76
|
<div align="center">
|
|
77
77
|
<h2>
|
|
78
78
|
<a href="https://huggingface.co/minishlab"><strong>🤗 Models</strong></a> |
|
|
79
79
|
<a href="https://github.com/MinishLab/model2vec/tree/main/tutorials"><strong>📚 Tutorials</strong></a> |
|
|
80
|
-
<a href="https://
|
|
80
|
+
<a href="https://huggingface.co/blog/Pringled/model2vec"><strong>📖 Blog</strong></a> |
|
|
81
|
+
<a href="https://github.com/MinishLab/model2vec/blob/main/results/README.md"><strong>🏆 Results</strong></a>
|
|
81
82
|
</h2>
|
|
82
83
|
</div>
|
|
83
84
|
|
|
@@ -96,17 +97,17 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
96
97
|
</h2>
|
|
97
98
|
</div>
|
|
98
99
|
|
|
100
|
+
<div align="center">
|
|
101
|
+
<img src="assets/images/model2vec_model_diagram_transparant_dark.png#gh-dark-mode-only" width="90%">
|
|
102
|
+
<img src="assets/images/model2vec_model_diagram_transparant_light.png#gh-light-mode-only" width="90%">
|
|
103
|
+
</div>
|
|
99
104
|
|
|
100
|
-
|
|
101
|
-
<img width="75%", src="assets/images/model2vec_model_diagram.png" alt="Model2Vec">
|
|
102
|
-
</p>
|
|
103
|
-
|
|
104
|
-
Model2Vec is a technique to turn any sentence transformer into a really small fast model, reducing model size by 15x and making the models up to 500x faster, with a small drop in performance. See our results [here](#classification-and-speed-benchmarks), or dive in to see how it works.
|
|
105
|
+
Model2Vec is a technique to turn any sentence transformer into a really small static model, reducing model size by 15x and making the models up to 500x faster, with a small drop in performance. See our results [here](results/README.md), or dive in to see how it works.
|
|
105
106
|
|
|
106
107
|
## Table of Contents
|
|
107
108
|
- [Quickstart](#quickstart)
|
|
108
|
-
- [What is Model2Vec?](#what-is-model2vec)
|
|
109
109
|
- [Main Features](#main-features)
|
|
110
|
+
- [What is Model2Vec?](#what-is-model2vec)
|
|
110
111
|
- [Usage](#usage)
|
|
111
112
|
- [Distilling a Model2Vec model](#distilling-a-model2vec-model)
|
|
112
113
|
- [Inferencing a Model2Vec model](#inference-with-a-model2vec-model)
|
|
@@ -118,7 +119,13 @@ Model2Vec is a technique to turn any sentence transformer into a really small fa
|
|
|
118
119
|
|
|
119
120
|
## Quickstart
|
|
120
121
|
|
|
121
|
-
Install the package with:
|
|
122
|
+
Install the package and all required extras with:
|
|
123
|
+
```bash
|
|
124
|
+
pip install model2vec[distill]
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
If you want a light-weight version of the package which only requires `numpy`, omit the `distill` extra. This means you can't distill your own models, but you can use pre-trained models. This is useful for inference pipelines.
|
|
128
|
+
|
|
122
129
|
```bash
|
|
123
130
|
pip install model2vec
|
|
124
131
|
```
|
|
@@ -154,25 +161,46 @@ m2v_model = distill(model_name=model_name, pca_dims=256)
|
|
|
154
161
|
m2v_model.save_pretrained("m2v_model")
|
|
155
162
|
```
|
|
156
163
|
|
|
157
|
-
|
|
164
|
+
Distillation is really fast, and only takes about 5 seconds on a 2024 macbook using the MPS backend, 30 seconds on CPU. Best of all, distillation requires no training data.
|
|
158
165
|
|
|
166
|
+
You can also directly use Model2Vec in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) using the [StaticEmbedding](https://github.com/UKPLab/sentence-transformers/blob/master/sentence_transformers/models/StaticEmbedding.py) module. You can either load a Model2Vec model into a Sentence Transformer with the following code snippet:
|
|
159
167
|
```python
|
|
160
|
-
from
|
|
168
|
+
from sentence_transformers import SentenceTransformer
|
|
169
|
+
from sentence_transformers.models import StaticEmbedding
|
|
161
170
|
|
|
162
|
-
|
|
171
|
+
# Initialize a StaticEmbedding module
|
|
172
|
+
static_embedding = StaticEmbedding.from_model2vec("minishlab/M2V_base_output")
|
|
173
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
174
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
175
|
+
```
|
|
163
176
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
177
|
+
Or you can distill a model directly into a Sentence Transformer model:
|
|
178
|
+
```python
|
|
179
|
+
from sentence_transformers import SentenceTransformer
|
|
180
|
+
from sentence_transformers.models import StaticEmbedding
|
|
168
181
|
|
|
169
|
-
|
|
182
|
+
static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
|
|
183
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
184
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
185
|
+
```
|
|
186
|
+
For more documentation, please refer to the [Sentence Transformers documentation](https://sbert.net/docs/package_reference/sentence_transformer/models.html#sentence_transformers.models.StaticEmbedding).
|
|
170
187
|
|
|
171
|
-
|
|
188
|
+
## Main Features
|
|
172
189
|
|
|
173
|
-
|
|
190
|
+
Model2Vec has the following features:
|
|
174
191
|
|
|
175
|
-
|
|
192
|
+
- **Small**: reduces the size of a Sentence Transformer model by a factor of 15, from 120M params, down to 7.5M (30 MB on disk, making it the smallest model on [MTEB](https://huggingface.co/spaces/mteb/leaderboard)!).
|
|
193
|
+
- **Static, but better**: smaller than GLoVe and BPEmb, but [much more performant](results/README.md), even with the same vocabulary.
|
|
194
|
+
- **Fast distillation**: make your own model in 30 seconds.
|
|
195
|
+
- **Fast inference**: up to 500 times faster on CPU than the original model. Go green or go home.
|
|
196
|
+
- **No data needed**: Distillation happens directly on the token level, so no dataset is needed.
|
|
197
|
+
- **Simple to use**: An easy to use interface for distilling and inferencing.
|
|
198
|
+
- **Integrated into Sentence Transformers**: Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers).
|
|
199
|
+
- **Bring your own model**: Can be applied to any Sentence Transformer model.
|
|
200
|
+
- **Bring your own vocabulary**: Can be applied to any vocabulary, allowing you to use your own domain-specific vocabulary. Need biomedical? Just get a medical dictionary, a biomedical model, and inference it.
|
|
201
|
+
- **Multi-lingual**: Use any language. Need a French model? [Pick one](https://huggingface.co/models?library=sentence-transformers&language=fr&sort=trending). Need multilingual? [Here you go](https://huggingface.co/sentence-transformers/LaBSE).
|
|
202
|
+
- **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](https://huggingface.co/minishlab). Feel free to share your own.
|
|
203
|
+
- **Easy Evaluation**: evaluate your models on MTEB and some of our own tasks to measure the performance of the distilled model. Model2Vec models work out of the box on [MTEB](https://huggingface.co/spaces/mteb/leaderboard).
|
|
176
204
|
|
|
177
205
|
## What is Model2Vec?
|
|
178
206
|
|
|
@@ -185,27 +213,20 @@ Model2vec has 3 modes:
|
|
|
185
213
|
- **Vocab (word level)**: creates a word-level tokenizer and only encodes words that are in the vocabulary. This is a bit slower to create and creates a larger model, but might be more performant on some tasks. Note that this model can go out-of-vocabulary, which might be beneficial if your domain is very noisy
|
|
186
214
|
- **Vocab (subword)**: a combination of the two methods above. In this mode, you can pass your own vocabulary, but it also uses the subword vocabulary to create representations for words not in the passed vocabulary.
|
|
187
215
|
|
|
188
|
-
|
|
216
|
+
For a technical deepdive into Model2Vec, please refer to our [blog post](https://huggingface.co/blog/Pringled/model2vec).
|
|
189
217
|
|
|
190
|
-
Model2Vec is:
|
|
191
218
|
|
|
192
|
-
- **Small**: reduces the size of a Sentence Transformer model by a factor of 15, from 120M params, down to 7.5M (30 MB on disk!).
|
|
193
|
-
- **Static, but better**: smaller than GLoVe, but much more performant, even with the same vocabulary.
|
|
194
|
-
- **Fast distillation**: make your own model in 30 seconds.
|
|
195
|
-
- **Fast inference**: up to 500 times faster on CPU than the original model. Go green or go home.
|
|
196
|
-
- **No data needed**: Distillation happens directly on the token level, so no dataset is needed.
|
|
197
|
-
- **Simple to use**: An easy to use interface for distilling and inferencing.
|
|
198
|
-
- **Bring your own model**: Can be applied to any Sentence Transformer model.
|
|
199
|
-
- **Bring your own vocabulary**: Can be applied to any vocabulary, allowing you to use your own domain-specific vocabulary. Need biomedical? Just get a medical dictionary, a biomedical model, and inference it.
|
|
200
|
-
- **Multi-lingual**: Use any language. Need a French model? [Pick one](https://huggingface.co/models?library=sentence-transformers&language=fr&sort=trending). Need multilingual? [Here you go](https://huggingface.co/sentence-transformers/LaBSE).
|
|
201
|
-
- **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](https://huggingface.co/minishlab). Feel free to share your own.
|
|
202
|
-
- **Easy Evaluation**: evaluate your models on MTEB and some of our own tasks to measure the performance of the distilled model. Model2Vec models work out of the box on [MTEB](https://huggingface.co/spaces/mteb/leaderboard).
|
|
203
219
|
|
|
204
220
|
## Usage
|
|
205
221
|
|
|
206
|
-
### Distilling a Model2Vec model
|
|
207
222
|
|
|
208
|
-
|
|
223
|
+
### Distillation
|
|
224
|
+
|
|
225
|
+
<details>
|
|
226
|
+
<summary> Distilling from a Sentence Transformer </summary>
|
|
227
|
+
<br>
|
|
228
|
+
|
|
229
|
+
The following code can be used to distill a model from a Sentence Transformer. As mentioned above, this leads to really small model that might be less performant.
|
|
209
230
|
```python
|
|
210
231
|
from model2vec.distill import distill
|
|
211
232
|
|
|
@@ -219,6 +240,53 @@ m2v_model = distill(model_name=model_name, pca_dims=256)
|
|
|
219
240
|
m2v_model.save_pretrained("m2v_model")
|
|
220
241
|
|
|
221
242
|
```
|
|
243
|
+
</details>
|
|
244
|
+
|
|
245
|
+
<details>
|
|
246
|
+
<summary> Distilling from a loaded model </summary>
|
|
247
|
+
<br>
|
|
248
|
+
|
|
249
|
+
If you already have a model loaded, or need to load a model in some special way, we also offer an interface to distill models in memory.
|
|
250
|
+
|
|
251
|
+
```python
|
|
252
|
+
from transformers import AutoModel, AutoTokenizer
|
|
253
|
+
|
|
254
|
+
from model2vec.distill import distill_from_model
|
|
255
|
+
|
|
256
|
+
# Assuming a loaded model and tokenizer
|
|
257
|
+
model_name = "baai/bge-base-en-v1.5"
|
|
258
|
+
model = AutoModel.from_pretrained(model_name)
|
|
259
|
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
260
|
+
|
|
261
|
+
m2v_model = distill_from_model(model=model, tokenizer=tokenizer, pca_dims=256)
|
|
262
|
+
|
|
263
|
+
m2v_model.save_pretrained("m2v_model")
|
|
264
|
+
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
</details>
|
|
268
|
+
|
|
269
|
+
<details>
|
|
270
|
+
<summary> Distilling with the Sentence Transformers library </summary>
|
|
271
|
+
<br>
|
|
272
|
+
|
|
273
|
+
The following code snippet shows how to distill a model using the [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) library. This is useful if you want to use the model in a Sentence Transformers pipeline.
|
|
274
|
+
|
|
275
|
+
```python
|
|
276
|
+
from sentence_transformers import SentenceTransformer
|
|
277
|
+
from sentence_transformers.models import StaticEmbedding
|
|
278
|
+
|
|
279
|
+
static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
|
|
280
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
281
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
</details>
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
<details>
|
|
288
|
+
<summary> Distilling with a custom vocabulary </summary>
|
|
289
|
+
<br>
|
|
222
290
|
|
|
223
291
|
If you pass a vocabulary, you get a set of static word embeddings, together with a custom tokenizer for exactly that vocabulary. This is comparable to how you would use GLoVe or traditional word2vec, but doesn't actually require a corpus or data.
|
|
224
292
|
```python
|
|
@@ -230,10 +298,7 @@ vocabulary = ["word1", "word2", "word3"]
|
|
|
230
298
|
model_name = "BAAI/bge-base-en-v1.5"
|
|
231
299
|
|
|
232
300
|
# Distill the model with the custom vocabulary
|
|
233
|
-
m2v_model = distill(model_name=model_name,
|
|
234
|
-
vocabulary=vocabulary,
|
|
235
|
-
pca_dims=None,
|
|
236
|
-
apply_zipf=True)
|
|
301
|
+
m2v_model = distill(model_name=model_name, vocabulary=vocabulary)
|
|
237
302
|
|
|
238
303
|
# Save the model
|
|
239
304
|
m2v_model.save_pretrained("m2v_model")
|
|
@@ -242,14 +307,22 @@ m2v_model.save_pretrained("m2v_model")
|
|
|
242
307
|
m2v_model.push_to_hub("my_organization/my_model", token="<it's a secret to everybody>")
|
|
243
308
|
```
|
|
244
309
|
|
|
245
|
-
|
|
310
|
+
By default, this will distill a model with a subword tokenizer, combining the models (subword) vocab with the new vocabulary. If you want to get a word-level tokenizer instead (with only the passed vocabulary), the `use_subword` parameter can be set to `False`, e.g.:
|
|
246
311
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
python3 -m model2vec.distill --model-name BAAI/bge-base-en-v1.5 --vocabulary-path vocab.txt --device mps --save-path model2vec_model
|
|
312
|
+
```python
|
|
313
|
+
m2v_model = distill(model_name=model_name, vocabulary=vocabulary, use_subword=False)
|
|
250
314
|
```
|
|
251
315
|
|
|
252
|
-
|
|
316
|
+
**Important note:** we assume the passed vocabulary is sorted in rank frequency. i.e., we don't care about the actual word frequencies, but do assume that the most frequent word is first, and the least frequent word is last. If you're not sure whether this is case, set `apply_zipf` to `False`. This disables the weighting, but will also make performance a little bit worse.
|
|
317
|
+
|
|
318
|
+
</details>
|
|
319
|
+
|
|
320
|
+
### Inference
|
|
321
|
+
|
|
322
|
+
<details>
|
|
323
|
+
<summary> Inference using pretrained model </summary>
|
|
324
|
+
<br>
|
|
325
|
+
|
|
253
326
|
Inference works as follows. The example shows one of our own models, but you can also just load a local one, or another one from the hub.
|
|
254
327
|
```python
|
|
255
328
|
from model2vec import StaticModel
|
|
@@ -265,15 +338,47 @@ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to ever
|
|
|
265
338
|
# Make sequences of token embeddings
|
|
266
339
|
token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
267
340
|
```
|
|
341
|
+
</details>
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
<details>
|
|
345
|
+
<summary> Inference using the Sentence Transformers library </summary>
|
|
346
|
+
<br>
|
|
347
|
+
|
|
348
|
+
The following code snippet shows how to use a Model2Vec model in the [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) library. This is useful if you want to use the model in a Sentence Transformers pipeline.
|
|
349
|
+
|
|
350
|
+
```python
|
|
351
|
+
from sentence_transformers import SentenceTransformer
|
|
352
|
+
from sentence_transformers.models import StaticEmbedding
|
|
353
|
+
|
|
354
|
+
# Initialize a StaticEmbedding module
|
|
355
|
+
static_embedding = StaticEmbedding.from_model2vec("minishlab/M2V_base_output")
|
|
356
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
357
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
</details>
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
### Evaluation
|
|
268
364
|
|
|
269
|
-
### Evaluating a Model2Vec model
|
|
270
365
|
|
|
271
|
-
|
|
366
|
+
<details>
|
|
367
|
+
<summary> Installation </summary>
|
|
368
|
+
<br>
|
|
369
|
+
|
|
370
|
+
Our models can be evaluated using our [evaluation package](https://github.com/MinishLab/evaluation). Install the evaluation package with:
|
|
371
|
+
|
|
272
372
|
```bash
|
|
273
|
-
pip install evaluation@git+https://github.com
|
|
373
|
+
pip install evaluation@git+https://github.com MinishLab/evaluation@main
|
|
274
374
|
```
|
|
375
|
+
</details>
|
|
275
376
|
|
|
276
|
-
|
|
377
|
+
<details>
|
|
378
|
+
<summary> Evaluation Code </summary>
|
|
379
|
+
<br>
|
|
380
|
+
|
|
381
|
+
The following code snippet shows how to evaluate a Model2Vec model:
|
|
277
382
|
```python
|
|
278
383
|
from model2vec import StaticModel
|
|
279
384
|
|
|
@@ -304,76 +409,26 @@ task_scores = summarize_results(parsed_results)
|
|
|
304
409
|
# Print the results in a leaderboard format
|
|
305
410
|
print(make_leaderboard(task_scores))
|
|
306
411
|
```
|
|
307
|
-
|
|
308
|
-
## Model List
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
| Model | Language | Description | Vocab | Sentence Transformer | Params |
|
|
312
|
-
|------------------------|-------------|-----------------------------------------------------------------------|----------------|-----------------------|--------------|
|
|
313
|
-
| [M2V_base_glove](https://huggingface.co/minishlab/M2V_base_glove) | English | Flagship embedding model based on GloVe vocab. | GloVe | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 102M |
|
|
314
|
-
| [M2V_base_output](https://huggingface.co/minishlab/M2V_base_output) | English | Flagship embedding model based on bge-base-en-v1.5 vocab. Uses a subword tokenizer. | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 7.5M |
|
|
315
|
-
| [M2V_base_glove_subword](https://huggingface.co/minishlab/M2V_base_glove_subword) | English | Flagship embedding model based on bge-base-en-v1.5 + glove vocab. Uses a subword tokenizer. | Output + GloVe | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 103M |
|
|
316
|
-
| [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | Flagship multilingual embedding model based on LaBSE vocab. Uses a subword tokenizer. | Output | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | 471M |
|
|
317
|
-
## Results
|
|
318
|
-
|
|
319
|
-
### Main Results
|
|
320
|
-
|
|
321
|
-
Model2Vec is evaluated on MTEB, as well as two additional tasks: [PEARL](https://github.com/tigerchen52/PEARL) (a phrase representation task) and WordSim (a collection of _word_ similarity tasks). The results are shown in the table below.
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
| Model | Avg (All) | Avg (MTEB) | Class | Clust | PairClass | Rank | Ret | STS | Sum | Pearl | WordSim |
|
|
326
|
-
|:-----------------------|:---------:|:----------:|:------:|:------:|:---------:|:------:|:------:|:------:|:------:|:------:|:-------:|
|
|
327
|
-
| all-MiniLM-L6-v2 | 56.08 | 56.09 | 62.62 | 41.94 | 82.37 | 58.04 | 41.95 | 78.90 | 30.81 | 60.83 | 49.91 |
|
|
328
|
-
| M2V_base_glove_subword | 49.06 | 46.69 | 61.27 | 30.03 | 74.71 | 49.15 | 27.16 | 69.09 | 30.08 | 56.82 | 57.99 |
|
|
329
|
-
| M2V_base_glove | 48.58 | 47.60 | 61.35 | 30.52 | 75.34 | 48.50 | 29.26 | 70.31 | 31.50 | 50.28 | 54.29 |
|
|
330
|
-
| M2V_base_output | 46.79 | 45.34 | 61.25 | 25.58 | 74.90 | 47.63 | 26.14 | 68.58 | 29.20 | 54.02 | 49.18 |
|
|
331
|
-
| GloVe_300d | 42.84 | 42.36 | 57.31 | 27.66 | 72.48 | 43.30 | 22.78 | 61.90 | 28.81 | 45.65 | 43.05 |
|
|
332
|
-
| BPEmb_50k_300d | 39.34 | 37.78 | 55.76 | 23.35 | 57.86 | 43.21 | 17.50 | 55.10 | 29.74 | 47.56 | 41.28 |
|
|
333
|
-
| WL256* | 48.88 | 49.36 | 58.98 | 33.34 | 74.00 | 52.03 | 33.12 | 73.34 | 29.05 | 48.81 | 45.16 |
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
<details>
|
|
337
|
-
<summary> Task Abbreviations </summary>
|
|
338
|
-
|
|
339
|
-
For readability, the MTEB task names are abbreviated as follows:
|
|
340
|
-
- Class: Classification
|
|
341
|
-
- Clust: Clustering
|
|
342
|
-
- PairClass: PairClassification
|
|
343
|
-
- Rank: Reranking
|
|
344
|
-
- Ret: Retrieval
|
|
345
|
-
- STS: Semantic Textual Similarity
|
|
346
|
-
- Sum: Summarization
|
|
347
412
|
</details>
|
|
348
413
|
|
|
349
|
-
|
|
350
|
-
\* WL256, introduced in the [WordLlama](https://github.com/dleemiller/WordLlama/tree/main) package is included for comparison due to its similarities to Model2Vec. However, we believe it is heavily overfit to the MTEB dataset since it is trained on datasets used in MTEB itself. This can be seen by the fact that the WL256 model performs much worse on the non-MTEB tasks (PEARL and WordSim) than our models and GLoVe. The results shown in the [Classification and Speed Benchmarks](#classification-and-speed-benchmarks) further support this.
|
|
351
|
-
|
|
352
|
-
### Classification and Speed Benchmarks
|
|
353
|
-
|
|
354
|
-
In addition to the MTEB evaluation, we evaluate Model2Vec on a number of classification datasets. These are used as additional evidence to avoid overfitting to the MTEB dataset and to benchmark the speed of the model. The results are shown in the table below.
|
|
355
|
-
|
|
414
|
+
## Model List
|
|
356
415
|
|
|
357
|
-
|
|
358
|
-
|:-----------------------|:-------:|:------:|:-----:|:------:|:-------:|
|
|
359
|
-
| bge-base-en-v1.5 | 90.00 | 91.54 | 91.88 | 85.16 | 91.45 |
|
|
360
|
-
| all-MiniLM-L6-v2 | 84.10 | 83.95 | 81.36 | 81.31 | 89.77 |
|
|
361
|
-
| M2V_base_output | 82.23 | 80.92 | 84.56 | 75.27 | 88.17 |
|
|
362
|
-
| M2V_base_glove_subword | 81.95 | 82.84 | 85.96 | 70.51 | 88.49 |
|
|
363
|
-
| BPEmb_50k_300d | 81.15 | 80.42 | 84.04 | 71.25 | 88.92 |
|
|
364
|
-
| M2V_base_glove | 80.76 | 83.07 | 85.24 | 66.12 | 88.61 |
|
|
365
|
-
| WL256 | 78.48 | 76.88 | 80.12 | 69.23 | 87.68 |
|
|
366
|
-
| GloVe_300d | 77.77 | 81.68 | 84.00 | 55.67 | 89.71 |
|
|
416
|
+
We provide a number of models that can be used out of the box. These models are available on the [HuggingFace hub](https://huggingface.co/collections/minishlab/model2vec-base-models-66fd9dd9b7c3b3c0f25ca90e) and can be loaded using the `from_pretrained` method. The models are listed below.
|
|
367
417
|
|
|
418
|
+
| Model | Language | Vocab | Sentence Transformer | Tokenizer Type | Params |
|
|
419
|
+
|------------------------|-------------|------------------|----------------------|----------------|--------------|
|
|
420
|
+
| [M2V_base_glove](https://huggingface.co/minishlab/M2V_base_glove) | English | GloVe | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Word-level | 102M |
|
|
421
|
+
| [M2V_base_output](https://huggingface.co/minishlab/M2V_base_output) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 7.5M |
|
|
422
|
+
| [M2V_base_glove_subword](https://huggingface.co/minishlab/M2V_base_glove_subword) | English | Output + GloVe | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 103M |
|
|
423
|
+
| [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | Output | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | Subword | 471M |
|
|
368
424
|
|
|
369
|
-
As can be seen, Model2Vec models outperform the GloVe, BPEmb, and WL256 models on all classification tasks, and are competitive with the all-MiniLM-L6-v2 model, while being much faster.
|
|
370
425
|
|
|
371
|
-
|
|
372
|
-
This plot shows that the Model2Vec models are much faster than the other models, while still being competitive in terms of classification performance with the [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) model.
|
|
426
|
+
## Results
|
|
373
427
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
428
|
+
We have performed extensive experiments to evaluate the performance of Model2Vec models. The results are documented in the [results](results/README.md) folder. The results are presented in the following sections:
|
|
429
|
+
- [MTEB Results](results/README.md#mteb-results)
|
|
430
|
+
- [Classification and Speed Benchmarks](results/README.md#classification-and-speed-benchmarks)
|
|
431
|
+
- [Ablations](results/README.md#ablations)
|
|
377
432
|
|
|
378
433
|
## Related work
|
|
379
434
|
|