LearnQuestion bank

Why are attention logits divided by the square root of the key dimension, and what happens to training if you drop it?

ResearchTechnicalEasy

Answer it yourself first, out loud or on paper, then compare.

Answer

Model answer, written in a candidate's voice

In short

  • Dot product variance grows with key dimension under i.i.d. zero-mean unit-variance components.
  • Dividing by sqrt(d_k) normalises logit variance to about 1.
  • Without it, logits get large, softmax saturates towards one-hot, and gradients shrink.
  • Training becomes slower/less stable and can lock into near-argmax attention.
  • Alternatives include QK-normalisation or a learned temperature.

In full

The sqrt(d_k) divisor is a variance-control choice: if q and k components are i.i.d. zero-mean unit-variance, q·k is a sum of d_k terms, so its variance is d_k and its typical magnitude is sqrt(d_k). Dividing by sqrt(d_k) keeps the logits at variance around 1, which keeps the softmax in a useful regime rather than extremely peaked. If you drop it, logits grow with d_k, the softmax saturates towards one-hot, and the softmax Jacobian p(1-p) becomes tiny, so gradients to Q and K shrink and training can become slow, unstable, or stuck in near-argmax attention. In practice the i.i.d. assumption is only approximate, but the same intuition holds. Alternatives include QK-normalisation, which constrains q and k before the dot product, or a learned temperature that lets the model choose the sharpness rather than fixing it at 1/sqrt(d_k).

Corrected by an independent AI reviewer.

What a strong answer covers

  • With i.i.d. zero-mean, unit-variance components, the dot product has variance dₖ.
  • Large logits saturate the softmax towards one-hot, which shrinks gradients.
  • Scaling keeps variance around 1.
  • Alternatives: QK-normalisation or a learned temperature.

Common mistakes

  • Calling it purely an overflow fix.
  • Dividing by dₖ instead of √dₖ.