model2vec 0.2.4__tar.gz → 0.3.1__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.1}/.github/workflows/ci.yaml +9 -3
- {model2vec-0.2.4 → model2vec-0.3.1}/.gitignore +0 -1
- {model2vec-0.2.4 → model2vec-0.3.1}/Makefile +3 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/PKG-INFO +189 -126
- {model2vec-0.2.4 → model2vec-0.3.1}/README.md +180 -117
- model2vec-0.3.1/assets/images/logo_v2.png +0 -0
- model2vec-0.3.1/assets/images/model2vec_model_diagram_transparant_dark.png +0 -0
- model2vec-0.3.1/assets/images/model2vec_model_diagram_transparant_light.png +0 -0
- model2vec-0.3.1/assets/images/speed_vs_accuracy_v4.png +0 -0
- model2vec-0.3.1/assets/images/speed_vs_mteb_score.png +0 -0
- model2vec-0.3.1/assets/images/speed_vs_mteb_score_v2.png +0 -0
- model2vec-0.3.1/model2vec/__init__.py +4 -0
- model2vec-0.3.1/model2vec/distill/__init__.py +10 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec/distill/distillation.py +13 -3
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec/distill/inference.py +33 -14
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec/distill/tokenizer.py +46 -41
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec/distill/utils.py +2 -0
- model2vec-0.2.4/model2vec/utils.py → model2vec-0.3.1/model2vec/hf_utils.py +5 -22
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec/model.py +97 -76
- model2vec-0.3.1/model2vec/utils.py +86 -0
- model2vec-0.3.1/model2vec/version.py +2 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec.egg-info/PKG-INFO +189 -126
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec.egg-info/SOURCES.txt +11 -2
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec.egg-info/requires.txt +6 -6
- {model2vec-0.2.4 → model2vec-0.3.1}/pyproject.toml +9 -11
- model2vec-0.3.1/results/README.md +66 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/tests/conftest.py +6 -3
- {model2vec-0.2.4 → model2vec-0.3.1}/tests/test_distillation.py +4 -2
- {model2vec-0.2.4 → model2vec-0.3.1}/tests/test_model.py +60 -16
- model2vec-0.3.1/tests/test_utils.py +121 -0
- model2vec-0.3.1/tutorials/README.md +15 -0
- model2vec-0.3.1/tutorials/semantic_deduplication.ipynb +552 -0
- model2vec-0.3.1/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.1}/.pre-commit-config.yaml +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/LICENSE +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/assets/images/logo.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/assets/images/model2vec_model_diagram.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/assets/images/sentences_per_second_vs_average_score.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/assets/images/speed_vs_accuracy.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/assets/images/speed_vs_accuracy_v2.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/assets/images/speed_vs_accuracy_v3.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/assets/images/tutorial_ezlo.png +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec/model_card_template.md +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec.egg-info/dependency_links.txt +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/model2vec.egg-info/top_level.txt +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/setup.cfg +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/tests/__init__.py +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/tests/data/test_tokenizer/special_tokens_map.json +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/tests/data/test_tokenizer/tokenizer.json +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/tests/data/test_tokenizer/tokenizer_config.json +0 -0
- {model2vec-0.2.4 → model2vec-0.3.1}/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.1
|
|
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,21 +97,26 @@ 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>
|
|
104
|
+
|
|
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.
|
|
99
106
|
|
|
100
|
-
<p align="center">
|
|
101
|
-
<img width="75%", src="assets/images/model2vec_model_diagram.png" alt="Model2Vec">
|
|
102
|
-
</p>
|
|
103
107
|
|
|
104
|
-
|
|
108
|
+
## Updates & Announcements
|
|
109
|
+
|
|
110
|
+
- **30/10/2024**: We released three new models: [potion-base-8M](https://huggingface.co/minishlab/potion-base-8M), [potion-base-4M](https://huggingface.co/minishlab/potion-base-4M), and [potion-base-2M](https://huggingface.co/minishlab/potion-base-2M). These models are trained using [Tokenlearn](https://github.com/MinishLab/tokenlearn). Find out more in our [blog post](https://minishlab.github.io/tokenlearn_blogpost/). NOTE: for users of any of our old English M2V models, we recommend switching to these new models as they [perform better on all tasks](https://github.com/MinishLab/model2vec/tree/main/results).
|
|
105
111
|
|
|
106
112
|
## Table of Contents
|
|
107
113
|
- [Quickstart](#quickstart)
|
|
108
|
-
- [What is Model2Vec?](#what-is-model2vec)
|
|
109
114
|
- [Main Features](#main-features)
|
|
115
|
+
- [What is Model2Vec?](#what-is-model2vec)
|
|
110
116
|
- [Usage](#usage)
|
|
111
|
-
- [
|
|
112
|
-
- [
|
|
113
|
-
- [
|
|
117
|
+
- [Distillation](#distillation)
|
|
118
|
+
- [Inference](#inference)
|
|
119
|
+
- [Evaluation](#evaluation)
|
|
114
120
|
- [Model List](#model-list)
|
|
115
121
|
- [Results](#results)
|
|
116
122
|
- [Related Work](#related-work)
|
|
@@ -118,7 +124,13 @@ Model2Vec is a technique to turn any sentence transformer into a really small fa
|
|
|
118
124
|
|
|
119
125
|
## Quickstart
|
|
120
126
|
|
|
121
|
-
Install the package with:
|
|
127
|
+
Install the package and all required extras with:
|
|
128
|
+
```bash
|
|
129
|
+
pip install model2vec[distill]
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
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.
|
|
133
|
+
|
|
122
134
|
```bash
|
|
123
135
|
pip install model2vec
|
|
124
136
|
```
|
|
@@ -127,8 +139,8 @@ The easiest way to get started with Model2Vec is to download one of our [flagshi
|
|
|
127
139
|
```python
|
|
128
140
|
from model2vec import StaticModel
|
|
129
141
|
|
|
130
|
-
# Load a model from the HuggingFace hub (in this case the
|
|
131
|
-
model_name = "minishlab/
|
|
142
|
+
# Load a model from the HuggingFace hub (in this case the potion-base-8M model)
|
|
143
|
+
model_name = "minishlab/potion-base-8M"
|
|
132
144
|
model = StaticModel.from_pretrained(model_name)
|
|
133
145
|
|
|
134
146
|
# Make embeddings
|
|
@@ -154,25 +166,46 @@ m2v_model = distill(model_name=model_name, pca_dims=256)
|
|
|
154
166
|
m2v_model.save_pretrained("m2v_model")
|
|
155
167
|
```
|
|
156
168
|
|
|
157
|
-
|
|
169
|
+
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
170
|
|
|
171
|
+
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
172
|
```python
|
|
160
|
-
from
|
|
173
|
+
from sentence_transformers import SentenceTransformer
|
|
174
|
+
from sentence_transformers.models import StaticEmbedding
|
|
161
175
|
|
|
162
|
-
|
|
176
|
+
# Initialize a StaticEmbedding module
|
|
177
|
+
static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
|
|
178
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
179
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
180
|
+
```
|
|
163
181
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
182
|
+
Or you can distill a model directly into a Sentence Transformer model:
|
|
183
|
+
```python
|
|
184
|
+
from sentence_transformers import SentenceTransformer
|
|
185
|
+
from sentence_transformers.models import StaticEmbedding
|
|
168
186
|
|
|
169
|
-
|
|
187
|
+
static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
|
|
188
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
189
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
190
|
+
```
|
|
191
|
+
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
192
|
|
|
171
|
-
|
|
193
|
+
## Main Features
|
|
172
194
|
|
|
173
|
-
|
|
195
|
+
Model2Vec has the following features:
|
|
174
196
|
|
|
175
|
-
|
|
197
|
+
- **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)!).
|
|
198
|
+
- **Static, but better**: smaller than GLoVe and BPEmb, but [much more performant](results/README.md), even with the same vocabulary.
|
|
199
|
+
- **Fast distillation**: make your own model in 30 seconds.
|
|
200
|
+
- **Fast inference**: up to 500 times faster on CPU than the original model. Go green or go home.
|
|
201
|
+
- **No data needed**: Distillation happens directly on the token level, so no dataset is needed.
|
|
202
|
+
- **Simple to use**: An easy to use interface for distilling and inferencing.
|
|
203
|
+
- **Integrated into Sentence Transformers**: Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers).
|
|
204
|
+
- **Bring your own model**: Can be applied to any Sentence Transformer model.
|
|
205
|
+
- **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.
|
|
206
|
+
- **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).
|
|
207
|
+
- **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.
|
|
208
|
+
- **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
209
|
|
|
177
210
|
## What is Model2Vec?
|
|
178
211
|
|
|
@@ -185,27 +218,23 @@ Model2vec has 3 modes:
|
|
|
185
218
|
- **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
219
|
- **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
220
|
|
|
188
|
-
|
|
221
|
+
For a technical deepdive into Model2Vec, please refer to our [blog post](https://huggingface.co/blog/Pringled/model2vec).
|
|
189
222
|
|
|
190
|
-
|
|
223
|
+
### Tokenlearn
|
|
224
|
+
|
|
225
|
+
Our flagship POTION models are pre-trained using [Tokenlearn](https://github.com/MinishLab/tokenlearn). This method is described in our [Tokenlearn blogpost](https://minishlab.github.io/tokenlearn_blogpost/).
|
|
191
226
|
|
|
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
227
|
|
|
204
228
|
## Usage
|
|
205
229
|
|
|
206
|
-
### Distilling a Model2Vec model
|
|
207
230
|
|
|
208
|
-
|
|
231
|
+
### Distillation
|
|
232
|
+
|
|
233
|
+
<details>
|
|
234
|
+
<summary> Distilling from a Sentence Transformer </summary>
|
|
235
|
+
<br>
|
|
236
|
+
|
|
237
|
+
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
238
|
```python
|
|
210
239
|
from model2vec.distill import distill
|
|
211
240
|
|
|
@@ -219,6 +248,53 @@ m2v_model = distill(model_name=model_name, pca_dims=256)
|
|
|
219
248
|
m2v_model.save_pretrained("m2v_model")
|
|
220
249
|
|
|
221
250
|
```
|
|
251
|
+
</details>
|
|
252
|
+
|
|
253
|
+
<details>
|
|
254
|
+
<summary> Distilling from a loaded model </summary>
|
|
255
|
+
<br>
|
|
256
|
+
|
|
257
|
+
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.
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
from transformers import AutoModel, AutoTokenizer
|
|
261
|
+
|
|
262
|
+
from model2vec.distill import distill_from_model
|
|
263
|
+
|
|
264
|
+
# Assuming a loaded model and tokenizer
|
|
265
|
+
model_name = "baai/bge-base-en-v1.5"
|
|
266
|
+
model = AutoModel.from_pretrained(model_name)
|
|
267
|
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
268
|
+
|
|
269
|
+
m2v_model = distill_from_model(model=model, tokenizer=tokenizer, pca_dims=256)
|
|
270
|
+
|
|
271
|
+
m2v_model.save_pretrained("m2v_model")
|
|
272
|
+
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
</details>
|
|
276
|
+
|
|
277
|
+
<details>
|
|
278
|
+
<summary> Distilling with the Sentence Transformers library </summary>
|
|
279
|
+
<br>
|
|
280
|
+
|
|
281
|
+
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.
|
|
282
|
+
|
|
283
|
+
```python
|
|
284
|
+
from sentence_transformers import SentenceTransformer
|
|
285
|
+
from sentence_transformers.models import StaticEmbedding
|
|
286
|
+
|
|
287
|
+
static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
|
|
288
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
289
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
</details>
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
<details>
|
|
296
|
+
<summary> Distilling with a custom vocabulary </summary>
|
|
297
|
+
<br>
|
|
222
298
|
|
|
223
299
|
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
300
|
```python
|
|
@@ -230,10 +306,7 @@ vocabulary = ["word1", "word2", "word3"]
|
|
|
230
306
|
model_name = "BAAI/bge-base-en-v1.5"
|
|
231
307
|
|
|
232
308
|
# 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)
|
|
309
|
+
m2v_model = distill(model_name=model_name, vocabulary=vocabulary)
|
|
237
310
|
|
|
238
311
|
# Save the model
|
|
239
312
|
m2v_model.save_pretrained("m2v_model")
|
|
@@ -242,20 +315,28 @@ m2v_model.save_pretrained("m2v_model")
|
|
|
242
315
|
m2v_model.push_to_hub("my_organization/my_model", token="<it's a secret to everybody>")
|
|
243
316
|
```
|
|
244
317
|
|
|
245
|
-
|
|
318
|
+
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
319
|
|
|
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
|
|
320
|
+
```python
|
|
321
|
+
m2v_model = distill(model_name=model_name, vocabulary=vocabulary, use_subword=False)
|
|
250
322
|
```
|
|
251
323
|
|
|
252
|
-
|
|
324
|
+
**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.
|
|
325
|
+
|
|
326
|
+
</details>
|
|
327
|
+
|
|
328
|
+
### Inference
|
|
329
|
+
|
|
330
|
+
<details>
|
|
331
|
+
<summary> Inference using pretrained model </summary>
|
|
332
|
+
<br>
|
|
333
|
+
|
|
253
334
|
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
335
|
```python
|
|
255
336
|
from model2vec import StaticModel
|
|
256
337
|
|
|
257
338
|
# Load a model from the HuggingFace hub, or a local one.
|
|
258
|
-
model_name = "minishlab/
|
|
339
|
+
model_name = "minishlab/potion-base-8M"
|
|
259
340
|
# You can optionally pass a token if you're loading a private model
|
|
260
341
|
model = StaticModel.from_pretrained(model_name, token=None)
|
|
261
342
|
|
|
@@ -265,15 +346,47 @@ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to ever
|
|
|
265
346
|
# Make sequences of token embeddings
|
|
266
347
|
token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
267
348
|
```
|
|
349
|
+
</details>
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
<details>
|
|
353
|
+
<summary> Inference using the Sentence Transformers library </summary>
|
|
354
|
+
<br>
|
|
268
355
|
|
|
269
|
-
|
|
356
|
+
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.
|
|
357
|
+
|
|
358
|
+
```python
|
|
359
|
+
from sentence_transformers import SentenceTransformer
|
|
360
|
+
from sentence_transformers.models import StaticEmbedding
|
|
361
|
+
|
|
362
|
+
# Initialize a StaticEmbedding module
|
|
363
|
+
static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
|
|
364
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
365
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
</details>
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
### Evaluation
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
<details>
|
|
375
|
+
<summary> Installation </summary>
|
|
376
|
+
<br>
|
|
377
|
+
|
|
378
|
+
Our models can be evaluated using our [evaluation package](https://github.com/MinishLab/evaluation). Install the evaluation package with:
|
|
270
379
|
|
|
271
|
-
Our models can be evaluated using our [evaluation package](https://github.com/MinishLab/evaluation). To run this, first install the optional evaluation package:
|
|
272
380
|
```bash
|
|
273
|
-
pip install
|
|
381
|
+
pip install git+https://github.com/MinishLab/evaluation.git@main
|
|
274
382
|
```
|
|
383
|
+
</details>
|
|
384
|
+
|
|
385
|
+
<details>
|
|
386
|
+
<summary> Evaluation Code </summary>
|
|
387
|
+
<br>
|
|
275
388
|
|
|
276
|
-
|
|
389
|
+
The following code snippet shows how to evaluate a Model2Vec model:
|
|
277
390
|
```python
|
|
278
391
|
from model2vec import StaticModel
|
|
279
392
|
|
|
@@ -304,83 +417,33 @@ task_scores = summarize_results(parsed_results)
|
|
|
304
417
|
# Print the results in a leaderboard format
|
|
305
418
|
print(make_leaderboard(task_scores))
|
|
306
419
|
```
|
|
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
420
|
</details>
|
|
348
421
|
|
|
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.
|
|
422
|
+
## Model List
|
|
355
423
|
|
|
424
|
+
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.
|
|
356
425
|
|
|
357
|
-
| Model | Average | SST2 | IMDB | TREC | AG News |
|
|
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 |
|
|
367
426
|
|
|
427
|
+
| Model | Language | Vocab | Sentence Transformer | Tokenizer Type | Params | Tokenlearn |
|
|
428
|
+
|-----------------------------------------------------------------------|-------------|------------------|-----------------------------------------------------------------|----------------|---------|------------|
|
|
429
|
+
| [potion-base-8M](https://huggingface.co/minishlab/potion-base-8M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 7.5M | True |
|
|
430
|
+
| [potion-base-4M](https://huggingface.co/minishlab/potion-base-4M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 3.7M | True |
|
|
431
|
+
| [potion-base-2M](https://huggingface.co/minishlab/potion-base-2M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 1.8M | True |
|
|
432
|
+
| [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | Output | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | Subword | 471M | False |
|
|
368
433
|
|
|
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
434
|
|
|
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.
|
|
435
|
+
## Results
|
|
373
436
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
437
|
+
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:
|
|
438
|
+
- [MTEB Results](results/README.md#mteb-results)
|
|
439
|
+
- [Ablations](results/README.md#ablations)
|
|
377
440
|
|
|
378
441
|
## Related work
|
|
379
442
|
|
|
380
443
|
If you are interested in fast small models, also consider looking at these techniques:
|
|
381
444
|
* [BPEmb](https://bpemb.h-its.org/): GLoVE embeddings trained on BPE-encoded Wikipedias. Huge inspiration to this project, multilingual, very fast. If you don't find a sentence transformer in the language you need, check this out.
|
|
382
445
|
* [fast-sentence-transformers](https://github.com/davidberenstein1957/fast-sentence-transformers): distillation using Model2Vec comes at a cost. If that cost is too steep for you, and you have access to a GPU, this package is for you. It automates the quantization and optimization of sentence transformers without loss of performance.
|
|
383
|
-
* [wordllama](https://github.com/dleemiller/WordLlama): Uses the _input_ embeddings of a LLama2 model and then performs contrastive learning on these embeddings.
|
|
446
|
+
* [wordllama](https://github.com/dleemiller/WordLlama): Uses the _input_ embeddings of a LLama2 model and then performs contrastive learning on these embeddings. We think this is a bit overfit on MTEB, as the model is trained on MTEB datasets, and only evaluated on MTEB. Fun fact: this was invented at the same time as Model2Vec.
|
|
384
447
|
|
|
385
448
|
If you find other related work, please let us know.
|
|
386
449
|
|