The core attention computation is three steps. Memorize them.
- Score:
scores = Q @ K.T / sqrt(d_k). Each entry (i, j) measures how much token i should attend to token j. Shape: (n, n). - Normalize:
weights = softmax(scores, axis=-1). Each row becomes a probability distribution that sums to 1. Token i's attention is now distributed across all positions. - Weighted sum:
out = weights @ V. Each row is a convex combination of the value vectors, weighted by attention. Shape: (n, d_v).
Three matmuls and a softmax. That is the entire mechanism. Multi-head, causal masking, RoPE, GQA, Flash Attention — every piece of complexity in modern attention is built on top of these three steps without changing them.