In this article
Before you start
This page is one step of the transformer architecture learning path.
Why interviewers ask this
Without the scaling term, attention trains badly once the key dimension is large, so this question separates a candidate who knows the mechanism from one who has memorized the formula. A weak answer says "it normalizes the scores" and cannot say what is being normalized. A strong answer names the three links in the chain: the dot-product variance grows with d_k, a large score saturates the softmax, and a saturated softmax passes a very small gradient. It also says why the factor is a square root.
A Staff-level answer adds the caveats. The variance argument assumes idealized components, and the original authors only say they suspect the effect. The scaling is a fixed constant with no learned parameters. It can be read as a fixed softmax temperature. Interviewers often follow with "what if you divided by d_k instead?", which the steps below answer with numbers.
Attention scaling from first principles
- Attention scaling factor
- The attention scaling factor is a fixed constant, one divided by the square root of the key dimension d_k, that multiplies every query-key dot product before the softmax in scaled dot-product attention. It adds no learned parameters, and its job is to keep the spread of the scores the same whatever the length of the query and key vectors.
- Softmax saturation
- Softmax saturation is the state in which one input to a softmax is so much larger than the others that the output is almost one-hot, with nearly all the weight on one entry and nearly none on the rest. In that state the softmax's gradient is close to zero, so the layers before it receive almost no learning signal.
In plain words
Picture a walker who takes one random step per vector component. After many steps the walker is typically far from the start, and more steps mean a bigger typical distance. A dot product is like that walk: each component adds one random push. The analogy breaks in one place. The pushes here are products of two numbers, not coin flips, but the same law decides how far the total drifts.
Now picture the softmax as a contrast knob. When scores are close, the weights are shared out. When one score towers over the rest, that entry takes everything.
In one precise sentence, scaling divides each score by the standard deviation the dot product would have for random inputs, so the softmax always sees scores of a similar size.
Step-by-step description
- The dot product has variance d_k.
- One key takes nearly all the weight.
- The softmax slope is nearly zero.
- Dividing by √d_k restores variance one.
- The dot product of two random vectors has variance d_k.
- Large scores saturate the softmax, leaving extremely small gradients.
- Dividing by √d_k returns the variance to one.
Step 1: Dot products grow with the key dimension
A query-key dot product is a sum of products, so its spread grows with . Variance is the average squared distance of a value from its mean, and the standard deviation is the square root of the variance. Vaswani et al. (2017) assume the components of q and k are independent random variables with mean zero and variance one. Then the dot product has mean zero and variance .
The reason is short. Each product of two independent unit-variance numbers has mean zero and variance one. Variances of independent terms add, so terms give variance :
The standard deviation is therefore , which is the square root the interviewer asks about. It is eight at a d_k of 64 and 32 at a d_k of 1,024.1 In twenty thousand seeded random trials per size, the measured variance stayed within five percent of d_k at every size from 4 to 1,024.1
You now know the cause: longer vectors produce scores with a wider spread.
Text description of the diagram
- The standard deviation of an unscaled score is the square root of d_k.
- It is eight at a d_k of 64 and 32 at a d_k of 1,024.
Step 2: Large scores saturate the softmax
A softmax saturates when its largest score exceeds the others by a wide margin, because the softmax exponentiates its inputs. Take one row of four scores, eight, zero, minus eight and four. This row is illustrative: it spreads about one standard deviation each way for a d_k of 64. A gap of eight multiplies a key's weight by about 2,981.1
The softmax weights of that row are about 0.98 on the first key, 0.018 on the last and under 0.0004 on the other two.1 The row is close to one-hot, so the model attends to a single token whatever the other keys contain.
Vaswani et al. (2017) describe the effect this way: they suspect large dot products push "the softmax function into regions where it has extremely small gradients". Large scores saturate the softmax; the next step shows what that does to learning.
Step 3: A saturated softmax passes almost no gradient
A saturated softmax passes almost no gradient because the softmax's slope shrinks as its output approaches one-hot. The Jacobian of the softmax is the table of partial derivatives that shows how each output weight changes when each input score changes. For weights p it equals the diagonal matrix of p minus the outer product of p with itself. Its diagonal entries are p times one minus p.
For a weight near one or near zero, that product is near zero. In the saturated row of Step 2, the largest entry of the Jacobian is about 0.018.1 After dividing the scores by √d_k, the largest entry is about 0.2493, roughly fourteen times larger.1
During training, the loss signal travels back through this Jacobian. If it is tiny, the scores for keys that deserve more weight barely move. Training slows or stalls even though the model is fine. Scaling attacks the cause, the score size, instead of the symptom.
Step 4: Dividing by √d_k restores a stable spread
Dividing each score by √d_k returns the variance of the dot product to one. The standard deviation √d_k divided by √d_k is one. The factor is a fixed constant, so it adds no learned parameters and almost no compute. In the paper's words, "we scale the dot products by" the reciprocal of √d_k. The full formula puts the factor inside the softmax:
Under the paper's independence assumption, dividing by instead overshoots. The variance becomes one over , and the weights go almost flat. The illustrative row shows it: for the example row the weights are about 0.28, 0.25, 0.22 and 0.26.1 With √d_k they are about 0.47, 0.17, 0.06 and 0.29: still peaked, but alive.1
Dot-product attention is worth keeping stable because the paper reports it is "much faster and more space-efficient in practice", since it uses optimized matrix multiplication. The same paper says additive attention "outperforms dot product attention without scaling for larger values of d_k". Scaling is the paper's remedy for that gap.
Text description of the diagram
- Without scaling, the first key takes about 0.98 of the attention weight.
- Dividing by d_k instead leaves the weights almost flat.
Knowing it is half the job. Saying it is the other half.
Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.
Start Free Trial →The Staff-level answer
Why does attention require scaling?
Attention scores are dot products of a query and a key, each with d_k components. If those components are roughly independent with zero mean and unit variance, the dot product has variance d_k, so its typical size grows like √d_k. Large scores saturate the softmax: nearly all the weight lands on one key, and the softmax gradient, p times one minus p, is close to zero, so training stalls. Dividing by √d_k returns the variance to one, with no parameters. The assumption is idealized, since trained queries and keys are neither independent nor unit variance, so I call it a well-motivated heuristic. The original paper itself says "we suspect".
What makes it Staff-level is the last two sentences: it names the assumption and the paper's own hedge instead of presenting a theorem. The spoken structure is cause, symptom, fix, caveat. The derivation from the formula side is in why attention scores are divided by sqrt(d_k), and scaled dot-product attention is the operation this factor belongs to.
The rubric
| Dimension | Weak answer | Strong answer | Staff answer |
|---|---|---|---|
| Correctness | "It normalizes the scores." | Dot-product variance grows with d_k, which saturates the softmax. | Adds that the factor is the standard deviation of the unscaled score. |
| Depth | States the formula only. | Explains the small gradient of a saturated softmax. | Shows the Jacobian entries shrinking as the output nears one-hot. |
| Trade-offs | None mentioned. | Says dividing by d_k flattens attention under the paper's assumptions. | Knows μP uses 1/d because trained q and k correlate; mentions query-key (QK) normalization. |
| Communication | Jumps between terms. | Orders cause, symptom, fix. | Gives a worked row and says what is illustrative. |
| Follow-up readiness | Stuck on "why square root?" | Answers it with variance of a sum. | Connects it to softmax temperature and additive attention. |
Three variants of this question
What happens if you do not scale the dot products in attention?
The scores have a spread of about √d_k, so for large d_k the softmax saturates, attention becomes nearly one-hot and gradients through it become extremely small. The model still runs but trains poorly, and the paper reports that unscaled dot-product attention falls behind additive attention at larger d_k.
Why is the scaling factor the square root of the key dimension?
The dot product is a sum of d_k terms with variance one each, so its variance is d_k and its standard deviation is √d_k. Dividing by the standard deviation restores unit spread, which is why the factor is a square root and not d_k.
What if the head dimension doubles?
The unscaled score spread multiplies by the square root of two, about 1.4142, so the scale must grow with it.1 A model that uses 1/√d_k adjusts automatically; a hard-coded constant from a smaller model would not.
Common mistakes
- "Scaling makes the softmax rows sum to one." (wrong) The softmax already does that. (right) "Scaling controls the size of the softmax's inputs so it does not saturate."
- "The square root depends on the sequence length." (wrong) The paper ties it to the key dimension, not the number of tokens. (right) "It comes from the standard deviation of a sum of d_k unit-variance products."
- "It is proven that unscaled attention fails." (wrong) The authors write that they suspect it. (right) "The evidence is the variance argument plus the comparison with additive attention."
- "Scaling fixes the quadratic cost of attention." (wrong) Cost comes from comparing every pair of tokens; see why attention is quadratic. (right) "Scaling affects training stability, not cost."
Follow-up questions
Is attention scaling the same as softmax temperature?
Mathematically, yes: temperature is a number that divides the scores before the softmax, and the scaling is a temperature fixed at √d_k. This is a way of reading the formula, not a claim the paper makes.
Does the scaling add parameters or cost?
No. It is one fixed constant applied to the scores, with no learned parameters and negligible compute. A faster kernel such as FlashAttention changes how attention is computed, not whether the scores are scaled.
Do trained models still need the scaling if queries and keys are not unit variance?
The standard formula keeps it, but the argument is only a starting point: after training, the components are neither independent nor unit variance, so the factor is a sensible default, not an exact normalizer. Maximal Update Parametrization (μP) scales attention logits by 1/d instead of 1/√d, because trained queries and keys become correlated and the dot product then grows like d (Yang et al., 2022). Query-key normalization (QK-norm) normalizes queries and keys before the dot product, because attention logits kept growing and destabilized training in very large vision transformers (Dehghani et al., 2023). That is why the honest wording is "heuristic".
Why not use additive attention and skip the scaling?
Additive attention passes its scores through a tanh, which bounds the hidden values, and the paper only reports it beating unscaled dot products at larger d_k. Dot-product attention is much faster and more space-efficient because it reduces to optimized matrix multiplication, and scaling is the paper's remedy for the gap.
Does sequence length change the scaling?
No: the standard formula does not depend on sequence length. The factor depends on the key dimension of each head, so multi-head attention uses the smaller per-head d_k. For the questions that follow on a transformer loop, see large language model (LLM) interview questions.
- Query-key dot products have variance d_k, so their spread is √d_k.
- Large scores saturate the softmax, and a saturated softmax passes almost no gradient.
- Dividing by √d_k returns the variance to one with no parameters and almost no cost.
- Dividing by d_k overshoots and flattens attention.
- The argument assumes idealized components; the paper says it only suspects the effect.
Sources
- Attention Is All You Need (arXiv 1706.03762) — arXiv (Vaswani et al.), 12 Jun 2017
- Attention Is All You Need (HTML, version 7) — arXiv (Vaswani et al.), 2 Aug 2023
- Tensor Programs V: Tuning Large Neural Networks via Zero-Shot Hyperparameter Transfer (arXiv 2203.03466) — arXiv (Yang et al.), 7 Mar 2022
- Scaling Vision Transformers to 22 Billion Parameters (arXiv 2302.05442) — arXiv (Dehghani et al.), 10 Feb 2023