all-MiniLM-L12-v2

The sentence‑transformers/all‑MiniLM‑L12‑v2 model is a compact, high‑performance sentence encoder that maps a sentence or short paragraph into a 384‑dimensional dense vector. It is built on the

sentence-transformers 3.8M downloads apache-2.0 Sentence Similarity Top 100
Frameworkssentence-transformerspytorchrustonnxsafetensorsopenvinotransformers
Languagesen
Datasetss2orcflax-sentence-embeddings/stackexchange_xmlms_marcogooaqyahoo_answers_topicscode_search_net
Tagsbertfeature-extractionsentence-similaritytext-embeddings-inference
Downloads
3.8M
License
apache-2.0
Pipeline
Sentence Similarity
Author
sentence-transformers

Run all-MiniLM-L12-v2 locally on a Q4KM hard drive

Accelerate your deployments with Q4KM hard drives pre‑loaded with the all‑MiniLM‑L12‑v2 model. Enjoy instant, plug‑and‑play access to a high‑quality sentence encoder without the need for additional...

Shop Q4KM Drives

Technical Overview

The sentence‑transformers/all‑MiniLM‑L12‑v2 model is a compact, high‑performance sentence encoder that maps a sentence or short paragraph into a 384‑dimensional dense vector. It is built on the MiniLM‑L12‑H384‑uncased backbone, a 12‑layer transformer with a hidden size of 384, and has been fine‑tuned for semantic similarity using a large‑scale contrastive learning objective. The model is part of the Sentence‑Transformers ecosystem, which adds a pooling layer and L2‑normalisation on top of the raw token embeddings, turning the transformer output into a single, fixed‑size sentence vector.

Key features and capabilities

  • Fast inference: only 12 transformer layers and a small hidden dimension keep latency low.
  • High‑quality semantic embeddings: trained on >1 billion sentence pairs from diverse sources (MS‑MARCO, Natural Questions, StackExchange, etc.).
  • Versatile downstream use: clustering, semantic search, duplicate detection, recommendation, and any task that benefits from a similarity metric.
  • Cross‑framework support: available as PyTorch, ONNX, TensorFlow, Rust, OpenVINO, and SAFETENSORS formats.
  • Out‑of‑the‑box normalization: the model returns L2‑normalized vectors, making cosine similarity a direct proxy for semantic distance.

Architecture highlights

  • Base encoder: MiniLM‑L12‑H384, a distilled BERT‑style transformer that retains most of BERT‑base’s representational power while being 2‑3× smaller.
  • Pooling strategy: Mean‑pooling over the token embeddings, weighted by the attention mask, followed by L2‑normalisation.
  • Dimensionality: 384‑dimensional output vector – a sweet spot between storage cost and expressive power.
  • Training regime: Contrastive learning on 1 B sentence pairs, using a cross‑entropy loss over cosine similarities within each batch.

Intended use cases

  • Semantic search over document collections (e.g., knowledge bases, FAQs).
  • Clustering of short texts for topic modelling or content moderation.
  • Duplicate or paraphrase detection in chat, forums, or code review tools.
  • Feature extraction for downstream classifiers (e.g., intent detection, sentiment analysis).
``` ```html

Benchmark Performance

For sentence‑embedding models, the most relevant benchmarks are semantic textual similarity (STS) tasks, information retrieval (IR) datasets such as MS‑MARCO, and natural language inference (NLI) benchmarks (SNLI, Multi‑NLI). The README lists a large collection of training and evaluation corpora, including snli, multi_nli, natural_questions, trivia_qa, and ms_marco. In the original Sentence‑Transformers paper, the MiniLM‑L12‑v2 variant achieved:

  • ≈ 84 % average STS‑12/13/14 Pearson correlation – comparable to larger BERT‑base models.
  • Top‑10% retrieval performance on MS‑MARCO (Mean Reciprocal Rank ≈ 0.31).
  • Near‑state‑of‑the‑art accuracy on NLI (≈ 84 % accuracy on SNLI).

These numbers matter because they directly reflect how well the vector space preserves semantic similarity, which is the core requirement for clustering, search, and duplicate detection. Compared with the older all‑MiniLM‑L6‑v2 (128‑dim) and the larger all‑mpnet‑base‑v2 (768‑dim), the all‑MiniLM‑L12‑v2 offers a better trade‑off: higher accuracy than L6‑v2 while staying far more lightweight than MPNet‑base.

``` ```html

Hardware Requirements

The model’s modest size (≈ 120 MB in PyTorch format) makes it suitable for a wide range of hardware. Below are practical guidelines for inference:

  • VRAM: 2 GB of GPU memory is sufficient for batch sizes up to 64 sentences. On a single‑GPU setup, a 4 GB card (e.g., RTX 3050) provides comfortable headroom.
  • Recommended GPUs: Any CUDA‑capable GPU with ≥ 4 GB VRAM (RTX 2060, GTX 1660 Super, or newer). For massive batch processing, consider 8 GB+ cards (RTX 3070, A100).
  • CPU: The model runs on CPU‑only environments, though inference will be slower (≈ 30‑40 ms per sentence on a modern 8‑core CPU). Using Intel AVX‑512 or AMD Zen 3 improves throughput.
  • Storage: The model files (weights, tokenizer, config) occupy ~ 120 MB. If you also store the ONNX or OpenVINO variants, allocate an additional 50 MB.
  • Performance characteristics: With mean‑pooling and L2‑normalisation, a single forward pass for a batch of 32 sentences takes ~ 12 ms on an RTX 3060 (FP16). The model supports mixed‑precision (FP16) to halve VRAM usage without sacrificing accuracy.
``` ```html

Use Cases

The all‑MiniLM‑L12‑v2 model shines wherever a compact yet semantically rich sentence representation is needed.

  • Semantic search engines: Index product catalogs, support tickets, or research papers and retrieve the most relevant items with a single cosine‑similarity lookup.
  • Content clustering: Group user reviews, news headlines, or forum posts into topical clusters for analytics dashboards.
  • Duplicate & paraphrase detection: Flag near‑identical questions on Q&A platforms (e.g., StackExchange, Reddit) or detect plagiarism in academic submissions.
  • Recommendation systems: Compute similarity between user‑generated text (e.g., search queries) and item descriptions to drive personalized suggestions.
  • Feature extraction for downstream classifiers: Feed the 384‑dimensional vectors into lightweight classifiers for intent detection, sentiment analysis, or spam filtering.

Because the model is available in ONNX and OpenVINO formats, it can be deployed on edge devices, mobile phones, or even within browser‑based WebAssembly runtimes, expanding its reach to latency‑critical applications.

``` ```html

Training Details

The model follows a two‑stage training pipeline:

  • Pre‑training backbone: The publicly released MiniLM‑L12‑H384‑uncased model, itself distilled from BERT‑large, provides the initial token‑level representations.
  • Fine‑tuning objective: A contrastive loss that maximises cosine similarity for true sentence pairs while minimising it for randomly sampled negatives within the same batch. The loss is implemented as a cross‑entropy over the similarity matrix.
  • Datasets: Over 1 billion sentence pairs drawn from a wide variety of corpora, including MS‑MARCO, Natural Questions, StackExchange XML, S2ORC, CodeSearchNet, Trivia QA, and many domain‑specific QA datasets. The README enumerates 22 distinct sources, ensuring coverage of both general‑domain and specialised language.
  • Compute resources: Training was performed on 7 TPU v3‑8 pods (each pod contains 8 TPU cores), leveraging JAX/Flax for efficient mixed‑precision execution. The total compute budget is estimated at several thousand TPU‑hours.
  • Fine‑tuning capabilities: Users can further adapt the model on domain‑specific sentence pairs by continuing the same contrastive training regime, or by applying lightweight supervised objectives (e.g., classification heads) on top of the 384‑dim embeddings.
``` ```html

Licensing Information

The model card lists the license as unknown, while the README states apache‑2.0. In practice, the underlying MiniLM checkpoint is released under the Apache 2.0 license, which is permissive and commercial‑friendly. Assuming the Apache 2.0 terms apply, you may:

  • Use the model in commercial products, SaaS platforms, or internal tools.
  • Modify the weights or code and redistribute the derivative work.
  • Combine the model with other software under different licenses.

Key requirements under Apache 2.0 are:

  • Provide proper attribution – include the original copyright notice and a link to the model card.
  • State any modifications you make to the model or its configuration.
  • Include a copy of the Apache 2.0 license text in your distribution.

If the “unknown” tag reflects a missing license file, it is safest to treat the model as Apache 2.0 (the default for Sentence‑Transformers) and verify the exact terms on the Hugging Face model page before deploying in a regulated environment.

``` ```html

Pre-loaded AI models. Ready to run.

Skip the downloads. Get a Q4KM hard drive with hundreds of models pre-configured and optimized.

Shop Q4KM Hard Drives