Skip to content

11.2 Masks: causal, padding, sliding window, additive

Scaled dot-product attention lets every position read every other position, which is exactly wrong for the language models built next. A language model is trained to predict token t+1t + 1 from tokens up to tt. If position tt can read position t+1t + 1, training loss falls to nearly zero by copying, and generation, which has no future to copy, produces noise. In a batch, short sequences are padded, and a query that reads padding learns from garbage. Long contexts in the modern block read only a recent window.

Concepts from other sections (7)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

All three problems have the same solution: a boolean matrix of allowed (query, key) pairs, turned into 00 or −∞-\infty added to the scores. Getting the off-by-ones right here is the difference between a model that trains and one that cheats, and the decode path of the inference chapter reuses the same function with an offset. In this section you will build causal, padding, and sliding-window masks from one convention, combine them, and convert them to the additive form the softmax consumes.

Nothing here calls earlier code. For background it helps to know AND and implication from the discrete math 1 problem set and the fact, from Stable numerics: logsumexp, softmax, and compensated sums in Mathematics, that the stable softmax maps an all-−∞-\infty row to zeros. The full solution is python/tinyllm/xfmr/masks.py (causal_mask, padding_mask, sliding_window_mask, combine, to_additive), its contract is course/contracts/py/tinyllm/xfmr/masks.pyi, and the model oracle tests are in test_masks.py.

Learning objectives

  • Write the causal, padding, and sliding-window rules as predicates on absolute positions, and build each with one broadcast comparison.
  • Use q_offset so that a decode chunk’s mask is exactly the last rows of the full training mask.
  • Combine masks by AND with broadcasting into one [B, 1, T, T] mask for every head.
  • Explain why the additive form must use −∞-\infty and not a large negative number.

These symbols recur through the section:

SymbolMeaningType / shape
TqT_q, TkT_knumber of queries and keysint
ii, jjquery row and key columnint
qoffq_{\text{off}} (q_offset)absolute position of query 0; query ii is at qoff+iq_{\text{off}} + iint ≥0\ge 0
MMa boolean mask, True = may attendbool[Tq, Tk] (or broadcastable)
ℓb\ell_breal length of sequence bb in a padded batchint
ww (window)sliding-window sizeint ≥1\ge 1
AAthe additive mask: 0 where MM is True, −∞-\infty where Falsefloat[Tq, Tk]
SSattention scores QK⊤/dQK^\top/\sqrt{d}float[..., Tq, Tk]

Every mask in the system means the same thing: entry (i,j)(i, j) True means “query ii may read key jj”. This is the convention of torch.nn.functional.scaled_dot_product_attention.

Warning: torch.nn.MultiheadAttention uses the opposite convention (True = blocked). Booleans are the interface here, so the functions reject float or integer masks rather than guessing which convention a 0/1 array meant.

With the convention fixed, the first rule is causality. Position pp may read positions ≤p\le p, itself included, since it needs its own token to predict the next one. With queries at absolute positions qoff+iq_{\text{off}} + i and keys at jj:

Mij=[ j≤qoff+i ].M_{ij} = [\,j \le q_{\text{off}} + i\,].

During training Tq=Tk=TT_q = T_k = T and qoff=0q_{\text{off}} = 0, which gives the lower triangle with the diagonal. During generation with a KV cache, the keys of all TkT_k tokens so far are cached, and a step computes only the last TqT_q queries, at positions Tk−Tq,…,Tk−1T_k - T_q, \ldots, T_k - 1. Then qoff=Tk−Tqq_{\text{off}} = T_k - T_q, and the triangle is aligned to the bottom right, so the last query sees every key. Torch ships both alignments (causal_upper_left, causal_lower_right); here one parameter covers both. The property to test is that the chunk’s mask is exactly the last TqT_q rows of the full mask, so cached and recomputed attention agree.

Because −∞-\infty contributes exact zeros (more on this below), causality holds to the bit: changing future tokens must leave past outputs bitwise unchanged.

