Why is masking applied before the softmax rather than after it?

Last updated 22 November 2026

  • 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.

  • https://llmdojo.dev/interview
  • https://www.mantramindai.in/courses/transformer-architecture-q-a/lessons/describe-how-causal-mask-is-combined-with-attention-scores-and-why-order-matters
  • https://machinalearning.com/lessons/transformers_llms/gpt-decoder
All Transformers interview questions