Part B · Chapter 11 · Attention

11. Multi-head attention

One attention gives each word one blend of the others. Multi-head attention runs several side by side, each with its own matrices and its own idea of what matters, and joins their results.

Four heads, side by side
Sentence
Encoder layer
The word that asks (the query)
Layer 2, head 1 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna90 %has40 %23 %a53 %44 %small52 %39 %dog96 %.14 %13 %34 %12 %She91 %likes31 %39 %10 %it13 %37 %33 %.15 %16 %30 %13 %</s>12 %22 %17 %15 %10 %

From “it” it looks most at “.” (37.4 %).

Layer 2, head 2 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna42 %42 %has32 %17 %20 %a35 %13 %10 %13 %small54 %15 %dog14 %11 %41 %.17 %28 %She12 %13 %30 %34 %likes24 %27 %30 %it16 %18 %19 %.17 %30 %</s>15 %12 %18 %13 %

From “it” it looks most at “</s>” (18.6 %).

Layer 2, head 3 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna85 %has35 %13 %a99 %small21 %48 %dog15 %15 %19 %13 %15 %.14 %22 %17 %12 %She11 %43 %likes17 %25 %18 %it88 %.16 %16 %15 %12 %</s>13 %14 %10 %15 %10 %21 %

From “it” it looks most at “dog” (87.9 %).

Layer 2, head 4 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna21 %11 %11 %12 %has18 %15 %15 %37 %a91 %small58 %31 %dog32 %35 %17 %.15 %24 %She16 %10 %18 %22 %15 %10 %likes18 %18 %24 %15 %it25 %14 %34 %.17 %24 %</s>37 %13 %11 %13 %

From “it” it looks most at “dog” (34.3 %).

Playback speed

The layer's attention output for “it”, first 8 of 64 numbers
Token12345678… 56 more
All heads on0.450.120.62−0.710.15−0.171.11−0.06…
As switched now0.450.120.62−0.710.15−0.171.11−0.06…

Every head is on: switch one off to see how much the output changes.

The four heads of one encoder layer of the paper's model as heat maps on the chosen sentence: each row is a word asking, each column a word it looks at, and each cell is as dark as the weight. Under each map, the word the chosen word looks at most. The clip below joins the four heads' outputs for that word end to end and multiplies them by W^O. Switch a head off and its part becomes zeros: the table shows the layer's output with every head and without the ones switched off, and how much it changed.

The idea

Charles's post: “Rather than applying attention once, multi-head attention performs the attention operation multiple times in parallel, each with its own learned parameters.” Three moves:

  • Split: each word's vector is projected into several smaller queries, keys and values, one set per head.
  • Attend: each head runs scaled dot-product attention (chapter 9) with its own matrices.
  • Join and transform: the heads' outputs are concatenated and passed through one more matrix, W^O.

“Each head can capture different relationships: one might focus on word order, another on syntax, and another on long-range context.” In the figure, for “it” in the second layer, one head looks back at the noun almost entirely, while the others spread their weight over the full stops, the noun phrase and the rest. A single head would have to squeeze all of that into one set of weights.

This is the layer that both GPT-style models and encoder models such as BERT stack, many times over (chapter 23 explains the difference between the two).

A worked example: four heads on “it”

In “Anna has a small dog . She likes it .”, layer 2 of the encoder: from “it”, head 1 looks most at “.”, head 2 at “</s>”, head 3 at “dog” and head 4 at “dog”. Each head returns 16 numbers; joined, they make 64, and W^O, a 64 × 64 matrix, turns them into the layer's output for “it”.

Switch heads off one at a time and the output for “it” changes by 42 % without head 1, 7 % without head 2, 66 % without head 3 and 24 % without head 4. Some heads matter far more than others for this word; none is the whole story.

The formula

\operatorname{MultiHead}(Q, K, V) = \operatorname{Concat}(\text{head}_1, \dots, \text{head}_h)\, W^O, \qquad \text{head}_i = \operatorname{Attention}(QW_i^Q,\ KW_i^K,\ VW_i^V)
\(h\)
the number of heads: 8 in the paper, 4 in our model
\(W_i^Q,\ W_i^K,\ W_i^V\)
head i's own projections, d_model by d_k (chapter 8)
\(\operatorname{Attention}\)
scaled dot-product attention (chapter 9)
\(\operatorname{Concat}\)
the heads' outputs side by side: h × d_v numbers per word
\(W^O\)
the output matrix, h·d_v by d_model: mixes the heads back into one vector per word

The paper takes \(d_k = d_v = d_{\text{model}}/h = 64\), so eight heads of 64 numbers join back into 512. Ours: four heads of 16 make 64.

Why it matters

Splitting into heads costs almost nothing extra: each head works on a slice of the vector, so h heads of size d_model/h do about the work of one full-size head, as the paper notes. You get several views of the sentence for the price of one. That is why the transformers behind today's APIs are built on it.

Where it fails

  • Heads are not guaranteed to mean anything. Some of ours can be named (the head gallery does it); others spread their weight and resist a label. Nothing in training asks a head to be readable.
  • Some heads hardly matter for a given word. Switching one off changes the output a little or a lot; the model has redundancy built in.
  • More is not always better. The paper found one head worse and too many heads worse too (Table 3, rows A), and our reruns of row A (the Table 3 tool) show how the number of heads changes training on our model.
  • The switch changes one layer only. The figure recomputes this layer's output without a head; the layers after it are not rerun. Chapter 24 removes a head from a whole translation.
Remember Multi-head attention runs h attentions side by side, each with its own W^Q, W^K and W^V on a slice of the vector, then joins their outputs and mixes them with W^O: several views of the sentence for the cost of one.

Exercises

Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.

Which head produced it?

In the figure (the first sentence, layer 2, the word “it”), one map puts most of the weight from “it” on “dog”. Which head is it?

How long is the concatenation?

Our model has 4 heads, each with d_v = 16. How many numbers does the concatenation hold for one word?

Your answer
A whole number

The paper's concatenation

The paper uses h = 8 heads with d_v = 64. How many numbers does its concatenation hold per word?

Your answer
A whole number

Which head matters most?

For “it” in layer 2 of the first sentence, switching which head off changes the output the most? Try the switches.

In the paper

Section 3.2.2 introduces multi-head attention: it “allows the model to jointly attend to information from different representation subspaces at different positions”, with h = 8 and \(d_k = d_v = d_{\text{model}}/h = 64\), and notes that “due to the reduced dimension of each head, the total computational cost is similar to that of single-head attention with full dimensionality”. Figure 2 (right) draws it: h scaled dot-product attentions side by side, Concat, Linear.

In the paper: section 3.2.2

Open the Theory tab
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.