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

Vision-Language Models (VLM) assembled

Hook

Chapter 1 measured distance between whole images and whole captions. Chapter 3 let a text token attend over image regions. Chained together, they answer an actual question about an image — not "how similar are these two things," but "what's in the part of the image this question is asking about?"

Intuition
sky patch0.77ground patch0.05dog patch0.19query
answer: "sky" — context is 0.39 units from that prototype

Drag the query — the embedding of a question like "what's in the sky?" Cross-attention (Chapter 3) blends the image patches into one context vector; that context vector is then matched, nearest-neighbor style (Chapter 1), against a small set of answer prototypes. Two ideas, chained, produce one answer.

Formalize

The whole pipeline is two formulas run back to back, neither one new:

context=isoftmax ⁣(qkid)iki,answer=argmina  contexta\text{context} = \sum_i \text{softmax}\!\left(\frac{q \cdot k_i}{\sqrt{d}}\right)_i k_i, \qquad \text{answer} = \underset{a}{\arg\min}\; \lVert \text{context} - a \rVert
  • qq — the query: the embedding of the question being asked about the image.
  • kik_i — the key vector for image patch ii, the same cross-attention mechanism from Chapter 3.
  • dd — the dimensionality used to rescale the raw attention scores.
  • context\text{context} — the blended vector cross-attention produces, summarizing where the query looked.
  • aa — one candidate answer prototype from a small set of possible answers.
  • answer\text{answer} — the answer prototype nearest to the context vector, chosen by nearest-neighbor search.
  1. Cross-attention decides where to look

    It blends the image patches into a single context vector, weighted by relevance to the query.

  2. Nearest-neighbor decides what to call it

    It matches that context vector against a small set of answer prototypes and picks the closest one.

  3. Neither step knows about the other's internals

    The context vector is just a point in space — the nearest-neighbor search doesn't care how that point was produced, only where it landed.

Play
sky patch0.05ground patch0.77dog patch0.19query
ranked answers: ground (0.39), dog (1.02), sky (2.44)

Drag the query anywhere and check the full ranked list, not just the winner. The runner-up answer is always the patch that received the second-most attention — the ranking isn't a coincidence, it's a direct consequence of how much each patch pulled the context vector toward itself.

Worked example

Query aligned with the sky patch, q=(2,0)q=(2,0) — the exact numbers from Chapter 3:

  1. Cross-attention produces a context vector

    With q=(2,0)q=(2,0), the same score→softmax steps as Chapter 3 give:

    • Sky: score 4/22.8284/\sqrt2\approx2.828, weight 0.768\approx0.768
    • Ground: score 0/2=00/\sqrt2=0, weight 0.045\approx0.045
    • Dog: score 2/21.4142/\sqrt2\approx1.414, weight 0.187\approx0.187

    Weighting each patch and summing: 0.768(2,0)+0.045(0,2)+0.187(1,1)(1.723, 0.277)0.768(2,0)+0.045(0,2)+0.187(1,1) \approx (1.723,\ 0.277).

  2. Nearest-neighbor picks the answer

    Distance from (1.723,0.277)(1.723, 0.277) to each prototype:

    • Sky (2,0)(2,0): (1.7232)2+(0.2770)20.39\sqrt{(1.723-2)^2+(0.277-0)^2}\approx0.39
    • Dog (1,1)(1,1): (1.7231)2+(0.2771)21.02\sqrt{(1.723-1)^2+(0.277-1)^2}\approx1.02
    • Ground (0,2)(0,2): (1.7230)2+(0.2772)22.44\sqrt{(1.723-0)^2+(0.277-2)^2}\approx2.44

    Sky wins by more than double the runner-up's distance.

  3. A query with no preference at all

    At q=(0,0)q=(0,0), attention is exactly uniform — 13\frac13 to each patch — and the context vector lands at (1,1)(1,1), which happens to be exactly the dog prototype's own coordinates. Perfectly even attention still produces a definite, confident answer here, just not necessarily the one you'd expect.

Checkpoint

Drag the query until the pipeline's answer becomes “dog.”

sky patch0.77ground patch0.05dog patch0.19query
current answer: "sky"
Drag the query to try it
Summary
context=isoftmax ⁣(qkid)iki,answer=argmina  contexta\text{context} = \sum_i \text{softmax}\!\left(\frac{q \cdot k_i}{\sqrt{d}}\right)_i k_i, \qquad \text{answer} = \underset{a}{\arg\min}\; \lVert \text{context} - a \rVert

This is the shape of every real vision-language model: an image encoder, a text encoder, a cross-modal attention layer connecting them, and a read-out step turning the result into an answer, a caption, or a classification. Every piece was already built in earlier chapters — assembling them is most of the actual engineering. The next chapter asks what happens when the model doesn't just answer from the image in front of it, but retrieves outside information first.