NEW
A masking strategy that prevents tokens from attending to future positions, enabling autoregressive sequence modeling.
DESCRIPTION
Causal masking ensures each position in self-attention only attends to itself and earlier tokens. This enforces the autoregressive constraint required for next-token prediction and causal language models. Transformers implement it by applying upper-triangular masks to attention scores. Causal masking is central to GPT-style decoders and streaming generation. It contrasts with bidirectional attention used in masked language modeling tasks like BERT.
ALTERNATIVE DEFINITIONS
Attention Is All You Need
We apply a mask to prevent positions from attending to subsequent positions.
Canonical introduction of masked self-attention in Transformer decoders.
The Annotated Transformer
Masks are used to hide future tokens in the decoder’s self-attention, preventing a position from attending to later positions.
Educational explanation with code and diagrams.
RELATED TERMS
Self-Attention Transformer Model Autoregressive Modeling Masked Self-Attention Language Modeling GPT Decoder Attention Mask Next-Token Prediction Sequence Modeling
COMPETING TERMS
Bidirectional Attention Masked Language Modeling
KNOWLEDGE AREAS
TARGET AUDIENCE
AI Engineer Data Scientist MLOps Engineer Software Developer
EXTERNAL SOURCES
- Wikipedia - Transformer (machine learning model)
- Vaswani et al. - Original Transformer paper introducing masks
- Hugging Face - Task guide for causal language modeling
- Harvard NLP - Annotated Transformer with masking details
TOP QUESTIONS
Why is causal masking required for GPT-like models?
It ensures predictions depend only on past tokens, preserving autoregressive semantics. Without masking, information leakage would invalidate next-token training.
Category: Requirements
How is causal masking implemented in attention?
An upper-triangular mask sets attention scores to negative infinity for future positions before softmax, preventing attention to them.
Category: Implementation
How does causal masking differ from bidirectional attention?
Bidirectional attention allows tokens to attend both past and future, used in masked language models (e.g., BERT). Causal masking restricts to past.
Category: Comparison
Is causal masking used during inference?
Yes. It preserves the autoregressive constraint for incremental generation and streaming outputs.
Category: Management
What are common mistakes with attention masks?
Mismatched dimensions, incorrect dtype, or wrong broadcast lead to shape errors. Forgetting to mask padding tokens skews attention.
Category: Mistakes
Does causal masking affect training speed?
It adds minimal overhead compared to full attention. However, sequence length still dominates compute.
Category: Performance
How do Transformers handle padding with causal masks?
Separate padding masks zero out attention to padded positions, combined with causal masks to enforce both constraints.
Category: Implementation
Can causal masking be adapted for vision transformers?
Autoregressive tasks on images can use causal ordering of patches, but most vision models use bidirectional attention.
Category: Opinion
What metric reveals masking issues?
Unexpectedly high perplexity or degenerate attention patterns may indicate future leakage or misapplied masks.
Category: Testing