What is causal masking, why is it needed, and how is it implemented?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Causal masking prevents position i from attending to positions j > i, enforcing that predictions depend only on the past. It's needed for two linked reasons. First, at inference time future tokens don't exist - the model must generate left to right, so training must match that conditional structure. Second, and more subtly, it's what makes causal LM training efficient and non-degenerate: with a causal mask, one forward pass over an n-token sequence produces n valid next-token predictions trained in parallel. Without the mask, predicting token t+1 while attending to token t+1 is trivial copying - the model would learn nothing.
Full worked answer with diagram
Causal masking prevents position i from attending to positions j > i, enforcing that predictions depend only on the past. It's needed for two linked reasons. First, at inference time future tokens don't exist - the model must generate left to right, so training must match that conditional structure. Second, and more subtly, it's what makes causal LM training efficient and non-degenerate: with a causal mask, one forward pass over an n-token sequence produces n valid next-token predictions trained in parallel. Without the mask, predicting token t+1 while attending to token t+1 is trivial copying - the model would learn nothing.
Implementation: before the softmax, add a mask matrix that is 0 on and below the diagonal and −∞ (in practice a large negative number like −1e9, or dtype min) strictly above it. After softmax, those positions get exactly zero weight, and the renormalization happens automatically because softmax normalizes over the remaining finite entries.
mask = np.triu(np.ones((n, n), dtype=bool), k=1) # True above diagonal
scores = np.where(mask, -1e9, scores) # before softmax
In production kernels (FlashAttention), the mask is never materialised as an n×n tensor - the kernel simply skips computing blocks that are entirely masked, which also halves the work since roughly half the score matrix is masked out.
Going further: follow-up territory
Details worth volunteering: encoder models (BERT) deliberately omit the mask - they trade generation ability for bidirectional context. Prefix-LM setups mask causally only over the generated portion while allowing bidirectional attention over the prompt. And during batched inference you additionally need padding masks so tokens don't attend to padding - a distinct mask that beginners often conflate with the causal one.
Worth sketching. It shows the mask lands before the softmax, so renormalisation is automatic rather than a second step you bolt on.
flowchart LR
S["raw scores, n x n"] --> D{"is key j after query i?"}
D -->|"yes, future"| NEG["set score to -inf"]
D -->|"no, past or self"| KEEP["keep score"]
NEG --> SM["row-wise softmax"]
KEEP --> SM
SM --> Z["future weights exactly 0,<br/>surviving weights renormalise themselves"]
What a good answer covers
- Causal masking prevents position i from attending to positions j > i, enforcing that predictions depend…
- It's needed for two linked reasons
- First, at inference time future tokens don't exist - the model must generate left to right, so training…
- Second, and more subtly, it's what makes causal LM training efficient and non-degenerate
- Without the mask, predicting token t+1 while attending to token t+1 is trivial copying - the model would…
In the glossary: Causal mask · FlashAttention · Attention
Interviewer follow-ups to expect
Why does the mask go before the softmax rather than after?
What's a prefix LM and when is bidirectional prompt attention useful?
How does FlashAttention exploit the causal structure for speed?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep