This is a BERT Hash Femto model fined-tuned using sentence-transformers. It maps sentences & paragraphs to a 50-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 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-femto-embeddings outputs 50-dimensional vectors.
The training dataset is a subset of this embedding training collection. The training workflow was a two step distillation process as follows.
Build a distilled dataset of teacher scores using the 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.
Usage (txtai)
This model can be used to build embeddings databases with txtai for semantic search and/or as a knowledge source for retrieval augmented generation (RAG).
python
1import txtai
23embeddings = txtai.Embeddings(4 path="neuml/bert-hash-femto-embeddings",5 content=True,6 vectors={"trust_remote_code":True}7)8embeddings.index(documents())910# Run a query11embeddings.search("query to run")
1from sentence_transformers import SentenceTransformer
2sentences =["This is an example sentence","Each sentence is converted"]34model = SentenceTransformer("neuml/bert-hash-femto-embeddings", trust_remote_code=True)5embeddings = model.encode(sentences)6print(embeddings)
Usage (Hugging Face Transformers)
The model can also be used directly with Transformers.
python
1from transformers import AutoTokenizer, AutoModel
2import torch
34# Mean Pooling - Take attention mask into account for correct averaging5defmeanpooling(output, mask):6 embeddings = output[0]# First element of model_output contains all token embeddings7 mask = mask.unsqueeze(-1).expand(embeddings.size()).float()8return torch.sum(embeddings * mask,1)/ torch.clamp(mask.sum(1),min=1e-9)910# Sentences we want sentence embeddings for11sentences =['This is an example sentence','Each sentence is converted']1213# Load model from HuggingFace Hub14tokenizer = AutoTokenizer.from_pretrained("neuml/bert-hash-femto-embeddings", trust_remote_code=True)15model = AutoModel.from_pretrained("neuml/bert-hash-femto-embeddings", trust_remote_code=True)1617# Tokenize sentences18inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')1920# Compute token embeddings21with torch.no_grad():22 output = model(**inputs)2324# Perform pooling. In this case, mean pooling.25embeddings = meanpooling(output, inputs['attention_mask'])2627print("Sentence embeddings:")28print(embeddings)
In analyzing the results, bert-hash-femto-embeddings scores lower than MUVERA with colbert-muvera-femto. Comparing the standard MUVERA output of 10240 vs 50 dimensions, 10K standard F32 vectors needs 400 MB of storage vs 2 MB
Keeping in mind this is only a 243K parameter model, the performance is still impressive at only ~1% of the number of parameters of popular small embeddings models.
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.