A batch stacks sequences of lengths ℓb\ell_b padded to TT. The key-padding mask is Pbt=[t<ℓb]P_{bt} = [t < \ell_b], shape [B, T]. To use it on scores of shape [B, H, Tq, Tk], index it as P[:, None, None, :]: one row per sequence, the same for every head and every query.

Rules compose by AND, with numpy broadcasting. combine(causal_mask(T), P[:, None, None, :]) has shape [B, 1, T, T] and opens (i,j)(i, j) only if j≤ij \le i and j<ℓbj < \ell_b. Padded queries still produce outputs, but the loss ignores them, so they need no mask.

Attention cost grows with TqTkT_q T_k. A sliding window keeps only the last ww keys:

Mij=[ j≤qoff+i ]∧[ (qoff+i)−j<w ].M_{ij} = [\,j \le q_{\text{off}} + i\,] \wedge [\,(q_{\text{off}} + i) - j < w\,].

Each query sees min⁡(w,p+1)\min(w, p + 1) keys, its own included. A stack of LL windowed layers still has a receptive field of about LwL w tokens, which is how Mistral reads long contexts with a 4096-token window. A window of at least TT is the causal mask; a window without the causal half lets queries read w−1w - 1 future tokens.

The softmax takes scores, not booleans, so the last step converts. Add Aij=0A_{ij} = 0 where allowed and −∞-\infty where blocked. Since e−∞=0e^{-\infty} = 0 exactly, blocked keys get weight exactly 0, and allowed scores are untouched (adding 0.00.0 changes nothing, not even the sign of a zero). If every key of a row is blocked, the row is all −∞-\infty, and the stable softmax defines that as zeros.

A “large negative” such as −109-10^9 looks equivalent but is not. In an all-blocked row every entry is −109-10^9, the softmax subtracts the maximum, and the row becomes uniform: the query averages over keys it was forbidden to read. In float16, −109-10^9 is not even representable (it rounds to −∞-\infty anyway). Exactness is also what makes causality bitwise: perturbing a future token changes its score, but the score is masked to −∞-\infty and its value vector is multiplied by an exact 0.

Here is every rule on small sizes. First causal with T=3T = 3, and a decode chunk of 2 queries over 5 keys (qoff=3q_{\text{off}} = 3: queries at positions 3 and 4):

causal_mask(3)=(100110111),causal_mask(2,5,3)=(1111011111).\text{causal\_mask}(3) = \begin{pmatrix} 1&0&0\\1&1&0\\1&1&1 \end{pmatrix}, \qquad \text{causal\_mask}(2, 5, 3) = \begin{pmatrix} 1&1&1&1&0\\1&1&1&1&1 \end{pmatrix}.

The chunk is rows 3 and 4 of causal_mask(5). A window of 2 over 4 positions is a band, where row pp opens p−1p - 1 and pp:

sliding_window_mask(4,4,2)=(1000110001100011).\text{sliding\_window\_mask}(4, 4, 2) = \begin{pmatrix} 1&0&0&0\\1&1&0&0\\0&1&1&0\\0&0&1&1 \end{pmatrix}.

Padding with lengths (3,1)(3, 1) and T=3T = 3 gives rows (1,1,1)(1, 1, 1) and (1,0,0)(1, 0, 0). Combined with the causal mask (padding broadcast over queries), sequence 0 keeps the triangle and sequence 1 opens only key 0 in every row.

Now the additive form on one row: scores (2,1,5)(2, 1, 5) with the third key blocked. A=(0,0,−∞)A = (0, 0, -\infty), the sum is (2,1,−∞)(2, 1, -\infty), and the softmax is (e2,e1,0)/(e2+e1)=(e/(e+1),1/(e+1),0)=(0.731059,0.268941,0)(e^2, e^1, 0)/(e^2 + e^1) = (e/(e + 1), 1/(e + 1), 0) = (0.731059, 0.268941, 0). The blocked key had the largest score and still gets exactly 0. A correct solution reproduces every matrix and this softmax row exactly.

Five functions cover every case:

