04 · C · Mix
Causal attention
The only place tokens mix. Scale, mask, softmax, GQA, and the softmax Jacobian.
Queries, keys, values
From normalized input , three linear maps produce Q, K, V. They are then split into heads of width . If RoPE is used, Q and K are rotated now.
- projections in R^{d × d} (or d × H d_h)
- head h, each T × d_h
Scores, scale, causal mask, softmax
Why it mattersA typical coordinate of q and k is O(1), so a typical dot product is O(√dₕ). The √dₕ scale (Vaswani et al., 2017) puts scores back at O(1). Without it, softmax saturates as dₕ grows and the Jacobian vanishes.
Future tokens must be invisible. The causal mask writes above the diagonal so those entries become zero after softmax.
- a probability distribution over past-and-present positions
Why it mattersAttention is the only place tokens mix. Everything else is applied positionwise. The causal mask is what makes the model a next-token predictor rather than a bidirectional encoder.
- output projection, d × d
- Attn(U), written back into the residual stream
Softmax Jacobian
For one row :
Why it mattersThis Jacobian is symmetric, has null space along 1 (softmax is shift-invariant), and is positive semidefinite on the simplex tangent space. You will meet it again in the p − y collapse.
Grouped-query and multi-query attention
Multi-head attention uses query and key/value heads. Multi-query (Shazeer, 2019) uses one KV head. Grouped-query (Ainslie et al., 2023) sits in between: Llama-3-class models share each KV head across a group of query heads. The score math is the same; the cache is smaller.
- number of query heads
- number of key/value heads, 1 ≤ H_kv ≤ H_q
- group size; g = 1 is MHA, H_kv = 1 is MQA
Why it mattersThe KV cache at inference is O(T · n · H_kv · d_h), not O(T · n · H_q · d_h). That memory, not the matmul, is what bounds generation length.