The core mechanism of the Transformer architecture (Vaswani et al., 2017) is Scaled Dot-Product Attention. Given query matrix , key matrix , and value matrix , attention is formulated as: where is the dimensionality of queries and keys.
Mathematical Proof of the Scaling Factor:
To understand the necessity of the scaling factor , let's analyze the variance of the dot product before softmax.
Let and be query and key vectors representing a single row in and . Assume that the components and are independent random variables, each with mean 0 and variance 1: We assume that and are mutually independent for all indices .
The dot product is:
- Mean of the Dot Product:
Using the linearity of expectation:
- Variance of the Dot Product:
Since the elements are mutually independent: For two independent random variables and with mean 0: Thus: Summing across all dimensions:
Softmax Gradient Saturation:
As the query/key dimension grows large, the variance of the dot products scales linearly with . This means the dot products are highly likely to take on extremely large positive or negative values. Recall that the derivative of softmax is . When input logits have high variance, softmax can become very sharp, assigning probability near one to a maximum and near zero elsewhere. Many Jacobian entries then become small, weakening useful gradient signal; they are not mathematically all zero in finite precision-free analysis.
Dividing the dot product by scales the variance back to 1: This keeps the inputs in the active, high-gradient region of the softmax function, stabilizing optimization.