Raghuram Shankar
← Language Modelling

Part 4 · The Maths II: Inside the Transformer

Why the 1/√dk is not cosmetic

The scaling factor is the single most-copied and least-explained line in the architecture. It has a short derivation, and the derivation predicts exactly what the widget below shows.

Faded derivation: the temperature of a dot product
1Model q and k as independent vectors with zero-mean, unit-variance components. What is E[q·k]?
𝔼[q·k] = ∑i 𝔼[qi]𝔼[ki] = 0

Independence lets the expectation factor; each factor is zero.

2Now the variance. This is the step that matters.
Var(q·k) = ∑i Var(qiki) = ∑i 1 = dk

Variances of independent terms add. So the logits have standard deviation √dk — they grow as the head gets wider.

3What does a softmax do to inputs whose spread keeps growing?

It saturates. Softmax is scale-sensitive: multiply all logits by c > 1 and the distribution sharpens; as c → ∞ it becomes one-hot. With spread √dk, a dk = 128 head has logits several times larger than a dk = 8 head, purely from dimensionality.

4Why is saturation fatal for training specifically?
∂softmaxi/∂zj = pi(δij − pj)

p the softmax output · z its input logits · δij the Kronecker delta, 1 when i = j and 0 otherwise

When p is one-hot, every entry of that Jacobian is ~0: pi ≈ 0 kills most terms and pi ≈ 1 gives p(1−p) ≈ 0. No gradient flows back through a saturated softmax, so the attention pattern cannot be learned.

5Choose the divisor that makes the logit variance independent of width.
Var( q·k√dk ) = dkdk = 1

Dividing by √dk holds the entering variance at 1 for every head width, so the softmax operates in the same regime whether the head is 8 or 128 wide. This is exactly the justification given in the original paper.

Step 2 is the one to remember: variances add, so dot products grow like √d. The same argument governs initialization schemes and is worth having in reflex memory.

This is not a reconstruction after the fact. The original paper gives the same argument in a footnote, suspecting that “for large values of dk, the dot products grow large in magnitude, pushing the softmax function into regions where it has extremely small gradients” — steps 2 through 4 above, in one sentence.

Real attention over 12 positions with seeded random queries and keys; darker means more weight. Turn the scaling off and drag dk: the causal rows collapse onto a single bright cell and the entropy falls toward zero — the saturation predicted in step 3.