Unit 2 of 7 · about 3 hours · implement, measure

Causal masks, padding masks and fully masked rows

Masks decide which tokens may speak to which. A leak through a future position or a padding slot silently changes the objective the model optimizes.

You'll be able to

  • Combine causal and padding masks into one score bias and predict each row's valid positions.
  • Implement valid-length masking and state exactly what a fully masked row returns.
  • Prove causal behavior with a future-token intervention test rather than by inspecting weights.

Covers

Causal masks, padding masks and all-masked rows
1

Read

In this order, only the parts listed

0 of 4 done

  1. Masked self-attention and interaction matricesUnderstanding Deep Learning - Transformers

    Section 12.7.2 (pp. 222 to 223): setting dot products to negative infinity before softmax, with Figure 12.12 loss wiring; Section 12.9 with Figure 12.15 (pp. 227 to 228) for encoder versus decoder interaction counts

  2. Masked softmax operationD2L - attention and transformer implementation route

    Section 11.3.2 Masked Softmax Operation: the -1e6 fill, and both runnable examples on (2, 2, 4) inputs with valid lengths [2, 3] and [[1, 3], [2, 4]]

  3. Decoder causal lengthsD2L - attention and transformer implementation route

    TransformerDecoderBlock: the training branch building dec_valid_lens rows [1, 2, ..., num_steps], and the inference branch passing None with cached key_values

  4. Mask semantics, causality and dropoutPyTorch - scaled dot product attention

    The boolean-mask Note (True means participate, inverse of MultiheadAttention key_padding_mask), the is_causal parameter entry including the non-square upper-left bias, and the dropout Warning that dropout applies in every mode

2

Focus on

  • Padding and causal masks compose by intersection: a score survives only if both allow it. D2L valid_lens expresses padding lengths, the decoder rows express causality, and a real batch needs both at once.
  • Mask value conventions differ by API. SDPA bool True means participate while MultiheadAttention key_padding_mask True means mask out. Copying one convention into the other leaks padding.
  • A fully masked row has no agreed meaning. D2L -1e6 gives uniform weights, all -inf through plain softmax gives NaN, and SDPA on CPU returned zeros in version 2.14. Test your convention instead of assuming one.
  • SDPA applies dropout_p in every mode. Pass 0.0 outside training or evaluation outputs stay stochastic.
Common mistakes · 3
  • Trusting uniform weights on a fully masked row. D2L returns there, so padding values still mix into the output unless you zero the row downstream.
  • Passing both attn_mask and is_causal to SDPA. The function raises an error; pick one expression of the same mask.
  • Leaving dropout_p nonzero during evaluation or testing. Outputs differ run to run and differential tests against a manual path fail for no structural reason.
3

Try it yourself

Closed-book, before checking anything

From a blank file, build combined causal plus padding masks for two sequences of valid lengths 3 and 2 over 4 keys, show every disallowed weight is exactly 0, then change a future token and a padded value and show the earlier valid outputs do not move.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.D2L masked_softmax meets a row with valid length 0 over 4 keys. What weights come out and why is that dangerous?Answer

    Uniform weights 0.25 each: all four -1e6 entries softmax equally. The output mixes padding values with full weight, so the row must be zeroed or excluded downstream.

  2. 2.An SDPA bool mask marks padding as True. What happens, and how does MultiheadAttention key_padding_mask differ?Answer

    SDPA treats True as participate, so padding joins attention: a leak. MultiheadAttention key_padding_mask uses the inverse convention, True means mask out. Invert when migrating.

  3. 3.Why is a future-token intervention a stronger causal proof than reading attention weights?Answer

    Weights show one layer; dropout, residuals and later layers remix paths. Perturbing a future token and requiring earlier outputs to stay fixed tests the composed block, including leaks through paths the weight plot never shows.

  4. 4.SDPA is called with dropout_p 0.5 during evaluation. What goes wrong?Answer

    Dropout still applies in every mode, so outputs stay stochastic. Pass 0.0 when the caller is not training.

5

Practice

  • TrenTorch: Causal mask

    Implement build_causal_mask(seq_len) as an additive mask: 0 on and below the diagonal, -inf above it

    Then extend it in your own repository: combine it with padding masks for valid lengths 3 and 2 over 4 keys, and decide what a fully masked row returns before softmax turns it into NaN.

You're done when

Your combined mask gives exact zeros in every disallowed slot, the intervention test passes, and you can state each API mask convention and fully-masked-row behavior from memory.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 2 more in the section
  • Understanding Deep Learning - TransformersSimon J. D. Prince · primary

    Use Chapter 12.1-12.11 and the matching notebooks for dot-product self-attention, extension to transformers, transformer layers and vision transformers.

  • D2L - attention and transformer implementation routeD2L authors · required

    Use queries-keys-values, attention-scoring-functions, multihead-attention, self-attention-and-positional-encoding, transformer and vision-transformer. Implement masked single-head attention, multi-head attention and one pre-normalized block before comparing with PyTorch.

  • PyTorch - scaled dot product attentionPyTorch · reference

    Use the official function documentation as a differential oracle for shapes, masks, dropout and backend behavior after the manual version passes intervention tests.

Also in this section

  • Deep-ML candidate - attention problemsDeep-ML · optional

    Use 490 Scaled Dot-Product Attention as the fifth platform admission sample. If the judge catches a seeded masking error, use at most one of 490, 491, 53, 904 or 905 as a changed-prompt drill.

  • TrenTorch - attention and block questionsTrenTorch · support

    Solve the multi-head split, sinusoidal positional encoding, causal mask and pre-norm versus post-norm questions the units name, then carry the same checks into your own block.