NotesByLex

Scaled Dot-Product Attention

The specific self-attention formulation from the Transformer paper, distinguished by scaling scores by the square root of the attention dimension.

Scaled Dot-Product Attention is the specific formulation of Self-Attention introduced in Attention Is All You Need (Vaswani et al., 2023). It is used in the Transformer architecture and in most subsequent large language models.

The mechanism is identical to standard dot-product self-attention, with one addition: scores are divided by the square root of the attention dimension before the softmax (dk\color{red}{\sqrt{d_k}}).

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\color{red}{\sqrt{d_k}}}\right)V

Why scale?

As the attention dimension dkd_k grows, the dot products between query and key vectors tend to grow in magnitude: there are more terms being summed. Large values fed into the softmax push it into regions with very small gradients, making training slow and unstable.

Dividing by dk\sqrt{d_k} keeps the scores in a stable range without changing their relative ordering, since all scores are scaled by the same factor (Vaswani et al., 2023).

scores = query @ key.transpose(2, 1)
scores = scores / math.sqrt(attention_dim)  # the "scaled" part
scores = softmax(scores, dim=-1)
out = scores @ value

Why dk\sqrt{d_k}?

Well, because it's the Standard Deviation of the Dot Product.

  • If you suppose the components of qq and kk are independent, each with mean 0 and variance 1 (standardise Normal Distribution)
  • Then q⋅k=∑i=1dkqikiq \cdot k = \sum\limits_{i=1}^{d_k} q_i k_i is the sum of dkd_k terms
  • So the sum has mean 0 and variance dkd_k, which means a standard deviation of dk\sqrt{d_k}.
  • Dividing by dk\sqrt{d_k} brings the variance back to 1 whatever the head size is. The softmax then sees inputs of the same scale at dk=64d_k = 64 or dk=1024d_k = 1024.
  • Dividing by dkd_k would shrink the scores too much and flatten the attention towards uniform. Dividing by dk\sqrt{d_k} is the exact correction for how the spread grows.

For a full walkthrough of the self-attention mechanism including input preparation, the QKV projections, masking, and the complete PyTorch module, see Self-Attention.

References

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, and Illia Polosukhin. Attention Is All You Need. August 2023. arXiv:1706.03762, doi:10.48550/arXiv.1706.03762. ↩ 1 2