File size: 7,304 Bytes
b723729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
pipeline_tag: sentence-similarity
tags:
- sentence-transformers
- feature-extraction
- sentence-similarity
- transformers
base_model: NeuML/bert-hash-nano
language: en
license: apache-2.0
---

# BERT Hash Nano Embeddings

This is a [BERT Hash Nano](https://hf.co/neuml/bert-hash-nano) model fined-tuned using [sentence-transformers](https://www.SBERT.net). It maps sentences & paragraphs to a 128-dimensional dense vector space and can be used for semantic textual similarity, semantic search, paraphrase mining, text classification, clustering, and more.

This model is an alternative to [MUVERA fixed-dimensional encoding](https://arxiv.org/abs/2405.19504) with ColBERT models. MUVERA encoding enables encoding the multi-vector outputs of ColBERT into single dense vector outputs. While this is a great step, the main issue with MUVERA is that it tends to need wide vectors to be effective (5K - 10K dimensional vectors). `bert-hash-nano-embeddings` outputs 128-dimensional vectors. 

The training dataset is a subset of [this embedding training collection](https://huggingface.co/collections/sentence-transformers/embedding-model-datasets). The training workflow was a two step distillation process as follows.

- Distill embeddings from the larger [mixedbread-ai/mxbai-embed-xsmall-v1](https://huggingface.co/mixedbread-ai/mxbai-embed-xsmall-v1) model using this [model distillation script](https://github.com/huggingface/sentence-transformers/blob/main/examples/sentence_transformer/training/distillation/model_distillation.py) from Sentence Transformers.
- Build a distilled dataset of teacher scores using the [mixedbread-ai/mxbai-rerank-xsmall-v1](https://hf.co/mixedbread-ai/mxbai-rerank-xsmall-v1) cross-encoder for a random sample of the training dataset mentioned above.
- Further fine-tune the model on the distilled dataset using [KLDivLoss](https://github.com/huggingface/sentence-transformers/blob/main/sentence_transformers/losses/DistillKLDivLoss.py).

## Usage (txtai)

This model can be used to build embeddings databases with [txtai](https://github.com/neuml/txtai) for semantic search and/or as a knowledge source for retrieval augmented generation (RAG).

```python
import txtai

embeddings = txtai.Embeddings(
  path="neuml/bert-hash-nano-embeddings",
  content=True,
  vectors={"trust_remote_code": True}
)
embeddings.index(documents())

# Run a query
embeddings.search("query to run")
```

## Usage (Sentence-Transformers)

Alternatively, the model can be loaded with [sentence-transformers](https://www.SBERT.net).

```python
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]

model = SentenceTransformer("neuml/bert-hash-nano-embeddings", trust_remote_code=True)
embeddings = model.encode(sentences)
print(embeddings)
```

## Usage (Hugging Face Transformers)

The model can also be used directly with Transformers. 

```python
from transformers import AutoTokenizer, AutoModel
import torch

# Mean Pooling - Take attention mask into account for correct averaging
def meanpooling(output, mask):
    embeddings = output[0] # First element of model_output contains all token embeddings
    mask = mask.unsqueeze(-1).expand(embeddings.size()).float()
    return torch.sum(embeddings * mask, 1) / torch.clamp(mask.sum(1), min=1e-9)

# Sentences we want sentence embeddings for
sentences = ['This is an example sentence', 'Each sentence is converted']

# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained("neuml/bert-hash-nano-embeddings", trust_remote_code=True)
model = AutoModel.from_pretrained("neuml/bert-hash-nano-embeddings", trust_remote_code=True)

# Tokenize sentences
inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')

# Compute token embeddings
with torch.no_grad():
    output = model(**inputs)

# Perform pooling. In this case, mean pooling.
embeddings = meanpooling(output, inputs['attention_mask'])

print("Sentence embeddings:")
print(embeddings)
```

## Evaluation

The following table shows a subset of BEIR scored with the [txtai benchmarks script](https://github.com/neuml/txtai/blob/master/examples/benchmarks.py). 

This evaluation is compared against the [ColBERT MUVERA](https://huggingface.co/collections/NeuML/colbert) series of models. 

Scores reported are `ndcg@10` and grouped into the following three categories.

### BERT Hash Embeddings vs MUVERA

| Model             | Parameters | NFCorpus | SciDocs  | SciFact | Average |
|:------------------|:-----------|:---------|:---------|:--------|:--------|
| [**BERT Hash Nano Embeddings**](https://huggingface.co/neuml/bert-hash-nano-embeddings) | **0.9M** | **0.2562** | **0.1179** | **0.5032** | **0.2924** |
| [ColBERT MUVERA Nano](https://huggingface.co/neuml/colbert-muvera-nano) | 0.9M | 0.2355 | 0.0807 | 0.4904 | 0.2689 |

### BERT Hash Embeddings vs MUVERA with maxsim re-ranking of the top 100 results per MUVERA paper

| Model             | Parameters | NFCorpus | SciDocs  | SciFact | Average |
|:------------------|:-----------|:---------|:---------|:--------|:--------|
| [**BERT Hash Nano Embeddings**](https://huggingface.co/neuml/bert-hash-nano-embeddings) | **0.9M** | **0.3101** | **0.1347** | **0.6327** | **0.3592** |
| [ColBERT MUVERA Nano](https://huggingface.co/neuml/colbert-muvera-nano) | 0.9M | 0.2996 | 0.1201 | 0.6249 | 0.3482 |

### Compare to other models

| Model             | Parameters | NFCorpus | SciDocs  | SciFact | Average |
|:------------------|:-----------|:---------|:---------|:--------|:--------|
| [ColBERT MUVERA Nano (full multi-vector maxsim)](https://huggingface.co/neuml/colbert-muvera-nano) | 0.9M | 0.3180 | 0.1262 | 0.6576 | 0.3673 |
| [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) | 22.7M | 0.3089 | 0.2164 | 0.6527 | 0.3927 |
| [mxbai-embed-xsmall-v1](https://huggingface.co/mixedbread-ai/mxbai-embed-xsmall-v1) | 24.1M | 0.3186 | 0.2155 | 0.6598 | 0.3980 |

In analyzing the results, `bert-hash-nano-embeddings` is better across the board vs MUVERA with `colbert-muvera-nano`. It keeps `98%` of the performance of full multi-vector maxsim vs `95%` for MUVERA. Comparing the standard MUVERA output of `10240` vs `128` dimensions, `10K` standard F32 vectors needs `400 MB` of storage vs `5 MB`

By itself for a `970K` parameter model, the scores are really good. When paired with re-ranking with a `970K` ColBERT model, the scores are even better. Competitive with common small models as shown above at only `~4%` of the number of parameters. 

While this isn't a state of the art model, it's an extremely competitive method for building vectors on edge and low resource devices.

## Full Model Architecture

```
SentenceTransformer(
  (0): Transformer({'max_seq_length': 512, 'do_lower_case': False, 'architecture': 'BertHashModel'})
  (1): Pooling({'word_embedding_dimension': 128, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})
)
```

## More Information

Read more about this model and how it was built in [this article](https://hf.co/blog/neuml/bert-hash-embeddings).