Scaled Dot-Product Attention and the √d Factor
Transformer attention scores are dot products, and dot products between high-dimensional vectors naturally grow larger just from having more dimensions to add up — dividing by √d cancels that growth so softmax doesn't collapse.
Prerequisites: Queries, Keys and Values, Bahdanau and Luong Attention
Transformer attention scores how relevant one token is to another using a dot product between a query vector and a key vector. That works well in low dimensions, but as the vectors get wider — modern models use dimensions in the hundreds — the raw dot products start growing large simply as a side effect of having more terms to sum, not because the tokens have become more related. Large scores fed into a softmax push it toward an almost one-hot output, and the training signal (gradients) through a nearly one-hot softmax collapses to almost nothing. The √d scaling factor is a small correction that exists to stop this from happening.
The analogy: averaging opinions from a growing committee
Imagine measuring how much two people agree by adding up how similarly they answer yes/no questions, scoring +1 for agreement and -1 for disagreement, then summing. With 4 questions, the max possible score is 4. With 400 questions, the max is 400 — not because people agree "400 times more," purely because more terms are summed. Feeding that raw sum into a rule like "higher score means exponentially more attention" would make a 400-question committee look wildly, artificially more extreme than a 4-question one, purely from arithmetic. The fix is obvious: normalize by the number of questions. Scaled dot-product attention does the equivalent normalization for dot products over dimensions.
The mechanics: why dot products scale with √d, and the fix
For a query and key , each with independent components roughly of unit variance, the dot product is a sum of terms:
If each term has mean 0 and variance roughly 1 (a standard modeling assumption for randomly-initialized components), then the variance of the sum of roughly-independent terms is times the variance of one term, so the standard deviation of the dot product grows like . In words: the typical size of the raw dot product grows with the square root of the dimension, purely from summing more terms — the same arithmetic effect as the committee example, just phrased in the language of variance. Scaled dot-product attention divides the raw score by exactly before applying softmax:
In words: compute every query-key dot product, shrink each one by dividing by the square root of the key dimension to undo the dimension-driven growth, turn the resulting scores into weights with softmax, then use those weights to blend the value vectors — the same score-softmax-blend pattern as Bahdanau and Luong Attention, with one extra division inserted specifically to keep the scores in a numerically healthy range regardless of .
Worked example 1: comparing softmax with and without scaling
Say two of three raw dot-product scores are , with , so . Unscaled softmax: , , , weights — essentially all the weight on one position, with the other two contributing almost nothing and receiving almost no gradient during training. Scaled by : scores become . Softmax: , , , weights — a much softer, more gradient-friendly distribution that still favors position 1 but doesn't crush the others to zero.
Worked example 2: how the effect grows with dimension
Suppose typical dot product magnitude scales as . At , a "typical large" score might be around . At (a wider model), the same relative signal produces a score around — nearly 3x larger, purely from the dimension increasing 8x. Dividing by in each case brings both back down to the same "typical large" scaled score of about 3, restoring comparable softmax behavior regardless of model width.
Raw dot-product magnitude grows like a square-root curve as dimension increases — dividing by is precisely the correction that flattens that growth back to a constant scale, whatever happens to be.
What this means in practice
Every standard transformer, from the original architecture through today's large language models, applies this exact division inside every attention head — it's not an optional tuning knob, it's load-bearing for training stability at the widths modern models use. Getting it wrong (or omitting it) when implementing attention from scratch is a classic silent bug: training doesn't crash, it just trains badly, with gradients vanishing through a saturated softmax in a way that's easy to misdiagnose as a learning-rate or initialization problem.
Raw dot-product attention scores grow in typical magnitude with purely as an artifact of summing more dimensions, which pushes softmax toward a near one-hot output and starves training of gradient signal. Dividing scores by before softmax cancels that dimension-driven growth and keeps attention weights, and their gradients, well-behaved regardless of model width.
Practice
- For raw scores and (so ), compute the softmax weights with and without scaling and compare how peaked each is.
- Explain in one sentence why the scaling factor is specifically, rather than or a constant.
- Why would omitting √d scaling be a "silent" bug rather than one that immediately crashes training?
The common confusion is thinking √d scaling controls the magnitude of the values being attended over. It has nothing to do with — it only rescales query-key dot products before softmax, to counteract dimension-driven score growth. Confusing it with general stability tricks like gradient clipping misses that this correction targets one specific fact: dot products over more dimensions are typically larger in magnitude, for reasons unrelated to how related two tokens actually are.
Related concepts
Practice in interviews
Further reading
- Vaswani et al., Attention Is All You Need (2017)