Part XV — Multimodal Foundation Models: CLIP, Cross-Attention & VLMs · Chapter 5

Patch projectors & image tokenization

Hook

Chapter 3 fed cross-attention a handful of ready-made "image patch" vectors and never asked where they came from. A raw image is just a grid of pixel numbers — how does anyone turn that grid into vectors a text-trained attention mechanism can even read?

Intuition
1.02.05.06.03.04.07.08.09.010.013.014.011.012.015.016.0
4×4 pixel image — click a cell

That cell belongs to the top-left patch — pixels [1, 2, 3, 4] flattened into one row.

This tiny picture is a 4×4 grid of pixel values. Click any cell — it belongs to one of four non-overlapping 2×2 blocks. That whole block, four raw pixel numbers, collapses into just two numbers: a patch embedding. Every one of those four blocks goes through the exact same collapsing step.

Formalize

Flatten a patch's pixels into one vector xx, then apply a fixed weight matrix WW:

e=Wxe = Wx
  • xx — one patch's pixels, flattened row-major into a length-4 vector.
  • WW — the projector: a fixed 2×42\times4 weight matrix, learned in a real model but hand-picked here.
  • ee — the resulting patch embedding, a single point in the same 2D space text tokens live in.
  1. Patchify

    Slice the image into non-overlapping blocks — here, four 2×2 blocks out of a 4×4 image.

  2. Flatten

    Read each block's pixels out in row order, turning a 2D block into a 1D vector xx of length 4.

  3. Project linearly

    Multiply by WW: row 0 of WW sums xx's top-row pixels, row 1 sums its bottom-row pixels — one genuine (if simplified) linear layer, mapping 4 numbers down to 2.

  4. Land in a shared space

    ee is now a point in the same 2D coordinate system as this chapter's text-token embeddings — nothing about ee's shape reveals it came from pixels rather than a word.

Play
1.02.05.06.03.04.07.08.09.010.013.014.011.012.015.016.0
4×4 pixel image — click a cell

Click through all four patches. Each one lands at a different point in the embedding space — and each point sits exactly on top of one particular text token's own embedding. That's not a coincidence built into this toy example so much as the entire goal of a real patch projector: train it so a patch of sky lands near the word "sky," a patch of dog lands near the word "dog," and so on.

Worked example

The bottom-right patch's raw pixels are [13141516]\begin{bmatrix}13 & 14\\ 15 & 16\end{bmatrix}:

  1. Flatten row-major

    x=(13,14,15,16)x = (13, 14, 15, 16).

  2. Apply the projector
    • Top-row sum: 13+14=2713+14=27.
    • Bottom-row sum: 15+16=3115+16=31.

    So e=Wx=(27,31)e = Wx = (27, 31).

  3. Compare to every text token

    The text token "dog" sits at exactly (27,31)(27, 31) — distance 00. Every other text token ("sky," "grass," "cat") is at least 88 units away in each coordinate. The nearest-neighbor search from Chapter 1 has an unambiguous winner.

Checkpoint

Click the patch whose projected embedding lands nearest the text token “dog” at (27, 31).

1.02.05.06.03.04.07.08.09.010.013.014.011.012.015.016.0
4×4 pixel image — click a cell

click a cell to try it

Click a patch to try it
Summary
e=Wxe = Wx

A patch projector is nothing more exotic than a linear layer: flatten a patch's pixels, multiply by a weight matrix, and the result is a token — indistinguishable in shape from a word embedding, and directly comparable to one by ordinary distance. This is the missing piece behind every image patch this Part has been feeding into attention and joint embedding spaces: real models learn WW end-to-end so that patches and words that mean the same thing land in the same place. The next chapter asks what happens once a model has to generate a whole sequence of these tokens — text and image alike — one at a time.