Core

Causal masking & why order matters

A causal mask prevents next-token training from leaking the answer while allowing every prefix position to be trained in parallel — and it is why decode-time caching works at all.

Updated

01 · Concept

Concept

Here is the trap every next-token trainer must avoid. Training data arrives as complete sequences: the model sees “The cache stores keys” all at once, yet it is being graded on predicting each token from only the ones before it. Self-attention, as built in lessons 4.2 and 4.3, lets every position read every other position. Left unconstrained, the position predicting “stores” would simply attend to “stores” sitting right there in the tensor, copy it forward, and achieve a spectacular training loss that means nothing. The model would learn to cheat, and generation — where the future genuinely does not exist yet — would collapse.

Formally, a causal language model learns the factorization

p(x1,,xn)=i=1np(xix<i),p(x_1,\ldots,x_n)=\prod_{i=1}^{n}p(x_i\mid x_{<i}),

so the computation for position ii may depend on earlier tokens only. Causal masking makes attention obey that factorization while keeping training parallel.

Thecachestoreskeys
The0.250.30.20.25
cache0.30.350.20.15
stores0.150.350.30.2
keys0.10.30.20.4

With no mask, every position attends to every other one — including positions that come after it. Each row sums to 1 across all four.

Thecachestoreskeys
The1maskedmaskedmasked
cache0.460.54maskedmasked
stores0.190.440.37masked
keys0.10.30.20.4

The hatched cells went to negative infinity before the softmax, so they contribute exactly zero — and the surviving weights renormalise over the past alone. Position 1 now attends only to itself; position 4 is unchanged because it had no future to lose.

What the mask removesScores are illustrative, not measured. Every cell above the diagonal is set to negative infinity before the softmax, so it contributes exactly zero afterwards — each row still sums to one, over the past only.

Work it through on a real prompt. Take the four-word input “The cache stores keys” and, for this example, treat each word as one token (a real tokenizer, as lesson 1.2 showed, may split words further — Qwen3.8-27B’s tokenizer draws from a 248,320-entry vocabulary, and the mask applies to its token positions whatever they turn out to be). Number the positions 1–4. The allowed-attention pattern is lower triangular:

[1000110011101111].\begin{bmatrix} 1&0&0&0\\ 1&1&0&0\\ 1&1&1&0\\ 1&1&1&1 \end{bmatrix}.

Row ii is the reading query position; column jj is the key being read. Position 1, “The,” sees only itself. Position 2, “cache,” sees “The” and itself. Position 3, “stores,” sees “The cache stores” — and crucially not “keys,” the very token its hidden state is about to predict. Position 4 sees everything. Implementations realize this with an additive mask: 0 on allowed entries, -\infty on forbidden ones, added to QK/dkQK^\top/\sqrt{d_k} before softmax. Since exp()=0\exp(-\infty)=0, forbidden positions get exactly zero weight — the same exponentiation mechanics from lesson 4.3, used here as an off switch. (In finite precision, libraries use the most negative representable value or a kernel flag; a casually chosen “large negative” constant can be insufficient in low-precision dtypes.)

Now the classic wrong fix, because it is genuinely tempting. “Why mask attention at all? Just compute the loss only from each position’s own prediction — future tokens have their own loss terms, so nothing is stolen.” This masks the grading, not the information. Even with a perfectly per-position loss, unmasked attention lets position 3’s hidden state absorb features of “keys” from position 4’s representation at an earlier layer, or read position 4 directly. The leak is in the activations, not the loss. The correction: constrain the computation graph itself, which is exactly what the triangular mask does.

The mask’s payoff is a rare free lunch: sequential information flow with parallel training computation. All rows of the score matrix are computed in one batched operation, yet row 3 depends only on columns 1–3. An RNN enforces causality by executing in order; a Transformer enforces it by connectivity, and so trains every position of every sequence simultaneously.

The same triangle explains decode. During generation the model holds a prefix, produces one token, appends it, repeats. Because the mask guarantees that position jj‘s key and value are read only by positions j\geq j — never revised by later ones — the per-position K and V computed while processing the prefix are final. They can be stored and reused instead of recomputed at every step. That storage is the KV cache; its full arithmetic for Qwen3.8-27B lands in lesson 7.2. In Qwen the mask story has one twist worth previewing: only its 16 full-attention layers implement causality with this triangular mask, while the other 48 Gated DeltaNet layers are causal by construction, processing tokens as an ordered recurrence (lesson 4.15).

Keep the causal mask distinct from its neighbors. A padding mask hides placeholder positions when different-length sequences share a batch — invalid by length, not by order — and packed-document training may add a boundary mask so one document cannot attend into the previous one; a batch can need all of these at once. And causality is not the same as knowing positions: the triangle says which tokens are earlier, but distinguishing “dog bites man” from “man bites dog” among the visible tokens requires the positional information of lessons 4.6–4.8. Bidirectional encoders like BERT drop the triangle entirely and pay for it by being unable to generate left-to-right.

The durable rule fits in one sentence: the training tensor may contain the future, but no prediction’s computation may. A triangular matrix turns that rule into structure — preserving parallel training, protecting the objective, and quietly making fast decode possible.

02 · Analogy

Analogy

Picture an exam printed as a strip of paper. For question five, a sliding cover reveals only questions one through five and hides every later answer. The teacher can prepare one cover position for every question at once, so all questions are graded in parallel, but no question sees its future. A causal attention mask is that triangular set of covers applied to a score matrix.

03 · Teach it back

Teach it back

Explain how a causal mask permits parallel training without future-token leakage, and why masking only the loss would not be enough.

Minimum: 80 characters and 15 words. Your writing stays only in this browser.

Waiting for your explanation.

Compare with a model answer

Training supplies the whole token sequence, but a triangular causal mask sets attention logits from position i to positions greater than i to negative infinity before softmax, so their weights become exactly zero. Every row of the attention matrix is computed in the same batched operation while receiving information only from its prefix. Masking only the loss fails because even a position with no loss term can write future-token information into hidden states that earlier predictions read through attention; the mask must constrain information flow, not just which positions are graded. At decode time the same property means past keys and values are final and can be cached.

04 · Check your understanding

Check your understanding

01When is the causal mask applied in standard attention?
Answer and explanation

To forbidden logits before softmax — Forbidden logits receive negative infinity or a sufficiently negative representable value so their softmax weights become zero.

02Why does adding negative infinity to a logit zero out its attention weight (lesson 4.3)?
Answer and explanation

Softmax exponentiates logits and exp(−∞) is 0, so the position gets zero normalized weight — Softmax turns each scaled logit s into exp(s) before normalizing; a logit of negative infinity exponentiates to zero and contributes nothing to the row.

03Why can causal language models still train all positions in parallel?
Answer and explanation

The whole triangular score matrix is computed in one batched operation — The mask constrains information flow inside a matrix operation; it does not require a recurrent step for every position during training.

Complete the teach-back and answer the quiz correctly to finish this lesson.

◎ · Evidence marker

Sources

  1. Ashish Vaswani et al. (2017). Attention Is All You Need.
  2. Alec Radford et al. (2019). Language Models are Unsupervised Multitask Learners.
  3. Qwen Team (2026). Qwen3.8-27B Model Card.