10. Multi-head attention — parallel crews with different habits¶
One spotlight beam cannot watch every clue well. So we split the job.
Built on the ELI5 in
00-eli5.md. The spotlight beam — one token consulting others — now becomes several parallel beams that notice different patterns.
Mental model — one beam is overloaded¶
One token may need many kinds of context. Syntax. Entities. Negation. Long-range references. Timing words. One head has one scorecard. So it must compress many questions into one pattern. That is too much pressure. See the sentence:
For tokenit, one clue asks:
Another clue asks:
A third clue asks:
So what to do? Do not force one crew to do all jobs.
Use multiple heads. Each head gets its own Q, K, V projections.
Each head learns its own habit.
same tokens
|
+--> head 1 : maybe tracks entity links
+--> head 2 : maybe tracks attribute links
+--> head 3 : maybe tracks syntax
+--> head 4 : maybe tracks negation
Why simple rescues fail¶
Failed rescue 1 — make one head wider¶
A wider head has more dimensions. Good. But it still produces one scorecard. One attention pattern cannot cleanly separate every job.
Failed rescue 2 — add more layers only¶
More layers help. But each layer still begins with overloaded heads. If the first routing is muddy, later layers inherit the mess.
Failed rescue 3 — hope the FFN fixes it later¶
The FFN mixes features per token. It does not create new cross-token routing. If attention missed the clue, the FFN cannot invent it. So yes, those tricks help a bit. They do not solve the routing bottleneck.
Picture first — parallel crews over the same input¶
All heads read the same residual stream. But they project it into different subspaces.
input X
|
+--> W_Q^(1), W_K^(1), W_V^(1) -> head 1 output
+--> W_Q^(2), W_K^(2), W_V^(2) -> head 2 output
+--> W_Q^(3), W_K^(3), W_V^(3) -> head 3 output
+--> W_Q^(4), W_K^(4), W_V^(4) -> head 4 output
|
v
concatenate -> W_O -> mixed output
Formula — split, attend, concatenate, mix¶
Let d_model = 8. Let number of heads h = 2.
Then each head gets:
i:
Then combine heads:
So the model does two things. First, it lets each head build its own scorecard.
Second, it lets W_O mix the head outputs back together.
Worked setup — same token, two different views¶
Use a toy context around it.
it. The input vector for it has 8 dimensions.
Both heads receive the same x_it. But they use different projection matrices.
So the resulting query vectors differ.
That is why the same token can ask different questions at once.
Worked numerical example — head 1 tracks the likely referent¶
Suppose head 1 builds this scorecard for it.
v_animal = [0.8, 0.1, 0.0, 0.2]
v_road = [0.1, 0.7, 0.1, 0.0]
v_because = [0.2, 0.2, 0.6, 0.1]
v_was = [0.1, 0.1, 0.2, 0.8]
v_tired = [0.3, 0.6, 0.1, 0.2]
o1 = 0.70*v_animal + 0.05*v_road + 0.10*v_because + 0.05*v_was + 0.10*v_tired
≈ [0.61, 0.20, 0.09, 0.21]
Worked numerical example — head 2 tracks the attribute clue¶
Suppose head 2 builds a different scorecard.
ASCII picture: Using the same value vectors, the weighted sum becomes:o2 = 0.20*v_animal + 0.55*v_tired + 0.05*v_road + 0.15*v_was + 0.05*v_because
≈ [0.33, 0.41, 0.08, 0.29]
Concatenate outputs, then apply W_O¶
Each head returns a 4-d vector. Concatenate them.
For a toy example, letW_O be the identity matrix. Then:
In a real model, W_O mixes the heads. But even this toy case shows the key idea.
One output now carries both clues.
What multi-head buys you¶
It does not mean each head gets a human job title. Heads are learned features. They can overlap. They can be messy. Still, the architecture gives the model room to specialize. That is the main win. One beam tries to be a generalist. Many beams can divide the labour.
Where this lives in the wild¶
- OpenAI GPT-4 class models use many attention heads so different patterns can coexist in one layer.
- GitHub Copilot relies on multi-head attention to track syntax, names, and distant code references together.
- Google Gemini uses multi-head style routing so prompts can mix entities, tools, and instructions.
- Anthropic Claude long documents benefit when some heads track local phrasing and others track far links.
- Meta Llama architecture reports head counts explicitly because serving cost and model quality depend on them.
Interview Q&A¶
Q: Why is one attention head often not enough?
A: Because one scorecard must compress syntax, entities, negation, and long-range links into one pattern.
Common wrong answer to avoid: "Just make the head wider." Width adds capacity, not independent routing patterns.
Q: What changes across heads?
A: Each head has its own W_Q, W_K, and W_V, so the same token is viewed in different subspaces.
Q: What does W_O do after concatenation?
A: It mixes the head outputs back into the model dimension.
Common wrong answer to avoid: "W_O chooses the best head and drops the rest." No. It learns a linear mix.
Q: Does every head learn a neat interpretable role?
A: No. Some heads are interpretable. Many are mixed and distributed.
Apply now (5 min)¶
Take the sentence The horse crossed the road because it was tired. Invent two heads for token it.
Make head 1 mostly attend to the referent. Make head 2 mostly attend to the attribute clue.
Write one 4-d value vector per important token. Compute two weighted sums.
Then concatenate them. Sketch from memory:
Draw the forked diagram input -> head 1/head 2 -> concat -> W_O.
Bridge. Now we stop isolating parts and walk the entire chain end to end in
11-full-pipeline.md.