M10 / ARCHITECTURES

Attention. How a model decides
what to look at.

Convolutions look at fixed neighbourhoods. Attention lets every element look at every other and weigh how much each one matters — dynamically, per input. It started in language, took over NLP, then ate computer vision. Let's build the intuition from the ground up, then make it interactive.

EXPERIMENT 05.01 · SELF.ATTENTION ORIGIN · "ATTENTION IS ALL YOU NEED" (2017) VISION · ViT (2020) STATUS · LIVE
// 01

Background · why attention exists

THE PROBLEM IT SOLVES

Before 2017, sequence models were recurrent (RNNs, LSTMs): they read one token at a time, carrying a hidden state forward. Two problems. (1) Distance: relating word 1 to word 100 means threading information through 99 steps — signal decays. (2) Sequential: you can't parallelize; step t needs step t−1.

The transformer threw recurrence away. Its claim: you don't need to walk the sequence — just let every element directly query every other in one parallel operation. The mechanism that does this is self-attention. Distance becomes irrelevant (token 1 and token 100 are one hop apart) and the whole thing is a few big matrix multiplies, perfect for GPUs.

Vision borrowed it in 2020 (the Vision Transformer): chop an image into patches, treat each patch like a "word", and run the exact same machinery. No convolutions required.

CONVOLUTION vs ATTENTION
receptive fieldlocal (k×k)global (all)
weightsfixed kerneldata-dependent
long-rangeneeds depth1 layer
costO(n)O(n²)
Attention's superpower (everything sees everything) is also its cost: the N×N attention matrix grows quadratically with the number of tokens.
// 02

Anatomy · queries, keys & values

The whole mechanism is a soft, learnable dictionary lookup. Every token emits three vectors via three learned projection matrices: a Query ("what am I looking for?"), a Key ("what do I offer?"), and a Value ("what I'll actually pass on"). A token's output is a weighted blend of everyone's Values, where the weights come from how well its Query matches each Key.

SCALED DOT-PRODUCT ATTENTION · one query, step by step step 1 / 5
Attention(Q, K, V) = softmax( Q·Kᵀ / √dk ) · V
Walk through how one token's output is computed.
// 03

Vision Transformer · turning an image into tokens

THE ViT PIPELINE · image → patches → tokens → blocks → class
image
224²×3
→
patchify
16×16 grid
→
linear
embed
→ 768-D
+
positional
embedding
"where"
→
prepend
[CLS]
summary
→
×N transformer
blocks
→
[CLS] →
classifier

Patchify + embed: a 224² image becomes a 14×14 grid of 16×16 patches → 196 patches. Each patch is flattened and linearly projected to a 768-D vector. Now it's a sequence of 196 "tokens", just like words.

Positional embedding: attention is order-blind — shuffle the patches and it can't tell. So we add a learned position vector to each token, encoding where it sat in the grid.

[CLS] token: a special extra token is prepended. It attends to all patches across every layer and, at the end, its vector is the image's summary — fed to a tiny classifier head. (This is also the token whose attention map DINO visualizes.)

N blocks: stack the transformer block 12–32 times. Early blocks mix local patches; deep blocks relate far-apart regions into object-level meaning.

// 04

Interactive · hover a patch, watch it attend

Now make it concrete. Below is a patchified image. Hover any patch — that patch becomes the query. Lines shoot to the patches it attends to most; the heatmap shows the full distribution; the big matrix is every patch's attention to every other (one row per query).

INPUT · PATCH GRID hover any patch
ATTENTION FROM SELECTED QUERY
FULL N×N MATRIX · ROW = QUERY
SELECTED PATCH → ATTENTION HEATMAP
patch (0,0) · top-1 = (0,0) @ 0.12
PATCH SIZE 6
VIEW HEAD 0
TEMPERATURE 1.0
// 05

Multi-head · several attentions in parallel

One attention can only express one notion of "relevant". So transformers run several heads in parallel, each with its own Q/K/V projections, then concatenate. One head might track colour, another position, another contrast. Hover the image above; each head's response is shown below.

// 06

The full transformer block · what wraps the attention

ONE BLOCK
input tokens (+ residual stream)
LayerNorm
Multi-Head Self-Attention
⊕ residual add
LayerNorm
Feed-Forward MLP (4× wide)
⊕ residual add
output tokens → next block
WHAT EACH PIECE DOES
SELF-ATTENTION
Mixes information across tokens. The only place tokens talk to each other.
FEED-FORWARD
A 2-layer MLP applied to each token independently. Adds capacity and non-linearity; "thinks" about each token after it has gathered context.
RESIDUAL ⊕
Each sub-layer adds to the input rather than replacing it. Keeps a clean gradient highway so deep stacks train. (Same trick as ResNet.)
LAYERNORM
Normalizes each token's vector to stable scale before every sub-layer. Without it, the residual stream's magnitude explodes.
Put it together → attention moves information between tokens; the MLP processes each token; residual + norm keep it trainable. Stack 12–32 of these and you have GPT, BERT, ViT, CLIP's text tower — all the same block, different data.
The one-sentence version → Self-attention is a content-based, fully-connected, data-dependent mixing layer — and that single idea, stacked deep, turned out to be a general-purpose architecture for sequences, images, audio, and code.