Upload README.md
Browse files
README.md
CHANGED
|
@@ -1,223 +1,223 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
base_model: microsoft/MiniLM-L6-v2
|
| 4 |
-
tags:
|
| 5 |
-
- transformers
|
| 6 |
-
- sentence-transformers
|
| 7 |
-
- sentence-similarity
|
| 8 |
-
- feature-extraction
|
| 9 |
-
- text-embeddings-inference
|
| 10 |
-
- information-retrieval
|
| 11 |
-
- knowledge-distillation
|
| 12 |
-
language:
|
| 13 |
-
- en
|
| 14 |
-
---
|
| 15 |
-
<div style="display: flex; justify-content: center;">
|
| 16 |
-
<div style="display: flex; align-items: center; gap: 10px;">
|
| 17 |
-
<img src="logo.webp" alt="MongoDB Logo" style="height: 36px; width: auto; border-radius: 4px;">
|
| 18 |
-
<span style="font-size: 32px; font-weight: bold">MongoDB/mdbr-leaf-ir</span>
|
| 19 |
-
</div>
|
| 20 |
-
</div>
|
| 21 |
-
|
| 22 |
-
# Content
|
| 23 |
-
|
| 24 |
-
1. [Introduction](#introduction)
|
| 25 |
-
2. [Technical Report](#technical-report)
|
| 26 |
-
3. [Highlights](#highlights)
|
| 27 |
-
4. [Benchmarks](#benchmark-comparison)
|
| 28 |
-
5. [Quickstart](#quickstart)
|
| 29 |
-
6. [Citation](#citation)
|
| 30 |
-
|
| 31 |
-
# Introduction
|
| 32 |
-
|
| 33 |
-
`mdbr-leaf-ir` is a compact high-performance text embedding model specifically designed for **information retrieval (IR)** tasks, e.g., the retrieval stage of Retrieval-Augmented Generation (RAG) pipelines.
|
| 34 |
-
|
| 35 |
-
To enable even greater efficiency, `mdbr-leaf-ir` supports [flexible asymmetric architectures](#asymmetric-retrieval-setup) and is robust to [vector quantization](#vector-quantization) and [MRL truncation](#mrl-truncation).
|
| 36 |
-
|
| 37 |
-
If you are looking to perform other tasks such as classification, clustering, semantic sentence similarity, summarization, please check out our [`mdbr-leaf-mt`](https://huggingface.co/MongoDB/mdbr-leaf-mt) model.
|
| 38 |
-
|
| 39 |
-
> [!Note]
|
| 40 |
-
> **Note**: this model has been developed by the ML team of MongoDB Research. At the time of writing it is not used in any of MongoDB's commercial product or service offerings.
|
| 41 |
-
|
| 42 |
-
# Technical Report
|
| 43 |
-
|
| 44 |
-
A technical report detailing our proposed `LEAF` training procedure
|
| 45 |
-
|
| 46 |
-
# Highlights
|
| 47 |
-
|
| 48 |
-
* **State-of-the-Art Performance**: `mdbr-leaf-ir` achieves state-of-the-art results for compact embedding models, **ranking #1** on the public [BEIR benchmark leaderboard](https://huggingface.co/spaces/mteb/leaderboard) for models with ≤100M parameters.
|
| 49 |
-
* **Flexible Architecture Support**: `mdbr-leaf-ir` supports asymmetric retrieval architectures enabling even greater retrieval results. [See below](#asymmetric-retrieval-setup) for more information.
|
| 50 |
-
* **MRL and Quantization Support**: embedding vectors generated by `mdbr-leaf-ir` compress well when truncated (MRL) and can be stored using more efficient types like `int8` and `binary`. [See below](#mrl-truncation) for more information.
|
| 51 |
-
|
| 52 |
-
## Benchmark Comparison
|
| 53 |
-
|
| 54 |
-
The table below shows the average BEIR benchmark scores (nDCG@10) for `mdbr-leaf-ir` compared to other retrieval models.
|
| 55 |
-
|
| 56 |
-
`mdbr-leaf-ir` ranks #1 on the BEIR public leaderboard, and when run in asymmetric "**(asym.)**" mode as described [here](#asymmetric-retrieval-setup), the results improve even further.
|
| 57 |
-
|
| 58 |
-
| Model | Size | BEIR Avg. (nDCG@10) |
|
| 59 |
-
|------------------------------------|---------|----------------------|
|
| 60 |
-
| OpenAI text-embedding-3-large | Unknown | 55.43 |
|
| 61 |
-
| **mdbr-leaf-ir (asym.)** | 23M | **54.03** |
|
| 62 |
-
| **mdbr-leaf-ir** | 23M | **53.55** |
|
| 63 |
-
| snowflake-arctic-embed-s | 32M | 51.98 |
|
| 64 |
-
| bge-small-en-v1.5 | 33M | 51.65 |
|
| 65 |
-
| OpenAI text-embedding-3-small | Unknown | 51.08 |
|
| 66 |
-
| granite-embedding-small-english-r2 | 47M | 50.87 |
|
| 67 |
-
| snowflake-arctic-embed-xs | 23M | 50.15 |
|
| 68 |
-
| e5-small-v2 | 33M | 49.04 |
|
| 69 |
-
| SPLADE++ | 110M | 48.88 |
|
| 70 |
-
| MiniLM-L6-v2 | 23M | 41.95 |
|
| 71 |
-
| BM25 | – | 41.14 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# Quickstart
|
| 75 |
-
|
| 76 |
-
## Sentence Transformers
|
| 77 |
-
|
| 78 |
-
```python
|
| 79 |
-
from sentence_transformers import SentenceTransformer
|
| 80 |
-
|
| 81 |
-
# Load the model
|
| 82 |
-
model = SentenceTransformer("MongoDB/mdbr-leaf-ir")
|
| 83 |
-
|
| 84 |
-
# Example queries and documents
|
| 85 |
-
queries = [
|
| 86 |
-
"What is machine learning?",
|
| 87 |
-
"How does neural network training work?"
|
| 88 |
-
]
|
| 89 |
-
|
| 90 |
-
documents = [
|
| 91 |
-
"Machine learning is a subset of artificial intelligence that focuses on algorithms that can learn from data.",
|
| 92 |
-
"Neural networks are trained through backpropagation, adjusting weights to minimize prediction errors."
|
| 93 |
-
]
|
| 94 |
-
|
| 95 |
-
# Encode queries and documents
|
| 96 |
-
query_embeddings = model.encode(queries, prompt_name="query")
|
| 97 |
-
document_embeddings = model.encode(documents)
|
| 98 |
-
|
| 99 |
-
# Compute similarity scores
|
| 100 |
-
scores = model.similarity(query_embeddings, document_embeddings)
|
| 101 |
-
|
| 102 |
-
# Print results
|
| 103 |
-
for i, query in enumerate(queries):
|
| 104 |
-
print(f"Query: {query}")
|
| 105 |
-
for j, doc in enumerate(documents):
|
| 106 |
-
print(f" Similarity: {scores[i, j]:.4f} | Document {j}: {doc[:80]}...")
|
| 107 |
-
|
| 108 |
-
# Query: What is machine learning?
|
| 109 |
-
# Similarity: 0.6857 | Document 0: Machine learning is a subset of ...
|
| 110 |
-
# Similarity: 0.4598 | Document 1: Neural networks are trained ...
|
| 111 |
-
#
|
| 112 |
-
# Query: How does neural network training work?
|
| 113 |
-
# Similarity: 0.4238 | Document 0: Machine learning is a subset of ...
|
| 114 |
-
# Similarity: 0.5723 | Document 1: Neural networks are trained ...
|
| 115 |
-
```
|
| 116 |
-
|
| 117 |
-
## Transformers Usage
|
| 118 |
-
|
| 119 |
-
See full example notebook [here](https://huggingface.co/MongoDB/mdbr-leaf-ir/blob/main/transformers_example.ipynb).
|
| 120 |
-
|
| 121 |
-
## Asymmetric Retrieval Setup
|
| 122 |
-
|
| 123 |
-
`mdbr-leaf-ir` is *aligned* to [`snowflake-arctic-embed-m-v1.5`](https://huggingface.co/Snowflake/snowflake-arctic-embed-m-v1.5), the model it has been distilled from. This enables flexible architectures in which, for example, documents are encoded using the larger model, while queries can be encoded faster and more efficiently with the compact `leaf` model:
|
| 124 |
-
```python
|
| 125 |
-
# Use mdbr-leaf-ir for query encoding (real-time, low latency)
|
| 126 |
-
query_model = SentenceTransformer("MongoDB/mdbr-leaf-ir")
|
| 127 |
-
query_embeddings = query_model.encode(queries, prompt_name="query")
|
| 128 |
-
|
| 129 |
-
# Use a larger model for document encoding (one-time, at index time)
|
| 130 |
-
doc_model = SentenceTransformer("Snowflake/snowflake-arctic-embed-m-v1.5")
|
| 131 |
-
document_embeddings = doc_model.encode(documents)
|
| 132 |
-
|
| 133 |
-
# Compute similarities
|
| 134 |
-
scores = query_model.similarity(query_embeddings, document_embeddings)
|
| 135 |
-
```
|
| 136 |
-
Retrieval results in asymmetric mode are often superior to the [standard mode above](#sentence-transformers).
|
| 137 |
-
|
| 138 |
-
## MRL Truncation
|
| 139 |
-
|
| 140 |
-
Embeddings have been trained via [MRL](https://arxiv.org/abs/2205.13147) and can be truncated for more efficient storage:
|
| 141 |
-
```python
|
| 142 |
-
from torch.nn import functional as F
|
| 143 |
-
|
| 144 |
-
query_embeds = model.encode(queries, prompt_name="query", convert_to_tensor=True)
|
| 145 |
-
doc_embeds = model.encode(documents, convert_to_tensor=True)
|
| 146 |
-
|
| 147 |
-
# Truncate and normalize according to MRL
|
| 148 |
-
query_embeds = F.normalize(query_embeds[:, :256], dim=-1)
|
| 149 |
-
doc_embeds = F.normalize(doc_embeds[:, :256], dim=-1)
|
| 150 |
-
|
| 151 |
-
similarities = model.similarity(query_embeds, doc_embeds)
|
| 152 |
-
|
| 153 |
-
print('After MRL:')
|
| 154 |
-
print(f"* Embeddings dimension: {query_embeds.shape[1]}")
|
| 155 |
-
print(f"* Similarities:\n\t{similarities}")
|
| 156 |
-
|
| 157 |
-
# After MRL:
|
| 158 |
-
# * Embeddings dimension: 256
|
| 159 |
-
# * Similarities:
|
| 160 |
-
# tensor([[0.7136, 0.4989],
|
| 161 |
-
# [0.4567, 0.6022]])
|
| 162 |
-
```
|
| 163 |
-
|
| 164 |
-
## Vector Quantization
|
| 165 |
-
Vector quantization, for example to `int8` or `binary`, can be performed as follows:
|
| 166 |
-
|
| 167 |
-
**Note**: For vector quantization to types other than binary, we suggest performing a calibration to determine the optimal ranges, [see here](https://sbert.net/examples/sentence_transformer/applications/embedding-quantization/README.html#scalar-int8-quantization).
|
| 168 |
-
Good initial values, according to the [teacher model's documentation](https://huggingface.co/Snowflake/snowflake-arctic-embed-m-v1.5#compressing-to-128-bytes), are:
|
| 169 |
-
* `int8`: -0.3 and +0.3
|
| 170 |
-
* `int4`: -0.18 and +0.18
|
| 171 |
-
```python
|
| 172 |
-
from sentence_transformers.quantization import quantize_embeddings
|
| 173 |
-
import torch
|
| 174 |
-
|
| 175 |
-
query_embeds = model.encode(queries, prompt_name="query")
|
| 176 |
-
doc_embeds = model.encode(documents)
|
| 177 |
-
|
| 178 |
-
# Quantize embeddings to int8 using -0.3 and +0.3 as calibration ranges
|
| 179 |
-
ranges = torch.tensor([[-0.3], [+0.3]]).expand(2, query_embeds.shape[1]).cpu().numpy()
|
| 180 |
-
query_embeds = quantize_embeddings(query_embeds, "int8", ranges=ranges)
|
| 181 |
-
doc_embeds = quantize_embeddings(doc_embeds, "int8", ranges=ranges)
|
| 182 |
-
|
| 183 |
-
# Calculate similarities; cast to int64 to avoid under/overflow
|
| 184 |
-
similarities = query_embeds.astype(int) @ doc_embeds.astype(int).T
|
| 185 |
-
|
| 186 |
-
print('After quantization:')
|
| 187 |
-
print(f"* Embeddings type: {query_embeds.dtype}")
|
| 188 |
-
print(f"* Similarities:\n{similarities}")
|
| 189 |
-
|
| 190 |
-
# After quantization:
|
| 191 |
-
# * Embeddings type: int8
|
| 192 |
-
# * Similarities:
|
| 193 |
-
# [[118022 79111]
|
| 194 |
-
# [ 72961 98333]]
|
| 195 |
-
```
|
| 196 |
-
|
| 197 |
-
# Evaluation
|
| 198 |
-
|
| 199 |
-
Please [see here](https://huggingface.co/MongoDB/mdbr-leaf-ir/blob/main/evaluate_models.ipynb).
|
| 200 |
-
|
| 201 |
-
# Citation
|
| 202 |
-
|
| 203 |
-
If you use this model in your work, please cite:
|
| 204 |
-
|
| 205 |
-
```bibtex
|
| 206 |
-
@
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
}
|
| 215 |
-
```
|
| 216 |
-
|
| 217 |
-
# License
|
| 218 |
-
|
| 219 |
-
This model is released under Apache 2.0 License.
|
| 220 |
-
|
| 221 |
-
# Contact
|
| 222 |
-
|
| 223 |
For questions or issues, please open an issue or pull request. You can also contact the MongoDB ML research team at robin.vujanic@mongodb.com.
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
base_model: microsoft/MiniLM-L6-v2
|
| 4 |
+
tags:
|
| 5 |
+
- transformers
|
| 6 |
+
- sentence-transformers
|
| 7 |
+
- sentence-similarity
|
| 8 |
+
- feature-extraction
|
| 9 |
+
- text-embeddings-inference
|
| 10 |
+
- information-retrieval
|
| 11 |
+
- knowledge-distillation
|
| 12 |
+
language:
|
| 13 |
+
- en
|
| 14 |
+
---
|
| 15 |
+
<div style="display: flex; justify-content: center;">
|
| 16 |
+
<div style="display: flex; align-items: center; gap: 10px;">
|
| 17 |
+
<img src="logo.webp" alt="MongoDB Logo" style="height: 36px; width: auto; border-radius: 4px;">
|
| 18 |
+
<span style="font-size: 32px; font-weight: bold">MongoDB/mdbr-leaf-ir</span>
|
| 19 |
+
</div>
|
| 20 |
+
</div>
|
| 21 |
+
|
| 22 |
+
# Content
|
| 23 |
+
|
| 24 |
+
1. [Introduction](#introduction)
|
| 25 |
+
2. [Technical Report](#technical-report)
|
| 26 |
+
3. [Highlights](#highlights)
|
| 27 |
+
4. [Benchmarks](#benchmark-comparison)
|
| 28 |
+
5. [Quickstart](#quickstart)
|
| 29 |
+
6. [Citation](#citation)
|
| 30 |
+
|
| 31 |
+
# Introduction
|
| 32 |
+
|
| 33 |
+
`mdbr-leaf-ir` is a compact high-performance text embedding model specifically designed for **information retrieval (IR)** tasks, e.g., the retrieval stage of Retrieval-Augmented Generation (RAG) pipelines.
|
| 34 |
+
|
| 35 |
+
To enable even greater efficiency, `mdbr-leaf-ir` supports [flexible asymmetric architectures](#asymmetric-retrieval-setup) and is robust to [vector quantization](#vector-quantization) and [MRL truncation](#mrl-truncation).
|
| 36 |
+
|
| 37 |
+
If you are looking to perform other tasks such as classification, clustering, semantic sentence similarity, summarization, please check out our [`mdbr-leaf-mt`](https://huggingface.co/MongoDB/mdbr-leaf-mt) model.
|
| 38 |
+
|
| 39 |
+
> [!Note]
|
| 40 |
+
> **Note**: this model has been developed by the ML team of MongoDB Research. At the time of writing it is not used in any of MongoDB's commercial product or service offerings.
|
| 41 |
+
|
| 42 |
+
# Technical Report
|
| 43 |
+
|
| 44 |
+
A technical report detailing our proposed `LEAF` training procedure is [available here](https://arxiv.org/abs/2509.12539).
|
| 45 |
+
|
| 46 |
+
# Highlights
|
| 47 |
+
|
| 48 |
+
* **State-of-the-Art Performance**: `mdbr-leaf-ir` achieves state-of-the-art results for compact embedding models, **ranking #1** on the public [BEIR benchmark leaderboard](https://huggingface.co/spaces/mteb/leaderboard) for models with ≤100M parameters.
|
| 49 |
+
* **Flexible Architecture Support**: `mdbr-leaf-ir` supports asymmetric retrieval architectures enabling even greater retrieval results. [See below](#asymmetric-retrieval-setup) for more information.
|
| 50 |
+
* **MRL and Quantization Support**: embedding vectors generated by `mdbr-leaf-ir` compress well when truncated (MRL) and can be stored using more efficient types like `int8` and `binary`. [See below](#mrl-truncation) for more information.
|
| 51 |
+
|
| 52 |
+
## Benchmark Comparison
|
| 53 |
+
|
| 54 |
+
The table below shows the average BEIR benchmark scores (nDCG@10) for `mdbr-leaf-ir` compared to other retrieval models.
|
| 55 |
+
|
| 56 |
+
`mdbr-leaf-ir` ranks #1 on the BEIR public leaderboard, and when run in asymmetric "**(asym.)**" mode as described [here](#asymmetric-retrieval-setup), the results improve even further.
|
| 57 |
+
|
| 58 |
+
| Model | Size | BEIR Avg. (nDCG@10) |
|
| 59 |
+
|------------------------------------|---------|----------------------|
|
| 60 |
+
| OpenAI text-embedding-3-large | Unknown | 55.43 |
|
| 61 |
+
| **mdbr-leaf-ir (asym.)** | 23M | **54.03** |
|
| 62 |
+
| **mdbr-leaf-ir** | 23M | **53.55** |
|
| 63 |
+
| snowflake-arctic-embed-s | 32M | 51.98 |
|
| 64 |
+
| bge-small-en-v1.5 | 33M | 51.65 |
|
| 65 |
+
| OpenAI text-embedding-3-small | Unknown | 51.08 |
|
| 66 |
+
| granite-embedding-small-english-r2 | 47M | 50.87 |
|
| 67 |
+
| snowflake-arctic-embed-xs | 23M | 50.15 |
|
| 68 |
+
| e5-small-v2 | 33M | 49.04 |
|
| 69 |
+
| SPLADE++ | 110M | 48.88 |
|
| 70 |
+
| MiniLM-L6-v2 | 23M | 41.95 |
|
| 71 |
+
| BM25 | – | 41.14 |
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Quickstart
|
| 75 |
+
|
| 76 |
+
## Sentence Transformers
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
from sentence_transformers import SentenceTransformer
|
| 80 |
+
|
| 81 |
+
# Load the model
|
| 82 |
+
model = SentenceTransformer("MongoDB/mdbr-leaf-ir")
|
| 83 |
+
|
| 84 |
+
# Example queries and documents
|
| 85 |
+
queries = [
|
| 86 |
+
"What is machine learning?",
|
| 87 |
+
"How does neural network training work?"
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
documents = [
|
| 91 |
+
"Machine learning is a subset of artificial intelligence that focuses on algorithms that can learn from data.",
|
| 92 |
+
"Neural networks are trained through backpropagation, adjusting weights to minimize prediction errors."
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
# Encode queries and documents
|
| 96 |
+
query_embeddings = model.encode(queries, prompt_name="query")
|
| 97 |
+
document_embeddings = model.encode(documents)
|
| 98 |
+
|
| 99 |
+
# Compute similarity scores
|
| 100 |
+
scores = model.similarity(query_embeddings, document_embeddings)
|
| 101 |
+
|
| 102 |
+
# Print results
|
| 103 |
+
for i, query in enumerate(queries):
|
| 104 |
+
print(f"Query: {query}")
|
| 105 |
+
for j, doc in enumerate(documents):
|
| 106 |
+
print(f" Similarity: {scores[i, j]:.4f} | Document {j}: {doc[:80]}...")
|
| 107 |
+
|
| 108 |
+
# Query: What is machine learning?
|
| 109 |
+
# Similarity: 0.6857 | Document 0: Machine learning is a subset of ...
|
| 110 |
+
# Similarity: 0.4598 | Document 1: Neural networks are trained ...
|
| 111 |
+
#
|
| 112 |
+
# Query: How does neural network training work?
|
| 113 |
+
# Similarity: 0.4238 | Document 0: Machine learning is a subset of ...
|
| 114 |
+
# Similarity: 0.5723 | Document 1: Neural networks are trained ...
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
## Transformers Usage
|
| 118 |
+
|
| 119 |
+
See full example notebook [here](https://huggingface.co/MongoDB/mdbr-leaf-ir/blob/main/transformers_example.ipynb).
|
| 120 |
+
|
| 121 |
+
## Asymmetric Retrieval Setup
|
| 122 |
+
|
| 123 |
+
`mdbr-leaf-ir` is *aligned* to [`snowflake-arctic-embed-m-v1.5`](https://huggingface.co/Snowflake/snowflake-arctic-embed-m-v1.5), the model it has been distilled from. This enables flexible architectures in which, for example, documents are encoded using the larger model, while queries can be encoded faster and more efficiently with the compact `leaf` model:
|
| 124 |
+
```python
|
| 125 |
+
# Use mdbr-leaf-ir for query encoding (real-time, low latency)
|
| 126 |
+
query_model = SentenceTransformer("MongoDB/mdbr-leaf-ir")
|
| 127 |
+
query_embeddings = query_model.encode(queries, prompt_name="query")
|
| 128 |
+
|
| 129 |
+
# Use a larger model for document encoding (one-time, at index time)
|
| 130 |
+
doc_model = SentenceTransformer("Snowflake/snowflake-arctic-embed-m-v1.5")
|
| 131 |
+
document_embeddings = doc_model.encode(documents)
|
| 132 |
+
|
| 133 |
+
# Compute similarities
|
| 134 |
+
scores = query_model.similarity(query_embeddings, document_embeddings)
|
| 135 |
+
```
|
| 136 |
+
Retrieval results in asymmetric mode are often superior to the [standard mode above](#sentence-transformers).
|
| 137 |
+
|
| 138 |
+
## MRL Truncation
|
| 139 |
+
|
| 140 |
+
Embeddings have been trained via [MRL](https://arxiv.org/abs/2205.13147) and can be truncated for more efficient storage:
|
| 141 |
+
```python
|
| 142 |
+
from torch.nn import functional as F
|
| 143 |
+
|
| 144 |
+
query_embeds = model.encode(queries, prompt_name="query", convert_to_tensor=True)
|
| 145 |
+
doc_embeds = model.encode(documents, convert_to_tensor=True)
|
| 146 |
+
|
| 147 |
+
# Truncate and normalize according to MRL
|
| 148 |
+
query_embeds = F.normalize(query_embeds[:, :256], dim=-1)
|
| 149 |
+
doc_embeds = F.normalize(doc_embeds[:, :256], dim=-1)
|
| 150 |
+
|
| 151 |
+
similarities = model.similarity(query_embeds, doc_embeds)
|
| 152 |
+
|
| 153 |
+
print('After MRL:')
|
| 154 |
+
print(f"* Embeddings dimension: {query_embeds.shape[1]}")
|
| 155 |
+
print(f"* Similarities:\n\t{similarities}")
|
| 156 |
+
|
| 157 |
+
# After MRL:
|
| 158 |
+
# * Embeddings dimension: 256
|
| 159 |
+
# * Similarities:
|
| 160 |
+
# tensor([[0.7136, 0.4989],
|
| 161 |
+
# [0.4567, 0.6022]])
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
## Vector Quantization
|
| 165 |
+
Vector quantization, for example to `int8` or `binary`, can be performed as follows:
|
| 166 |
+
|
| 167 |
+
**Note**: For vector quantization to types other than binary, we suggest performing a calibration to determine the optimal ranges, [see here](https://sbert.net/examples/sentence_transformer/applications/embedding-quantization/README.html#scalar-int8-quantization).
|
| 168 |
+
Good initial values, according to the [teacher model's documentation](https://huggingface.co/Snowflake/snowflake-arctic-embed-m-v1.5#compressing-to-128-bytes), are:
|
| 169 |
+
* `int8`: -0.3 and +0.3
|
| 170 |
+
* `int4`: -0.18 and +0.18
|
| 171 |
+
```python
|
| 172 |
+
from sentence_transformers.quantization import quantize_embeddings
|
| 173 |
+
import torch
|
| 174 |
+
|
| 175 |
+
query_embeds = model.encode(queries, prompt_name="query")
|
| 176 |
+
doc_embeds = model.encode(documents)
|
| 177 |
+
|
| 178 |
+
# Quantize embeddings to int8 using -0.3 and +0.3 as calibration ranges
|
| 179 |
+
ranges = torch.tensor([[-0.3], [+0.3]]).expand(2, query_embeds.shape[1]).cpu().numpy()
|
| 180 |
+
query_embeds = quantize_embeddings(query_embeds, "int8", ranges=ranges)
|
| 181 |
+
doc_embeds = quantize_embeddings(doc_embeds, "int8", ranges=ranges)
|
| 182 |
+
|
| 183 |
+
# Calculate similarities; cast to int64 to avoid under/overflow
|
| 184 |
+
similarities = query_embeds.astype(int) @ doc_embeds.astype(int).T
|
| 185 |
+
|
| 186 |
+
print('After quantization:')
|
| 187 |
+
print(f"* Embeddings type: {query_embeds.dtype}")
|
| 188 |
+
print(f"* Similarities:\n{similarities}")
|
| 189 |
+
|
| 190 |
+
# After quantization:
|
| 191 |
+
# * Embeddings type: int8
|
| 192 |
+
# * Similarities:
|
| 193 |
+
# [[118022 79111]
|
| 194 |
+
# [ 72961 98333]]
|
| 195 |
+
```
|
| 196 |
+
|
| 197 |
+
# Evaluation
|
| 198 |
+
|
| 199 |
+
Please [see here](https://huggingface.co/MongoDB/mdbr-leaf-ir/blob/main/evaluate_models.ipynb).
|
| 200 |
+
|
| 201 |
+
# Citation
|
| 202 |
+
|
| 203 |
+
If you use this model in your work, please cite:
|
| 204 |
+
|
| 205 |
+
```bibtex
|
| 206 |
+
@misc{mdbr_leaf,
|
| 207 |
+
title={LEAF: Knowledge Distillation of Text Embedding Models with Teacher-Aligned Representations},
|
| 208 |
+
author={Robin Vujanic and Thomas Rueckstiess},
|
| 209 |
+
year={2025},
|
| 210 |
+
eprint={2509.12539},
|
| 211 |
+
archivePrefix={arXiv},
|
| 212 |
+
primaryClass={cs.IR},
|
| 213 |
+
url={https://arxiv.org/abs/2509.12539},
|
| 214 |
+
}
|
| 215 |
+
```
|
| 216 |
+
|
| 217 |
+
# License
|
| 218 |
+
|
| 219 |
+
This model is released under Apache 2.0 License.
|
| 220 |
+
|
| 221 |
+
# Contact
|
| 222 |
+
|
| 223 |
For questions or issues, please open an issue or pull request. You can also contact the MongoDB ML research team at robin.vujanic@mongodb.com.
|