def causal_mask(Tq: int, Tk: int | None = None, q_offset: int = 0) -> NDArray: ... # bool [Tq, Tk]
def padding_mask(lengths: ArrayLike, T: int) -> NDArray: ... # bool [B, T]
def sliding_window_mask(Tq: int, Tk: int, window: int, q_offset: int = 0) -> NDArray: ...
def combine(*masks: ArrayLike) -> NDArray: ... # AND, broadcast
def to_additive(mask: ArrayLike, dtype=np.float32) -> NDArray: ... # 0 / -inf

Tk defaults to q_offset + Tq. Everything raises ValueError on sizes below 1, negative offsets, lengths outside [0,T][0, T], non-bool masks, shapes that do not broadcast, or a non-float dtype.

The reasoning chain comes first; the code then follows it directly.

  1. Every rule is a predicate on two absolute positions. Query ii is at qoff+iq_{\text{off}} + i, key jj at jj. Writing each rule as a comparison of a column of query positions against a row of key positions makes NumPy broadcasting produce the whole [Tq, Tk] matrix in one expression, with no loops.
  2. Causal is ≤\le, not <<. A position must read its own token. The worked example’s causal_mask(3) has ones on the diagonal.
  3. One offset covers prefill and decode. Using absolute query positions means the chunk mask is literally the last TqT_q rows of the full mask (causal_mask(2, 5, 3) equals rows 3 and 4 of causal_mask(5)). Defaulting Tk to q_offset + Tq means “every key up to the last query”, so cached keys are never cut off.
  4. A window is the causal mask AND a distance bound. Distance p−jp - j ranges over 0..w−10..w-1 for exactly ww keys, so the bound is strict, <w< w. Building it on top of causal_mask guarantees no future key leaks.
  5. Rules compose by AND. A key is readable only if every rule allows it. Broadcasting lets a [T, T] causal mask and a [B, 1, 1, T] padding mask combine into [B, 1, T, T].
  6. The additive form must use −∞-\infty. It is the only value whose exponential is exactly 0 and that keeps an all-blocked row all-blocked after the max is subtracted.
  7. Fail loudly on ambiguous inputs. A 0/1 integer array could mean either convention, so only bool masks are accepted; sizes, offsets, and lengths are validated.
def causal_mask(Tq, Tk=None, q_offset=0):
Tq = _count(Tq, "Tq", 1) # integer >= 1 (bools rejected)
q_offset = _count(q_offset, "q_offset", 0)
Tk = q_offset + Tq if Tk is None else _count(Tk, "Tk", 1) # default: up to the last query
# Query i sits at absolute position q_offset + i; key j at position j.
pos_q = q_offset + np.arange(Tq)[:, None] # [Tq, 1]
pos_k = np.arange(Tk)[None, :] # [1, Tk]
return pos_k <= pos_q # [Tq, Tk], the diagonal included
def padding_mask(lengths, T):
T = _count(T, "T", 1)
n = np.asarray(lengths)
# ... (1-D integers in [0, T]; 0 is an empty slot, allowed)
return np.arange(T)[None, :] < n[:, None] # [B, T], True on the l_b real tokens

The window reuses the causal mask and adds the distance rule:

def sliding_window_mask(Tq, Tk, window, q_offset=0):
window = _count(window, "window", 1)
Tk = _count(Tk, "Tk", 1)
causal = causal_mask(Tq, Tk, q_offset) # never the future
pos_q = q_offset + np.arange(Tq)[:, None]
pos_k = np.arange(Tk)[None, :]
# At most `window` keys, the query's own position included.
return causal & (pos_q - pos_k < window)

For sliding_window_mask(4, 4, 2), row 2 opens keys with 0≤2−j<20 \le 2 - j < 2, that is j∈{1,2}j \in \{1, 2\}: the band from the worked example.

combine checks that the shapes broadcast, then ANDs into an all-True array of the broadcast shape:

def combine(*masks):
if not masks:
raise ValueError("combine needs at least one mask")
arrs = [_bool(m) for m in masks] # bool only: no guessing the convention
try:
shape = np.broadcast_shapes(*(a.shape for a in arrs))
except ValueError as e:
raise ValueError(f"masks do not broadcast: {[a.shape for a in arrs]}") from e
out = np.ones(shape, dtype=bool) # the identity of AND
for a in arrs:
out &= a
return out

And the additive form:

def to_additive(mask, dtype=np.float32):
m = _bool(mask)
dt = np.dtype(dtype)
if dt.kind != "f":
raise ValueError(f"dtype must be a float type, got {dt}")
# -inf, not a large negative number: exp(-inf) is exactly 0, and a row
# with every key blocked stays all -inf, which the stable softmax maps
# to zeros instead of a uniform average over forbidden keys.
return np.where(m, dt.type(0.0), dt.type(-np.inf)).astype(dt)

Building the constants in the target dtype means float16 masks hold a true −∞-\infty, and allowed entries are an exact +0.0+0.0. The full solution is masks.py.

Each mask is O(TqTk)O(T_q T_k) booleans built by one broadcast comparison; nothing is computed per element in Python.

Why absolute positions and one q_offset instead of two functions (upper-left, lower-right)? One formula serves training, prefill, and every decode step, and the row-slice property (chunk = last rows of the full mask) falls out of it. That property is what makes cached and recomputed attention agree in the KV cache.

Why boolean masks as the interface, not additive ones? Booleans compose by AND without arithmetic surprises (−∞+−∞-\infty + -\infty is fine, but 0⋅−∞0 \cdot -\infty is NaN), and kernels can take them as flags. The additive form is produced once, at the end.

Why materialize masks at all? At the sizes in this book a [T, T] bool array is cheap and easy to test. Production kernels (FlashAttention, FlexAttention) never build it and skip whole blocked tiles instead; the rules they implement are exactly these.

The checks a correct solution must pass mirror the rules. The worked matrices pin down the convention, and the additive softmax row (0.731059,0.268941,0)(0.731059, 0.268941, 0) confirms that blocked means weight exactly 0. Against torch, causal_mask must reproduce the upper-left and lower-right causal biases (prefill and decode alignment), and attention through causal, decode-chunk, and padded masks must equal torch’s sdpa. The sliding window must match torch flex_attention’s window of 3, the same rule as the Mistral window in sliding window and sinks.

The properties are the ones argued above: future perturbations leave past outputs bitwise equal; chunk masks are the last rows of the full masks; each window row has min⁡(w,p+1)\min(w, p + 1) keys and a wide window is causal; and combine is AND, order-free, and produces [B, 1, T, T] from [T, T] and [B, 1, 1, T]. At the boundaries, padding must handle lengths 0 and TT (empty slots in a batch), and an all-blocked row must be all −∞-\infty and attend to nothing. to_additive must produce exact 0.0 and −∞-\infty in float16, float32, and float64. causal_mask(2, q_offset=3) must be [2, 5], so cached keys are not dropped. Bad sizes, offsets, lengths, float masks, and int dtypes must raise, so a convention mix-up fails loudly.

The model suite test_masks.py uses the definition written as two nested loops over (i,j)(i, j) as its oracle. causal_mask and sliding_window_mask are compared with it on several shapes and offsets (including a decode step); padding is checked on lengths 0 and TT, combine against &, the exact 00 and −∞-\infty of to_additive (including an all-blocked row), and causality through a few lines of numpy attention. It imports only tinyllm.xfmr.masks.

The common mistakes are almost all off-by-ones or sign flips:

PitfallSymptomHow the solution avoids it
a strict j<ij < i causal maskposition 0 sees nothing; each token cannot see itselfpos_k <= pos_q, the diagonal included
ignoring q_offset (top-left alignment for a decode chunk), or a default Tk = Tqcached decoding differs from recomputationpos_q = q_offset + arange(Tq); Tk defaults to q_offset + Tq
a window of w+1w + 1 keys (≤\le for <<)disagrees with Mistral checkpointspos_q - pos_k < window, strict
a window without the causal halfthe model reads w−1w - 1 future tokenssliding_window_mask starts from causal_mask(...) and ANDs the window
padding open at t=ℓbt = \ell_bevery sequence reads one pad tokenarange(T) < n, strict
a finite “minus infinity”an all-blocked row averages forbidden keysto_additive writes dt.type(-np.inf)
the inverted convention (−∞-\infty where True)attention reads only what it should notnp.where(m, 0.0, -inf): 0 where True
combining by ORpadding re-opened by the causal ruleout &= a for every mask

