Attention Masking › Attention Masking
Language AI HandbookAll chapters Attention Masking › Attention MaskingOne section · Sections in this chapter1 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Self-attention computes pairwise interactions between all positions in a sequence, letting every token gather information from every other token. This generality is one of attention's greatest strengths, but it also raises an immediate practical problem: sometimes we need to block certain interactions entirely. A model generating text one token at a time must not be allowed to peek at the tokens that come after the current position. Sequences padded to a uniform length so they can be batched together should not let real tokens attend to meaningless padding. Controlling which positions are "visible" to which others is not a minor detail but a foundational design decision that shapes what a transformer can and cannot do.
Think of attention masking as a privacy screen on a monitor. Without the screen, anyone walking past can read what is displayed. With the screen, only people sitting in the correct position see the content, while everyone else sees darkness. In the same way, an attention mask designates which query-key pairs are "in view" (allowed to interact) and which are "blocked" (prevented from interacting). The screen does not change the underlying pixels; it simply controls visibility. The mask does not change the attention mechanism; it simply controls which scores contribute to the final weighted sum.
Masking modifies the attention computation by adding large negative values to specific positions before the softmax. Recall that attention weights are computed as:
where:
- : the attention weight from query position to key position , representing how much position "attends to" position
- : the raw attention score between positions and , computed as the dot product of their query and key vectors (scaled by )
- : the sequence length
When we add a large negative value (like ) to a score , the exponential becomes vanishingly small, driving to near-zero. These masked positions effectively disappear from the weighted sum. This simple mechanism enables causal language modeling, efficient batch processing, and custom attention patterns.
The elegance of this approach is that it requires no changes to the attention mechanism itself. The formula remains identical. The mask is simply an extra additive term inserted at one specific point in the computation, yet that term determines which positions survive softmax. Masked positions contribute nothing to the output, so from the model's perspective, they simply do not exist during that forward pass.
Understanding attention masking deeply matters for several reasons. First, it is the difference between a model that cheats during training (by reading future tokens) and one that learns to predict. Second, it is what makes batched training of variable-length sequences possible without corrupting representations. Third, it is the foundation of efficient attention variants that scale transformers to sequences far longer than standard full attention can handle. Every serious transformer practitioner needs to understand that masks exist, how they work, when to apply them, and what happens when they are misconfigured.
Attention masking was introduced as part of the original Transformer architecture in "Attention Is All You Need" (Vaswani et al., 2017). The paper described the causal mask as applying "an additional mask in the scaled dot-product attention, which prevents positions from attending to subsequent positions." At the time, this was described almost as a minor implementation detail, but it proved to be one of the most consequential design choices in modern deep learning. The causal mask is what allowed GPT-style autoregressive models to train on entire sequences in parallel rather than token by token, reducing training time by orders of magnitude and making large-scale language modeling practical. The padding mask, while simpler conceptually, was equally critical for enabling batched training on heterogeneous real-world text corpora. Without these two masking mechanisms working in concert, the transformer's rise to dominance in NLP would have been far slower.