A language model has one job: given the tokens so far, predict the next
one. That job comes with a non-negotiable rule of fair play — when the model scores its
guess for position
Recall the
Step 1 — start from the raw scores. Entry
Position
Step 2 — build the causal mask. Define an
Step 3 — add the mask to the scores. Adding
Step 4 — softmax kills the future exactly. Apply the
Step 5 — read off the shape. Every weight above the diagonal is zero, so the
attention matrix
Position
Here is the payoff that made the transformer a training rocket. A naïve autoregressive model
would have to generate token by token even during training, one slow step at a time. But with
the mask in place, position
The mask is a design knob, and which way you set it decides what kind of model you are building.
No mask (bidirectional). Every position attends to every other, future
included. The model sees the whole context at once, which is ideal for
understanding a fixed input — filling in a blanked-out word needs the words on
both sides. This is the encoder reading of a sentence. What it cannot do is
generate: if position
Causal mask (left-to-right). Each position sees only itself and the past,
so "predict the next token" is a genuine, leak-free task at every position simultaneously.
This is the decoder reading, and the foundation of every
It is tempting to treat "transformer" and "causal mask" as synonyms — after all, the famous generative language models are decoder-only stacks built entirely from causally-masked attention. But causal masking is a requirement for one job, not a property every transformer must carry. It is needed only when a model must not cheat by peeking at tokens it hasn't generated yet — that is, when it produces text left-to-right, one token at a time.
An encoder that processes a whole, fixed input all at once — reading a
sentence to translate, or a paragraph to classify — has no such restriction. Every input
token is already sitting there in full; there is no "future" to leak. Encoders therefore
deliberately leave attention unmasked, letting every position attend to every other
position in both directions. Assuming every transformer, encoder included, must be causally
masked is a common mix-up. See
Each cell is one attention weight