model2vec 0.3.1__tar.gz → 0.3.3__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.3.1 → model2vec-0.3.3}/.github/workflows/ci.yaml +1 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/PKG-INFO +95 -6
- {model2vec-0.3.1 → model2vec-0.3.3}/README.md +86 -2
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/utils.py +10 -8
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/version.py +1 -1
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec.egg-info/PKG-INFO +95 -6
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec.egg-info/SOURCES.txt +1 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec.egg-info/requires.txt +8 -2
- {model2vec-0.3.1 → model2vec-0.3.3}/pyproject.toml +7 -3
- {model2vec-0.3.1 → model2vec-0.3.3}/results/README.md +11 -10
- model2vec-0.3.3/scripts/export_to_onnx.py +208 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/uv.lock +59 -1
- {model2vec-0.3.1 → model2vec-0.3.3}/.gitignore +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/.pre-commit-config.yaml +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/LICENSE +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/Makefile +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/logo.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/logo_v2.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/model2vec_model_diagram.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/model2vec_model_diagram_transparant_dark.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/model2vec_model_diagram_transparant_light.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/sentences_per_second_vs_average_score.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/speed_vs_accuracy.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/speed_vs_accuracy_v2.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/speed_vs_accuracy_v3.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/speed_vs_accuracy_v4.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/speed_vs_mteb_score.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/speed_vs_mteb_score_v2.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/assets/images/tutorial_ezlo.png +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/__init__.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/distill/__init__.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/distill/distillation.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/distill/inference.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/distill/tokenizer.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/distill/utils.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/hf_utils.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/model.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec/model_card_template.md +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec.egg-info/dependency_links.txt +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/model2vec.egg-info/top_level.txt +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/setup.cfg +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tests/__init__.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tests/conftest.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tests/data/test_tokenizer/special_tokens_map.json +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tests/data/test_tokenizer/tokenizer.json +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tests/data/test_tokenizer/tokenizer_config.json +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tests/test_distillation.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tests/test_model.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tests/test_utils.py +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tutorials/README.md +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tutorials/recipe_search.ipynb +0 -0
- {model2vec-0.3.1 → model2vec-0.3.3}/tutorials/semantic_deduplication.ipynb +0 -0
|
@@ -34,6 +34,7 @@ jobs:
|
|
|
34
34
|
if: ${{ runner.os == 'Windows' }}
|
|
35
35
|
run: |
|
|
36
36
|
irm https://astral.sh/uv/install.ps1 | iex
|
|
37
|
+
$env:Path = "C:\Users\runneradmin\.local\bin;$env:Path"
|
|
37
38
|
uv venv .venv
|
|
38
39
|
"VIRTUAL_ENV=.venv" | Out-File -FilePath $env:GITHUB_ENV -Append
|
|
39
40
|
"$PWD/.venv/Scripts" | Out-File -FilePath $env:GITHUB_PATH -Append
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: model2vec
|
|
3
|
-
Version: 0.3.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 0.3.3
|
|
4
|
+
Summary: The Fastest State-of-the-Art Static Embeddings in the World
|
|
5
5
|
Author-email: Stéphan Tulkens <stephantul@gmail.com>, Thomas van Dongen <thomas123@live.nl>
|
|
6
6
|
License: MIT License
|
|
7
7
|
|
|
@@ -43,11 +43,13 @@ Classifier: Natural Language :: English
|
|
|
43
43
|
Requires-Python: >=3.9
|
|
44
44
|
Description-Content-Type: text/markdown
|
|
45
45
|
License-File: LICENSE
|
|
46
|
+
Requires-Dist: jinja2
|
|
46
47
|
Requires-Dist: numpy
|
|
47
48
|
Requires-Dist: rich
|
|
48
|
-
Requires-Dist:
|
|
49
|
-
Requires-Dist: tokenizers>=0.20
|
|
49
|
+
Requires-Dist: safetensors
|
|
50
50
|
Requires-Dist: setuptools
|
|
51
|
+
Requires-Dist: tokenizers>=0.20
|
|
52
|
+
Requires-Dist: tqdm
|
|
51
53
|
Provides-Extra: dev
|
|
52
54
|
Requires-Dist: black; extra == "dev"
|
|
53
55
|
Requires-Dist: ipython; extra == "dev"
|
|
@@ -60,6 +62,9 @@ Provides-Extra: distill
|
|
|
60
62
|
Requires-Dist: torch; extra == "distill"
|
|
61
63
|
Requires-Dist: transformers; extra == "distill"
|
|
62
64
|
Requires-Dist: scikit-learn; extra == "distill"
|
|
65
|
+
Provides-Extra: onnx
|
|
66
|
+
Requires-Dist: onnx; extra == "onnx"
|
|
67
|
+
Requires-Dist: torch; extra == "onnx"
|
|
63
68
|
|
|
64
69
|
|
|
65
70
|
<div align="center">
|
|
@@ -70,7 +75,7 @@ Requires-Dist: scikit-learn; extra == "distill"
|
|
|
70
75
|
</div>
|
|
71
76
|
|
|
72
77
|
<div align="center">
|
|
73
|
-
<h2>
|
|
78
|
+
<h2>The Fastest State-of-the-Art Static Embeddings in the World</h2>
|
|
74
79
|
</div>
|
|
75
80
|
|
|
76
81
|
<div align="center">
|
|
@@ -102,7 +107,7 @@ Requires-Dist: scikit-learn; extra == "distill"
|
|
|
102
107
|
<img src="assets/images/model2vec_model_diagram_transparant_light.png#gh-light-mode-only" width="90%">
|
|
103
108
|
</div>
|
|
104
109
|
|
|
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.
|
|
110
|
+
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. Our [best model](https://huggingface.co/minishlab/potion-base-8M) is the most performant static embedding model in the world. See our results [here](results/README.md), or dive in to see how it works.
|
|
106
111
|
|
|
107
112
|
|
|
108
113
|
## Updates & Announcements
|
|
@@ -117,6 +122,7 @@ Model2Vec is a technique to turn any sentence transformer into a really small st
|
|
|
117
122
|
- [Distillation](#distillation)
|
|
118
123
|
- [Inference](#inference)
|
|
119
124
|
- [Evaluation](#evaluation)
|
|
125
|
+
- [Integrations](#integrations)
|
|
120
126
|
- [Model List](#model-list)
|
|
121
127
|
- [Results](#results)
|
|
122
128
|
- [Related Work](#related-work)
|
|
@@ -419,6 +425,89 @@ print(make_leaderboard(task_scores))
|
|
|
419
425
|
```
|
|
420
426
|
</details>
|
|
421
427
|
|
|
428
|
+
### Integrations
|
|
429
|
+
<details>
|
|
430
|
+
<summary> Sentence Transformers </summary>
|
|
431
|
+
<br>
|
|
432
|
+
|
|
433
|
+
Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) using the `StaticEmbedding` module.
|
|
434
|
+
|
|
435
|
+
The following code snippet shows how to load a Model2Vec model into a Sentence Transformer model:
|
|
436
|
+
```python
|
|
437
|
+
from sentence_transformers import SentenceTransformer
|
|
438
|
+
from sentence_transformers.models import StaticEmbedding
|
|
439
|
+
|
|
440
|
+
# Initialize a StaticEmbedding module
|
|
441
|
+
static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
|
|
442
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
443
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
The following code snippet shows how to distill a model directly into a Sentence Transformer model:
|
|
447
|
+
|
|
448
|
+
```python
|
|
449
|
+
from sentence_transformers import SentenceTransformer
|
|
450
|
+
from sentence_transformers.models import StaticEmbedding
|
|
451
|
+
|
|
452
|
+
static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
|
|
453
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
454
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
</details>
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
<details>
|
|
461
|
+
<summary> Transformers.js </summary>
|
|
462
|
+
|
|
463
|
+
<br>
|
|
464
|
+
|
|
465
|
+
To use a Model2Vec model in [transformers.js](https://github.com/huggingface/transformers.js), the following code snippet can be used as a starting point:
|
|
466
|
+
|
|
467
|
+
```javascript
|
|
468
|
+
import { AutoModel, AutoTokenizer, Tensor } from '@huggingface/transformers';
|
|
469
|
+
|
|
470
|
+
const modelName = 'minishlab/potion-base-8M';
|
|
471
|
+
|
|
472
|
+
const modelConfig = {
|
|
473
|
+
config: { model_type: 'model2vec' },
|
|
474
|
+
dtype: 'fp32',
|
|
475
|
+
revision: 'refs/pr/1'
|
|
476
|
+
};
|
|
477
|
+
const tokenizerConfig = {
|
|
478
|
+
revision: 'refs/pr/2'
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
const model = await AutoModel.from_pretrained(modelName, modelConfig);
|
|
482
|
+
const tokenizer = await AutoTokenizer.from_pretrained(modelName, tokenizerConfig);
|
|
483
|
+
|
|
484
|
+
const texts = ['hello', 'hello world'];
|
|
485
|
+
const { input_ids } = await tokenizer(texts, { add_special_tokens: false, return_tensor: false });
|
|
486
|
+
|
|
487
|
+
const cumsum = arr => arr.reduce((acc, num, i) => [...acc, num + (acc[i - 1] || 0)], []);
|
|
488
|
+
const offsets = [0, ...cumsum(input_ids.slice(0, -1).map(x => x.length))];
|
|
489
|
+
|
|
490
|
+
const flattened_input_ids = input_ids.flat();
|
|
491
|
+
const modelInputs = {
|
|
492
|
+
input_ids: new Tensor('int64', flattened_input_ids, [flattened_input_ids.length]),
|
|
493
|
+
offsets: new Tensor('int64', offsets, [offsets.length])
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
const { embeddings } = await model(modelInputs);
|
|
497
|
+
console.log(embeddings.tolist()); // output matches python version
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
Note that this requires that the Model2Vec has a `model.onnx` file and several required tokenizers file. To generate these for a model that does not have them yet, the following code snippet can be used:
|
|
501
|
+
|
|
502
|
+
```bash
|
|
503
|
+
python scripts/export_to_onnx.py --model_path <path-to-a-model2vec-model> --save_path "<path-to-save-the-onnx-model>"
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
<br>
|
|
508
|
+
</details>
|
|
509
|
+
|
|
510
|
+
|
|
422
511
|
## Model List
|
|
423
512
|
|
|
424
513
|
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.
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
</div>
|
|
8
8
|
|
|
9
9
|
<div align="center">
|
|
10
|
-
<h2>
|
|
10
|
+
<h2>The Fastest State-of-the-Art Static Embeddings in the World</h2>
|
|
11
11
|
</div>
|
|
12
12
|
|
|
13
13
|
<div align="center">
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
<img src="assets/images/model2vec_model_diagram_transparant_light.png#gh-light-mode-only" width="90%">
|
|
40
40
|
</div>
|
|
41
41
|
|
|
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.
|
|
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. Our [best model](https://huggingface.co/minishlab/potion-base-8M) is the most performant static embedding model in the world. See our results [here](results/README.md), or dive in to see how it works.
|
|
43
43
|
|
|
44
44
|
|
|
45
45
|
## Updates & Announcements
|
|
@@ -54,6 +54,7 @@ Model2Vec is a technique to turn any sentence transformer into a really small st
|
|
|
54
54
|
- [Distillation](#distillation)
|
|
55
55
|
- [Inference](#inference)
|
|
56
56
|
- [Evaluation](#evaluation)
|
|
57
|
+
- [Integrations](#integrations)
|
|
57
58
|
- [Model List](#model-list)
|
|
58
59
|
- [Results](#results)
|
|
59
60
|
- [Related Work](#related-work)
|
|
@@ -356,6 +357,89 @@ print(make_leaderboard(task_scores))
|
|
|
356
357
|
```
|
|
357
358
|
</details>
|
|
358
359
|
|
|
360
|
+
### Integrations
|
|
361
|
+
<details>
|
|
362
|
+
<summary> Sentence Transformers </summary>
|
|
363
|
+
<br>
|
|
364
|
+
|
|
365
|
+
Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) using the `StaticEmbedding` module.
|
|
366
|
+
|
|
367
|
+
The following code snippet shows how to load a Model2Vec model into a Sentence Transformer model:
|
|
368
|
+
```python
|
|
369
|
+
from sentence_transformers import SentenceTransformer
|
|
370
|
+
from sentence_transformers.models import StaticEmbedding
|
|
371
|
+
|
|
372
|
+
# Initialize a StaticEmbedding module
|
|
373
|
+
static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
|
|
374
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
375
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
The following code snippet shows how to distill a model directly into a Sentence Transformer model:
|
|
379
|
+
|
|
380
|
+
```python
|
|
381
|
+
from sentence_transformers import SentenceTransformer
|
|
382
|
+
from sentence_transformers.models import StaticEmbedding
|
|
383
|
+
|
|
384
|
+
static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
|
|
385
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
386
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
</details>
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
<details>
|
|
393
|
+
<summary> Transformers.js </summary>
|
|
394
|
+
|
|
395
|
+
<br>
|
|
396
|
+
|
|
397
|
+
To use a Model2Vec model in [transformers.js](https://github.com/huggingface/transformers.js), the following code snippet can be used as a starting point:
|
|
398
|
+
|
|
399
|
+
```javascript
|
|
400
|
+
import { AutoModel, AutoTokenizer, Tensor } from '@huggingface/transformers';
|
|
401
|
+
|
|
402
|
+
const modelName = 'minishlab/potion-base-8M';
|
|
403
|
+
|
|
404
|
+
const modelConfig = {
|
|
405
|
+
config: { model_type: 'model2vec' },
|
|
406
|
+
dtype: 'fp32',
|
|
407
|
+
revision: 'refs/pr/1'
|
|
408
|
+
};
|
|
409
|
+
const tokenizerConfig = {
|
|
410
|
+
revision: 'refs/pr/2'
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const model = await AutoModel.from_pretrained(modelName, modelConfig);
|
|
414
|
+
const tokenizer = await AutoTokenizer.from_pretrained(modelName, tokenizerConfig);
|
|
415
|
+
|
|
416
|
+
const texts = ['hello', 'hello world'];
|
|
417
|
+
const { input_ids } = await tokenizer(texts, { add_special_tokens: false, return_tensor: false });
|
|
418
|
+
|
|
419
|
+
const cumsum = arr => arr.reduce((acc, num, i) => [...acc, num + (acc[i - 1] || 0)], []);
|
|
420
|
+
const offsets = [0, ...cumsum(input_ids.slice(0, -1).map(x => x.length))];
|
|
421
|
+
|
|
422
|
+
const flattened_input_ids = input_ids.flat();
|
|
423
|
+
const modelInputs = {
|
|
424
|
+
input_ids: new Tensor('int64', flattened_input_ids, [flattened_input_ids.length]),
|
|
425
|
+
offsets: new Tensor('int64', offsets, [offsets.length])
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
const { embeddings } = await model(modelInputs);
|
|
429
|
+
console.log(embeddings.tolist()); // output matches python version
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
Note that this requires that the Model2Vec has a `model.onnx` file and several required tokenizers file. To generate these for a model that does not have them yet, the following code snippet can be used:
|
|
433
|
+
|
|
434
|
+
```bash
|
|
435
|
+
python scripts/export_to_onnx.py --model_path <path-to-a-model2vec-model> --save_path "<path-to-save-the-onnx-model>"
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
<br>
|
|
440
|
+
</details>
|
|
441
|
+
|
|
442
|
+
|
|
359
443
|
## Model List
|
|
360
444
|
|
|
361
445
|
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.
|
|
@@ -26,17 +26,19 @@ _MODULE_MAP = (("scikit-learn", "sklearn"),)
|
|
|
26
26
|
|
|
27
27
|
def get_package_extras(package: str, extra: str) -> Iterator[str]:
|
|
28
28
|
"""Get the extras of the package."""
|
|
29
|
-
|
|
29
|
+
try:
|
|
30
|
+
message = metadata(package)
|
|
31
|
+
except Exception as e:
|
|
32
|
+
raise ImportError(f"Could not retrieve metadata for package '{package}': {e}")
|
|
33
|
+
|
|
30
34
|
all_packages = message.get_all("Requires-Dist") or []
|
|
31
35
|
for package in all_packages:
|
|
32
36
|
name, *rest = package.split(";", maxsplit=1)
|
|
33
|
-
if
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if found_extra == extra:
|
|
39
|
-
yield name
|
|
37
|
+
if rest:
|
|
38
|
+
# Extract and clean the extra requirement
|
|
39
|
+
found_extra = rest[0].split("==")[-1].strip(" \"'")
|
|
40
|
+
if found_extra == extra:
|
|
41
|
+
yield name.strip()
|
|
40
42
|
|
|
41
43
|
|
|
42
44
|
def importable(module: str, extra: str) -> None:
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
__version_triple__ = (0, 3,
|
|
1
|
+
__version_triple__ = (0, 3, 3)
|
|
2
2
|
__version__ = ".".join(map(str, __version_triple__))
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: model2vec
|
|
3
|
-
Version: 0.3.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 0.3.3
|
|
4
|
+
Summary: The Fastest State-of-the-Art Static Embeddings in the World
|
|
5
5
|
Author-email: Stéphan Tulkens <stephantul@gmail.com>, Thomas van Dongen <thomas123@live.nl>
|
|
6
6
|
License: MIT License
|
|
7
7
|
|
|
@@ -43,11 +43,13 @@ Classifier: Natural Language :: English
|
|
|
43
43
|
Requires-Python: >=3.9
|
|
44
44
|
Description-Content-Type: text/markdown
|
|
45
45
|
License-File: LICENSE
|
|
46
|
+
Requires-Dist: jinja2
|
|
46
47
|
Requires-Dist: numpy
|
|
47
48
|
Requires-Dist: rich
|
|
48
|
-
Requires-Dist:
|
|
49
|
-
Requires-Dist: tokenizers>=0.20
|
|
49
|
+
Requires-Dist: safetensors
|
|
50
50
|
Requires-Dist: setuptools
|
|
51
|
+
Requires-Dist: tokenizers>=0.20
|
|
52
|
+
Requires-Dist: tqdm
|
|
51
53
|
Provides-Extra: dev
|
|
52
54
|
Requires-Dist: black; extra == "dev"
|
|
53
55
|
Requires-Dist: ipython; extra == "dev"
|
|
@@ -60,6 +62,9 @@ Provides-Extra: distill
|
|
|
60
62
|
Requires-Dist: torch; extra == "distill"
|
|
61
63
|
Requires-Dist: transformers; extra == "distill"
|
|
62
64
|
Requires-Dist: scikit-learn; extra == "distill"
|
|
65
|
+
Provides-Extra: onnx
|
|
66
|
+
Requires-Dist: onnx; extra == "onnx"
|
|
67
|
+
Requires-Dist: torch; extra == "onnx"
|
|
63
68
|
|
|
64
69
|
|
|
65
70
|
<div align="center">
|
|
@@ -70,7 +75,7 @@ Requires-Dist: scikit-learn; extra == "distill"
|
|
|
70
75
|
</div>
|
|
71
76
|
|
|
72
77
|
<div align="center">
|
|
73
|
-
<h2>
|
|
78
|
+
<h2>The Fastest State-of-the-Art Static Embeddings in the World</h2>
|
|
74
79
|
</div>
|
|
75
80
|
|
|
76
81
|
<div align="center">
|
|
@@ -102,7 +107,7 @@ Requires-Dist: scikit-learn; extra == "distill"
|
|
|
102
107
|
<img src="assets/images/model2vec_model_diagram_transparant_light.png#gh-light-mode-only" width="90%">
|
|
103
108
|
</div>
|
|
104
109
|
|
|
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.
|
|
110
|
+
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. Our [best model](https://huggingface.co/minishlab/potion-base-8M) is the most performant static embedding model in the world. See our results [here](results/README.md), or dive in to see how it works.
|
|
106
111
|
|
|
107
112
|
|
|
108
113
|
## Updates & Announcements
|
|
@@ -117,6 +122,7 @@ Model2Vec is a technique to turn any sentence transformer into a really small st
|
|
|
117
122
|
- [Distillation](#distillation)
|
|
118
123
|
- [Inference](#inference)
|
|
119
124
|
- [Evaluation](#evaluation)
|
|
125
|
+
- [Integrations](#integrations)
|
|
120
126
|
- [Model List](#model-list)
|
|
121
127
|
- [Results](#results)
|
|
122
128
|
- [Related Work](#related-work)
|
|
@@ -419,6 +425,89 @@ print(make_leaderboard(task_scores))
|
|
|
419
425
|
```
|
|
420
426
|
</details>
|
|
421
427
|
|
|
428
|
+
### Integrations
|
|
429
|
+
<details>
|
|
430
|
+
<summary> Sentence Transformers </summary>
|
|
431
|
+
<br>
|
|
432
|
+
|
|
433
|
+
Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) using the `StaticEmbedding` module.
|
|
434
|
+
|
|
435
|
+
The following code snippet shows how to load a Model2Vec model into a Sentence Transformer model:
|
|
436
|
+
```python
|
|
437
|
+
from sentence_transformers import SentenceTransformer
|
|
438
|
+
from sentence_transformers.models import StaticEmbedding
|
|
439
|
+
|
|
440
|
+
# Initialize a StaticEmbedding module
|
|
441
|
+
static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
|
|
442
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
443
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
The following code snippet shows how to distill a model directly into a Sentence Transformer model:
|
|
447
|
+
|
|
448
|
+
```python
|
|
449
|
+
from sentence_transformers import SentenceTransformer
|
|
450
|
+
from sentence_transformers.models import StaticEmbedding
|
|
451
|
+
|
|
452
|
+
static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
|
|
453
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
454
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
</details>
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
<details>
|
|
461
|
+
<summary> Transformers.js </summary>
|
|
462
|
+
|
|
463
|
+
<br>
|
|
464
|
+
|
|
465
|
+
To use a Model2Vec model in [transformers.js](https://github.com/huggingface/transformers.js), the following code snippet can be used as a starting point:
|
|
466
|
+
|
|
467
|
+
```javascript
|
|
468
|
+
import { AutoModel, AutoTokenizer, Tensor } from '@huggingface/transformers';
|
|
469
|
+
|
|
470
|
+
const modelName = 'minishlab/potion-base-8M';
|
|
471
|
+
|
|
472
|
+
const modelConfig = {
|
|
473
|
+
config: { model_type: 'model2vec' },
|
|
474
|
+
dtype: 'fp32',
|
|
475
|
+
revision: 'refs/pr/1'
|
|
476
|
+
};
|
|
477
|
+
const tokenizerConfig = {
|
|
478
|
+
revision: 'refs/pr/2'
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
const model = await AutoModel.from_pretrained(modelName, modelConfig);
|
|
482
|
+
const tokenizer = await AutoTokenizer.from_pretrained(modelName, tokenizerConfig);
|
|
483
|
+
|
|
484
|
+
const texts = ['hello', 'hello world'];
|
|
485
|
+
const { input_ids } = await tokenizer(texts, { add_special_tokens: false, return_tensor: false });
|
|
486
|
+
|
|
487
|
+
const cumsum = arr => arr.reduce((acc, num, i) => [...acc, num + (acc[i - 1] || 0)], []);
|
|
488
|
+
const offsets = [0, ...cumsum(input_ids.slice(0, -1).map(x => x.length))];
|
|
489
|
+
|
|
490
|
+
const flattened_input_ids = input_ids.flat();
|
|
491
|
+
const modelInputs = {
|
|
492
|
+
input_ids: new Tensor('int64', flattened_input_ids, [flattened_input_ids.length]),
|
|
493
|
+
offsets: new Tensor('int64', offsets, [offsets.length])
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
const { embeddings } = await model(modelInputs);
|
|
497
|
+
console.log(embeddings.tolist()); // output matches python version
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
Note that this requires that the Model2Vec has a `model.onnx` file and several required tokenizers file. To generate these for a model that does not have them yet, the following code snippet can be used:
|
|
501
|
+
|
|
502
|
+
```bash
|
|
503
|
+
python scripts/export_to_onnx.py --model_path <path-to-a-model2vec-model> --save_path "<path-to-save-the-onnx-model>"
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
<br>
|
|
508
|
+
</details>
|
|
509
|
+
|
|
510
|
+
|
|
422
511
|
## Model List
|
|
423
512
|
|
|
424
513
|
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.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "model2vec"
|
|
3
|
-
description = "
|
|
3
|
+
description = "The Fastest State-of-the-Art Static Embeddings in the World"
|
|
4
4
|
readme = { file = "README.md", content-type = "text/markdown" }
|
|
5
5
|
license = { file = "LICENSE" }
|
|
6
6
|
requires-python = ">=3.9"
|
|
@@ -23,11 +23,13 @@ classifiers = [
|
|
|
23
23
|
]
|
|
24
24
|
|
|
25
25
|
dependencies = [
|
|
26
|
+
"jinja2",
|
|
26
27
|
"numpy",
|
|
27
28
|
"rich",
|
|
28
|
-
"
|
|
29
|
-
"tokenizers>=0.20",
|
|
29
|
+
"safetensors",
|
|
30
30
|
"setuptools",
|
|
31
|
+
"tokenizers>=0.20",
|
|
32
|
+
"tqdm",
|
|
31
33
|
]
|
|
32
34
|
|
|
33
35
|
[build-system]
|
|
@@ -53,6 +55,8 @@ dev = [
|
|
|
53
55
|
]
|
|
54
56
|
distill = ["torch", "transformers", "scikit-learn"]
|
|
55
57
|
|
|
58
|
+
onnx = ["onnx", "torch"]
|
|
59
|
+
|
|
56
60
|
[project.urls]
|
|
57
61
|
"Homepage" = "https://github.com/MinishLab"
|
|
58
62
|
"Bug Reports" = "https://github.com/MinishLab/model2vec/issues"
|
|
@@ -8,18 +8,19 @@ This page contains the experiments results of the Model2Vec project. The results
|
|
|
8
8
|
|
|
9
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.
|
|
10
10
|
|
|
11
|
+
Note: The `potion` and `M2V` models are our static models.
|
|
11
12
|
|
|
12
13
|
| Model | Avg (All) | Avg (MTEB) | Class | Clust | PairClass | Rank | Ret | STS | Sum | Pearl | WordSim |
|
|
13
14
|
|:-----------------------|------------:|-------------:|--------:|--------:|------------:|-------:|-------:|-------:|-------:|--------:|----------:|
|
|
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 |
|
|
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 |
|
|
15
|
+
| [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/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 |
|
|
16
|
+
| [potion-base-8M](https://huggingface.co/minishlab/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 |
|
|
17
|
+
| [M2V_base_glove_subword](https://huggingface.co/minishlab/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
|
+
| [potion-base-4M](https://huggingface.co/minishlab/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 |
|
|
19
|
+
| [M2V_base_glove](https://huggingface.co/minishlab/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 |
|
|
20
|
+
| [M2V_base_output](https://huggingface.co/minishlab/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 |
|
|
21
|
+
| [potion-base-2M](https://huggingface.co/minishlab/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 |
|
|
22
|
+
| [GloVe_300d](https://huggingface.co/sentence-transformers/average_word_embeddings_glove.6B.300d) | 42.84 | 42.36 | 57.31 | 27.66 | 72.48 | 43.3 | 22.78 | 61.9 | 28.81 | 45.65 | 43.05 |
|
|
23
|
+
| [BPEmb_50k_300d](https://github.com/bheinzerling/bpemb) | 39.34 | 37.78 | 55.76 | 23.35 | 57.86 | 43.21 | 17.5 | 55.1 | 29.74 | 47.56 | 41.28 |
|
|
23
24
|
|
|
24
25
|
|
|
25
26
|
<details>
|
|
@@ -36,7 +37,7 @@ For readability, the MTEB task names are abbreviated as follows:
|
|
|
36
37
|
</details>
|
|
37
38
|
|
|
38
39
|
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
|
|
40
|
+
This plot shows that the potion and M2V 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.
|
|
40
41
|
|
|
41
42
|
|  |
|
|
42
43
|
|:--:|
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
from model2vec.utils import get_package_extras, importable
|
|
2
|
+
|
|
3
|
+
# Define the optional dependency group name
|
|
4
|
+
_REQUIRED_EXTRA = "onnx"
|
|
5
|
+
|
|
6
|
+
# Check if each dependency for the "onnx" group is importable
|
|
7
|
+
for extra_dependency in get_package_extras("model2vec", _REQUIRED_EXTRA):
|
|
8
|
+
importable(extra_dependency, _REQUIRED_EXTRA)
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import json
|
|
12
|
+
import logging
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
import torch
|
|
16
|
+
from tokenizers import Tokenizer
|
|
17
|
+
from transformers import AutoTokenizer, PreTrainedTokenizerFast
|
|
18
|
+
|
|
19
|
+
from model2vec import StaticModel
|
|
20
|
+
|
|
21
|
+
logging.basicConfig(level=logging.INFO)
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TorchStaticModel(torch.nn.Module):
|
|
26
|
+
def __init__(self, model: StaticModel) -> None:
|
|
27
|
+
"""Initialize the TorchStaticModel with a StaticModel instance."""
|
|
28
|
+
super().__init__()
|
|
29
|
+
# Convert NumPy embeddings to a torch.nn.EmbeddingBag
|
|
30
|
+
embeddings = torch.tensor(model.embedding, dtype=torch.float32)
|
|
31
|
+
self.embedding_bag = torch.nn.EmbeddingBag.from_pretrained(embeddings, mode="mean", freeze=True)
|
|
32
|
+
self.normalize = model.normalize
|
|
33
|
+
# Save tokenizer attributes
|
|
34
|
+
self.tokenizer = model.tokenizer
|
|
35
|
+
self.unk_token_id = model.unk_token_id
|
|
36
|
+
self.median_token_length = model.median_token_length
|
|
37
|
+
|
|
38
|
+
def forward(self, input_ids: torch.Tensor, offsets: torch.Tensor) -> torch.Tensor:
|
|
39
|
+
"""
|
|
40
|
+
Forward pass of the model.
|
|
41
|
+
|
|
42
|
+
:param input_ids: The input token ids.
|
|
43
|
+
:param offsets: The offsets to compute the mean pooling.
|
|
44
|
+
:return: The embeddings.
|
|
45
|
+
"""
|
|
46
|
+
# Perform embedding lookup and mean pooling
|
|
47
|
+
embeddings = self.embedding_bag(input_ids, offsets)
|
|
48
|
+
# Normalize if required
|
|
49
|
+
if self.normalize:
|
|
50
|
+
embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=-1)
|
|
51
|
+
return embeddings
|
|
52
|
+
|
|
53
|
+
def tokenize(self, sentences: list[str], max_length: int | None = None) -> tuple[torch.Tensor, torch.Tensor]:
|
|
54
|
+
"""
|
|
55
|
+
Tokenize the input sentences.
|
|
56
|
+
|
|
57
|
+
:param sentences: The input sentences.
|
|
58
|
+
:param max_length: The maximum length of the input_ids.
|
|
59
|
+
:return: The input_ids and offsets.
|
|
60
|
+
"""
|
|
61
|
+
# Tokenization logic similar to your StaticModel
|
|
62
|
+
if max_length is not None:
|
|
63
|
+
m = max_length * self.median_token_length
|
|
64
|
+
sentences = [sentence[:m] for sentence in sentences]
|
|
65
|
+
encodings = self.tokenizer.encode_batch(sentences, add_special_tokens=False)
|
|
66
|
+
encodings_ids = [encoding.ids for encoding in encodings]
|
|
67
|
+
if self.unk_token_id is not None:
|
|
68
|
+
# Remove unknown tokens
|
|
69
|
+
encodings_ids = [
|
|
70
|
+
[token_id for token_id in token_ids if token_id != self.unk_token_id] for token_ids in encodings_ids
|
|
71
|
+
]
|
|
72
|
+
if max_length is not None:
|
|
73
|
+
encodings_ids = [token_ids[:max_length] for token_ids in encodings_ids]
|
|
74
|
+
# Flatten input_ids and compute offsets
|
|
75
|
+
offsets = torch.tensor([0] + [len(ids) for ids in encodings_ids[:-1]], dtype=torch.long).cumsum(dim=0)
|
|
76
|
+
input_ids = torch.tensor(
|
|
77
|
+
[token_id for token_ids in encodings_ids for token_id in token_ids],
|
|
78
|
+
dtype=torch.long,
|
|
79
|
+
)
|
|
80
|
+
return input_ids, offsets
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def export_model_to_onnx(model_path: str, save_path: Path) -> None:
|
|
84
|
+
"""
|
|
85
|
+
Export the StaticModel to ONNX format and save tokenizer files.
|
|
86
|
+
|
|
87
|
+
:param model_path: The path to the pretrained StaticModel.
|
|
88
|
+
:param save_path: The directory to save the model and related files.
|
|
89
|
+
"""
|
|
90
|
+
save_path.mkdir(parents=True, exist_ok=True)
|
|
91
|
+
|
|
92
|
+
# Load the StaticModel
|
|
93
|
+
model = StaticModel.from_pretrained(model_path)
|
|
94
|
+
torch_model = TorchStaticModel(model)
|
|
95
|
+
|
|
96
|
+
# Save the model using save_pretrained
|
|
97
|
+
model.save_pretrained(save_path)
|
|
98
|
+
|
|
99
|
+
# Prepare dummy input data
|
|
100
|
+
texts = ["hello", "hello world"]
|
|
101
|
+
input_ids, offsets = torch_model.tokenize(texts)
|
|
102
|
+
|
|
103
|
+
# Export the model to ONNX
|
|
104
|
+
onnx_model_path = save_path / "onnx/model.onnx"
|
|
105
|
+
onnx_model_path.parent.mkdir(parents=True, exist_ok=True)
|
|
106
|
+
torch.onnx.export(
|
|
107
|
+
torch_model,
|
|
108
|
+
(input_ids, offsets),
|
|
109
|
+
str(onnx_model_path),
|
|
110
|
+
export_params=True,
|
|
111
|
+
opset_version=14,
|
|
112
|
+
do_constant_folding=True,
|
|
113
|
+
input_names=["input_ids", "offsets"],
|
|
114
|
+
output_names=["embeddings"],
|
|
115
|
+
dynamic_axes={
|
|
116
|
+
"input_ids": {0: "num_tokens"},
|
|
117
|
+
"offsets": {0: "batch_size"},
|
|
118
|
+
"embeddings": {0: "batch_size"},
|
|
119
|
+
},
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
logger.info(f"Model has been successfully exported to {onnx_model_path}")
|
|
123
|
+
|
|
124
|
+
# Save the tokenizer files required for transformers.js
|
|
125
|
+
save_tokenizer(model.tokenizer, save_path)
|
|
126
|
+
logger.info(f"Tokenizer files have been saved to {save_path}")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def save_tokenizer(tokenizer: Tokenizer, save_directory: Path) -> None:
|
|
130
|
+
"""
|
|
131
|
+
Save tokenizer files in a format compatible with Transformers.
|
|
132
|
+
|
|
133
|
+
:param tokenizer: The tokenizer from the StaticModel.
|
|
134
|
+
:param save_directory: The directory to save the tokenizer files.
|
|
135
|
+
:raises FileNotFoundError: If config.json is not found in save_directory.
|
|
136
|
+
:raises FileNotFoundError: If tokenizer_config.json is not found in save_directory.
|
|
137
|
+
:raises ValueError: If tokenizer_name is not found in config.json.
|
|
138
|
+
"""
|
|
139
|
+
tokenizer_json_path = save_directory / "tokenizer.json"
|
|
140
|
+
tokenizer.save(str(tokenizer_json_path))
|
|
141
|
+
|
|
142
|
+
# Save vocab.txt
|
|
143
|
+
vocab = tokenizer.get_vocab()
|
|
144
|
+
vocab_path = save_directory / "vocab.txt"
|
|
145
|
+
with open(vocab_path, "w", encoding="utf-8") as vocab_file:
|
|
146
|
+
for token in sorted(vocab, key=vocab.get):
|
|
147
|
+
vocab_file.write(f"{token}\n")
|
|
148
|
+
|
|
149
|
+
# Load config.json to get tokenizer_name
|
|
150
|
+
config_path = save_directory / "config.json"
|
|
151
|
+
if config_path.exists():
|
|
152
|
+
with open(config_path, "r", encoding="utf-8") as f:
|
|
153
|
+
config = json.load(f)
|
|
154
|
+
else:
|
|
155
|
+
raise FileNotFoundError(f"config.json not found in {save_directory}")
|
|
156
|
+
|
|
157
|
+
tokenizer_name = config.get("tokenizer_name")
|
|
158
|
+
if not tokenizer_name:
|
|
159
|
+
raise ValueError("tokenizer_name not found in config.json")
|
|
160
|
+
|
|
161
|
+
# Load the original tokenizer
|
|
162
|
+
original_tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
|
|
163
|
+
|
|
164
|
+
# Extract special tokens and tokenizer class
|
|
165
|
+
special_tokens = original_tokenizer.special_tokens_map
|
|
166
|
+
tokenizer_class = original_tokenizer.__class__.__name__
|
|
167
|
+
|
|
168
|
+
# Load the tokenizer using PreTrainedTokenizerFast with special tokens
|
|
169
|
+
fast_tokenizer = PreTrainedTokenizerFast(
|
|
170
|
+
tokenizer_file=str(tokenizer_json_path),
|
|
171
|
+
**special_tokens,
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
# Save the tokenizer files
|
|
175
|
+
fast_tokenizer.save_pretrained(str(save_directory))
|
|
176
|
+
# Modify tokenizer_config.json to set the correct tokenizer_class
|
|
177
|
+
tokenizer_config_path = save_directory / "tokenizer_config.json"
|
|
178
|
+
if tokenizer_config_path.exists():
|
|
179
|
+
with open(tokenizer_config_path, "r", encoding="utf-8") as f:
|
|
180
|
+
tokenizer_config = json.load(f)
|
|
181
|
+
else:
|
|
182
|
+
raise FileNotFoundError(f"tokenizer_config.json not found in {save_directory}")
|
|
183
|
+
|
|
184
|
+
# Update the tokenizer_class field
|
|
185
|
+
tokenizer_config["tokenizer_class"] = tokenizer_class
|
|
186
|
+
|
|
187
|
+
# Write the updated tokenizer_config.json back to disk
|
|
188
|
+
with open(tokenizer_config_path, "w", encoding="utf-8") as f:
|
|
189
|
+
json.dump(tokenizer_config, f, indent=4, sort_keys=True)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
if __name__ == "__main__":
|
|
193
|
+
parser = argparse.ArgumentParser(description="Export StaticModel to ONNX format")
|
|
194
|
+
parser.add_argument(
|
|
195
|
+
"--model_path",
|
|
196
|
+
type=str,
|
|
197
|
+
required=True,
|
|
198
|
+
help="Path to the pretrained StaticModel",
|
|
199
|
+
)
|
|
200
|
+
parser.add_argument(
|
|
201
|
+
"--save_path",
|
|
202
|
+
type=str,
|
|
203
|
+
required=True,
|
|
204
|
+
help="Directory to save the exported model and files",
|
|
205
|
+
)
|
|
206
|
+
args = parser.parse_args()
|
|
207
|
+
|
|
208
|
+
export_model_to_onnx(args.model_path, Path(args.save_path))
|
|
@@ -505,11 +505,13 @@ wheels = [
|
|
|
505
505
|
|
|
506
506
|
[[package]]
|
|
507
507
|
name = "model2vec"
|
|
508
|
-
version = "0.2
|
|
508
|
+
version = "0.3.2"
|
|
509
509
|
source = { editable = "." }
|
|
510
510
|
dependencies = [
|
|
511
|
+
{ name = "jinja2" },
|
|
511
512
|
{ name = "numpy" },
|
|
512
513
|
{ name = "rich" },
|
|
514
|
+
{ name = "safetensors" },
|
|
513
515
|
{ name = "setuptools" },
|
|
514
516
|
{ name = "tokenizers" },
|
|
515
517
|
{ name = "tqdm" },
|
|
@@ -530,22 +532,30 @@ distill = [
|
|
|
530
532
|
{ name = "torch" },
|
|
531
533
|
{ name = "transformers" },
|
|
532
534
|
]
|
|
535
|
+
onnx = [
|
|
536
|
+
{ name = "onnx" },
|
|
537
|
+
{ name = "torch" },
|
|
538
|
+
]
|
|
533
539
|
|
|
534
540
|
[package.metadata]
|
|
535
541
|
requires-dist = [
|
|
536
542
|
{ name = "black", marker = "extra == 'dev'" },
|
|
537
543
|
{ name = "ipython", marker = "extra == 'dev'" },
|
|
544
|
+
{ name = "jinja2" },
|
|
538
545
|
{ name = "mypy", marker = "extra == 'dev'" },
|
|
539
546
|
{ name = "numpy" },
|
|
547
|
+
{ name = "onnx", marker = "extra == 'onnx'" },
|
|
540
548
|
{ name = "pre-commit", marker = "extra == 'dev'" },
|
|
541
549
|
{ name = "pytest", marker = "extra == 'dev'" },
|
|
542
550
|
{ name = "pytest-coverage", marker = "extra == 'dev'" },
|
|
543
551
|
{ name = "rich" },
|
|
544
552
|
{ name = "ruff", marker = "extra == 'dev'" },
|
|
553
|
+
{ name = "safetensors" },
|
|
545
554
|
{ name = "scikit-learn", marker = "extra == 'distill'" },
|
|
546
555
|
{ name = "setuptools" },
|
|
547
556
|
{ name = "tokenizers", specifier = ">=0.20" },
|
|
548
557
|
{ name = "torch", marker = "extra == 'distill'" },
|
|
558
|
+
{ name = "torch", marker = "extra == 'onnx'" },
|
|
549
559
|
{ name = "tqdm" },
|
|
550
560
|
{ name = "transformers", marker = "extra == 'distill'" },
|
|
551
561
|
]
|
|
@@ -797,6 +807,38 @@ wheels = [
|
|
|
797
807
|
{ url = "https://files.pythonhosted.org/packages/87/20/199b8713428322a2f22b722c62b8cc278cc53dffa9705d744484b5035ee9/nvidia_nvtx_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl", hash = "sha256:781e950d9b9f60d8241ccea575b32f5105a5baf4c2351cab5256a24869f12a1a", size = 99144 },
|
|
798
808
|
]
|
|
799
809
|
|
|
810
|
+
[[package]]
|
|
811
|
+
name = "onnx"
|
|
812
|
+
version = "1.17.0"
|
|
813
|
+
source = { registry = "https://pypi.org/simple" }
|
|
814
|
+
dependencies = [
|
|
815
|
+
{ name = "numpy" },
|
|
816
|
+
{ name = "protobuf" },
|
|
817
|
+
]
|
|
818
|
+
sdist = { url = "https://files.pythonhosted.org/packages/9a/54/0e385c26bf230d223810a9c7d06628d954008a5e5e4b73ee26ef02327282/onnx-1.17.0.tar.gz", hash = "sha256:48ca1a91ff73c1d5e3ea2eef20ae5d0e709bb8a2355ed798ffc2169753013fd3", size = 12165120 }
|
|
819
|
+
wheels = [
|
|
820
|
+
{ url = "https://files.pythonhosted.org/packages/2e/29/57053ba7787788ac75efb095cfc1ae290436b6d3a26754693cd7ed1b4fac/onnx-1.17.0-cp310-cp310-macosx_12_0_universal2.whl", hash = "sha256:38b5df0eb22012198cdcee527cc5f917f09cce1f88a69248aaca22bd78a7f023", size = 16645616 },
|
|
821
|
+
{ url = "https://files.pythonhosted.org/packages/75/0d/831807a18db2a5e8f7813848c59272b904a4ef3939fe4d1288cbce9ea735/onnx-1.17.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d545335cb49d4d8c47cc803d3a805deb7ad5d9094dc67657d66e568610a36d7d", size = 15908420 },
|
|
822
|
+
{ url = "https://files.pythonhosted.org/packages/dd/5b/c4f95dbe652d14aeba9afaceb177e9ffc48ac3c03048dd3f872f26f07e34/onnx-1.17.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3193a3672fc60f1a18c0f4c93ac81b761bc72fd8a6c2035fa79ff5969f07713e", size = 16046244 },
|
|
823
|
+
{ url = "https://files.pythonhosted.org/packages/08/a9/c1f218085043dccc6311460239e253fa6957cf12ee4b0a56b82014938d0b/onnx-1.17.0-cp310-cp310-win32.whl", hash = "sha256:0141c2ce806c474b667b7e4499164227ef594584da432fd5613ec17c1855e311", size = 14423516 },
|
|
824
|
+
{ url = "https://files.pythonhosted.org/packages/0e/d3/d26ebf590a65686dde6b27fef32493026c5be9e42083340d947395f93405/onnx-1.17.0-cp310-cp310-win_amd64.whl", hash = "sha256:dfd777d95c158437fda6b34758f0877d15b89cbe9ff45affbedc519b35345cf9", size = 14528496 },
|
|
825
|
+
{ url = "https://files.pythonhosted.org/packages/e5/a9/8d1b1d53aec70df53e0f57e9f9fcf47004276539e29230c3d5f1f50719ba/onnx-1.17.0-cp311-cp311-macosx_12_0_universal2.whl", hash = "sha256:d6fc3a03fc0129b8b6ac03f03bc894431ffd77c7d79ec023d0afd667b4d35869", size = 16647991 },
|
|
826
|
+
{ url = "https://files.pythonhosted.org/packages/7b/e3/cc80110e5996ca61878f7b4c73c7a286cd88918ff35eacb60dc75ab11ef5/onnx-1.17.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f01a4b63d4e1d8ec3e2f069e7b798b2955810aa434f7361f01bc8ca08d69cce4", size = 15908949 },
|
|
827
|
+
{ url = "https://files.pythonhosted.org/packages/b1/2f/91092557ed478e323a2b4471e2081fdf88d1dd52ae988ceaf7db4e4506ff/onnx-1.17.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4a183c6178be001bf398260e5ac2c927dc43e7746e8638d6c05c20e321f8c949", size = 16048190 },
|
|
828
|
+
{ url = "https://files.pythonhosted.org/packages/ac/59/9ea23fc22d0bb853133f363e6248e31bcbc6c1c90543a3938c00412ac02a/onnx-1.17.0-cp311-cp311-win32.whl", hash = "sha256:081ec43a8b950171767d99075b6b92553901fa429d4bc5eb3ad66b36ef5dbe3a", size = 14424299 },
|
|
829
|
+
{ url = "https://files.pythonhosted.org/packages/51/a5/19b0dfcb567b62e7adf1a21b08b23224f0c2d13842aee4d0abc6f07f9cf5/onnx-1.17.0-cp311-cp311-win_amd64.whl", hash = "sha256:95c03e38671785036bb704c30cd2e150825f6ab4763df3a4f1d249da48525957", size = 14529142 },
|
|
830
|
+
{ url = "https://files.pythonhosted.org/packages/b4/dd/c416a11a28847fafb0db1bf43381979a0f522eb9107b831058fde012dd56/onnx-1.17.0-cp312-cp312-macosx_12_0_universal2.whl", hash = "sha256:0e906e6a83437de05f8139ea7eaf366bf287f44ae5cc44b2850a30e296421f2f", size = 16651271 },
|
|
831
|
+
{ url = "https://files.pythonhosted.org/packages/f0/6c/f040652277f514ecd81b7251841f96caa5538365af7df07f86c6018cda2b/onnx-1.17.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3d955ba2939878a520a97614bcf2e79c1df71b29203e8ced478fa78c9a9c63c2", size = 15907522 },
|
|
832
|
+
{ url = "https://files.pythonhosted.org/packages/3d/7c/67f4952d1b56b3f74a154b97d0dd0630d525923b354db117d04823b8b49b/onnx-1.17.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4f3fb5cc4e2898ac5312a7dc03a65133dd2abf9a5e520e69afb880a7251ec97a", size = 16046307 },
|
|
833
|
+
{ url = "https://files.pythonhosted.org/packages/ae/20/6da11042d2ab870dfb4ce4a6b52354d7651b6b4112038b6d2229ab9904c4/onnx-1.17.0-cp312-cp312-win32.whl", hash = "sha256:317870fca3349d19325a4b7d1b5628f6de3811e9710b1e3665c68b073d0e68d7", size = 14424235 },
|
|
834
|
+
{ url = "https://files.pythonhosted.org/packages/35/55/c4d11bee1fdb0c4bd84b4e3562ff811a19b63266816870ae1f95567aa6e1/onnx-1.17.0-cp312-cp312-win_amd64.whl", hash = "sha256:659b8232d627a5460d74fd3c96947ae83db6d03f035ac633e20cd69cfa029227", size = 14530453 },
|
|
835
|
+
{ url = "https://files.pythonhosted.org/packages/49/e1/c5301ff2afa4c473d32a4e9f1bed5c589cfc4947c79002a00183f4cc0fa1/onnx-1.17.0-cp39-cp39-macosx_12_0_universal2.whl", hash = "sha256:67e1c59034d89fff43b5301b6178222e54156eadd6ab4cd78ddc34b2f6274a66", size = 16645989 },
|
|
836
|
+
{ url = "https://files.pythonhosted.org/packages/61/94/d753c230d56234dd01ad939590a2ed33221b57c61abe513ff6823a69af6e/onnx-1.17.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3e19fd064b297f7773b4c1150f9ce6213e6d7d041d7a9201c0d348041009cdcd", size = 15908316 },
|
|
837
|
+
{ url = "https://files.pythonhosted.org/packages/3d/da/c19d0f20d310045f4701d75ecba4f765153251d48a32f27a5d6b0a7e3799/onnx-1.17.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8167295f576055158a966161f8ef327cb491c06ede96cc23392be6022071b6ed", size = 16046488 },
|
|
838
|
+
{ url = "https://files.pythonhosted.org/packages/57/1a/79623a6cd305dfcd21888747364994109dfcb6194343157cb8653f1612dc/onnx-1.17.0-cp39-cp39-win32.whl", hash = "sha256:76884fe3e0258c911c749d7d09667fb173365fd27ee66fcedaf9fa039210fd13", size = 14423724 },
|
|
839
|
+
{ url = "https://files.pythonhosted.org/packages/57/8e/ce0e20200bdf8e8b47679cd56efb1057aa218b29ccdf60a3b4fb6b91064c/onnx-1.17.0-cp39-cp39-win_amd64.whl", hash = "sha256:5ca7a0894a86d028d509cdcf99ed1864e19bfe5727b44322c11691d834a1c546", size = 14524172 },
|
|
840
|
+
]
|
|
841
|
+
|
|
800
842
|
[[package]]
|
|
801
843
|
name = "packaging"
|
|
802
844
|
version = "24.1"
|
|
@@ -882,6 +924,22 @@ wheels = [
|
|
|
882
924
|
{ url = "https://files.pythonhosted.org/packages/a9/6a/fd08d94654f7e67c52ca30523a178b3f8ccc4237fce4be90d39c938a831a/prompt_toolkit-3.0.48-py3-none-any.whl", hash = "sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e", size = 386595 },
|
|
883
925
|
]
|
|
884
926
|
|
|
927
|
+
[[package]]
|
|
928
|
+
name = "protobuf"
|
|
929
|
+
version = "5.28.3"
|
|
930
|
+
source = { registry = "https://pypi.org/simple" }
|
|
931
|
+
sdist = { url = "https://files.pythonhosted.org/packages/74/6e/e69eb906fddcb38f8530a12f4b410699972ab7ced4e21524ece9d546ac27/protobuf-5.28.3.tar.gz", hash = "sha256:64badbc49180a5e401f373f9ce7ab1d18b63f7dd4a9cdc43c92b9f0b481cef7b", size = 422479 }
|
|
932
|
+
wheels = [
|
|
933
|
+
{ url = "https://files.pythonhosted.org/packages/d1/c5/05163fad52d7c43e124a545f1372d18266db36036377ad29de4271134a6a/protobuf-5.28.3-cp310-abi3-win32.whl", hash = "sha256:0c4eec6f987338617072592b97943fdbe30d019c56126493111cf24344c1cc24", size = 419624 },
|
|
934
|
+
{ url = "https://files.pythonhosted.org/packages/9c/4c/4563ebe001ff30dca9d7ed12e471fa098d9759712980cde1fd03a3a44fb7/protobuf-5.28.3-cp310-abi3-win_amd64.whl", hash = "sha256:91fba8f445723fcf400fdbe9ca796b19d3b1242cd873907979b9ed71e4afe868", size = 431464 },
|
|
935
|
+
{ url = "https://files.pythonhosted.org/packages/1c/f2/baf397f3dd1d3e4af7e3f5a0382b868d25ac068eefe1ebde05132333436c/protobuf-5.28.3-cp38-abi3-macosx_10_9_universal2.whl", hash = "sha256:a3f6857551e53ce35e60b403b8a27b0295f7d6eb63d10484f12bc6879c715687", size = 414743 },
|
|
936
|
+
{ url = "https://files.pythonhosted.org/packages/85/50/cd61a358ba1601f40e7d38bcfba22e053f40ef2c50d55b55926aecc8fec7/protobuf-5.28.3-cp38-abi3-manylinux2014_aarch64.whl", hash = "sha256:3fa2de6b8b29d12c61911505d893afe7320ce7ccba4df913e2971461fa36d584", size = 316511 },
|
|
937
|
+
{ url = "https://files.pythonhosted.org/packages/5d/ae/3257b09328c0b4e59535e497b0c7537d4954038bdd53a2f0d2f49d15a7c4/protobuf-5.28.3-cp38-abi3-manylinux2014_x86_64.whl", hash = "sha256:712319fbdddb46f21abb66cd33cb9e491a5763b2febd8f228251add221981135", size = 316624 },
|
|
938
|
+
{ url = "https://files.pythonhosted.org/packages/57/b5/ee3d918f536168def73b3f49edeba065429ab3a7e7b033d33e69c46ddff9/protobuf-5.28.3-cp39-cp39-win32.whl", hash = "sha256:135658402f71bbd49500322c0f736145731b16fc79dc8f367ab544a17eab4535", size = 419648 },
|
|
939
|
+
{ url = "https://files.pythonhosted.org/packages/53/54/e1bdf6f1d29828ddb6aca0a83bf208ab1d5f88126f34e17e487b2cd20d93/protobuf-5.28.3-cp39-cp39-win_amd64.whl", hash = "sha256:70585a70fc2dd4818c51287ceef5bdba6387f88a578c86d47bb34669b5552c36", size = 431591 },
|
|
940
|
+
{ url = "https://files.pythonhosted.org/packages/ad/c3/2377c159e28ea89a91cf1ca223f827ae8deccb2c9c401e5ca233cd73002f/protobuf-5.28.3-py3-none-any.whl", hash = "sha256:cee1757663fa32a1ee673434fcf3bf24dd54763c79690201208bafec62f19eed", size = 169511 },
|
|
941
|
+
]
|
|
942
|
+
|
|
885
943
|
[[package]]
|
|
886
944
|
name = "ptyprocess"
|
|
887
945
|
version = "0.7.0"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{model2vec-0.3.1 → model2vec-0.3.3}/assets/images/model2vec_model_diagram_transparant_dark.png
RENAMED
|
File without changes
|
{model2vec-0.3.1 → model2vec-0.3.3}/assets/images/model2vec_model_diagram_transparant_light.png
RENAMED
|
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
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|