Why is the softmax in attention applied over the key dimension rather than the query dimension?
Last updated
Each output is
Σ_j w_ij v_j, so the weights must sum to 1 over keys to form a proper weighted average at a consistent scale.Softmax over queries would make queries compete with each other and give arbitrary output magnitudes. It would also let future queries affect past outputs, breaking causal decoding.
Masks and KV-cache decoding rely on normalizing each query independently over its own key set, i.e.
softmax(dim=-1).
- https://apxml.com/courses/foundations-transformers-architecture/chapter-2-attention-mechanism-core-concepts/softmax-attention-weights
- https://kgptalkie.com/tutorials/transformers/day-16-masked-causal-attention
- https://medium.com/@can.demir/how-attention-really-works-q-k-v-and-why-we-split-into-heads-f14373392418