Name the scalar that QK^T is divided by inside scaled dot-product attention.
sqrt(d_k), the per-head key dimension. Not sqrt(d_model) and not sqrt(num_heads). It keeps pre-softmax score variance near 1.
Picture a dart competition where each round you sum up scores from many dartboards at once. The more boards you add, the wider the range of total scores swings, just from random luck. To keep the leaderboard meaningful round to round, you divide the total by something that scales with how many boards you summed over, so a typical total stays around the same size. Attention does the same thing: the dot product between query and key is a sum over d_k terms, and dividing by the square root of d_k keeps the typical magnitude steady regardless of how big you make a single head.
Detailed answer & concept explanation~5 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.
State the divisor sqrt(d_k), derive it from the variance of a d_k-term dot product, contrast with wrong candidates (1, d_k, sqrt(d_model)), and explain why the scaling is invariant across head sizes.
| Candidate divisor | Effect on score variance | Softmax behavior |
|---|---|---|
| 1 (no scaling) | Variance grows as d_k | Saturates at large d_k, near one-hot, gradient vanishes off-peak |
| sqrt(d_k) | Variance ≈ 1 regardless of d_k | Stable across head sizes; the canonical choice |
| d_k | Variance ≈ 1/d_k | Over-corrects; softmax never sharpens, attention is near-uniform |
| sqrt(d_model) | Variance ≈ d_k / d_model (wrong scale) | Wrong magnitude per head; under-corrects for multi-head |
Real products, models, and research that use this idea.
- Vaswani et al. 2017, equation 1: explicitly motivates sqrt(d_k) via the variance argument in section 3.2.1.
- Llama 4 Maverick, Llama 3 70B: d_k = 128, divisor = sqrt(128) ≈ 11.3.
- Mistral 7B and Mixtral: d_k = 128 per attention head, same sqrt(128) scaling.
- Gemma 4 27B: head_dim = 256, so the divisor is sqrt(256) = 16.
- PyTorch's torch.nn.functional.scaled_dot_product_attention applies the sqrt(d_k) divisor automatically and dispatches to FlashAttention kernels under the hood.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is the scaling specifically sqrt(d_k) rather than d_k or 1/d_k?
QHow does FlashAttention apply the sqrt(d_k) scaling inside its blockwise softmax computation?
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.
Writing sqrt(d_model) instead of sqrt(d_k). The scaling is per-head, so d_k = hidden_size / num_heads is the right value.
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.