Why is a causal mask required for autoregressive language model training?
Causal mask blocks position t from attending to t+1, t+2, ... at training. Without it, teacher forced training trivially leaks the answer.
Imagine reading a mystery novel where you've slid a piece of paper down the page so you can only see lines you've already read: never the line below. That's what a causal mask does: when the model is guessing what word comes next at position t, the paper covers up positions t+1, t+2, and beyond. Now compare that to a crossword puzzle, where you can see every letter on the grid at all times to fill in a missing word, that's bidirectional attention, used by models like BERT. The causal mask is the 'no peeking ahead' rule that forces the model to actually guess the next word instead of secretly reading it.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Explain teacher forcing, the mask mechanics (-∞ before softmax), why inference is naturally causal, FlashAttention's exploitation of the mask structure, and prefix LM as a useful variant.
# Apply causal mask BEFORE softmax
import torch
scores = q @ k.transpose(-2, -1) / math.sqrt(d_head) # (B, n_heads, T, T)
causal_mask = torch.triu(torch.full((T, T), float('-inf')), diagonal=1)
scores = scores + causal_mask # broadcasts over batch, heads
attn_weights = torch.softmax(scores, dim=-1)
# Now position i has zero weight on positions j > i because e^(-inf) = 0Real products, models, and research that use this idea.
- GPT-2/3/4 and all decoder only LLMs use causal masks during training; standard PyTorch implementations apply via torch.tril.
- FlashAttention v2 / v3 exploit the causal mask structure to skip computation on the upper triangular half: roughly 2x speedup vs naive.
- Prefix LM in T5 / UL2 uses a hybrid mask: bidirectional in the prefix block, causal in the continuation block.
- BERT's MLM training does NOT use a causal mask, it predicts masked tokens from full bidirectional context.
What an interviewer would ask next. Try answering before peeking at the approach.
QIf inference is naturally causal, why do many production codebases still apply the causal mask at inference?
QHow does FlashAttention v2 exploit the causal mask to save compute?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Thinking the mask is for inference: it's most critical at training. Inference is naturally causal because future tokens don't exist yet.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.