Attention / Core
verifiedCausal Masking
A model trained to predict the next word must not be allowed to see it. Causal masking blocks every position from attending to anything later than itself, so the prediction at each step is made from the past alone. It is what makes a single forward pass over a sentence into n training examples at once rather than one.
Add negative infinity to the upper triangle of the score matrix before the softmax, so those weights come out as exactly zero. Masking after the softmax would be wrong: the denominator would still include the future. This is the difference between a decoder and an encoder, and it is why decoder attention patterns are always triangular.
Scores S sub ij + M sub ij where M sub ij = 0 for j ≤ i and −∞ otherwise, then row-wise softmax. exp(−∞) = 0 makes the masked weights vanish while the remaining row still sums to one. The lower-triangular structure is what allows the KV cache: position i's keys and values never change when position i+1 arrives.
8 queries against 8 keys; a brighter cell means more of that query's attention went to that key. Each query sees itself and everything before it, and nothing after — the upper triangle is masked.
The triangular pattern masking leaves behind: every position sees itself and everything before it, nothing after. Drag the sequence length to watch the visible half grow while the masked half grows with it.
Reviewed by opendroid · 2026-08-04
- arXiv:1706.03762 — Attention Is All You Need