Chapter 14: Vision Foundations
Everything in the book so far reads token ids. This chapter teaches the system to see: by its end, a compressed image file becomes the exact float tensors a published vision tower was trained on, that tower reproduces Hugging Face’s hidden states at every layer, and a small dual encoder trained on captions alone can classify images it has never seen labels for.
Concepts from other sections (16)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- hidden states: the block inputs and outputs, before the final norm, that a VLM can tap. Read more: 14.3 Vision transformer encoder
- dual encoder: two towers, one per modality, projecting into a shared embedding space. Read more: 14.5 Contrastive image-text: CLIP and SigLIP
- patch embedding: a convolution with kernel equal to stride, equivalent to one linear map applied to each flattened patch. Read more: 14.3 Vision transformer encoder
- axial 2D RoPE: rotary embedding where half the pairs rotate by the patch row and half by the column, so scores depend on . Read more: 14.2 2D RoPE and M-RoPE
- M-RoPE: Qwen2-VL’s multimodal RoPE, giving each token three position ids . Read more: 14.2 2D RoPE and M-RoPE
- Contrastive training: fine-tuning an embedder by pulling matching pairs together and pushing negatives apart. Read more: 16.3 Embeddings, small models, and platforms
- zero-shot classifier: a linear classifier whose rows are text embeddings of class prompts. Read more: 14.5 Contrastive image-text: CLIP and SigLIP
- RoPE: rotary position embedding, which rotates queries and keys by position to encode relative position. Read more: 5.3 Model landscape and lifecycle
- Linear map: a function between vector spaces with ; once bases are chosen, every linear map is a matrix. Read more: Chapter 9: Linear Algebra in Mathematics
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- im2col: the linear map that copies each receptive field of an image into a column, so a convolution becomes a matmul. Read more: 14.1 Conv layers and pooling on autograd
- tie rule: the fixed backtrace preference (diagonal, deletion, insertion) that makes the S, D, I split deterministic. Read more: 15.5 Multimodal eval metrics: WER, CER, VQA, captions, zero-shot
- pixel shuffle: a permutation that moves an block of tokens into the channels of one token. Read more: 14.4 Dynamic resolution and tiling
- InfoNCE: a softmax cross-entropy that picks the matching item from a batch of candidates; CLIP applies it in both directions. Read more: 14.5 Contrastive image-text: CLIP and SigLIP
- pairwise sigmoid loss: SigLIP’s loss, one independent yes-or-no logistic term per pair. Read more: 14.5 Contrastive image-text: CLIP and SigLIP
- pixel budget: the configured maximum
width*height, checked from the header before decompression. Read more: 14.6 Image preprocessing to the specification
The arc runs from layers to pixels. You start with convolution and pooling as autograd layers, the patch embedding every vision transformer begins with. Then come positions for a two-dimensional grid: axial 2D RoPE for patches, and Qwen2-VL’s M-RoPE, which lets an image occupy a small square of positions inside a language model’s sequence. With those pieces, the third section builds one configurable vision transformer that reproduces the ViT, CLIP, SigLIP, and Idefics3 towers. The fourth handles images larger than the tower’s training size by tiling them, and makes the resulting token count an exact function of the image size. The fifth gives the tower’s vectors meaning through contrastive training on image-text pairs, which also yields zero-shot classifiers. The last section closes the loop at the input end, turning untrusted encoded bytes into tensors exactly to the processor’s specification.
One thread runs through every section: match a published reference exactly. A wrong switch, rounding, or pixel order still loads and runs; it just produces features the model was never trained on. So each section pins its behaviour against Hugging Face and against the mathematical properties it derives. A planned vision-language model section will join these features to the language model.
Learning objectives
- Implement convolution and pooling layers with hand-written backward passes and train LeNet-5 with them.
- Build axial 2D RoPE and M-RoPE position schemes and explain why each preserves relative position.
- Configure one vision transformer to reproduce four published towers at every hidden state.
- Compute tiling and image token counts for Idefics3 and LLaVA-NeXT exactly from the image size.
- Train a CLIP or SigLIP dual encoder and use it for zero-shot classification.
- Preprocess encoded images safely and exactly to a model’s processor specification.
Before you begin
Section titled “Before you begin”The mathematics comes from chapter 12 of Mathematics, Signals and images: convolution as a linear map, image layouts, and resampling. You should be comfortable with the autograd engine and layers of Foundations, attention from The Transformer (2017), and the RoPE of The Modern Decoder Block. The contrastive section also uses the cross-entropy and softmax-with-temperature ideas covered in those chapters.
In this chapter
Section titled “In this chapter”- Conv layers and pooling on autograd. Turn im2col into
Conv2d,Conv1d, and pooling layers with three-formula backward passes, pin the max-pool tie rule, and prove the layers learn by training LeNet-5 on digits. - 2D RoPE and M-RoPE. Give half the rotary pairs to the row and half to the column so attention scores depend only on patch offsets, and give language-model tokens three position ids so an image costs its side length in positions, not its token count.
- Vision transformer encoder. Make an image a sequence with a patch embedding, choose among learned, sin-cos, and rotary positions, hide padding on the key side, and reproduce four published towers from one config.
- Dynamic resolution and tiling. Cut large images into tower-sized tiles the way Idefics3 and LLaVA-NeXT do, count image tokens from the size alone, and trade sequence length for width with pixel shuffle.
- Contrastive image-text: CLIP and SigLIP. Derive CLIP’s symmetric InfoNCE loss and SigLIP’s pairwise sigmoid loss, learn and clamp the temperature, and write a classifier in words.
- Image preprocessing to the specification. Decode untrusted bytes within a pixel budget, apply orientation and alpha exactly, follow each processor family’s geometry and normalization, and hash media by its original bytes.