Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 39 min read Research hardMaster

Train 400x faster Static Embedding Models with Sentence Transformers

Static embedding models, which replace the attention-based encoder with a token embedding lookup plus mean pooling (an EmbeddingBag), can be trained with modern contrastive losses and Matryoshka Representation Learning to run 100x to 400x faster on CPU than models like all-mpnet-base-v2 and multilingual-e5-small, while keeping at least 85% of their benchmark quality. The recipe trains two 1024-dimensional models from randomly initialized weights using BERT tokenizers and Sentence Transformers' StaticEmbedding module: an English retrieval model trained on 13 QA datasets chosen to avoid overlap with BEIR, and a multilingual similarity model trained on parallel-sentence and positive-pair datasets. The post covers dataset selection, loss choice, training arguments, evaluators, NanoBEIR results on CPU and GPU, and Matryoshka truncation tradeoffs. The resulting models suit on-device, in-browser and edge workloads.

Assumes: text embeddingscontrastive learningPyTorch basics

Related posts

  • Train a Sentence Embedding Model with 1B Training Pairs

    Hugging Face hardMaster

    Sentence embedding models trained with a contrastive multiple-negatives ranking loss (InfoNCE) on up to 1 billion sentence pairs, using in-batch negatives, a scaled similarity (C≈20), and cosine vs. dot-product tradeoffs for clustering and ANN search. Batch composition drives quality: larger batches, hard negatives, and batches drawn mostly from one dataset but mixing at least two to learn both local and global topic structure. Training ran on TPU v3-8s with JAX/Flax and produced 20 general-purpose Sentence Transformers models (MiniLM, RoBERTa, DistilBERT, MPNet) reported as state of the art on sentence similarity tasks, plus 8 datasets and a Spaces demo.

  • 🪆 Introduction to Matryoshka Embedding Models

    Hugging Face mediumAdept

    Matryoshka embedding models are trained so that the most important information sits in the earliest dimensions, which lets embeddings be truncated (e.g., 768 down to 64) and still work well for retrieval and similarity. Training applies the same loss at several truncated dimensionalities and sums the results, which Sentence Transformers exposes as MatryoshkaLoss with no notable training overhead. At inference you truncate with truncate_dim and should re-normalize afterward. On STSBenchmark, a Matryoshka-trained MPNet model outperformed a standard one at every dimension and kept 98.37% of its performance at 8.3% of the embedding size, versus 96.46% for the standard model. That makes truncated embeddings useful for cheap shortlisting followed by full-dimension reranking.

  • Training and Finetuning Embedding Models with Sentence Transformers

    Hugging Face mediumAdept

    Sentence Transformers v3 trains and finetunes embedding models through a SentenceTransformerTrainer that combines Hugging Face datasets, a loss function, optional training arguments and evaluators. Finetuning matters because each task needs its own notion of similarity. Dataset column order must match the chosen loss's inputs, with an optional "label" or "score" column, and extraneous columns must be removed. A worked example finetunes MPNet on AllNLI triplets with MultipleNegativesRankingLoss and a no-duplicates batch sampler, and evaluates with TripletEvaluator and EmbeddingSimilarityEvaluator on STSb.

  • Training and Finetuning Sparse Embedding Models with Sentence Transformers

    Hugging Face mediumAdept

    Sentence Transformers' SparseEncoder API can train and finetune neural sparse embedding models such as SPLADE, inference-free SPLADE and Contrastive Sparse Representation (CSR). The guide walks through each training component: model architecture, datasets, loss functions, training arguments, evaluators and the trainer. Sparse models produce vocabulary-sized vectors with learned query and document expansion, which makes them interpretable and strong in hybrid search. Finetuning teaches domain-specific expansions, for example mapping "cephalalgia" to "headache". Practical details include using router_mapping for the query/document split in inference-free SPLADE and a much higher learning rate for the SparseStaticEmbedding module.

  • Train and Fine-Tune Sentence Transformers Models

    Hugging Face mediumAdept

    Sentence Transformers models map text to fixed-size embeddings by running a pre-trained Transformer and pooling its token embeddings, and training them depends on matching the dataset format to the right loss. Four common formats map to specific losses: labeled pairs (ContrastiveLoss, SoftmaxLoss, CosineSimilarityLoss), unlabeled positive pairs (MultipleNegativesRankingLoss), class-labeled sentences (batch triplet losses), and unlabeled triplets (TripletLoss). A worked example builds a distilroberta-base model, trains it on QQP triplets with TripletLoss via the legacy `fit` API, and pushes it to the Hugging Face Hub. The guide is marked outdated: since v3.0, SentenceTransformerTrainer replaces this API.

The quest for software engineering knowledge.

Look:
Layout: