Walk me through scaled dot-product self-attention in one decoder layer: tensor shapes, why the scores are divided by the square root of the key dimension, what the causal mask does, and how compute and memory grow with sequence length.
Theme reported publicly: Google DeepMind, Mistral AI
Answer
Model answer, written in a candidate's voice
In short
- Input X [n,d_model] projected to Q,K,V; split into h heads; per head scores = Q_h K_hᵀ/√d_k, softmax, weighted sum of V_h; concat heads and apply output projection
- Divide by √d_k so dot-product logits have roughly constant variance as d_k grows, avoiding saturated softmax and tiny gradients
- Causal mask sets future key positions to −∞ before softmax so position i attends only to j≤i, enabling parallel teacher-forced training of an autoregressive decoder
- Per head compute is O(n²·d_k) and score memory is O(n²); across heads this is O(n²·d_model) compute and O(h·n²) scores; FlashAttention tiles/online-softmaxes to avoid materialising the n×n matrix with the same maths
- Attention sits inside the decoder block with residual connections and (pre-)normalisation, followed by a per-position MLP with its own residual
In full
In one decoder self-attention layer, the input X of shape [n,d_model] is linearly projected to Q, K, and V, split across h heads, and each head computes scores Q_h K_hᵀ/√d_k of shape [n,n], applies softmax over keys, and forms a weighted sum of V_h to get [n,d_v]; the heads are concatenated and passed through an output projection. The √d_k scaling keeps the dot-product logits from growing with key dimension, so softmax stays in a useful range instead of saturating and gradients remain healthy. The causal mask sets positions j>i to −∞ before softmax, making those weights zero so each position can only attend to the past; this preserves autoregressive behaviour while still allowing all positions to be trained in parallel with teacher forcing. Compute is O(n²·d) overall and naive score memory is O(n²) per head, which is why FlashAttention tiles the computation with online softmax and avoids materialising the full n×n matrix while computing the same result. In the block, attention is wrapped with residual connections and (pre-)normalisation, and the MLP is applied independently per position, also with a residual connection.
What a strong answer covers
- Q, K, V as linear projections of the input; scores = QKᵀ/√d_k; softmax; weighted sum of V; heads split and concatenated, then an output projection
- Scaling keeps logit variance roughly constant so softmax does not saturate and gradients stay healthy
- Causal mask sets future positions to −∞ before softmax, enabling parallel teacher-forced training of an autoregressive model
- O(n²·d) compute and O(n²) score memory per head; FlashAttention tiles the computation to avoid materialising the n×n matrix (same maths, IO-aware)
- Places attention inside the block: residual connections, (pre-)normalisation, MLP applied per position
Common mistakes
- Explaining the √d_k factor as “normalising by sequence length”
- claiming FlashAttention reduces the asymptotic FLOPs