█

AI/TLDR

Buy me a coffee
Buy me a coffee

Nomic Contrastors

Train and evaluate contrastive text and multimodal embedding models

Embedding Models & InferenceOpen source
Language
Python
IN SHORT

What is Nomic Contrastors?

Nomic Contrastors is Train and evaluate contrastive text and multimodal embedding models. It sits in the Embedding Models & Inference category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python. To get started: create and activate a virtual environment.

Overview

Contrastors is Nomic AI's contrastive learning toolkit. It holds the training code and recipes behind the fully open nomic-embed family of long-context text and multimodal embedding models, so researchers and engineers can train and evaluate contrastive models themselves.

It is aimed at teams who want to build their own embedding models rather than only consume a hosted API. It is built on Flash Attention, supports multi-GPU training, and uses GradCache so you can train with large batch sizes even when GPU memory is limited.

Within the embedding-models category, it sits on the training side: it covers masked language modeling pretraining, contrastive pretraining and finetuning, Matryoshka representation learning for flexible embedding sizes, and CLIP/LiT style training for vision.

What it does

  • Built on Flash Attention for fast, memory-efficient training
  • Multi-GPU training with DeepSpeed and torchrun
  • GradCache support for large batch sizes in limited GPU memory
  • Matryoshka Representation Learning for flexible embedding sizes
  • CLIP and LiT style contrastive learning for vision, plus ViT/timm model loading
  • Hugging Face support for common models (Pythia/GPTNeoX, BERT) and MLM pretraining

Getting started

Contrastors relies on custom CUDA kernels from Flash Attention, so set up a Python environment with CUDA 11.8+ before installing. Verify your CUDA version with nvcc --version.

Create and activate a virtual environment

Use a clean Python venv to keep the CUDA and Flash Attention dependencies isolated.

bashbash
python3 -m venv env
source env/bin/activate

Install torch and build tools

Install torch (see the PyTorch docs for the right command for your system), then the build helpers Flash Attention needs.

bashbash
pip3 install torch torchvision torchaudio
pip install wheel packaging ninja setuptools

Install Flash Attention kernels and the package

Install Flash Attention with its custom kernels, then install contrastors itself in editable mode.

bashbash
pip install --no-cache-dir flash-attn --no-build-isolation git+https://github.com/HazyResearch/flash-attention.git#subdirectory=csrc/rotary git+https://github.com/HazyResearch/flash-attention.git#subdirectory=csrc/layer_norm git+https://github.com/HazyResearch/flash-attention.git#subdirectory=csrc/fused_dense_lib git+https://github.com/HazyResearch/flash-attention.git#subdirectory=csrc/xentropy
pip install -e .

Run contrastive pretraining

Launch a training run with torchrun across your GPUs, pointing at one of the provided configs.

bashbash
cd src/contrastors
torchrun --nproc-per-node=8 train.py --config=configs/train/contrastive_pretrain.yaml --dtype=bf16

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

When to use it

  • Train your own BERT-based text embedding model from scratch with MLM pretraining followed by contrastive pretraining and finetuning
  • Reproduce or adapt the nomic-embed-text-v1 training recipe on your own corpus
  • Fit large effective batch sizes on memory-limited GPUs using GradCache
  • Train CLIP/LiT style multimodal embeddings and produce Matryoshka embeddings with flexible output sizes

How Nomic Contrastors compares

Nomic Contrastors alongside other open-source embedding models & inference tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
Sentence Transformers★ 19.3kThe standard Python framework for loading, training, and computing embeddings with sentence and reranking models.
OpenCLIP★ 14.3kAn open implementation of CLIP for training and running image-text embedding models, with a large catalogue of pretrained checkpoints for zero-shot classification and retrieval.
PyTorch Metric Learning★ 6.3kPyTorch library of losses, miners, distances, samplers, trainers and accuracy testers for training embedding models with deep metric and self-supervised learning.
EmbeddingGemma (Gemma)★ 5.9kGoogle DeepMind's Gemma repo, home to EmbeddingGemma, a 308M multilingual embedding model small enough to run on-device for RAG and semantic search.
Text Embeddings Inference (TEI)★ 5.1kHugging Face's Rust-based server for deploying embedding, reranking, and sequence-classification models with high throughput on GPU or CPU.
text2vec★ 5kPython library for sentence embeddings and text similarity, implementing Word2Vec, BM25, Sentence-BERT, CoSENT and BGE with Chinese and multilingual models.
Infinity (Embeddings)★ 3kA high-throughput serving engine for text embeddings, rerankers, CLIP, and ColPali models, exposing an OpenAI-compatible API.
Nomic Contrastors★ 807Train and evaluate contrastive text and multimodal embedding models