4.1.11 · D5Transformer Architecture
Question bank — Masked attention for autoregression
True or false — justify
True or false: The causal mask is a lower-triangular matrix of allowed positions.
True. Position may attend to , so the allowed entries form the lower triangle including the diagonal; the masked (blocked) entries are the strict upper triangle.
True or false: We fill blocked entries with before the softmax.
False. We fill them with so that maps them to exactly ; filling with would leave of weight on future tokens.
True or false: Masking makes the decoder slower than an RNN during training.
False. Masking still computes all rows in parallel in one matrix operation; it only forbids certain columns. RNNs are forced to step sequentially — masking keeps the transformer's parallelism.
True or false: A token is allowed to attend to itself.
True. The diagonal is left open — self-attention gives position its own content and positional signal, which it needs to predict the next token.
True or false: If you removed the mask entirely, training loss would look worse.
False, and that's the danger. Loss would look better because the model copies the visible answer — but the skill is useless at inference where the answer doesn't exist yet.
True or false: The mask changes which tokens exist; masked tokens are deleted from the sequence.
False. Every token still exists and still has a , , . The mask only zeroes certain attention weights, not the tokens themselves.
True or false: The same fixed causal mask shape works for every sequence in a batch of equal length.
True. The causal pattern depends only on positions (), not on content, so one triangular mask is reused. (A separate padding mask handles variable real lengths.)
True or false: Applying a mask after softmax gives the same result as a mask before softmax.
False. Post-softmax zeroing breaks the sum-to-one property; the surviving weights no longer form a probability distribution unless you renormalize — which is exactly what the pre-softmax does for free.
Spot the error
"Row of the attention weights should sum to because only of the columns are active."
Error: every row still sums to . Softmax is computed only over the unmasked entries, so those weights renormalize among themselves — the masked columns contribute , not a shrunken total.
"We should mask the diagonal too, since token hasn't been generated when we predict it."
Error: we predict from position , so position 's own representation is legitimately available. Only strictly-future positions leak the answer. Masking the diagonal would blind a token to itself.
"torch.triu(ones, diagonal=0) builds the correct causal mask."
Error:
diagonal=0 includes the main diagonal, so it would mask a token from attending to itself. You need diagonal=1 (strictly-upper) to block only ."At inference we generate one token at a time, so the mask is pointless and can be dropped from the code."
Error: with KV Caching you still batch a growing key/value set, and any parallel-scored context must respect causality. More importantly the mask must exist during training so the model learns the constraint it faces at inference.
"Since , we can equivalently subtract a huge finite number like with no side effects."
Half-error: works numerically and is common, but it is an approximation. In edge cases (all-masked rows, low precision) it can produce tiny nonzero leakage or NaNs, so it must be handled carefully.
"The mask should be applied after scaling by , otherwise the gets divided and shrinks."
The ordering is right (mask after scaling) but the reason is wrong: is still . We add the mask after scaling simply so the finite scores are already stabilized before the softmax sees them.
"Bidirectional (encoder) attention is just causal attention with the mask applied twice."
Error: an encoder uses no causal mask at all — every position sees every other. Causal masking is the decoder's defining restriction, not a doubled version of the encoder.
Why questions
Why do we prevent attention to the future but not to the past?
The future contains the very tokens we must predict — seeing them is leakage. The past is legitimate context that is genuinely available at inference, so attending backward mirrors the real generation setting.
Why is the correct fill value rather than a "very negative" learned parameter?
We want exactly zero attention on future tokens, independent of scores. Only guarantees for every possible score; a learned value could accidentally let some future weight survive.
Why does masking preserve the model's ability to parallelize training while an RNN cannot?
The mask is a single additive matrix applied to a full score matrix computed at once. There is no sequential dependency in the computation — only in what information each row is allowed to use. RNNs must physically pass a hidden state step by step.
Why does the mask make KV Caching valid at inference?
Because position 's output depends only on keys/values at positions , past vectors never change when new tokens arrive — so they can be stored once and reused, saving recomputation.
Why must the masked model's training set-up match its inference set-up (train–test consistency)?
If training let the model peek ahead, it would learn a copy shortcut absent at inference, producing a train–test mismatch that collapses generation quality. The mask forces training to face the same information limits as generation.
Why doesn't Positional Encoding alone prevent a token from seeing the future?
Positional encoding only labels order; it doesn't restrict the attention weights. Without the mask, a token could still attend with full weight to a later position it can identify as "future."
Edge cases
What happens for the first row ()?
Only column is unmasked, so over a single value gives weight on itself — the first token can attend to nothing but itself, which is correct since it has no past.
What happens for the last row ()?
All columns are unmasked; the final position attends over the entire sequence up to and including itself, giving it the richest context — appropriate since it predicts the token after the whole sequence.
What if an entire row would be masked (e.g. a fully padded position)?
over all- is undefined (NaN). Implementations guard against this by never fully masking a valid query, or by clamping, since such rows carry no meaningful output anyway.
For a sequence of length , what does the causal mask look like?
A matrix with a single (allowed). There is no future to hide, so masking is a no-op and the token attends only to itself.
How does a padding mask differ from the causal mask, and can they coexist?
The causal mask blocks future positions (content-independent, triangular); the padding mask blocks meaningless filler positions (varies per sequence). They are combined by taking the union of blocked entries — both are added as before softmax.
If Teacher Forcing feeds the ground-truth previous tokens during training, why is the mask still needed?
Teacher forcing supplies the correct past; the mask still forbids the future. Without it, teacher forcing would hand the model the whole ground-truth sequence at once, letting it read the answer it's supposed to predict.
Recall One-line summary to seal it
The mask blocks the future, keeps the diagonal, uses before softmax so rows still sum to 1, and exists mainly so training faces the same blindness as inference.