Multi-Head Attention
Many readers, one book
Multi-head attention runs several attention mechanisms in parallel with different projections, letting the model track pronouns, syntax and semantics at once — the reason a single softmax can't do everything.
What you will learn
- Explain the head-split and merge
- Appreciate head specialization
- Tie head count to capacity and cost
What is this?
Multi-head attention splits the model dimension into h parallel heads, each with its own Q/K/V projections. Each head runs self-attention independently; outputs concatenate and project back. Different heads learn different patterns — syntactic, relational, positional — giving one layer many 'reading strategies' instead of one average.
Why do you need it?
- A single attention bag averages patterns; heads can specialize while sharing capacity.
- Heads are cheap extra capacity: the compute of h heads equals one big head, but with more general shapes.
- Head ablation studies show some heads are near-irreplaceable (core expertise).
- Increasing head count is a standard path to better representations.
Mental model
A relay team of readers
Multi-head attention
One reader catches pronouns, another notices dates, a third tracks who's doing what. They all read the same book (the sequence) and each contributes their finding; the note-taking merge produces the final understanding.
How it works
- Input dimension d splits into h heads of d/h each via projections.
- Each head computes its own attention output independently.
- All head outputs concatenate back to d.
- A final linear projection mixes across heads.
- Models expose ~12..128 heads by size; capacity grows with h, cost grows linearly.
Split, attend, merge.
In production
- It's the default everywhere: all decoder blocks use multi-head attention.
- Head pruning/ablation tweaks model quality/speed without retraining everything.
- KV cache amortization naturally splits per head for serving speedups.
Best practices
- Prefer framework defaults for head count; tune the model dimension, not h in isolation.
- Expect heads to specialize; explore with visualizations when debugging behavior.
- Watch memory: KV cache is per-head, so head count directly affects cache size.
Common mistakes
- Hand-splitting dimensions wrong (d/h not integer) breaks shapes.
- Assuming every head needs identical behavior — specialization is the point.
- Forgetting head dimension when budgeting KV cache memory.
Common issues
Shape error after concatHead dimension mismatch
Verify d % h == 0 and correct split sizes.
Memory growth with head countKV cache scales per head
Budget cache in d·layers·tokens; raise heads only with actual benefit.
Debugging tips
- Odd head output → check d % h and projection shapes.
- Inspect single-head attention matrices to spot collapsed leaders.
- Memory blow-up with many heads + long context → cache size formula h·d/h... equals d — still watch count.
Interview checkpoint
Quick quiz
What gets concatenated at the end of multi-head attention?
Heads train to be identical redundant copies.