Multimodal Models Needs review

CLIP (Contrastive Language–Image Pre-training)

CLIP

Year of appearance
2021
Researchers
Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever
Organisation
OpenAI
Architecture type
Dual-encoder contrastive model
Learning mechanism
self-supervised
Tasks
Zero-shot classification, Image–text retrieval

An image encoder and a text encoder trained together with a contrastive loss on 400 million image-text pairs, so they learn a shared space that enables classification without training examples.

Architecture diagram

Displayed configuration: CLIP ViT-B/32: 224 image + 77-token text, shared space 512 — A ViT-B/32 image encoder and a Transformer text encoder of width 512.

Layer table (13 nodes)
LayerTypeInputOutputRepetitionParametersFormulaMACs
224×224 imageinput3 × 224 × 22410—
Text (77 BPE tokens)input7710—
32×32 patches + [class] + positionpatch3 × 224 × 22450 × 76812,398,464From the reference PyTorch implementation115,605,504
LayerNorm before the encoderln50 × 76850 × 76811,536From the reference PyTorch implementation0
Transformer encoder × 12 (768, 12 heads)encoder50 × 76850 × 768127,087,872From the reference PyTorch implementation357,734,400
LayerNorm on [class]ln76876811,536From the reference PyTorch implementation0
Linear projection → 512projection7685121393,216From the reference PyTorch implementation393,216
Token + position embeddingembed7777 × 512125,336,320From the reference PyTorch implementation0
Transformer with masked attention × 12 (512, 8 heads)masked_mha77 × 51277 × 512123,152,384From the reference PyTorch implementation248,292,352
LayerNorm on the [EOS] tokenln77 × 51277 × 51211,024From the reference PyTorch implementation0
Linear projection → 512projection5125121262,144From the reference PyTorch implementation262,144
Cosine similarity × e^tprocess512, 512N × N11Learned logarithmic temperature (logit_scale)512
Symmetric contrastive losslossN × NL10—

Configuration figures: CLIP ViT-B/32: 224 image + 77-token text, shared space 512

Parameters (computed from the layers)151,277,313 (≈151.28M)
Parameters (published in the source)Unspecified
MACs7,388,582,400 (≈7.39B)
FLOPs ≈ 2 × MACs14,777,164,800 (≈14.78B)
Input shape
A 3×224×224 image and a 77-token text
Output shape
Two 512 representations + similarity
Depth
12 + 12 layers
Complexity
The two encoders are independent: image and text representations can be precomputed for retrieval.
MACs note
For one image and one text (77 tokens).
Calculation assumptions: CLIPModel from Hugging Face with the default configuration (openai/clip-vit-base-patch32).

MAC = a multiplication followed by an addition. The figures are for one forward pass of one sample with the stated dimensions; they are an analytical calculation, not a measurement of run time.

Scientific explanation

Historical background

Published at ICML 2021 by OpenAI; it matched ResNet-50 on ImageNet without using any image from its data, and its text and image encoders became components in generative models such as Stable Diffusion.

The problem it addresses

Vision models trained on fixed classes do not generalise to new concepts without labelled data.

Core idea

Learning from the natural supervision in the text that accompanies images: predicting which text goes with which image within the batch.

Architecture description

An image encoder (ResNet or ViT) and a text encoder (Transformer), two linear projections into a shared space, and a learned temperature.

Data flow

Image → ViT → u; text → Transformer → v; s = cos(u, v)/τ → contrastive loss. For classification: “a photo of a {class}” for each class → highest similarity.

Inputs

An image and a text.

Outputs

Two representations in a shared space and a similarity score.

Training procedure

A batch of 32,768 pairs, Adam with weight decay, a cosine schedule, and 32 epochs on the WIT dataset (400 million pairs).

Loss functions

Symmetric cross-entropy on the similarity matrix (InfoNCE).

Computational complexity

Two independent encoders; about 7.4 billion multiply-adds for an image and a text in ViT-B/32 (computed here).

Computational requirements

The largest model, ViT-L/14, was trained for 12 days on 256 V100 GPUs.

Advantages

  • Classification without training examples
  • Strong transferable representations
  • Efficient retrieval with two independent encoders

Limitations

  • Weak at counting and spatial relationships
  • Biases of web data
  • Sensitive to the wording of the text (prompt engineering)

Equations

Symmetric contrastive loss Symmetric contrastive loss (InfoNCE)

\mathcal{L} = \frac{1}{2}\left[\frac{1}{N}\sum_{i=1}^{N} -\log \frac{e^{s_{ii}/\tau}}{\sum_{j} e^{s_{ij}/\tau}} + \frac{1}{N}\sum_{i=1}^{N} -\log \frac{e^{s_{ii}/\tau}}{\sum_{j} e^{s_{ji}/\tau}}\right],\quad s_{ij} = \frac{\mathbf{u}_i^\top \mathbf{v}_j}{\lVert \mathbf{u}_i \rVert \lVert \mathbf{v}_j \rVert}

Each image must match its text among the N texts in the batch, and vice versa.

Symbols: τ: learned temperature, u: image representation, v: text representation

Zero-shot classification Zero-shot prediction

\hat{y} = \arg\max_{c} \; \cos\!\big(f_I(\mathbf{x}),\, f_T(\text{“a photo of a } c\text{”})\big)

Each class is represented by a text that describes it.

Symbols: f_I, f_T: the encoders

Projection into the shared space Joint embedding

\mathbf{u} = \frac{W_i f_I(\mathbf{x})}{\lVert W_i f_I(\mathbf{x}) \rVert},\quad \mathbf{v} = \frac{W_t f_T(\mathbf{t})}{\lVert W_t f_T(\mathbf{t}) \rVert}

A linear projection, then L2 normalisation.

Applications

Related models

References

  1. Original paper Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever. (2021). Learning transferable visual models from natural language supervision. ICML 2021 (PMLR 139). arXiv:2103.00020 Verified
  2. Related reference Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby. (2021). An image is worth 16x16 words: Transformers for image recognition at scale. ICLR 2021. arXiv:2010.11929 Verified
Scientific review note: A first scientific draft based on the original paper and reference implementations; it needs review by a specialist before final approval.

Version 1 — last updated 2026-11-15.