In a decoder-only model trained on next-token prediction, position t must predict the token at position t+1. During training we want to compute losses for all positions in parallel — but the model must not be allowed to peek at future tokens during the forward pass. The fix is simple and load-bearing: a causal mask.
Concretely, we add a tensor of -∞ values to the upper triangle of the attention score matrix before softmax. After softmax, those entries become exactly 0 — the corresponding positions are completely ignored. Position 3 can attend to positions 0, 1, 2, 3 but not 4, 5, .... The model gets the parallelism of computing all positions at once while behaving, position by position, as if it had only seen the past.