Implement correct attention masking
Autoregressive Transformer: Correct Attention Masking with Padding
Context
You are tasked with implementing the decoder self-attention mechanism for an autoregressive Transformer model. In this setup, input sequences within a batch are right-padded to a common maximum length.
Task
Your implementation must ensure two critical masking behaviors:
- Causal Mask: Prevent the attention mechanism from attending to future tokens in the sequence.
- Padding Mask: Exclude padded positions from being attended to.
Specifically, you need to:
- Implement a causal mask that enforces the autoregressive property.
- Implement a padding mask that correctly identifies and ignores padded positions.
- Demonstrate how to combine these two masks and apply them within a multi-head attention layer.
- Explain common bugs associated with attention masking, describe how these bugs typically manifest in model performance (e.g., loss or accuracy), and outline effective strategies for testing and debugging them.
Assumptions
- Tensors are batch-first, with a shape of
(B, T, D), whereBis batch size,Tis sequence length, andDis model dimension. - Padding is applied to the right of sequences.
- An
attention_maskof shape(B, T)is provided, where1indicates a real token and0indicates a padded position. Alternatively, you may assume access topad_token_idandinput_ids. - The PyTorch deep learning framework is available for implementation.
AI interview prep
Core dilemma
The central engineering conflict is correctly and efficiently applying multiple, potentially interacting, masking constraints (causality and padding) within the highly parallelized and vectorized operations of multi-head attention, ensuring numerical stability and avoiding subtle bugs that can lead to model degradation.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement correct attention masking
Autoregressive Transformer: Correct Attention Masking with Padding
Context
You are tasked with implementing the decoder self-attention mechanism for an autoregressive Transformer model. In this setup, input sequences within a batch are right-padded to a common maximum length.
Task
Your implementation must ensure two critical masking behaviors:
- Causal Mask: Prevent the attention mechanism from attending to future tokens in the sequence.
- Padding Mask: Exclude padded positions from being attended to.
Specifically, you need to:
- Implement a causal mask that enforces the autoregressive property.
- Implement a padding mask that correctly identifies and ignores padded positions.
- Demonstrate how to combine these two masks and apply them within a multi-head attention layer.
- Explain common bugs associated with attention masking, describe how these bugs typically manifest in model performance (e.g., loss or accuracy), and outline effective strategies for testing and debugging them.
Assumptions
- Tensors are batch-first, with a shape of
(B, T, D), whereBis batch size,Tis sequence length, andDis model dimension. - Padding is applied to the right of sequences.
- An
attention_maskof shape(B, T)is provided, where1indicates a real token and0indicates a padded position. Alternatively, you may assume access topad_token_idandinput_ids. - The PyTorch deep learning framework is available for implementation.