The Causal Mask.
Forbid each position from looking ahead, and a sequence of T tokens becomes T honest prediction problems solved in one pass.
A language model predicts each token from the ones before it. If attention were allowed to look at every position, the model at position t could simply read token t + 1 and copy it: training loss would collapse to zero and the model would learn nothing usable for generation, where the future does not exist yet. The causal mask forbids it: before the softmax, every score from a query to a later key is set to minus infinity, so its weight becomes exactly zero. The attention matrix becomes lower-triangular.
The mask also makes training efficient. Because each position sees only its past, a single forward pass over a sequence of T tokens produces T next-token predictions, each made with exactly the information it would have during generation, and all T losses can be averaged at once. This is called teacher forcing: the model is always given the true previous tokens, never its own guesses.
Below: one attention head of micro reading a sentence: each row is a query position, each column a key. Above the diagonal, the mask has set every weight to zero. Switch to the mask itself to see which pairs are allowed.
- Explain what the causal mask prevents.
- Recognise the lower-triangular attention matrix.
- Count the predictions one forward pass makes.
- Describe teacher forcing.
Honest predictions, all at once
The sentence is 14 tokens. With the mask, each position may attend to itself and everything before it — 105 of the 14 × 14 possible pairs — and 46% of the matrix is forced to zero. This head of micro uses its allowed region mostly along the diagonal and just below it, reading the current and previous token. Whatever it reads, it cannot read the future, so the 14 predictions the pass makes — one per position — are each an honest guess, and the training loss is their average.
During generation the same model is run one new token at a time, and the mask is no longer needed for the newest token: nothing after it exists. But the earlier positions’ keys and values are unchanged by the new token — precisely because of the mask — so they can be computed once and kept. That cache of keys and values is what makes generation fast (plate XXIII.2), and it exists only because attention is causal. Encoder models such as BERT, which see a whole text at once and are trained to fill in masked words, drop the causal mask; they are not built to generate (plate V.4).
Terms
- Causal mask
- Setting attention scores from a position to later positions to −∞, so their weights are zero.
- Teacher forcing
- Training on the true previous tokens at every position, all in one pass.
- Lower-triangular
- A matrix whose entries above the diagonal are zero.
- Encoder
- A model that sees the whole text bidirectionally, without a causal mask.