Masks and Cross-Attention: Extending Self-Attention
This post follows From Kernel Regression to Self-Attention and uses the same notation. There we ended with full self-attention, where every position in a sequence attends to every other position using learned projections of itself. Transformer architectures add two extensions:
- Masks, which restrict which positions a query is allowed to read from. The most important is the causal mask used in autoregressive language modeling, but padding masks come up routinely in batched training.
- Cross-attention, which relaxes the self part: queries come from one sequence, keys and values from another. This is what lets one sequence be conditioned on another.
Both are small modifications to equation (13) of the previous post. The mechanism — scaled dot product, softmax, weighted average — is unchanged in both cases. What changes is which positions are allowed to attend to which, and which sequence each input comes from.
1. The leakage problem
A decoder-only language model such as GPT (the next post explains the name) is trained on next-token prediction: given tokens , it should predict from the output at position , for every . For that to be a real prediction, may depend only on .
At inference time, this constraint is automatic — when generating token , only tokens exist. There is nothing to leak. At training time, the situation is different. The point of a Transformer is parallelism: we want to compute in a single forward pass, with all targets visible. But equation (16) from the previous post says
Position attends to all positions , including those with . The model can read the future and copy it into . Loss goes to zero; nothing has been learned.
We need a way to keep the parallelism but block any flow from positions to position .
2. The fix: before softmax
The mechanism is small. In the score for query against key , replace with whenever . After softmax, , so those positions receive exactly zero attention weight. The remaining weights — over keys — renormalize to sum to one.
Concretely, this is achieved by adding a mask matrix to the score matrix before softmax:
Equation (13) from the previous post becomes
The softmax is applied row-wise. After masking, the attention matrix is lower-triangular: row has nonzero entries only in columns .
In code, is typically a large negative constant like , which is numerically equivalent for floating-point softmax.
3. What changes, what doesn’t
The mechanism is unchanged. Queries still come from , keys from , values from ; the dot-product score and the rescaling are exactly as in §5 of the previous post. All that has changed is which scores survive the softmax.
The mask is asymmetric. Position no longer sees positions , but position (with ) still sees position . Information flows strictly forward through the sequence. Each output is now a weighted average over only its own past — exactly the constraint autoregressive training requires.

Parallelism is preserved. All outputs are still computed with the same matrix multiplications, just with a triangular attention pattern instead of a full one. This is what lets decoder-only Transformers train at the same speed as bidirectional ones (no causal mask, so every position sees every other), despite the stricter information constraint.
4. Padding masks
A second, more mundane use of masking comes from batching. Real training batches contain sequences of different lengths. To pack them into a single tensor, shorter sequences are padded with a special token up to the length of the longest. These padding tokens carry no meaning and should not contribute to any output.
The same trick handles them. Build a padding mask where whenever key is a padding token, and otherwise. Add it to the scores before softmax. Only key columns are masked: the rows for padding queries still produce outputs, and the loss simply ignores them. With a causal mask, both masks combine: , which is still wherever either is.
5. Cross-attention
The second extension is cross-attention. In self-attention, the query, key, and value all come from the same sequence — that is what makes it self. Cross-attention relaxes this: queries come from one sequence, keys and values from another.
Concretely, given two sequences — call them with rows and with rows — cross-attention lets every position in attend to every position in . The learned projections from §8 of the previous post split as follows (a cross-attention layer has its own projection matrices, separate from those of the self-attention layer):
The comparison with self-attention has two parts:
- The score matrix changes shape. Stacking ( rows), and and (both rows), the score matrix is instead of the we have seen so far. Each row holds the scores from query position in against every key position in .
- Self-attention is the special case where . When the two sequences are identical, queries, keys, and values all come from the same source, and we are back to (13) plus the learned projections.
In matrix form:
— one output vector per position in , where each output is a weighted blend of the value rows derived from .
Masks carry over with one change. A padding mask over the keys of becomes an matrix; the causal mask is not used, because every position in may read the whole of .
The most prominent use is in encoder–decoder Transformers (next post), where decoder positions attend to the encoder’s output — that is what lets a translation model align “die Katze saß” with “the cat sat.” But the mechanism is general: it shows up wherever a model needs to condition one sequence on another.
Summary
A single formula subsumes all three cases — vanilla self-attention, masked self-attention, and cross-attention:
When , , come from the same sequence and , this is the original (13). When encodes a causal or padding mask, this is masked self-attention. When comes from one sequence and , from another, this is cross-attention. The next post shows how Transformers stack these into encoder-only, decoder-only, and encoder–decoder architectures.
Built on the notation and derivation in From Kernel Regression to Self-Attention. Causal masking and the encoder–decoder attention layer used here are from Vaswani et al., “Attention Is All You Need”, NeurIPS 2017, §3.2. Attention from a decoder over encoder states goes back to Bahdanau, Cho and Bengio, “Neural Machine Translation by Jointly Learning to Align and Translate”, 2014.