Lesson 3 of 8

Many heads and the causal mask

Transformers run several attention heads side by side, each free to learn its own pattern, and language models hide the future from every token with a causal mask.

Advanced18 min

In this lesson you will

  • Explain why transformers split attention into several heads, and what that costs
  • Describe the causal mask and why text-generating models need it
  • Recognize common attention patterns such as previous-token, induction, and first-token heads

In the last lesson a token’s query produced one set of weights, and the token received one weighted average of the values. That is a tight budget. A word may need several kinds of information at once: which noun it refers to, what word came just before it, whether it appeared earlier in the text. With one softmax, all of those needs compete for the same total weight of 1.

Transformers handle this by running several attention operations in parallel, called heads, and they make text generation possible with one more ingredient, a mask that stops tokens from seeing the future.

Multi-head attention

Multi-head attentionRunning several attention heads in parallel, each with its own query, key, and value projections, then concatenating their outputs and mixing them with one more matrix.Open in glossary runs hh separate Attention headOne independent attention computation inside a multi-head attention layer, with its own learned projections and its own pattern of weights.Open in glossary. Each head has its own learned projections, so it computes its own queries, keys, and values and produces its own pattern of weights:

headi=Attention(XWiQ,  XWiK,  XWiV)\mathrm{head}_i = \mathrm{Attention}(X W^Q_i,\; X W^K_i,\; X W^V_i) MultiHead(X)=Concat(head1,…,headh) WO\mathrm{MultiHead}(X) = \mathrm{Concat}(\mathrm{head}_1, \ldots, \mathrm{head}_h)\, W^O

The heads do not add width. Each one works in dk=dmodel/hd_k = d_{\text{model}} / h dimensions, and concatenating hh of them gives back a vector of width dmodeld_{\text{model}}, which the output matrix WOW^O mixes together. The original transformer used dmodel=512d_{\text{model}} = 512 and h=8h = 8, so each head had 64 dimensions.

Splitting the model width into heads

More heads means narrower heads. The total size of the attention layer does not change.

One token's 512-dimensional vector, cut into 8 heads of 64 dimensions. Each head has its own query, key, and value projections of shape 512 × 64, runs attention independently, and the 8 outputs are concatenated back to width 512 and multiplied by WO (512 × 512).

Model
8
Model width, d_model512
Per-head width, d_k = d_model / h64
Projection weights, 4 × d_model²1,048,576

Try this

  • Pick GPT-2 small and move the heads slider. The per-head width changes; the number of projection weights stays at 2,359,296.
  • Pick GPT-3 175B: 96 heads of 128 dimensions each, inside a 12,288-dimensional model.

In standard multi-head attention, where each head has width dmodel/hd_{\text{model}}/h, the per-head projections together form dmodel×dmodeld_{\text{model}} \times d_{\text{model}} matrices, so one attention layer has 4 dmodel24\,d_{\text{model}}^2 projection weights (plus biases, if the model uses them), whatever hh is. Some newer models share keys and values across heads to save memory, which changes this count; the KV cache lesson covers that. The number of heads is a choice about how to divide that capacity: many narrow heads, each free to attend somewhere different, or a few wide ones.

Hiding the future: the causal mask

A language model is trained to predict the next token at every position of a training sequence at the same time. That parallelism is a big part of why transformers train efficiently. But it creates a problem: if position 5 can attend to position 6, it can simply read the answer it is supposed to predict.

The fix is the Causal maskA mask that stops each token from attending to tokens after it, by setting those attention scores to negative infinity before the softmax. Used by models that generate text left to right.Open in glossary. Before the softmax, every score where the key comes after the query is replaced by −∞-\infty:

softmax ⁣(QK⊤dk+M),Mij={0j≤i−∞j>i\mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}} + M\right), \qquad M_{ij} = \begin{cases} 0 & j \le i \\ -\infty & j > i \end{cases}

Since e−∞=0e^{-\infty} = 0, those positions get exactly zero weight, and the weight matrix becomes lower triangular. Each token sees itself and everything before it, never after. This matches how generation works: when the model produces token 6, tokens 7 onward do not exist yet.

Not every transformer uses the mask. Encoder models such as BERT let every token see the whole input in both directions, which suits tasks like classifying or searching text, but they are not built to generate text one token at a time.

What heads learn to do

Nobody tells a head what to attend to. Its pattern emerges from training. Interpretability research has nonetheless found patterns that show up again and again. The demo builds five of them on one short sentence so you can compare them side by side.

Five heads, one sentence

Each head applies its own scoring rule to the same tokens. Pick a head, then pick a query token to see where it looks.

Choose a head

Each token finds an earlier copy of itself and looks at the token that came next, which helps predict a repeat.

Induction heads are described by Elhage et al. (2021) and Olsson et al. (2022), who tie them to in-context learning.

