Why is a large negative value such as negative infinity used for masked positions in the attention score matrix?

Last updated 22 November 2026

  • Masked logits are exponentiated, so −∞ yields exp = 0 (zero probability) and the softmax renormalizes over the rest. A logit of 0 gives exp(0) = 1, an ordinary weight, so 0 means neutral, not blocked.

  • True −∞ makes fully masked rows produce NaN (−∞ − (−∞)). Many implementations use torch.finfo(dtype).min or −1e9 instead, giving a uniform distribution on such rows.

  • Use a dtype-aware minimum: −1e9 overflows to −∞ in fp16 (max about 65504).

  • https://sachinkalsi.github.io/blog/why-do-we-use-negative-inf-for-masking/
  • https://www.interviewbit.com/varsity/blog/self-attention-in-transformers-maths-step-by-step/
  • https://www.geeksforgeeks.org/nlp/how-do-self-attention-masks-work/
All Transformers interview questions