Causal masking restricts Self-Attention so that each position can only attend to itself and earlier positions — never future tokens.
Implementation: before the softmax in attention, set all entries above the diagonal of the matrix to :
After softmax, entries become 0 → zero attention to future positions.
Why it's needed:
- Decoder-only models (GPT) are trained with next-token prediction → at position , the model must predict token using only tokens
- Without masking, the model could "cheat" by looking at the answer
- During generation, future tokens don't exist yet — masking during training simulates this
Types of transformers:
- Encoder-only (BERT): no causal mask, full bidirectional attention → good for understanding
- Decoder-only (GPT): causal mask → Autoregressive Generation
- Encoder-decoder (T5, original transformer): encoder is bidirectional, decoder is causal with cross-attention to encoder
Efficiency: causal masking means attention is a triangular operation. FlashAttention and other optimized kernels exploit this structure.
See also: Self-Attention, Autoregressive Generation, Multi-Head Attention