Answers · How models work
How does masking actually make attention causal?
In shortThe mask is applied to the scores before they become weights, so forbidden positions end up with exactly zero weight and the rest are renormalised automatically.
The mask is applied in score space, before the normalisation that turns scores into weights. Forbidden positions are given a very large negative score, so their exponential is zero and the denominator sums only over allowed positions.
Causal attention does not compute the full set of weights and then hide the future entries. It edits the score matrix first. For a query at position , every key at position has a large negative number added to its score . Softmax then carries out the exclusion by ordinary arithmetic: exponentiate, add, divide.
The mask is usually stored as a matrix of zeros and very negative numbers, or applied as a lower triangular condition. The arithmetic is the same either way: forbidden scores are not deleted from the tensor, they are made so negative that their contribution to the sum is zero. That is why the same softmax function serves an encoder, which masks nothing, and a decoder, which masks the upper triangle.
Take three scores before masking, . Without a mask, , and , so the denominator is . The weights are , and . Now forbid the third position. Replace its score by negative infinity before exponentiating. Its term becomes , the denominator becomes , and the weights are , and . The forbidden position is not small. It is exactly zero, and the allowed positions renormalise without a second pass.
| position | score | exp(score) | weight before mask | weight after mask |
|---|---|---|---|---|
| 1 | 2 | 7.389 | 0.665 | 0.731 |
| 2 | 1 | 2.718 | 0.245 | 0.269 |
| 3 | 0 | 1.000 | 0.090 | 0 |
The first row shows the extreme case. Token one has one legal target, itself. Its row contains one finite score and the rest are negative infinity. The denominator is a single term, , so . The score is ignored, not because the code special cases position one, but because normalising over a set of size one forces the answer. That is what makes the triangle in a causal mask exact.
Why before the exponential matters
The natural guess is to compute all the weights, zero the future ones, then divide by the new total. That can be made to agree numerically, but it is a different operation. It needs a second normalisation after softmax. The mask that makes attention causal is a score edit inside the one normalisation. Softmax is unchanged by adding a constant to every score, so the masked score only has to sit far enough below the real scores for its exponential to underflow. The normaliser has no separate masking branch to get wrong.
That identity is also why implementations subtract the row maximum before exponentiating. If the largest allowed score is , subtracting gives exponents , whose exponentials are and whose sum is . The weights are , , , the same as before. The subtraction changes no weight; it only keeps the exponent from overflowing. A finite very negative mask value works for the same reason. True negative infinity gives an exact zero, but multiplying it by a zero gradient can produce a NaN in the backward pass, so code uses a large finite negative number instead.
What the mask does not do
Masking enforces which positions are available. It does not give the head a way to attend to nothing. Softmax always produces a full distribution over the surviving positions, so a head that finds nothing useful must still put its weight somewhere. Trained models often learn to dump spare weight on the first token, the attention sink. If that token is removed, the place where the spare weight was stored is removed too. The causal constraint is exact; the usefulness of what remains is learned.
Worth keeping
- A causal mask edits the scores before softmax, so forbidden positions have exponential zero and contribute nothing to the denominator.
- Softmax is shift invariant, so subtracting the row maximum changes no weight and only prevents overflow.
- Masking chooses which tokens are legal; it does not let a head abstain, because the weights still sum to one over whatever is legal.
Where this goes next
This question is one step of The Mathematics of Attention, a course that works through its subject from the beginning. The page that teaches this part of it is Softmax Over Scores, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- What do multiple attention heads actually give you that one big head doesn't?Several heads let a model track several relationships at once, each in its own lower-dimensional subspace, instead of squeezing every relationship into one weighted average that destroys information.
- Why do we divide the attention scores by the square root of the key dimension?The square root of the key dimension is the typical size of an unscaled attention score, so dividing by it stops softmax from becoming a hard pick-one.
- Why does attention use softmax instead of just dividing each score by the sum of all the scores?Dividing by the sum can give negative weights and fails when scores cancel; exponentiating first makes weights positive and turns score gaps into ratios.
- Why does attention need positional encoding if it already looks at the whole sequence at once?Attention treats tokens as an unordered set, so position has to be added to the vectors; otherwise a sentence and its anagram get the same representations.
- Why does the model get stuck repeating the same sentence over and over?The model is not stuck in a memory loop: once it has written a phrase, that phrase is in its context and makes writing it again more likely, so a decoder that always picks the top word cannot leave.
- Why does diffusion add Gaussian noise instead of any random corruption?Diffusion adds Gaussian noise because Gaussian errors combine exactly: each step keeps the spread fixed and gives a closed-form noisy picture at any later step, which makes the reverse step learnable.