Attention, visualized
straight from the paper
One idea runs the whole Transformer: every token asks a question, every other token answers, and the answers get blended by how well they match. This page reproduces the two figures of “Attention Is All You Need” — and makes every step something you can drag, sweep, and read the actual numbers off of.
- paperVaswani et al. 2017
- the opsoftmax(QKᵀ/√dₖ)·V
- costO(T²) in sequence length
- headsrun in parallel
00 Watch it happen first
Before a single formula, watch the whole thing move. Below, one word — the verb “chased” — works out which earlier words matter to it. Press play and read the line under the stage; each scene is one small step. Scrub back and forth, or click a chapter dot to jump. Nothing here needs to make sense yet — the point is to see the shape of the computation before we name its parts.
We’ll watch how one word decides what to pay attention to.
That's the entire mechanism. Said in one sentence: attention is a soft dictionary lookup. Each token emits a query — “what am I looking for?” — and every token also advertises a key— “here's what I am.” Match every query against every key, turn the matches into weights that sum to 1, and use those weights to average the tokens' values. A token's new representation is a blend of the tokens it found relevant. Everything below is that one sentence, drawn at deeper and deeper zoom — exactly the two figures the 2017 paper is famous for.
01 Scaled dot-product attention — Figure 2, left
The paper writes the whole operation as one line:
Attention(Q, K, V) = softmax( Q·Kᵀ / √dₖ ) · VRead it right to left through the pipeline and nothing is mysterious:
- Q·Kᵀ — every query dotted with every key. For
Ttokens that's aT×Tgrid of raw match scores. High score = “this key answers my query.” - ÷ √dₖ — the scaled part. Dot products of
dₖ-dimensional vectors grow like√dₖ; left unscaled they'd push softmax into a corner where its gradient vanishes. Dividing by√dₖkeeps the scores in a sane range. (Withdₖ = 64, that's ÷8.) - mask — in a decoder (GPT), a token may not see the future. Positions ahead are set to
−∞so softmax gives them exactly zero weight. - softmax — each row becomes a probability distribution: how much this query attends to each key, summing to 1.
- · V — those weights average the value vectors. The output for each token is a weighted blend of the values it attended to.
The animation up top drove itself. This one you drive — same operation, new sentence, now a sandbox. Pick a word on the left to make it the query. Switch heads. Toggle the causal mask. Presssweep to watch every token take its turn. The connection lines, the matrix, and the arithmetic strip are all the same numbers, shown three ways:
click a word on the left · line thickness = attention weight
Two things are worth pausing on. First, the matrix is lower-triangular once the mask is on — that wedge of zeros in the top-right is causality, the reason a language model can't cheat by peeking ahead. Second, on Head 2 the query “it” puts almost all its weight on “cat”: the model has, in this head, learned to resolve the pronoun. Different heads, different jobs — which is the whole point of the next level.
02 Multi-head — Figure 2, right
One attention is one point of view. If a single head has to track grammar and coreference andlocal phrasing all at once, those signals collide. So the Transformer runs h heads in parallel, each with its own small learned projections of Q, K and V into a d/h-dimensional subspace:
MultiHead(Q,K,V) = Concat(head₁, …, head_h) · Wᴼ
headᵢ = Attention(Q·Wᵢᵠ, K·Wᵢᴷ, V·Wᵢⱽ)Each headᵢ is exactly the scaled dot-product attention you just used — just on its own slice of the space, free to specialise. In the widget above, “local” and “reference” are two such heads: one wires each word to its predecessor, the other wires “it” to “cat.” Their outputs are concatenatedback to width d and passed through one more linear layer Wᴼ. Same cost as one big head, many more relationships captured.
[A][B] … [A] → [B] — doing one legible job. The per-head view here is the toy version of how that research reads a model.03 The block, and the stack — Figure 1
Attention is one sub-layer. Wrap it the way the paper does — a residual add and aLayerNorm around it, then a position-wise feed-forward network wrapped the same way — and you have one Transformer block. Stack N = 12 (or 96) of them, put an embedding at the bottom and a softmax head at the top, and that's the model. Click through it:
d/h-dim sliceClick any box. Data enters at the bottom as token ids and rises up the residual stream — the vertical line — being read and nudged by every layer until, at the top, the last position becomes a distribution over the next token.
The vertical line is the residual stream — the paper's quiet hero. Each sub-layer computes a small update and adds it; it never overwrites. That's why 12, 96, 175 layers deep still trains: the gradient always has a clean path straight down the skip connections, and every layer only has to learn anudge. Attention moves information between tokens; the feed-forward network then does the per-token processing. Alternating those two is the rhythm of the whole architecture.
04 Where the endgame lives
Look again at the matrix. It is T×T: attention's cost grows with the squareof the sequence length. On a long context that Q·Kᵀ is one of the biggest, most expensive things the GPU does — and, crucially, it is memory-bound, not compute-bound. The chip spends most of the operation waiting on HBM, not multiplying.
Run it and nvidia-smi will happily read 100% the entire time — while the tensor cores sit mostly idle, starved for data. That exact gap, on this exact operation, is whatFlashAttention (Walk 04) attacks by never writing theT×T matrix to slow memory, and whatutilization-truth is built to measure. You just watched, weight by weight, the operation where “busy” and “doing useful work” diverge the most. That divergence is the company.
↗ Full references — go to the source
We rebuild the paper's figures in our own words and never rehost anyone's material. These are the originals — start with the paper itself.
- paperVaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, Polosukhin“Attention Is All You Need” (2017) — the paper, and the two figures this page reproduces
- blogJay Alammar“The Illustrated Transformer” — the canonical picture-first explainer
- codeJesse Vig — BertVizthe interactive head-view tool that inspired the connection-line visual here
- blogHarvard NLP“The Annotated Transformer” — the paper reimplemented line by line in PyTorch
- video3Blue1Brown“Attention in transformers, visually explained” — the geometry of Q, K, V
- blogLilian Weng“The Transformer Family (v2)” — every attention variant since, in one map
- paperElhage, Nanda et al. (Anthropic)“A Mathematical Framework for Transformer Circuits” — attention heads as readable, composable operations
- paperOlsson et al. (Anthropic)“In-context Learning and Induction Heads” — the most famous specific thing a head learns to do
- blogPeter Bloem“Transformers from scratch” — a careful, math-complete derivation
- courseStanford CS224Nlecture on self-attention and Transformers — the classroom version
- paperBa, Kiros & Hinton“Layer Normalization” — the Norm in every Add & Norm
- codekarpathy/nanoGPTthe same attention in ~40 lines of runnable PyTorch — read it in Walk 02