Medium5 min readUpdated 2026-08-12

Why do we scale the dot product attention by √dₖ in the Transformer architecture?

Scale the dot product attention by √dₖ in the Transformer architecture explains why Transformer attention divides the query key dot product by the square root of the key dimension $\sqrt{d_k}$. This keeps logits at a stable scale so softmax gradients behave and training is stable across different head sizes. Learn the variance argument, a worked numeric example, and practical implications for multi head design and initialization.

Hand-drawn card showing queries, keys, scaling factor, scaled dot product, and final attention weights
TL;DR
  • Scaling the dot product attention by dk\sqrt{d_k} keeps the logits from growing with the key dimension dkd_k, which stabilizes the softmax and its gradients.
  • Without scaling, larger dkd_k makes logits numerically large and softmax becomes extremely peaky, hurting learning.
  • Dividing by dk\sqrt{d_k} normalizes the variance of the dot product so heads behave consistently across sizes. Key tradeoffs: smaller logits improve gradient flow and stability but change the effective temperature of attention; choose head dimension and scaling together.

In this question, we will learn why the Transformer architecture divides the raw query key dot product by dk\sqrt{d_k} before applying softmax. We will show the statistical reason, a concrete numeric example, and how this ties into multi head design and initialization.

We will cover the following:

  • The intuition
  • How it actually works (variance argument plus a worked example)
  • Relation to multi head attention and initialization
  • Tradeoffs and failure modes
  • Questions the interviewer might ask

Direct answer: We scale by dk\sqrt{d_k} to keep the dot-product logits at a consistent variance so the softmax is not driven into saturation and gradients remain well behaved. This normalization makes attention heads stable across different dkd_k and simplifies optimization.

The intuition (an analogy that makes it click)

Imagine you are comparing similarity scores from longer and shorter lists. If longer lists produce sums of many small contributions, their totals will typically be larger just because there are more terms. The dot product of two dkd_k dimensional vectors is the sum of dkd_k componentwise products. As dkd_k grows the raw sum tends to grow in magnitude, so we rescale by dk\sqrt{d_k} to restore a fair comparison. That makes the softmax see scores on a similar scale whether you have small or large head dimensions.

How it actually works (the real mechanics, with one concrete worked example)

Assume each component of query and key vectors is independent with mean zero and variance σ2\sigma^2. The dot product qkq\cdot k is a sum of dkd_k such products. For zero mean and independent components the expected mean is zero and the variance grows roughly linearly with dkd_k.

A compact expression when components have variance σ2\sigma^2 is:

Var(qk)=dkσ2.\mathrm{Var}(q\cdot k) = d_k \sigma^2.

If we divide the dot product by dk\sqrt{d_k} the variance becomes σ2\sigma^2, independent of dkd_k. That keeps the distribution of logits stable as we change head size.

Worked numeric example. Suppose a head produces raw dot products (logits) for three keys as [16, 8, 4]. Those are already somewhat large relative to 1, so softmax will be very peaky. Now scale by dk\sqrt{d_k}, assume dk=8\sqrt{d_k}=8, to get [2, 1, 0.5]. Compare their softmax probabilities.

scoresoftmax prob (unscaled)score/\sqrt{d_k}softmax prob (scaled)
160.9996620.628
80.0003310.231
40.000010.50.140

The unscaled logits produce an almost one-hot distribution that kills gradient flow to the lower-scoring keys. After scaling we get a smoother distribution that allows learning to adjust weights for multiple keys.

Relation to multi head attention and initialization

Multi head attention splits the model dimension into multiple heads, each with dimension dkd_k. If we did not scale, heads with larger dkd_k would produce logits with larger variance. Scaling by dk\sqrt{d_k} makes head outputs comparable and avoids having some heads be always dominant because of dimension alone.

Scaling also interacts with parameter initialization. Standard initializations aim to keep activations and gradients roughly constant with depth. The dk\sqrt{d_k} factor is consistent with that goal: it normalizes the attention logits so the softmax output variance does not blow up as dkd_k changes.

Tradeoffs and failure modes

Scaling by dk\sqrt{d_k} is simple and effective, but it implicitly sets an attention temperature. Smaller effective temperature leads to sharper attention; larger temperature gives smoother attention. If you change dkd_k or add explicit temperature parameters you must be careful to maintain the intended sharpness of attention.

If you omit scaling for large dkd_k the softmax will saturate. That makes gradients tiny for all but the maximal logit, which can stall learning and produce unstable or poor models. Conversely, aggressive over-scaling can make attention too uniform and underfit local associations.

Questions the interviewer might ask

Some follow-up questions you might get:

Why assume zero mean and independence for components? Those assumptions are a simplifying model that captures the main effect: sums of many small independent terms have variance that grows with count. Real activations are not independent, but the scaling still empirically helps.

Could we learn the temperature instead of fixing dk\sqrt{d_k}? Yes, some variants learn a scalar temperature or add a learned scale per head. That adds flexibility but also more parameters and potential instability without careful regularization.

Why use dk\sqrt{d_k} not dkd_k or another factor? The dot product variance grows linearly in dkd_k, so the standard deviation grows with dk\sqrt{d_k}. Dividing by dkd_k would shrink too fast; dividing by dk\sqrt{d_k} normalizes variance to a constant.

Does layer normalization change the need for scaling? Layer normalization helps stabilize activations but does not remove the variance growth of the dot product between independent vectors. Scaling remains useful even with normalization layers.

How does scaling affect attention heads when d_k is small? When dkd_k is small scaling is less dramatic, but the factor still keeps behavior consistent across designs. With very small dkd_k the division can increase noise sensitivity, so design choices should balance head count and head size.

Some things to note:

  • The dk\sqrt{d_k} factor is a variance normalization choice, not a magic constant. It follows from basic statistics of sums.
  • Empirically this scaling improves training stability across many Transformer models.

What the interviewer is really testing

They want to know you understand why raw dot products change scale with vector dimension and how that affects the softmax and gradients. They also want to see you can connect a statistical argument to practical architectural choices like multi head design and initialization. A concise variance argument plus a numerical example shows you grasp both theory and practice.

Related questions

#transformer#attention-scaling#llm#deep-learning

How would you rate the quality of this article?

Prepare for your AI engineering interview

This is one of many detailed questions and explainers on StudyAIDesign. Browse the full set, work through the curriculum, and walk into your interview ready.

Follow along for new questions and explainers:Instagram