model2vec 0.3.0__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.
Files changed (51) hide show
  1. {model2vec-0.3.0 → model2vec-0.3.1}/PKG-INFO +26 -18
  2. {model2vec-0.3.0 → model2vec-0.3.1}/README.md +25 -17
  3. model2vec-0.3.1/assets/images/speed_vs_accuracy_v4.png +0 -0
  4. model2vec-0.3.1/assets/images/speed_vs_mteb_score.png +0 -0
  5. model2vec-0.3.1/assets/images/speed_vs_mteb_score_v2.png +0 -0
  6. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/distill/inference.py +9 -1
  7. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/distill/tokenizer.py +7 -6
  8. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/model.py +11 -2
  9. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/version.py +1 -1
  10. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec.egg-info/PKG-INFO +26 -18
  11. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec.egg-info/SOURCES.txt +3 -0
  12. {model2vec-0.3.0 → model2vec-0.3.1}/results/README.md +14 -36
  13. {model2vec-0.3.0 → model2vec-0.3.1}/tests/conftest.py +1 -1
  14. {model2vec-0.3.0 → model2vec-0.3.1}/tests/test_model.py +32 -0
  15. {model2vec-0.3.0 → model2vec-0.3.1}/.github/workflows/ci.yaml +0 -0
  16. {model2vec-0.3.0 → model2vec-0.3.1}/.gitignore +0 -0
  17. {model2vec-0.3.0 → model2vec-0.3.1}/.pre-commit-config.yaml +0 -0
  18. {model2vec-0.3.0 → model2vec-0.3.1}/LICENSE +0 -0
  19. {model2vec-0.3.0 → model2vec-0.3.1}/Makefile +0 -0
  20. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/logo.png +0 -0
  21. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/logo_v2.png +0 -0
  22. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/model2vec_model_diagram.png +0 -0
  23. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/model2vec_model_diagram_transparant_dark.png +0 -0
  24. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/model2vec_model_diagram_transparant_light.png +0 -0
  25. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/sentences_per_second_vs_average_score.png +0 -0
  26. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/speed_vs_accuracy.png +0 -0
  27. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/speed_vs_accuracy_v2.png +0 -0
  28. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/speed_vs_accuracy_v3.png +0 -0
  29. {model2vec-0.3.0 → model2vec-0.3.1}/assets/images/tutorial_ezlo.png +0 -0
  30. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/__init__.py +0 -0
  31. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/distill/__init__.py +0 -0
  32. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/distill/distillation.py +0 -0
  33. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/distill/utils.py +0 -0
  34. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/hf_utils.py +0 -0
  35. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/model_card_template.md +0 -0
  36. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec/utils.py +0 -0
  37. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec.egg-info/dependency_links.txt +0 -0
  38. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec.egg-info/requires.txt +0 -0
  39. {model2vec-0.3.0 → model2vec-0.3.1}/model2vec.egg-info/top_level.txt +0 -0
  40. {model2vec-0.3.0 → model2vec-0.3.1}/pyproject.toml +0 -0
  41. {model2vec-0.3.0 → model2vec-0.3.1}/setup.cfg +0 -0
  42. {model2vec-0.3.0 → model2vec-0.3.1}/tests/__init__.py +0 -0
  43. {model2vec-0.3.0 → model2vec-0.3.1}/tests/data/test_tokenizer/special_tokens_map.json +0 -0
  44. {model2vec-0.3.0 → model2vec-0.3.1}/tests/data/test_tokenizer/tokenizer.json +0 -0
  45. {model2vec-0.3.0 → model2vec-0.3.1}/tests/data/test_tokenizer/tokenizer_config.json +0 -0
  46. {model2vec-0.3.0 → model2vec-0.3.1}/tests/test_distillation.py +0 -0
  47. {model2vec-0.3.0 → model2vec-0.3.1}/tests/test_utils.py +0 -0
  48. {model2vec-0.3.0 → model2vec-0.3.1}/tutorials/README.md +0 -0
  49. {model2vec-0.3.0 → model2vec-0.3.1}/tutorials/recipe_search.ipynb +0 -0
  50. {model2vec-0.3.0 → model2vec-0.3.1}/tutorials/semantic_deduplication.ipynb +0 -0
  51. {model2vec-0.3.0 → model2vec-0.3.1}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: model2vec
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
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
@@ -104,14 +104,19 @@ Requires-Dist: scikit-learn; extra == "distill"
104
104
 