Tap a token to make it the query. Shading shows where it looks.
Attention weights for the Induction head. Rows are queries, columns are keys. Cells above the diagonal are masked.
Querythecatchasedthedog.thecatchasedthe
1.00maskedmaskedmaskedmaskedmaskedmaskedmaskedmaskedmasked
0.880.12maskedmaskedmaskedmaskedmaskedmaskedmaskedmasked
0.790.110.11maskedmaskedmaskedmaskedmaskedmaskedmasked
0.050.940.010.01maskedmaskedmaskedmaskedmaskedmasked
0.650.090.090.090.09maskedmaskedmaskedmaskedmasked
0.600.080.080.080.080.08maskedmaskedmaskedmasked
0.020.480.000.000.480.000.00maskedmaskedmasked
0.050.010.920.010.010.010.010.01maskedmasked
0.050.010.010.910.010.010.010.010.01masked
0.020.320.000.000.320.000.000.320.000.00
HeadInduction
Querycat (7)
Looks most atchased (2), 92%

Try this

  • Choose Induction and tap the second cat (position 7). It looks at chased, the word that came after “cat” the first time, and “chased” is exactly what comes next.
  • Still on Induction, tap the last the (position 9). Its attention splits between cat and dog, the two different words that followed earlier copies of “the”.
  • Choose Same word, tap the first the, and switch the causal mask off. Now it can see later copies of itself. In a model that generates text, those later tokens would not exist yet.
  • Choose First token and compare the mini maps. Every row piles onto position 0.

What these patterns are, and where they were described:

  • Previous-token heads attend to the token just before the current one. They are simple, common, and useful as a building block for other heads.
  • Duplicate-token heads attend to earlier copies of the current token. Wang et al. (2022) found them in GPT-2 small as part of the circuit it uses to complete sentences like “When Mary and John went to the store, John gave a drink to ___”.
  • Induction headAn attention head that, at a token A, looks back for earlier copies of A and attends to the token that followed them, helping the model continue repeated patterns.Open in glossary complete patterns of the form A B … A → B. Elhage et al. (2021) and Olsson et al. (2022) described them and linked them to in-context learning, a model’s ability to pick up patterns from its prompt.
  • First-token heads put most of their weight on the first position whenever nothing else is relevant. Xiao et al. (2023) studied this as “attention sinks” and found it matters for running models on long streams of text. In trained models this is less a separate kind of head than a habit shared by most heads beyond the first couple of layers; the demo gives it a head of its own to make it visible.
  • Broad heads spread attention evenly, averaging over the context. Clark et al. (2019) observed broad heads in BERT’s lower layers.
How an induction head works with two layersOptional

A single head cannot implement the induction rule alone, because a key only describes its own token. The trick, described by Elhage et al. (2021), takes two layers:

  1. In an earlier layer, a previous-token head copies information about token j−1j - 1 into position jj‘s vector. Now each position “knows” what came before it.
  2. In a later layer, the induction head’s query at the current token AA looks for keys whose previous token was AA. That is the position holding BB, and its value carries BB forward to help predict the next token.

In the demo the rule is applied directly, so one head shows the end result. The causal mask also makes this efficient during generation: since earlier tokens never attend to later ones, their keys and values never change as new tokens arrive, so a model can compute them once and reuse them. That reuse is the KV cache, covered later in this track.

Key ideas

  • Multi-head attention runs hh attention heads in parallel, each in dk=dmodel/hd_k = d_{\text{model}}/h dimensions, then concatenates them and mixes with WOW^O.
  • Splitting into heads does not change the number of projection weights, 4 dmodel24\,d_{\text{model}}^2 per layer. It lets the model attend to several things at once.
  • The causal mask sets scores for future positions to −∞-\infty so their weights are exactly 0. It makes parallel training honest and matches left-to-right generation.
  • Trained heads show recurring patterns, including previous-token, duplicate-token, induction, and first-token heads, but many heads are messier than these examples.

Check yourself

Pick an answer to see why it is right or wrong. Nothing is graded. Your first answer is saved in this browser so the question can come back for review.

1A model has d_model = 768 and 12 attention heads. What is the width of each head's queries and keys?
2Why does a GPT-style language model need a causal mask during training?
3In a sequence that contains "... A B ... A", what does an induction head do at the second A?

Progress is saved in this browser only.

Up nextWhere words are
Next
Transformers and LLMs
  1. 1Predicting the next token
  2. 2Attention, step by step
  3. 3Many heads and the causal mask
  4. 4Where words are
  5. 5The transformer block
  6. 6Inside a real language model
  7. 7How LLMs are trained
  8. 8Generating fast: the KV cache

Try "embedding", "softmax", "overfitting", or "backpropagation".