← Applied intuition Interview Insights
This one went deeper than I was ready for.
Start by clarifying the requirements: autoregressive decoding, variable-length sequences with padding, and the need for both causal and padding masks. Then describe how to construct each mask, combine them via logical OR (or addition with -inf), and apply them to attention scores. Finally, discuss common bugs, their symptoms, and a testing strategy that includes unit tests and gradient checks.
Pro tip: Emphasize that padding masks must be applied to both keys and queries to avoid attending to padding tokens, and that using -inf before softmax is safer than multiplying by 0 to prevent NaNs. Also, mention that mask shapes must broadcast correctly across batch and head dimensions.
Confirm that the model is autoregressive (decoder-only), sequences are padded to the same length within a batch, and padding tokens should be ignored in attention. Ask about mask dtype (boolean vs. additive) and whether the implementation uses PyTorch, TensorFlow, or custom code.
For causal mask, create a lower-triangular matrix of shape (seq_len, seq_len) with True/1 for allowed positions. For padding mask, create a mask of shape (batch_size, seq_len) where True/1 indicates valid tokens (or False/0 for padding), then expand to (batch_size, 1, 1, seq_len) for broadcasting.
Combine causal and padding masks using logical OR (if boolean) or addition (if additive with -inf). Apply the combined mask to attention scores before softmax: scores = scores.masked_fill(mask == 0, -inf) or scores + mask. Ensure the mask broadcasts correctly across batch and head dimensions.
Describe bugs like: attending to future tokens (causal mask not applied), attending to padding tokens (padding mask missing or misaligned), NaNs from softmax on all -inf rows, and incorrect broadcasting leading to shape errors. Symptoms include degraded generation quality, loss not decreasing, or NaN losses.
Propose unit tests: check mask shapes, verify that masked positions have zero attention weight after softmax, test with variable-length sequences and padding, and use gradient checks to ensure no gradients flow to padding tokens. Also, test edge cases like sequence length 1 and all-padding sequences.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.