How does padding masking differ in purpose from causal masking?

Last updated 22 November 2026

Padding masking is a batching fix: it hides fake pad tokens so they don't contaminate real ones. Causal masking is a modeling constraint: it hides real future tokens so the model can't see what it must predict.

  • Padding mask:

    • Purpose: sequences of different lengths are padded to the same length for batching. Pad tokens carry no meaning and must not receive attention weight.

    • Data-dependent: it differs per example (from attention_mask).

    • Masks along the key dimension, shape (B, 1, 1, T_k).

    • Used everywhere batches have variable lengths: encoder, decoder and cross-attention.

    • Pad queries still produce outputs; they are excluded from the loss instead (e.g. ignore_index=-100).

  • Causal mask:

    • Purpose: enforce autoregressive information flow so position t depends only on tokens ≤ t.

    • Structural: it depends only on positions and is identical for every example.

    • Masks query-key pairs where j > i, a T×T upper triangle.

    • Used only in decoder self-attention.

  • Key distinction:

    • Removing the padding mask makes outputs depend on batch composition (a correctness bug).

    • Removing the causal mask leaks the training targets (the model cheats).

    • They are orthogonal and are usually combined (AND of allowed positions) in decoders.

  • https://www.geeksforgeeks.org/nlp/how-do-self-attention-masks-work/
  • https://machinelearningmastery.com/a-gentle-introduction-to-attention-masking-in-transformer-models/
  • https://discuss.huggingface.co/t/difference-between-attention-mask-and-causal-mask/104922
All Transformers interview questions