↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Layer 1 of 12 12 heads · 64-D queries, keys and values per head · scale 1/√64 · causal mask
Loading recorded attention…

What multi-head attention is

A language model has to let each token use what came before it: “it” needs to find “animal”, the second “brown” needs to recall what followed the first. Attention does this with a weighted average. Each token asks a question (a query), every earlier token offers a label (a key) and a message (a value), and the token takes the messages in proportion to how well their keys match its query. Multi-head attention runs several of these averages side by side, each with its own projections, so one layer can follow several relations at once.

This page records it in GPT-2 small: all 12 layers, 12 heads per layer, 64-dimensional queries, keys and values per head and a causal mask, on three prompts (Coreference, Repeated phrase, Two names). It is the worked example that the other Attention Explainer tabs follow, so its panels reappear on each of them.

Queries, keys and values

The Attention input strips are each token's vector after the block's LayerNorm (the first 64 of its 768 numbers). The Q · K · V bank projects every token three times. For head with input :

GPT-2 computes all three for all heads with one matrix of weights and splits the result into 12 heads of 64. Clicking a Q, K or V letter opens the projection with the token's recorded input and the selected head's recorded query, key or value.

Scaled dot-product attention

Inside one head, the score of key for query is their dot product, scaled by with , and a softmax over the visible keys turns the scores into weights that sum to 1:

The scale matters because the dot product of two -dimensional vectors with independent unit-variance entries has variance ; without it, large scores push the softmax towards a single key and its gradients towards zero. The condition is the causal mask: GPT-2 is trained to predict the next token, so a token may not look at later ones, and the head card greys those cells out.

The head card draws one head: keys along the top, queries down the side, the weight matrix between them and the values below. Clicking a row opens its scores, weights and weighted sum, all from the head's recorded tensors.

Many heads, one output

The 12 heads run in parallel on the same input, each with its own , and . Their 64-dimensional outputs are concatenated back to 768 numbers and mixed by one more matrix, :

The Output strips are this result, before it is added back to the residual stream. Splitting 768 dimensions into 12 heads of 64 costs no extra parameters over one 768-dimensional head; what it buys is 12 separate weightings of the context instead of one. The head pager on the card steps through them.

What the heads do here

The prompts are chosen so that well-known kinds of head have something to do, and the recording shows them. In Repeated phrase, layer 6 head 6 puts 0.84 of the last “ brown”'s weight on “ fox”, the token that followed “ brown” the first time: an induction head, which Olsson et al. tie to in-context learning. In Two names, at the final “ to”, layer 10 head 7 puts 0.49 on “ Mary”, the name that was not repeated; Wang et al. traced this indirect-object task in GPT-2 small to a circuit of such name-mover heads. In Coreference, layer 5 head 4 puts 0.85 of “ it”'s weight on “ animal”.

Not every head is this legible. Voita et al. found that a few specialised heads do most of the work in a translation model and the rest can be pruned, and Michel et al. that many heads can be removed at test time with little loss.

Where it came from

Attention began as a way for a translation decoder to look back at its encoder (Bahdanau et al., 2014; see the Cross tab). Vaswani et al. (2017) built a whole network from it, introducing scaled dot-product attention and multiple heads, and GPT-2 (2019) is a decoder-only stack of such layers. Most of the tabs that follow keep this computation and change one part of it: MQA, GQA and MLA shrink the keys and values a model must keep while it generates; Window and Sparse limit which keys a query sees; ALiBi changes how position enters the score; Flash computes exactly the same result with far less memory traffic.

Papers

  1. Attention Is All You Need Ashish Vaswani et al. · NeurIPS 2017 Introduces scaled dot-product attention, multiple heads and the Transformer built from them.
  2. Language Models are Unsupervised Multitask Learners Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever · 2019 GPT-2, the decoder-only model this page records.
  3. In-context Learning and Induction Heads Catherine Olsson et al. · arXiv 2022 Induction heads, which find the earlier copy of the current token and attend to what came next, as in the Repeated phrase prompt.
  4. Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 small Kevin Wang, Alexandre Variengien, Arthur Conmy, Buck Shlegeris, Jacob Steinhardt · ICLR 2023 The circuit of GPT-2 small heads behind prompts like Two names, including the name-mover heads.
  5. Analyzing Multi-Head Self-Attention: Specialized Heads Do the Heavy Lifting, the Rest Can Be Pruned Elena Voita, David Talbot, Fedor Moiseev, Rico Sennrich, Ivan Titov · ACL 2019 Finds positional, syntactic and rare-word heads, and prunes most of the others.
  6. Are Sixteen Heads Really Better than One? Paul Michel, Omer Levy, Graham Neubig · NeurIPS 2019 Removes heads at test time and finds that many layers work nearly as well with one.

See also