Attention: a token
reaches across the sentence.
A token scores every other token by alignment, focuses those scores into a soft distribution with softmax, then mixes the tokens' values in those proportions. This demo isolates that mechanism end-to-end on a real sentence — driven by GPT-2's actual attention across all 12 layers and heads, or by GloVe word vectors as a transparent stand-in. Click ⤢ Expand to blow the panel up for the room.
AMix — a weighted blend of values
ΣPutting it together
Q Kᵀ is the score · softmax(·/√dk) is the focus · multiplying by V is the mix you've been driving above. One head does this once. Multi-head attention runs h of them in parallel on different learned subspaces, then concatenates and projects:
Different heads specialise — one tracks syntax, another coreference, another position. Slide through GPT-2's heads above to find them.
⚠What we're simplifying
- GloVe mode uses identity projections on static word vectors. We set WQ=WK=WV=I and use GloVe vectors, so attention there is driven by raw word similarity. Real heads learn WQ,WK,WV (the “randomize” button hints at this), operate inside the residual stream, and add an output projection WO. The GPT-2 (real) mode shows the genuine learned attention.
- One head at a time. Real layers run many heads in parallel (the coda), and the interesting behaviour — induction, in-context learning — comes from composing attention across layers.
- LayerNorm and the MLP are out of frame. This demo isolates the attention sub-layer; the residual-stream panel (GPT-2 mode) shows how a token's vector is reshaped as context folds in, but the full block has more moving parts.
Sources
- Vaswani et al. (2017). Attention Is All You Need. NeurIPS. — scaled dot-product attention.
- Bahdanau, Cho & Bengio (2015). Neural Machine Translation by Jointly Learning to Align and Translate. ICLR. — attention's origin.
- Elhage et al. (2021). A Mathematical Framework for Transformer Circuits. Anthropic. — the QK / OV decomposition; previous-token and induction heads.
Vectors: GloVe 100-d via npm wink-embeddings-sg-100d (MIT; Stanford GloVe, PDDL). Real attention exported from gpt2-small. Math: KaTeX. Runs entirely from this folder — no build, no network.