Walk through how a token's d_model embedding is split into per-head pieces inside multi-head attention.
After the Q, K, V projections produce d_model-wide vectors, a reshape + transpose splits each vector into (num_heads, d_head), a tensor view, not a copy.
Picture a long candy bar. You can cut it into 8 equal pieces or call it one bar with 8 labeled sections; the candy is the same either way. Multi-head attention does the second thing. The token vector is one long buffer in memory, and the model just relabels contiguous chunks of it as 'this part is head 1, this part is head 2', and so on. Nothing is copied or duplicated. Each head then does its own small attention computation on its slice, and the model concatenates the slices back together at the end.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Trace the shape transformations from x through QKV projection, reshape, transpose, per-head attention, concat, and W_O. Highlight that the split is a view, name the GQA/MQA variations, and end with why per-head softmax is the real reason multi-head wins.
Real products, models, and research that use this idea.
- PyTorch's `nn.MultiheadAttention` uses exactly this view and transpose pattern internally.
- Llama 4 Maverick uses grouped-query attention: Q stays full multi-head while K and V share groups, but the reshape mechanism is identical.
- FlashAttention-3 expects the (batch, num_heads, seq, d_head) layout after reshape; the kernel's block streaming depends on it.
- Mistral 7B and Llama 4 typically use d_model = 4096 and num_heads = 32, giving d_head = 128,the most common modern configuration.
What an interviewer would ask next. Try answering before peeking at the approach.
QIf multi-head attention is mathematically equivalent to one big head with the same total dimension, why use multiple heads at all?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Believing each head has its own private d_model-wide projection. The projections produce one d_model vector that is then sliced into per-head pieces.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.