Real attention patterns

Open up GPT-2 and look at its 144 attention heads on real sentences. Some read the previous token, some resolve pronouns, some copy repeated text, and many dump their attention on the first token. Here is an explorer, and the stories of the heads researchers understand best.

Key idea
Trained heads specialise: previous-token heads, induction heads that continue repeated text, pronoun heads, and first-token “sinks”
Real world
Watching where people’s eyes go while they read. Some glance back at the last word, some jump back to a name mentioned earlier, and when a line needs no checking their eyes rest on the start of the page.
In an LLM
Give GPT-2 “The quick brown fox jumps over the lazy dog.” twice. On the second “ fox”, head 5.5 puts 96% of its attention on “ jumps”, the word that followed “fox” the first time, and GPT-2 predicts “ jumps” next.

How to read an attention pattern

Heads are named layer.head, and both numbers start at 0, so “4.3” is head 3 (the fourth head) in layer 4 (the fifth of the 12 layers).

Each head in each layer produces a T×TT \times T matrix of weights. Row ii shows how token ii splits its attention over tokens 11 to ii. Two ways to draw it:

  • Lines: queries on the left, keys on the right, a line for each weight, thicker for more. This is the style of the bertviz tool.
  • Heatmap: the matrix itself, darker for more weight, empty above the diagonal because of the causal mask.

A pattern shows where a head reads, not what it does with it. The value vectors and WOW_O decide what is carried. So a pattern is a strong clue, not a full explanation.

The explorer

All the numbers below come from running GPT-2 small (12 layers, 12 heads each) on four prompts. The grid on the left scores every head on the repeated fox sentence, by how much of its attention goes to the previous token, the first token, or the induction target. Click around.

Loading GPT-2 data…

Heads with a known job

Previous-token head: 4.11

Head 11 in layer 4 gives over 99% of every token’s attention to the token just before it, on every prompt. It does no “understanding” at all. What it does is useful plumbing: it copies information about the previous token into each position, so that later heads can ask “which token came before me?” using only the current position. That is the first half of an induction head.

Induction heads: 5.1, 5.5, 7.10

Language repeats itself: names, phrases, code, quotes. An exploits that. At the current token, say “fox”, it looks for earlier positions whose previous token was also “fox”, and attends to them. The token at such a position is the one that came after “fox” last time, “jumps”, which is a good prediction for what comes next.

It needs two heads working together, in different layers:

  1. A (like 4.11) writes into each position “the token before me was X”.
  2. The induction head’s query asks “who was preceded by my current token?”, and its keys answer using what step 1 wrote.

Here is the idea with exact rules instead of learned weights. Press play, then try the other sentences, including one where nothing matches:

the text
The
← start
·
quick
← The
·
brown
← quick
·
fox
← brown
·
jumps
← fox
·
over
← jumps
·
the
← over
·
lazy
← the
·
dog
← lazy
·
.
← dog
·
The
← .
·
quick
← The
·
brown
← quick
·
fox
← brown
now
step 1 of 4: the text
The model has read 14 tokens. The last one, “fox”, must predict what comes next.
Notice the match is by the token before: the head finds earlier copies of the current word, then reads the word after each copy. That is why it needs two heads.
A toy induction circuit with exact rules, not learned weights. Press play, then try the other sentences. First every position gets a note “← X” with the token before it. Then the last token asks which positions have a note equal to itself, and finally it copies those tokens.

Now the real thing. These are GPT-2’s own heads on the fox sentence typed twice:

Loading GPT-2 data…

The effect on predictions is large. The first time through the sentence, GPT-2’s top guess for the next token is right only once in ten tries (“ over” → “ the”). The second time, it is right eight times out of nine, often with over 90% confidence: after the second “ lazy” it gives “ dog” 99%, where the first time it guessed a comma at 6%.

example · a song’s chorus

The first time you hear a new song, you can’t sing along. The second time the chorus comes round, you can: when you hear its first line, you remember what followed it before. You aren’t predicting from the meaning of the words; you are matching “I’ve heard this bit before” and reading off what came next. That is what an induction head does.

Induction heads are a big part of how models learn from the prompt (in-context learning). Anthropic researchers found that they appear suddenly during training, in a sharp “phase change”, at the same moment the model gets much better at using long contexts.

Pronoun head: 4.3

On “The cat sat on the mat because it was tired”, head 4.3 sends 84% of the attention of “it” to “cat” and 4% to “mat”. This is the example from the intuition page. We have checked only this one sentence, so it is safer to say “4.3 does this here” than “4.3 is the pronoun head”.

Name movers: 9.6 and 9.9

In “When Mary and John went to the store, John gave a drink to”, GPT-2 predicts “ Mary” (45%). At the final “ to”, head 9.6 gives 49% of its attention to “ Mary”, and only 12% and 6% to the two mentions of “ John”; head 9.9 gives 36% to “ Mary”. A 2022 study traced the whole circuit for this kind of sentence and called these name mover heads: they copy the name that hasn’t been repeated to the last position. Other heads, earlier in the network, work out which name is the repeated one.

Attention sinks

Pick a head at random and the most common thing you will see is not a clever pattern: most of the weight goes to the very first token, whatever the query. Heads 4.10, 8.11 and 9.1 send it about 87% of their attention on average, and on the fox sentence 95 of GPT-2’s 144 heads send it more than half on average.

The first token is rarely the most informative word, so why? The best current explanation:

  • Softmax weights must add up to 1. A head has no way to say “nothing here is relevant”; the weight has to go somewhere.
  • The first token is visible to every other token (thanks to the causal mask), so it is a convenient, always-available place to park unwanted weight. Models learn to give it a small value vector, so attending to it adds almost nothing: a “no-op”. In heads 4.10, 8.11 and 9.1 of GPT-2, what the first token passes on (its value times WOW_O) is about a tenth of the size of what any other token in the cat sentence would pass on.
  • The first token’s vector becomes very large inside the model, which helps it attract this attention. In GPT-2 on the cat sentence, by layer 6 its vector has a length of about 3,000, against roughly 70 to 100 for every other token.

This is called an . It matters in practice. The StreamingLLM paper found that if you drop the first tokens from the KV cache to save memory on a long conversation, quality collapses; keep just those few tokens and it recovers. Newer models sometimes add a dedicated sink token, or a way for softmax to output “nothing”, for this reason.

example · the default answer

A survey asks “which of these shops did you visit?” and requires exactly one tick. People who visited none still have to tick something, so they tick the first box. Count the ticks and the first box looks very popular, but the ticks mean “none of the above”. An attention sink is that first box.

A word of caution

Patterns are suggestive, but:

  • A head can attend strongly to a token and still pass on almost nothing, if its value vectors are small (sinks are the extreme case).
  • Many heads mix several behaviours, and change them depending on the prompt.
  • Labels like “pronoun head” come from a few examples. Proper evidence comes from interventions: change the head’s output and see what breaks. That is the subject of interpretability.
going deeper

How the scores in the grid are computed

On the fox prompt (20 tokens, the second copy starts at position 11), for head (l,h)(l, h) with weights aija_{ij}:

  • previous token: mean of ai,i−1a_{i,i-1} over all positions i≥2i \ge 2;
  • first token: mean of ai,1a_{i,1} over all positions i≥2i \ge 2;
  • induction: mean of ai, i−9a_{i,\,i-9} over the second copy, the weight on the token after the earlier occurrence of the current token.

Top heads: previous 4.11 (1.00); first 4.10 (0.89), 8.11 (0.88), 9.1 (0.86); induction 5.1 (0.78), 5.5 (0.75), 7.10 (0.63).

The induction circuit

Let the previous-token head write et−1e_{t-1} (information about the previous token) into position tt. The induction head’s query at position tt is built from the current token ete_t, and its key at position jj from what the previous-token head wrote there, ej−1e_{j-1}. The score qt⋅kjq_t \cdot k_j is high when et≈ej−1e_t \approx e_{j-1}: “the token before jj is the same as me”. The head then copies eje_j through its values, and WOW_O writes it towards the unembedding of that token. This QK and OV split is from Elhage et al. (2021).

In papers

Related

  • multi-head - One attention head can only follow one pattern at a time. A transformer layer runs several heads side by side, each on its own slice of the vector, so different heads can track different relations at once. Their outputs are glued back together and mixed by one more matrix.
  • attention-intuition - Attention lets each token pull in information from other tokens. It works like a dictionary lookup that never fails: instead of returning the one entry whose key matches, it returns a weighted average of every entry, weighted by how well each key matches the question.
  • interpretability - Interpretability asks what a trained network is actually computing. Here we take apart one real circuit in GPT-2 small, see why single neurons are hard to read (superposition), and how sparse autoencoders and attribution graphs pull readable features and causal paths out of large models.
  • kv-cache - When a model writes text, each new token needs the keys and values of every token before it. Those never change, so they are computed once and stored. This KV cache turns generation from work that grows with the square of the length into work that grows linearly, at the price of a lot of memory.