09. Causal masking — blocking the future in decoders¶
If a decoder can peek ahead, the exam is fake. See the cheating rule clearly.
Built on the ELI5 in
00-eli5.md. The scorecard — softmax weights over visible tokens — now needs a hard rule that hides tomorrow's answer.
Mental model — the decoder must read only leftward¶
A decoder predicts the next token. So position t may use tokens <= t. Not token t + 1.
Not the future. Training gives the whole sequence at once. That is fast.
That is also dangerous. Without a mask, each row of the spotlight beam can read the answer key. Loss drops.
But learning is dishonest. Then generation starts. The future token is not present yet.
The cheat path vanishes. Quality collapses. So what to do?
Keep parallel training. But block rightward visibility inside attention. See the picture:
query at position 1 -> may see [1]
query at position 2 -> may see [1 2]
query at position 3 -> may see [1 2 3]
query at position 4 -> may see [1 2 3 4]
Picture first — the lower-triangular rule¶
For four tokens, the allow matrix is lower triangular.
Same thing as a matrix: And as a logit mask: Lower triangle allowed. Upper triangle blocked. One triangle direction. Huge consequence.Formula — block logits before softmax¶
Start with raw attention scores.
Then add the causal mask. The rule is simple. Why-inf? Because softmax turns it into zero probability.
So blocked columns get exactly zero weight. That means no future leak reaches the weighted sum. See.
We do not delete tokens. We delete their visibility.
Worked example setup — four tokens, one head¶
Take this tiny training sequence:
Suppose one head produces these raw scores. Rowi is one query token. Column j is one key token. Now mask row by row.
Worked numerical example — apply the mask row by row¶
Row 1¶
Raw row:
After causal mask: Softmax: ASCII view:Row 2¶
Raw row:
After causal mask: Exponentials over visible positions: Softmax: ASCII view:Row 3¶
Raw row:
After causal mask: Exponentials over visible positions: Softmax: ASCII view:Row 4¶
Raw row:
After causal mask: Nothing is blocked here. Softmax: ASCII view:Three scenarios — no mask, wrong mask, correct mask¶
Scenario 1 — no mask¶
Look at row 3 without masking.
So tokensamosa mostly reads EOS. That is tomorrow's answer. Training loss looks excellent.
Generation later will fail.
Scenario 2 — wrong triangle orientation¶
Suppose a bug keeps the upper triangle.
Now row 2 cannot see row 1. History disappears. Only self and future remain. The code still runs. The model learns the wrong game.Scenario 3 — correct causal mask¶
Keep the lower triangle. Future columns go to -inf. Softmax gives zero there.
Now each token learns from available history only. That matches generation-time reality. See the contrast:
no mask -> cheat path exists
wrong mask -> history path breaks
right mask -> training matches decoding
Why loss can look good while generation fails¶
Teacher forcing shows the full target sequence during training. Without causal masking, the decoder can copy future clues. That makes next-token prediction too easy.
So cross-entropy falls for the wrong reason. At inference, token t+1 is missing. The model must rely on left context only.
If training never enforced that rule, generation becomes shaky. You see repetition. You see incoherence.
You see brittle continuations. Good training loss. Bad real behavior.
Classic cheating symptom.
Padding masks — related, but not the same¶
Causal masks block the future. Padding masks block fake tokens. Suppose a batch has:
ThePAD tokens are not real words. They were added only to make shapes match. So what to do?
Hide PAD columns from attention.
In practice, many systems combine both masks. One mask says, "do not look right." The other says, "do not look at padding garbage."
Where this lives in the wild¶
- OpenAI ChatGPT decoder pretraining needs causal masks so next-token learning stays honest.
- GitHub Copilot code completion must not peek at future tokens during training batches.
- Anthropic Claude long-form generation still relies on left-to-right masking inside decoder attention.
- Meta Llama training stacks combine causal masks with padding masks for packed batches.
- Google Gemini decoder-style text generation uses the same future-blocking rule during autoregressive training.
Interview Q&A¶
Q: Why do we set blocked logits to -inf before softmax?
A: Because softmax then gives exactly zero probability to blocked positions.
Common wrong answer to avoid: "We remove those tokens from the sequence." No. The tokens stay. Only visibility changes.
Q: Why can training loss improve without causal masking?
A: Because the model reads future tokens and solves an easier, dishonest task.
Common wrong answer to avoid: "Lower loss always means better generation." Not here. The task itself became invalid.
Q: Is a padding mask the same as a causal mask?
A: No. Padding masks hide fake batch filler. Causal masks hide future positions.
Q: Does the last token need masking?
A: No future exists to its right, so the last row often stays fully visible.
Apply now (5 min)¶
Take a 4-token sentence of your own. Write a 4 x 4 raw score matrix. Now draw the lower-triangular causal mask.
Set blocked scores to -inf. Compute one masked softmax row by hand. Then say, in one line, why unmasked training is cheating.
Sketch from memory: Draw the ✓/X visibility table for positions 1 to 4.
Bridge. One masked spotlight is still too limited. Next, we split the work across parallel crews with different habits in
10-multi-head-attention.md.