105
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.
106
106
 
107
+
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).
111
+
107
112
  ## Table of Contents
108
113
  - [Quickstart](#quickstart)
109
114
  - [Main Features](#main-features)
110
115
  - [What is Model2Vec?](#what-is-model2vec)
111
116
  - [Usage](#usage)
112
- - [Distilling a Model2Vec model](#distilling-a-model2vec-model)
113
- - [Inferencing a Model2Vec model](#inference-with-a-model2vec-model)
114
- - [Evaluating a Model2Vec model](#evaluating-a-model2vec-model)
117
+ - [Distillation](#distillation)
118
+ - [Inference](#inference)
119
+ - [Evaluation](#evaluation)
115
120
  - [Model List](#model-list)
116
121
  - [Results](#results)
117
122
  - [Related Work](#related-work)
@@ -134,8 +139,8 @@ The easiest way to get started with Model2Vec is to download one of our [flagshi
134
139
  ```python
135
140
  from model2vec import StaticModel
136
141
 
137
- # Load a model from the HuggingFace hub (in this case the M2V_base_output model)
138
- model_name = "minishlab/M2V_base_output"
142
+ # Load a model from the HuggingFace hub (in this case the potion-base-8M model)
143
+ model_name = "minishlab/potion-base-8M"
139
144
  model = StaticModel.from_pretrained(model_name)
140
145
 
141
146
  # Make embeddings
@@ -169,7 +174,7 @@ from sentence_transformers import SentenceTransformer
169
174
  from sentence_transformers.models import StaticEmbedding
170
175
 
171
176
  # Initialize a StaticEmbedding module
172
- static_embedding = StaticEmbedding.from_model2vec("minishlab/M2V_base_output")
177
+ static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
173
178
  model = SentenceTransformer(modules=[static_embedding])
174
179
  embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
175
180
  ```
@@ -215,6 +220,9 @@ Model2vec has 3 modes:
215
220
 
216
221
  For a technical deepdive into Model2Vec, please refer to our [blog post](https://huggingface.co/blog/Pringled/model2vec).
217
222
 
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/).
218
226
 
219
227
 
220
228
  ## Usage
@@ -328,7 +336,7 @@ Inference works as follows. The example shows one of our own models, but you can
328
336
  from model2vec import StaticModel
329
337
 
330
338
  # Load a model from the HuggingFace hub, or a local one.
331
- model_name = "minishlab/M2V_base_output"
339
+ model_name = "minishlab/potion-base-8M"
332
340
  # You can optionally pass a token if you're loading a private model
333
341
  model = StaticModel.from_pretrained(model_name, token=None)
334
342
 
@@ -352,7 +360,7 @@ from sentence_transformers import SentenceTransformer
352
360
  from sentence_transformers.models import StaticEmbedding
353
361
 
354
362
  # Initialize a StaticEmbedding module
355
- static_embedding = StaticEmbedding.from_model2vec("minishlab/M2V_base_output")
363
+ static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
356
364
  model = SentenceTransformer(modules=[static_embedding])
357
365
  embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
358
366
  ```
@@ -370,7 +378,7 @@ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to ever
370
378
  Our models can be evaluated using our [evaluation package](https://github.com/MinishLab/evaluation). Install the evaluation package with:
371
379
 
372
380
  ```bash
373
- pip install evaluation@git+https://github.com MinishLab/evaluation@main
381
+ pip install git+https://github.com/MinishLab/evaluation.git@main
374
382
  ```
375
383
  </details>
376
384
 
@@ -415,19 +423,19 @@ print(make_leaderboard(task_scores))
415
423
 
416
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.
417
425
 
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 |
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 |
424
433
 
425
434
 
426
435
  ## Results
427
436
 
428
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:
429
438
  - [MTEB Results](results/README.md#mteb-results)
430
- - [Classification and Speed Benchmarks](results/README.md#classification-and-speed-benchmarks)
431
439
  - [Ablations](results/README.md#ablations)
432
440
 
433
441
  ## Related work
@@ -435,7 +443,7 @@ We have performed extensive experiments to evaluate the performance of Model2Vec
435
443
  If you are interested in fast small models, also consider looking at these techniques:
436
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.
437
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.
438
- * [wordllama](https://github.com/dleemiller/WordLlama): Uses the _input_ embeddings of a LLama2 model and then performs contrastive learning on these embeddings. As we show above, we think this is a bit overfit on MTEB, as the model is trained on MTEB datasets, and only evaluated on MTEB. It provides an interesting point of comparison to Model2Vec, and, fun fact, was invented at the same time.
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.
439
447
 
440
448
  If you find other related work, please let us know.
441
449
 
@@ -41,14 +41,19 @@
41
41
 
42
42
  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.
43
43
 
44
+
45
+ ## Updates & Announcements
46
+
47
+ - **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).
48
+
44
49
  ## Table of Contents
45
50
  - [Quickstart](#quickstart)
46
51
  - [Main Features](#main-features)
47
52
  - [What is Model2Vec?](#what-is-model2vec)
48
53
  - [Usage](#usage)
49
- - [Distilling a Model2Vec model](#distilling-a-model2vec-model)
50
- - [Inferencing a Model2Vec model](#inference-with-a-model2vec-model)
51
- - [Evaluating a Model2Vec model](#evaluating-a-model2vec-model)
54
+ - [Distillation](#distillation)
55
+ - [Inference](#inference)
56
+ - [Evaluation](#evaluation)
52
57
  - [Model List](#model-list)
53
58
  - [Results](#results)
54
59
  - [Related Work](#related-work)
@@ -71,8 +76,8 @@ The easiest way to get started with Model2Vec is to download one of our [flagshi
71
76
  ```python
72
77
  from model2vec import StaticModel
73
78
 
74
- # Load a model from the HuggingFace hub (in this case the M2V_base_output model)
75
- model_name = "minishlab/M2V_base_output"
79
+ # Load a model from the HuggingFace hub (in this case the potion-base-8M model)
80
+ model_name = "minishlab/potion-base-8M"
76
81
  model = StaticModel.from_pretrained(model_name)
77
82
 
78
83
  # Make embeddings
@@ -106,7 +111,7 @@ from sentence_transformers import SentenceTransformer
106
111
  from sentence_transformers.models import StaticEmbedding
107
112
 
108
113
  # Initialize a StaticEmbedding module
109
- static_embedding = StaticEmbedding.from_model2vec("minishlab/M2V_base_output")
114
+ static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
110
115
  model = SentenceTransformer(modules=[static_embedding])
111
116
  embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
112
117
  ```
@@ -152,6 +157,9 @@ Model2vec has 3 modes:
152
157
 
153
158
  For a technical deepdive into Model2Vec, please refer to our [blog post](https://huggingface.co/blog/Pringled/model2vec).
154
159
 
160
+ ### Tokenlearn
161
+
162
+ 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/).
155
163
 
156
164
 
157
165
  ## Usage
@@ -265,7 +273,7 @@ Inference works as follows. The example shows one of our own models, but you can
265
273
  from model2vec import StaticModel
266
274
 
267
275
  # Load a model from the HuggingFace hub, or a local one.
268
- model_name = "minishlab/M2V_base_output"
276
+ model_name = "minishlab/potion-base-8M"
269
277
  # You can optionally pass a token if you're loading a private model
270
278
  model = StaticModel.from_pretrained(model_name, token=None)
271
279
 
@@ -289,7 +297,7 @@ from sentence_transformers import SentenceTransformer
289
297
  from sentence_transformers.models import StaticEmbedding
290
298
 
291
299
  # Initialize a StaticEmbedding module
292
- static_embedding = StaticEmbedding.from_model2vec("minishlab/M2V_base_output")
300
+ static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
293
301
  model = SentenceTransformer(modules=[static_embedding])
294
302
  embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
295
303
  ```
@@ -307,7 +315,7 @@ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to ever
307
315
  Our models can be evaluated using our [evaluation package](https://github.com/MinishLab/evaluation). Install the evaluation package with:
308
316
 
309
317
  ```bash
310
- pip install evaluation@git+https://github.com MinishLab/evaluation@main
318
+ pip install git+https://github.com/MinishLab/evaluation.git@main
311
319
  ```
312
320
  </details>
313
321
 
@@ -352,19 +360,19 @@ print(make_leaderboard(task_scores))
352
360
 
353
361
  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.
354
362
 
355
- | Model | Language | Vocab | Sentence Transformer | Tokenizer Type | Params |
356
- |------------------------|-------------|------------------|----------------------|----------------|--------------|
357
- | [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 |
358
- | [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 |
359
- | [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 |
360
- | [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | Output | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | Subword | 471M |
363
+
364
+ | Model | Language | Vocab | Sentence Transformer | Tokenizer Type | Params | Tokenlearn |
365
+ |-----------------------------------------------------------------------|-------------|------------------|-----------------------------------------------------------------|----------------|---------|------------|
366
+ | [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 |
367
+ | [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 |
368
+ | [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 |
369
+ | [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | Output | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | Subword | 471M | False |
361
370
 
362
371
 
363
372
  ## Results
364
373
 
365
374
  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:
366
375
  - [MTEB Results](results/README.md#mteb-results)
367
- - [Classification and Speed Benchmarks](results/README.md#classification-and-speed-benchmarks)
368
376
  - [Ablations](results/README.md#ablations)
369
377
 
370
378
  ## Related work
@@ -372,7 +380,7 @@ We have performed extensive experiments to evaluate the performance of Model2Vec
372
380
  If you are interested in fast small models, also consider looking at these techniques:
373
381
  * [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.
374
382
  * [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.
375
- * [wordllama](https://github.com/dleemiller/WordLlama): Uses the _input_ embeddings of a LLama2 model and then performs contrastive learning on these embeddings. As we show above, we think this is a bit overfit on MTEB, as the model is trained on MTEB datasets, and only evaluated on MTEB. It provides an interesting point of comparison to Model2Vec, and, fun fact, was invented at the same time.
383
+ * [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.
376
384
 
377
385
  If you find other related work, please let us know.
378
386
 
@@ -115,7 +115,15 @@ def create_output_embeddings_from_model_name(
115
115
  :return: The tokens and output embeddings.
116
116
  """
117
117
  model = model.to(device)
118
- ids = torch.arange(tokenizer.vocab_size)
118
+
119
+ # Quick check to see if the tokenizer is consistent.
120
+ vocab_length = len(tokenizer.get_vocab())
121
+ if vocab_length != tokenizer.vocab_size:
122
+ logger.warning(
123
+ f"Reported vocab size {tokenizer.vocab_size} is inconsistent with the vocab size {vocab_length}."
124
+ )
125
+
126
+ ids = torch.arange(vocab_length)
119
127
 
120
128
  # Work-around to get the eos and bos token ids without having to go into tokenizer internals.
121
129
  dummy_encoding = tokenizer.encode("A")
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  import json
4
4
  import logging
5
+ from typing import Any
5
6
 
6
7
  from tokenizers import Tokenizer
7
8
 
@@ -36,11 +37,11 @@ def remove_tokens(tokenizer: Tokenizer, tokens_to_remove: list[str]) -> Tokenize
36
37
  logger.info("No tokens to remove.")
37
38
  return Tokenizer.from_str(tokenizer.to_str())
38
39
 
39
- tokenizer_data = json.loads(tokenizer.to_str())
40
+ tokenizer_data: dict[str, Any] = json.loads(tokenizer.to_str())
40
41
 
41
42
  # Find all added tokens
42
- added_tokens = tokenizer_data["added_tokens"]
43
- added_tokens_str = {token["content"] for token in added_tokens}
43
+ added_tokens: list[dict[str, Any]] = tokenizer_data.get("added_tokens", [])
44
+ added_tokens_str: set[str] = {token["content"] for token in added_tokens}
44
45
 
45
46
  # Remove all added tokens from the list of tokens to remove.
46
47
  # Things will go bad if we keep them.
@@ -76,9 +77,9 @@ def remove_tokens(tokenizer: Tokenizer, tokens_to_remove: list[str]) -> Tokenize
76
77
  raise ValueError(f"Unknown model type {model_type}")
77
78
 
78
79
  # Reindex the special tokens (i.e., CLS and SEP for BertTokenizers.)
79
- special_tokens_post_processor: dict[str, dict] = tokenizer_data["post_processor"]["special_tokens"]
80
- for token, token_data in special_tokens_post_processor.items():
81
- token_data["ids"] = [reindexed[token] for token in token_data["tokens"]]
80
+ added_tokens = tokenizer_data.get("added_tokens", [])
81
+ for token_data in added_tokens:
82
+ token_data["id"] = reindexed[token_data["content"]]
82
83
 
83
84
  # Reinitialize the tokenizer from the json.
84
85
  tokenizer = Tokenizer.from_str(json.dumps(tokenizer_data))
@@ -59,6 +59,10 @@ class StaticModel:
59
59
  self.config = config or {}
60
60
  self.base_model_name = base_model_name
61
61
  self.language = language
62
+ if hasattr(self.tokenizer, "encode_batch_fast"):
63
+ self._can_encode_fast = True
64
+ else:
65
+ self._can_encode_fast = False
62
66
 
63
67
  if normalize is not None:
64
68
  self.normalize = normalize
@@ -121,7 +125,11 @@ class StaticModel:
121
125
  m = max_length * self.median_token_length
122
126
  sentences = [sentence[:m] for sentence in sentences]
123
127
 
124
- encodings: list[Encoding] = self.tokenizer.encode_batch_fast(sentences, add_special_tokens=False)
128
+ if self._can_encode_fast:
129
+ encodings: list[Encoding] = self.tokenizer.encode_batch_fast(sentences, add_special_tokens=False)
130
+ else:
131
+ encodings = self.tokenizer.encode_batch(sentences, add_special_tokens=False)
132
+
125
133
  encodings_ids = [encoding.ids for encoding in encodings]
126
134
 
127
135
  if self.unk_token_id is not None:
@@ -264,7 +272,8 @@ class StaticModel:
264
272
 
265
273
  out_array = np.stack(out)
266
274
  if self.normalize:
267
- out_array = out_array / np.linalg.norm(out_array, axis=1, keepdims=True)
275
+ norm = np.linalg.norm(out_array, axis=1, keepdims=True) + 1e-32
276
+ out_array = out_array / norm
268
277
 
269
278
  return out_array
270
279
 
@@ -1,2 +1,2 @@
1
- __version_triple__ = (0, 3, 0)
1
+ __version_triple__ = (0, 3, 1)
2
2
  __version__ = ".".join(map(str, __version_triple__))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: model2vec
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
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
@@ -104,14 +104,19 @@ Requires-Dist: scikit-learn; extra == "distill"
104
104
 
105
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.
106
106
 
107
+
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).
111
+
107
112
  ## Table of Contents
108
113
  - [Quickstart](#quickstart)
109
114
  - [Main Features](#main-features)
110
115
  - [What is Model2Vec?](#what-is-model2vec)
111
116
  - [Usage](#usage)
112
- - [Distilling a Model2Vec model](#distilling-a-model2vec-model)
113
- - [Inferencing a Model2Vec model](#inference-with-a-model2vec-model)
114
- - [Evaluating a Model2Vec model](#evaluating-a-model2vec-model)
117
+ - [Distillation](#distillation)
118
+ - [Inference](#inference)
119
+ - [Evaluation](#evaluation)
115
120
  - [Model List](#model-list)
116
121
  - [Results](#results)
117
122
  - [Related Work](#related-work)
@@ -134,8 +139,8 @@ The easiest way to get started with Model2Vec is to download one of our [flagshi
134
139
  ```python
135
140
  from model2vec import StaticModel
136
141
 
137
- # Load a model from the HuggingFace hub (in this case the M2V_base_output model)
138
- model_name = "minishlab/M2V_base_output"
142
+ # Load a model from the HuggingFace hub (in this case the potion-base-8M model)
143
+ model_name = "minishlab/potion-base-8M"
139
144
  model = StaticModel.from_pretrained(model_name)
140
145
 
141
146
  # Make embeddings
@@ -169,7 +174,7 @@ from sentence_transformers import SentenceTransformer
169
174
  from sentence_transformers.models import StaticEmbedding
170
175
 
171
176
  # Initialize a StaticEmbedding module
172
- static_embedding = StaticEmbedding.from_model2vec("minishlab/M2V_base_output")
177
+ static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
173
178
  model = SentenceTransformer(modules=[static_embedding])
174
179
  embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
175
180
  ```
@@ -215,6 +220,9 @@ Model2vec has 3 modes:
215
220
 
216
221
  For a technical deepdive into Model2Vec, please refer to our [blog post](https://huggingface.co/blog/Pringled/model2vec).
217
222
 
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/).
218
226
 
219
227
 
220
228
  ## Usage
@@ -328,7 +336,7 @@ Inference works as follows. The example shows one of our own models, but you can
328
336
  from model2vec import StaticModel
329
337
 
330
338
  # Load a model from the HuggingFace hub, or a local one.
331
- model_name = "minishlab/M2V_base_output"
339
+ model_name = "minishlab/potion-base-8M"
332
340
  # You can optionally pass a token if you're loading a private model
333
341
  model = StaticModel.from_pretrained(model_name, token=None)
334
342
 
@@ -352,7 +360,7 @@ from sentence_transformers import SentenceTransformer
352
360
  from sentence_transformers.models import StaticEmbedding
353
361
 
354
362
  # Initialize a StaticEmbedding module
355
- static_embedding = StaticEmbedding.from_model2vec("minishlab/M2V_base_output")
363
+ static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
356
364
  model = SentenceTransformer(modules=[static_embedding])
357
365
  embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
358
366
  ```
@@ -370,7 +378,7 @@ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to ever
370
378
  Our models can be evaluated using our [evaluation package](https://github.com/MinishLab/evaluation). Install the evaluation package with:
371
379
 
372
380
  ```bash
373
- pip install evaluation@git+https://github.com MinishLab/evaluation@main
381
+ pip install git+https://github.com/MinishLab/evaluation.git@main
374
382
  ```
375
383
  </details>
376
384
 
@@ -415,19 +423,19 @@ print(make_leaderboard(task_scores))
415
423
 
416
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.
417
425
 
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 |
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 |
424
433
 
425
434
 
426
435
  ## Results
427
436
 
428
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:
429
438
  - [MTEB Results](results/README.md#mteb-results)
430
- - [Classification and Speed Benchmarks](results/README.md#classification-and-speed-benchmarks)
431
439
  - [Ablations](results/README.md#ablations)
432
440
 
433
441
  ## Related work
@@ -435,7 +443,7 @@ We have performed extensive experiments to evaluate the performance of Model2Vec
435
443
  If you are interested in fast small models, also consider looking at these techniques:
436
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.
437
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.
438
- * [wordllama](https://github.com/dleemiller/WordLlama): Uses the _input_ embeddings of a LLama2 model and then performs contrastive learning on these embeddings. As we show above, we think this is a bit overfit on MTEB, as the model is trained on MTEB datasets, and only evaluated on MTEB. It provides an interesting point of comparison to Model2Vec, and, fun fact, was invented at the same time.
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.
439
447
 
440
448
  If you find other related work, please let us know.
441
449
 
@@ -15,6 +15,9 @@ assets/images/sentences_per_second_vs_average_score.png
15
15
  assets/images/speed_vs_accuracy.png
16
16
  assets/images/speed_vs_accuracy_v2.png
17
17
  assets/images/speed_vs_accuracy_v3.png
18
+ assets/images/speed_vs_accuracy_v4.png
19
+ assets/images/speed_vs_mteb_score.png
20
+ assets/images/speed_vs_mteb_score_v2.png
18
21
  assets/images/tutorial_ezlo.png
19
22
  model2vec/__init__.py
20
23
  model2vec/hf_utils.py
@@ -2,7 +2,6 @@
2
2
 
3
3
  This page contains the experiments results of the Model2Vec project. The results are presented in the following sections:
4
4
  - [MTEB Results](#mteb-results)
5
- - [Classification and Speed Benchmarks](#classification-and-speed-benchmarks)
6
5
  - [Ablations](#ablations)
7
6
 
8
7
  ## MTEB Results
@@ -10,16 +9,17 @@ This page contains the experiments results of the Model2Vec project. The results
10
9
  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.
11
10
 
12
11
 
13
-
14
- | Model | Avg (All) | Avg (MTEB) | Class | Clust | PairClass | Rank | Ret | STS | Sum | Pearl | WordSim |
15
- |:-----------------------|:---------:|:----------:|:------:|:------:|:---------:|:------:|:------:|:------:|:------:|:------:|:-------:|
12
+ | Model | Avg (All) | Avg (MTEB) | Class | Clust | PairClass | Rank | Ret | STS | Sum | Pearl | WordSim |
13
+ |:-----------------------|------------:|-------------:|--------:|--------:|------------:|-------:|-------:|-------:|-------:|--------:|----------:|
16
14
  | 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 |
17
- | 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 |
18
- | 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 |
19
- | 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 |
20
- | GloVe_300d | 42.84 | 42.36 | 57.31 | 27.66 | 72.48 | 43.30 | 22.78 | 61.90 | 28.81 | 45.65 | 43.05 |
21
- | 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 |
22
- | WL256* | 48.88 | 49.36 | 58.98 | 33.34 | 74.00 | 52.03 | 33.12 | 73.34 | 29.05 | 48.81 | 45.16 |
15
+ | potion-base-8M | 50.54 | 50.03 | 64.44 | 32.93 | 76.62 | 49.73 | 31.71 | 73.24 | 29.28 | 53.54 | 50.75 |
16
+ | 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 |
17
+ | potion-base-4M | 48.87 | 48.23 | 62.19 | 31.47 | 75.37 | 48.75 | 29.11 | 72.19 | 28.89 | 52.55 | 49.21 |
18
+ | M2V_base_glove | 48.58 | 47.6 | 61.35 | 30.52 | 75.34 | 48.5 | 29.26 | 70.31 | 31.5 | 50.28 | 54.29 |
19
+ | M2V_base_output | 46.79 | 45.34 | 61.25 | 25.58 | 74.9 | 47.63 | 26.14 | 68.58 | 29.2 | 54.02 | 49.18 |
20
+ | potion-base-2M | 45.52 | 44.77 | 58.45 | 27.5 | 73.72 | 46.82 | 24.13 | 70.14 | 31.51 | 50.82 | 44.72 |
21
+ | GloVe_300d | 42.84 | 42.36 | 57.31 | 27.66 | 72.48 | 43.3 | 22.78 | 61.9 | 28.81 | 45.65 | 43.05 |
22
+ | BPEmb_50k_300d | 39.34 | 37.78 | 55.76 | 23.35 | 57.86 | 43.21 | 17.5 | 55.1 | 29.74 | 47.56 | 41.28 |
23
23
 
24
24
 
25
25
  <details>
@@ -35,34 +35,12 @@ For readability, the MTEB task names are abbreviated as follows:
35
35
  - Sum: Summarization
36
36
  </details>
37
37
 
38
- \
39
- \* 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.
40
-
41
- ## Classification and Speed Benchmarks
42
-
43
- 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.
44
-
45
-
46
- | Model | Average | SST2 | IMDB | TREC | AG News |
47
- |:-----------------------|:-------:|:------:|:-----:|:------:|:-------:|
48
- | bge-base-en-v1.5 | 90.00 | 91.54 | 91.88 | 85.16 | 91.45 |
49
- | all-MiniLM-L6-v2 | 84.10 | 83.95 | 81.36 | 81.31 | 89.77 |
50
- | M2V_base_output | 82.23 | 80.92 | 84.56 | 75.27 | 88.17 |
51
- | M2V_base_glove_subword | 81.95 | 82.84 | 85.96 | 70.51 | 88.49 |
52
- | BPEmb_50k_300d | 81.15 | 80.42 | 84.04 | 71.25 | 88.92 |
53
- | M2V_base_glove | 80.76 | 83.07 | 85.24 | 66.12 | 88.61 |
54
- | WL256 | 78.48 | 76.88 | 80.12 | 69.23 | 87.68 |
55
- | GloVe_300d | 77.77 | 81.68 | 84.00 | 55.67 | 89.71 |
56
-
57
-
58
- 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.
59
-
60
- The figure below shows the relationship between the number of sentences per second and the average classification score. The circle sizes correspond to the number of parameters in the models (larger = more parameters).
61
- 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.
38
+ The figure below shows the relationship between the number of sentences per second and the average MTEB score. The circle sizes correspond to the number of parameters in the models (larger = more parameters).
39
+ This plot shows that the Model2Vec models are much faster than the other models, while still being competitive in terms of performance with the [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) model.
62
40
 
63
- | ![Description](../assets/images/speed_vs_accuracy_v3.png) |
41
+ | ![Description](../assets/images/speed_vs_mteb_score_v2.png) |
64
42
  |:--:|
65
- |*Figure: The average accuracy over all classification datasets plotted against sentence per second. The circle size indicates model size.*|
43
+ |*Figure: The average MTEB score plotted against sentences per second. The circle size indicates model size.*|
66
44
 
67
45
 
68
46
  ## Ablations
@@ -24,7 +24,7 @@ def mock_tokenizer() -> Tokenizer:
24
24
  return tokenizer
25
25
 
26
26
 
27
- @pytest.fixture
27
+ @pytest.fixture(scope="function")
28
28
  def mock_berttokenizer() -> AutoTokenizer:
29
29
  """Load the real BertTokenizerFast from the provided tokenizer.json file."""
30
30
  return AutoTokenizer.from_pretrained("tests/data/test_tokenizer")
@@ -25,6 +25,27 @@ def test_initialization_token_vector_mismatch(mock_tokenizer: Tokenizer, mock_co
25
25
  StaticModel(vectors=mock_vectors, tokenizer=mock_tokenizer, config=mock_config)
26
26
 
27
27
 
28
+ def test_tokenize(mock_vectors: np.ndarray, mock_tokenizer: Tokenizer, mock_config: dict[str, str]) -> None:
29
+ """Test tokenization of a sentence."""
30
+ model = StaticModel(vectors=mock_vectors, tokenizer=mock_tokenizer, config=mock_config)
31
+ model._can_encode_fast = True
32
+ tokens_fast = model.tokenize(["word1 word2"])
33
+ model._can_encode_fast = False
34
+ tokens_slow = model.tokenize(["word1 word2"])
35
+
36
+ assert tokens_fast == tokens_slow
37
+
38
+
39
+ def test_encode_batch_fast(
40
+ mock_vectors: np.ndarray, mock_berttokenizer: Tokenizer, mock_config: dict[str, str]
41
+ ) -> None:
42
+ """Test tokenization of a sentence."""
43
+ if hasattr(mock_berttokenizer, "encode_batch_fast"):
44
+ del mock_berttokenizer.encode_batch_fast
45
+ model = StaticModel(vectors=mock_vectors, tokenizer=mock_berttokenizer, config=mock_config)
46
+ assert not model._can_encode_fast
47
+
48
+
28
49
  def test_encode_single_sentence(
29
50
  mock_vectors: np.ndarray, mock_tokenizer: Tokenizer, mock_config: dict[str, str]
30
51
  ) -> None:
@@ -34,6 +55,17 @@ def test_encode_single_sentence(
34
55
  assert encoded.shape == (2,)
35
56
 
36
57
 
58
+ def test_encode_single_sentence_empty(
59
+ mock_vectors: np.ndarray, mock_tokenizer: Tokenizer, mock_config: dict[str, str]
60
+ ) -> None:
61
+ """Test encoding of a single empty sentence."""
62
+ model = StaticModel(vectors=mock_vectors, tokenizer=mock_tokenizer, config=mock_config)
63
+ model.normalize = True
64
+ encoded = model.encode("")
65
+ assert not np.isnan(encoded).any()
66
+ assert np.all(encoded == 0)
67
+
68
+
37
69
  def test_encode_multiple_sentences(
38
70
  mock_vectors: np.ndarray, mock_tokenizer: Tokenizer, mock_config: dict[str, str]
39
71
  ) -> None:
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes