# Nomic Contrastors

> Train and evaluate contrastive text and multimodal embedding models

- Category: Retrieval & Knowledge (RAG) / Embedding Models & Inference
- Language: Python
- Repository: https://github.com/nomic-ai/contrastors
- Website: https://www.nomic.ai/
- Canonical page: https://ai-tldr.dev/tools/nomic-contrastors/

## In short

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.

1. Create and activate a virtual environment

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

```bash
python3 -m venv env
source env/bin/activate
```

2. 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.

```bash
pip3 install torch torchvision torchaudio
pip install wheel packaging ninja setuptools
```

3. Install Flash Attention kernels and the package

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

```bash
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 .
```

4. Run contrastive pretraining

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

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

## 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
