scaled-dot-attention -- the single-head pipeline
This is the step where words read each other. In "the cat sat on it", something has to work out that it means the cat: every word compares its question against every other word's offer, those comparisons are turned into percentages, and each word pulls in a blend of what the others carry, weighted by how well they matched. Two details do real work. Every score is divided by the same fixed number first so the percentages do not collapse onto a single winner, and a word may only look backwards, never forwards. Every other form of attention is a variation on this one formula.
Interactive page: the full scaled dot-product attention computation over a sequence, stage by stage. Anchor: A3 attention pattern (Family B2).
What it shows
Attention(Q,K,V) = softmax(QKᵀ/√d + mask)·V, with the N×N score matrix as the centerpiece. The pipeline auto-plays + loops; drag a Q (query) cell vertically to re-steer that query and watch its row of scores, the softmax weights, and the output recompute live; hover a score / weight / output cell for its derivation. Scrub the transport through the five stages:
- QKᵀ — each cell
[i,j]isqᵢ·kⱼ(query i against key j). - / √d — scale, so the softmax doesn't saturate.
- causal mask — query
imay only see keysj ≤ i; the upper triangle goes to −∞ (shown veiled). - softmax (rows) — each row becomes a probability distribution over the visible keys (lower-triangular, rows sum to 1).
- × V → output — each output row is
Σⱼ wᵢⱼ·vⱼ, the weighted sum of values.
Q, K, V are shown as input strips; the output [N×d] appears at the final stage. This is the prefill view (all positions at once) — the companion to ../kv-cache/, which shows the decode view (one new query over a growing cache).
Render tier
T2 (WebGL2 heatmaps for the matrices; Canvas2D overlays for the causal veil, labels, and stage highlight).
Wiring
layout.mount + controls (tokens, head_dim, seed) + a 5-stage Transport built in page.js (uses tensor.dot/softmax/seededRandn), drawn with render.heatmap/cell + ctx. Interactivity per the framework contract: autoplay+loop transport, onPointer Q-cell drag with cellAt hit-testing + recompute, setTip hover derivations. Headless hooks ?hover=x,y / ?drag=i,c,val / ?step=N / ?play=1 (drag/hover pause + park on a deterministic stage) as on the dot-product + matmul pages. Source: page.js.