Complete the canonical attention formula: Attention(Q, K, V) = ___(QKᵀ / ___) · V
Attention(Q, K, V) = softmax(QKᵀ / √d_k) · V. The softmax turns raw scores into weights; the √d_k stabilizes the variance so softmax doesn't saturate.
Picture a chef tasting a row of sauces to decide how much of each to ladle into a final stew. Each sauce gets a raw 'matches my dish' score, but those scores can be wildly different in scale, so the chef shrinks them down to a fair size first. Then the chef turns the scores into percentages that add up to 100%, and finally pours each sauce into the stew in those proportions. That whole tasting and mixing routine is what the attention formula does.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Equation 1 of Vaswani et al. 2017 is the most-recited formula in modern deep learning.
Knowing it cold, including the shape flow and the rationale for each piece, is the bare minimum for any interview that touches attention. But the surface formula hides three substantial mathematical questions: why the dot product specifically (and not additive scoring), why the square-root scaling (and not d_k or 1/d_k), and why softmax (and not sparsemax or argmax). This deep dive answers all three, then walks the modern implementation landscape, FlashAttention's blockwise computation, the additive modifications (masks, ALiBi, RoPE), and where approximation variants give up exactness for scalability.
The four-step recipe
Reading the formula left to right is reading the four-step recipe attention executes for every output position.
Score, scale, softmax, sum
- Score,
QKᵀproduces ann×ncompatibility matrix. Each entry (i, j) isq_i · k_j. - Scale, divide by
√d_kto keep score variance ≈ 1 regardless of how larged_kis. - Softmax: apply row-wise over the keys axis. Each row becomes a probability distribution; the row entry (i, j) is the attention weight from query i to key j.
- Sum, multiply by V to take a weighted aggregation of value vectors. The output for each query position is a convex combination of all value vectors.
What the output represents
Output shape is (n, d_v): one context-aware vector per query position. Every output is a soft retrieval over the entire input sequence, with retrieval weights determined by content-based query key compatibility.
The recipe is identical whether attention is causal (with mask), bidirectional (no mask), cross-attention (Q from decoder, K/V from encoder), or self-attention (Q/K/V all from same input).
\text{Attn}(Q,K,V) = \text{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)VSituations where this technique stops working.
2–4 min · Everything important, quickly.
import torch, torch.nn.functional as F
def attention(Q, K, V):
# Q, K: (..., n, d_k); V: (..., n, d_v)
d_k = Q.size(-1)
scores = (Q @ K.transpose(-2, -1)) / (d_k ** 0.5)
weights = F.softmax(scores, dim=-1) # row-wise over keys
return weights @ VReal products, models, and research that use this idea.
- Equation 1 of Vaswani et al. 2017, the foundational definition of attention used in every transformer since.
- FlashAttention computes this exact formula with O(n) HBM memory by tiling and fused kernels.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is the scaling specifically √d_k rather than d_k or 1/d_k?
Variance of a sum of d_k unit variance products is d_k, so standard deviation is √d_k. Dividing by √d_k normalizes the standard deviation back to 1, which is the natural unit for softmax inputs. d_k would over-correct (variance 1/d_k → softmax never sharpens); 1/d_k makes no statistical sense.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Forgetting the √d_k or putting d_k instead of √d_k: the square root matters. Or applying softmax over the wrong axis.
60 second bullets to scan on the way to the call.
Write the formula from memory exactly
Shape of QKᵀ for sequences of length n
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.