Autoregressive factorization needs an implementation mechanism: causal attention (masked self-attention). Volume 10 covered masked attention; here we specialize it for GPT-style LMs and connect to KV-cache-friendly inference from Module 11.1.
Learning Objectives
By the end of this lesson, students should be able to:
- Define causal attention as forbidding attend-to-future positions.
- Build / visualize a causal mask matrix.
- Explain why the mask is required for parallel teacher-forced training.
- Relate causal attention to streaming generation and KV caching.
- Contrast causal vs. bidirectional attention with BERT.
- Apply additive
-infmasking before softmax in code.
Causal attention (look-ahead masked self-attention) restricts each query position i so it may attend only to key positions j ≤ i. Future tokens receive no attention weight, preserving autoregressive consistency.
Mask Shape
For sequence length T, a common Boolean upper-triangular mask marks forbidden (i, j) pairs with j > i. Implementations add large negative values to those logits before softmax so their probabilities become ~0.
| k0 | k1 | k2 | k3 | |
|---|---|---|---|---|
| q0 | OK | BLOCK | BLOCK | BLOCK |
| q1 | OK | OK | BLOCK | BLOCK |
| q2 | OK | OK | OK | BLOCK |
| q3 | OK | OK | OK | OK |
Bidirectional
- All positions visible
- BERT / MLM encoders
- Not for next-token LM
Causal
- Past + present only
- GPT decoders
- AR-safe
Prefix-LM hybrids
- Bidirectional prompt
- Causal on continuation
- Some seq2seq recipes
Training vs. Inference
Full sequence + causal mask in parallel.
Attend to cached past K/V.
New token extends cache.
The mask guarantees that even when all tokens are processed in one training forward pass, no position peeks ahead—matching what generation will do one token at a time.
“Causal attention means the model ignores the user prompt after the first token.” Prompt tokens are in the past of later positions; every new token can attend to the entire prompt and prior outputs.
Strengths and Tradeoffs
Strengths
- Enables parallel AR training.
- Matches streaming generation.
- KV cache friendly.
Tradeoffs
- No future context for disambiguation.
- Still O(T²) without sparse/approx methods.
- Mask bugs silently break AR validity.
Knowledge Check
- Short Answer: Position i may attend to which keys? Answer: Positions j ≤ i.
- True/False: Causal masks are unnecessary if you train left-to-right one token per step only. Answer: True for that scheme—but parallel teacher forcing needs the mask.
- Multiple Choice: Before softmax, blocked positions get: (a) -inf-like values, (b) +inf always, (c) dropout only. Answer: (a).
- Short Answer: Why use a causal mask during a full-sequence forward? Answer: To prevent leakage from future tokens while training in parallel.
- True/False: BERT uses the same causal mask as GPT. Answer: False.
- Multiple Choice: KV caching helps: (a) reuse past keys/values at decode time, (b) delete attention, (c) only train CNNs. Answer: (a).
- Short Answer: What does the upper triangle of a causal mask typically represent? Answer: Forbidden future attentions.
- Short Answer: Name the Volume 10 lecture on masked attention. Answer: Masked Attention.
- Multiple Choice: Softmax after -inf masking yields: (a) ~0 weight on blocked keys, (b) uniform future mass, (c) NaN always. Answer: (a).
- True/False: Causal attention alone chooses the next token string. Answer: False—the LM head + decoding policy do.
Key Takeaways
- Causal attention blocks future positions for AR safety.
- Masks enable parallel teacher-forced training.
- Generation reuses the same constraint with KV caches.
- Opposite of BERT’s bidirectional self-attention.
- Next: Prompt.
Hands-on idea: Intentionally remove the mask in a tiny model and show training loss collapsing via future leak (toy demo).
Discussion prompt: How do prefix-LM hybrids change the mask pattern?
Recap: Causal attention is the AR enforcement layer. Continue with Prompt.