Pick the formula that defines head_dim from hidden_size and num_heads.
head_dim = hidden_size / num_heads, integer divide. hidden_size must be divisible by num_heads or the per-head reshape errors out.
Imagine a 12-pack of soda you want to split evenly into smaller packs. If you want 4 packs, each gets 3 cans. If you want 6 packs, each gets 2. The arithmetic is just: 12 / number of packs = cans per pack. Multi-head attention does the same with a flat hidden vector: split the hidden_size into num_heads chunks, each chunk has head_dim entries, and the math is head_dim = hidden_size / num_heads. If your split does not divide evenly, the soda example breaks the same way the model does: leftover cans, errored reshape.
Detailed answer & concept explanation~8 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.
60s: state the formula head_dim = hidden_size / num_heads, note the divisibility requirement, explain that the multi-head reshape enforces it, give worked examples (GPT-2 small: 768/12 = 64, Llama-2 7B: 4096/32 = 128), mention parameter-count preservation as the original Transformer's motivation.
Real products, models, and research that use this idea.
- GPT-2 small: hidden_size = 768, num_heads = 12, head_dim = 64.
- BERT base: hidden_size = 768, num_heads = 12, head_dim = 64 (same as GPT-2 small).
- Llama-2 7B and Mistral 7B: hidden_size = 4096, num_heads = 32, head_dim = 128.
- Llama-2 70B and Llama 4 Maverick continue with head_dim = 128 across configurations.
- DeepSeek V4 MLA decouples per-head dim from the standard ratio via explicit projection fields in its config.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhat happens at runtime if you try to build a model with hidden_size = 800 and num_heads = 12?
QWhy does the original Transformer pick parameter count to preserve under num_heads variation?
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.
Treating head_dim as a free hyperparameter independent of hidden_size and num_heads. It is locked: head_dim = hidden_size / num_heads, with hidden_size % num_heads == 0 required.
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.