Part B · Chapter 10 · Attention

10. Masking

A model that writes one word at a time must not peek at the words it has not written yet. A mask hides the future before the softmax, so each word sees only itself and what came before.

The future, masked
Sentence
Playback speed

The grid of scores of one head of the GPT-style model on the chosen sentence: one row for each word that asks, one column for each word it could look at. First every score, as the model would compute it; then every cell to the right of the diagonal, the future, is greyed out and set to −∞; then the softmax runs row by row, the future becomes exactly 0 and each row's weights add up to 1.

The idea

A model that writes text (chapter 1) predicts each word from the words before it. When it is trained, it sees whole sentences at once and practises predicting every position in parallel, which is what makes training fast. But if the word at position 3 could look at position 4 during training, it would simply copy the answer, and at writing time, when position 4 does not exist yet, it would be lost.

The mask forbids that. Before the softmax, every score of a word against a later word is replaced by −∞. e to the power of −∞ is 0, so those words get no weight at all, and the remaining weights still add up to 1. Each word sees only itself and the past. That is masked self-attention.

The paper's decoder uses it, because it writes the German one word at a time; the encoder does not, because the whole English sentence is known from the start. GPT-style models, which only ever write, mask every layer.

A worked example: “The cat sees the dog .”

The GPT-style model reads <s> and the sentence: 7 tokens, so each head computes a 7 × 7 grid of scores. Of those, 21 lie above the diagonal, in the future, and are masked.

Take the row of “sees”. Its scores against <s>, The, cat and sees are −1.06, −3.03, 1.59, 2.69; against the, dog and the full stop, which come later, they would be −0.80, 1.69, −3.27, but they are set to −∞. The softmax of the four visible scores gives 2 %, 0 %, 24 %, 74 %, and 0 to the three future words.

The formula

The mask is a grid M added to the scores before the softmax:

\operatorname{softmax}\!\left(\frac{QK^{\top}}{\sqrt{d_k}} + M\right) V, \qquad M_{ij} = \begin{cases} 0 & j \le i \\ -\infty & j > i \end{cases}
\(i\)
the word that asks: a row of the grid
\(j\)
a word it might look at: a column
\(M_{ij}\)
0 where j is i or before, so the score is kept; −∞ where j comes later
\(e^{-\infty} = 0\)
why a masked word gets no weight, while the visible ones still add up to 1

Why it matters

The mask is why a model can learn from a whole document at once but must write one token at a time. Reading your prompt is one parallel pass over all its tokens; writing the answer is one pass per new token, each waiting for the last. That is why the first word of an answer can take a moment, why the rest streams at a steady pace, and why many price lists charge more for output tokens than for input tokens.

It also bounds what a model can use: it can only look back, never ahead, and only as far as its context window reaches.

Where it fails

  • No second thoughts. A word, once written, is fixed; the mask means the model never sees what it will write next and cannot revise what it wrote before.
  • Writing is sequential. However parallel the training, generation needs one pass per token, which is the main reason long answers are slow.
  • The first words see little. The first position can look only at itself and the start token; early words are predicted from almost nothing, which is why a good prompt matters.
Remember Before the softmax, the scores of later words are set to −∞, so each word attends only to itself and the words before it. That keeps training honest and is why text is generated one token at a time.

Exercises

Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.

How many cells are masked?

A sentence of 6 tokens gives a 6 × 6 grid of scores. How many cells does the mask set to −∞?

Your answer
A whole number

The weight of a masked word

What weight does the softmax give a word whose score was set to −∞?

A row by hand

In the animation's first sentence, “cat” can see <s>, The and itself, with scores 4.53, 1.15, −0.40. What weight does the softmax give <s>? Two decimals.

Your answer
Within 0.01 of the answer

Which attention is masked?

In the paper's encoder–decoder model, which of its three kinds of attention is masked?

In the paper

Section 3.2.3 explains the decoder's self-attention: “We need to prevent leftward information flow in the decoder to preserve the auto-regressive property. We implement this inside of scaled dot-product attention by masking out (setting to −∞) all values in the input of the softmax which correspond to illegal connections.” Figure 2 (left) marks the step as “Mask (opt.)”.

In the paper: section 3.2.3

Open the Theory tab
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.