Masked attention restricts self-attention so each position uses only permitted context. Causal attention allows a token to attend to itself and earlier tokens, preventing future-token leakage during next-token prediction. section 15.3.3
Lower-triangular causal mask
For five target positions, a causal mask is: 1 0 0 0 0; 1 1 0 0 0; 1 1 1 0 0; 1 1 1 1 0; 1 1 1 1 1. Row i specifies which positions token i may attend to, reproducing one-directional information flow without a bidirectional shortcut. section 15.3.3
Use causal masking when:
- Training a Transformer decoder to predict the next word without exposing the label it must predict. section 15.3.3
- Generating text autoregressively, where each new position may depend on past and present tokens but not future tokens. section 16.2.3
- Calling Keras MultiHeadAttention with use_causal_mask to create the required lower-triangular restriction. section 15.3.3
The mask also enables efficient generation: because future positions cannot change past key and value vectors, implementations can cache those vectors and compute one position at a time. section 16.2.3





