Part XXIII — Modern Architectures, Generative Models & LLM Engineering · Chapter 1

Vision Transformers

Hook

A convolution kernel only ever looks at its immediate neighborhood — reaching a patch on the far side of an image takes as many stacked layers as it takes hops to get there. What if a single layer could look at every patch at once, the same way self-attention already looks at every word in a sentence?

Intuition
0.30.00.00.00.30.00.00.00.3
Attention weights from top-left to every patch

A 3x3 grid of image patches, with a bright diagonal running through it. Pick a query patch and watch which other patches it attends to — not just its neighbors, but any patch anywhere in the grid, scored purely by how similar its content is.

Formalize

A Vision Transformer (ViT) cuts an image into fixed-size patches, embeds each one, adds a position embedding, and feeds the resulting sequence into the exact same self-attention block from Part IV — no convolution anywhere:

patchiembeddingi+positioni    self-attention over all patches at once\text{patch}_i \to \text{embedding}_i + \text{position}_i \;\longrightarrow\; \text{self-attention over all patches at once}
  • patchi\text{patch}_i — the ii-th fixed-size image patch, treated like a token in a sequence.
  • embeddingi\text{embedding}_i — the learned vector representation of patch ii.
  • positioni\text{position}_i — a position embedding added to patch ii, since self-attention has no inherent sense of spatial order.
  1. A conv kernel's reach grows one hop at a time

    A convolution kernel's receptive field grows by one step of Chebyshev distance per layer — reaching a patch dd steps away takes at least dd stacked layers.

  2. Self-attention reaches everywhere in one layer

    Self-attention has no such restriction: every patch can attend to every other patch in a single layer, regardless of how far apart they sit in the image.

Play
0.30.00.00.00.30.00.00.00.3
top-left's attention over the full 3x3 grid

Top-left's attention lands overwhelmingly on the two other bright patches — including bottom-right, which sits on the opposite corner of the grid. A single 3x3 convolution centered on top-left couldn't reach bottom-right at all; self-attention reaches it in one hop, using nothing but content similarity.

Worked example

A 3x3 grid, bright diagonal (top-left, center, bottom-right), query = top-left:

  1. Distance rules out a single conv layer entirely

    Top-left and bottom-right sit at Chebyshev distance 22 — outside a single 3×33{\times}3 kernel's reach, which only covers distance 11. Connecting them with convolutions alone would take at least two stacked layers.

  2. Self-attention connects them anyway

    Query = top-left, embedding (2,0)(2,0). Both top-left and bottom-right are "bright" patches, so their dot products with the query are identical: qkbottom-right=(2)(2)+(0)(0.8)=4q\cdot k_{\text{bottom-right}} = (2)(2)+(0)(0.8) = 4, scaled to 4/22.8284/\sqrt2\approx2.828 — the same score top-left gets against itself. Softmax over all nine patches:

    • Three "bright" scores of 2.8282.828: e2.82816.92e^{2.828}\approx16.92 each
    • Six "dark" scores of 00: e0=1e^0=1 each

    Summing to 3(16.92)+6(1)=56.763(16.92)+6(1)=56.76. Each bright patch's share is 16.92/56.760.29816.92/56.76\approx0.298 — bottom-right gets that same 0.298\approx0.298 of top-left's attention, in a single layer, with no distance penalty of any kind.

  3. Content beats proximity

    The adjacent top-middle and mid-left patches are "dark," so their dot product with the query is 00 (e.g. qktop-middle=(2)(0)+(0)(0.1)=0q\cdot k_{\text{top-middle}}=(2)(0)+(0)(0.1)=0), giving e0=1e^0=1 in that same softmax. From the sum of 56.7656.76 above, each dark patch's share is 1/56.760.0181/56.76\approx0.018 — squarely inside a conv kernel's reach, but dark, they receive only 0.018\approx0.018 each. Being close doesn't matter if the content doesn't match; being far away doesn't matter if it does.

Checkpoint

Find the patch, between the two candidates, that receives more attention from top-left — despite being on the opposite corner of the grid.

Pick a patch to try it
Summary
conv: reach grows by 1 hop per layervsattention: every patch, every layer, one hop\text{conv: reach grows by 1 hop per layer} \qquad\text{vs}\qquad \text{attention: every patch, every layer, one hop}

ViT doesn't invent a new mechanism for images — it removes the assumption, baked into convolution since Part IV, that nearby pixels matter more than far ones by default. Patches become tokens, position becomes an embedding, and the same attention machinery that connects distant words in a sentence connects distant regions of an image just as directly. The next chapter asks a different question about that same idea: instead of building a new model from scratch, how much of what one already-trained network learned can be reused for something else entirely?