scaled_dot_product_attention(q, k, v, mask) takes these masks directly (True = attend). Multi-head attention broadcasts one mask over every head, and the encoder-decoder Transformer builds its decoder mask as combine(causal_mask(T), padding). Later, GPT trains under causal_mask(T), the modern block uses sliding-window attention, the KV cache step is causal_mask(Tq, Tk, q_offset=Tk - Tq), and the C kernels for FlashAttention and paged attention take causal, q_offset, and window flags with exactly these meanings. Whisper decoding calls this code through its declared contract.

In production, the same rules appear without materialized masks:

This pieceProduction equivalentWhat it addsWhere to look
causal_mask with q_offsettorch.nn.attention.bias.causal_lower_righta lazy bias the fused kernels recognize, never materializedtorch/nn/attention/bias.py
sliding_window_maskFlexAttention mask_mod + BlockMaskarbitrary rules compiled into block-sparse kernels that skip fully blocked tilestorch/nn/attention/flex_attention.py
to_additiveFlashAttention’s causal and window_size argumentsthe mask is never a tensor; tiles above the diagonal are skippedflash_attn/flash_attn_interface.py
combine with paddingHF transformers masking_utilsone function builds causal, sliding, chunked, and padding masks per layer typesrc/transformers/masking_utils.py
  • Vaswani et al., “Attention Is All You Need” (2017), section 3.2.3 (masking in the decoder).
  • Beltagy, Peters, and Cohan, “Longformer” (2020), and Jiang et al., “Mistral 7B” (2023), section 2 (sliding-window attention).
  • mask: a boolean matrix over (query, key) pairs where True means the query may attend to the key.
  • causal mask: the rule j≤qoff+ij \le q_{\text{off}} + i; each position reads itself and the past.
  • q_offset: the absolute position of the first query, which aligns a decode chunk to the bottom right of the full mask.
  • key-padding mask: [t<ℓb][t < \ell_b] per sequence, blocking padded keys.
  • sliding window: the causal mask AND a distance bound <w< w, at most ww keys per query.
  • additive mask: the float form, 0 where allowed and −∞-\infty where blocked, added to scores before the softmax.
  • A mask is a boolean matrix over (query, key) with True = may attend; rules compose by AND.
  • Causal: query ii at absolute position qoff+iq_{\text{off}} + i sees keys j≤qoff+ij \le q_{\text{off}} + i. One offset turns the training triangle into a decode chunk aligned to the bottom right.
  • Causality is checkable to the bit: changing future tokens must leave past outputs bitwise unchanged.
  • A sliding window is causal AND local: at most window keys, the query’s own included.
  • The additive form is 00 and −∞-\infty, never a big negative number: a fully blocked row must give zero weights, not a uniform average over forbidden keys.
  1. Why is causal_mask(2, 5, 3) equal to the last two rows of causal_mask(5)?
    AnswerBoth compute $[j \le p]$ for the same absolute query positions $p = 3, 4$ and the same keys $0..4$. Using `q_offset + arange(Tq)` as the query positions is what makes a chunk a row slice of the full mask.
  2. Why does the window bound use pos_q - pos_k < window and not <=?
    AnswerTogether with causality the distance $p - j$ is in $\{0, \dots, w-1\}$, exactly $w$ keys including the query's own. `<=` would open $w + 1$ keys and disagree with models trained with a window of $w$.
  3. What goes wrong if combine uses OR?
    AnswerAny rule could re-open what another blocked: the causal rule would open padded keys before the query, and padding would open future keys. A key must satisfy every rule, which is AND.
  4. Why does to_additive refuse an integer dtype?
    AnswerIntegers cannot represent $-\infty$, so the blocked entries would become some finite value and an all-blocked row would turn into a uniform average over forbidden keys.
  5. Why do padded queries not need a mask?
    AnswerTheir outputs are never used: the loss ignores padded target positions. Blocking them would only risk all-blocked rows; what matters is that real queries never read padded keys.