Causal Masking
About 25 min
Goal
By the end of this lesson, you can build and test a causal mask and explain why future logits must be masked before softmax in next-token prediction.
For a decoder predicting left to right, query position t may use positions 0..t and must not use t+1....
For length 4, query position 2 may attend to keys 0, 1, and 2. Key 3 is future information. The standard numerical move is to replace illegal logits with negative infinity before softmax; softmax then assigns those positions probability zero.
Why masking must happen before normalization
Take unmasked logits [2, 1, 3] for a query that is allowed to use only the first two positions. If you softmax all three first, the illegal future logit 3 steals probability mass. Simply setting its probability to zero afterward leaves the legal weights summing to less than one.
The correct procedure is:
raw logits: [2, 1, 3]
mask future: [2, 1, -∞]
softmax legal: [~0.73, ~0.27, 0]
Now all normalized mass is redistributed only among legal positions.
This is more than an implementation preference. If an earlier token can use a later target token during training, the task becomes easier for the wrong reason. Loss can look excellent while the model is learning from information it will not have during generation.
That is why prefix invariance is such a strong end-to-end check: two sequences that share the same prefix but differ only later should produce the same earlier outputs. A triangular-looking heatmap is suggestive; unchanged prefix outputs are behavioral evidence that the future did not leak through the stack.
See the triangular legality pattern
For a four-token decoder sequence, the legality pattern is:
query 0: can use 0
query 1: can use 0,1
query 2: can use 0,1,2
query 3: can use 0,1,2,3
Written as a matrix:
1 0 0 0
1 1 0 0
1 1 1 0
1 1 1 1
Each row is a query position; each column is a key/value position.
The zeros are not “low attention preferences.” They are illegal information paths for next-token training.
This distinction matters. A low learned score can change with training. A causal mask encodes a task rule that must remain enforced regardless of learned parameters.
Later, when generation runs one token at a time, there is no future token to leak. The mask is crucial during parallel training because all target tokens are physically present in the training tensor.
Predict
Turn causality into an executable invariant
- Click Run unchanged. Every row prints the same weights,
[0.082, 0.149, 0.272, 0.496], because nothing is masked yet. Row0showsfuture_mass= 0.9179782167678582: most of the first token's attention goes to later tokens. The checks fail. - Complete the TODO in
causal_logitsso key positions afterquery_indexreceiveNEG_INFbefore softmax. Run again. Row0becomes[1.0, 0.0, 0.0, 0.0], row1becomes[0.354, 0.646, 0.0, 0.0], row2becomes[0.163, 0.297, 0.541, 0.0], and every row prints a future mass of0(0.0, or0for the last row, which has no future positions). The checks pass. - Before editing again, predict what happens if each query may also see one position ahead.
- Deliberately unmask one future position: in the comparison you wrote, change
<= query_indexto<= query_index + 1(for example,k <= query_indexbecomesk <= query_index + 1) and run. Row0now prints[0.354, 0.646, 0.0, 0.0]withfuture_mass= 0.6456563062257954. - On this run the future-mass check is supposed to fail. That failure is the point of the step: the invariant catches a single leaked future position even though every row still sums to
1. Restore your comparison and run again to see every check pass.
Loading lab…
The strongest simple check is not “the heatmap looks triangular.” It is numerical: future attention mass = 0. Another end-to-end check is prefix invariance: changing only future tokens must not alter outputs for an earlier shared prefix.
Quick Check
Explain it back
Explain why masking after softmax and simply zeroing forbidden weights without renormalizing changes the meaning of the remaining mixture.
Check your explanation
A strong answer mentions:
- Forbidden future-position scores must be masked before softmax so they receive zero probability.
- Zeroing weights afterward without renormalizing makes the remaining weights sum to less than one, changing the mixture's scale.
- A causal decoder should satisfy prefix invariance: a prefix output cannot depend on appended future tokens.
Key Takeaways
- Causal masking is a correctness contract for decoder attention.
- Future logits must be blocked before softmax.
- Future attention mass should be exactly zero.
- Prefix invariance is a powerful end-to-end causal test.
Next Lesson
Complete the checkpoint now. Then split the feature channels into multiple attention heads while preserving the same causal rule.
References
- Vaswani et al., Attention Is All You Need.
Completion is stored locally on this device.