Why is masking applied before the softmax rather than after it?
Last updated
Setting blocked logits to −∞ before softmax gives exp(−∞) = 0. Blocked positions drop out of both numerator and denominator, so allowed weights still sum to 1.
Zeroing after softmax leaves rows not summing to 1. Masked logits still sat in the denominator, so future or pad tokens influence the weights and receive gradient (leakage).
An additive pre-softmax mask is also cheap and fuses easily into kernels like FlashAttention.