Separate padding masks from causal masks
Watch the spaces around tokens and probability values. · 185 words
A padding mask marks artificial positions added only to make batch entries the same length. These positions are not real text, so the model must not use them as evidence. For example, one review says "Great book" and another says "Great book about maps." After tokenization, the short review may receive PAD tokens. A padding mask records 0 for real tokens and 1 for PAD tokens, in one common convention. During attention, PAD columns are blocked. This mask depends on the batch's padded shape, not on word order inside a sequence.
A causal mask has a different job: it prevents a token from seeing later tokens in the same sequence. This is needed when a model predicts the next word from left to right. For [The, cat, sat], the prediction at "The" may use only "The". The prediction at "cat" may use "The cat". The causal mask blocks future positions while leaving earlier positions available. It is normally a fixed triangular pattern. The masks can work together: padding removes non-text positions, while causality removes future text. One follows batch lengths. The other follows sequence order.
Check your recall
In a batch where "Great book" is padded to match "Great book about maps," which mask blocks the PAD positions, and in [The, cat, sat], which mask stops "The" from attending to "cat" and "sat"?
Check your answer
The padding mask blocks the added PAD positions. The causal mask stops "The" from attending to the later tokens "cat" and "sat"; "cat" may attend to "The" but not to a token after it.
Sources and credits
Original KeyTouch lesson. Background reading · Hugging Face · LLM Course.