Concepts / Self-Attention in Transformer Language Models

Self-Attention in Transformer Language Models

A causal attention mask is used when an LLM reads and generates text from left to right, to prevent access to future tokens.

  • Programming

Predicting What a Token Can See

Attention is the mechanism that lets a language model decide how much each token should draw on other tokens in the sequence. That flexibility is powerful, but it creates a problem when the model is meant to generate text from left to right. If every token could freely look at every other token, the model could peek at words that have not been produced yet.

What do you think happens?

In the sentence 'The cat sat', while the model is processing the token 'cat' during left-to-right generation, which tokens should 'cat' be allowed to draw on?

  • All three tokens: The, cat, and sat

  • Only the tokens at or before its own position: The and cat

  • Only the token 'sat', because it comes last

Reveal answer
Answer:

Only the tokens at or before its own position: The and cat

The causal attention mask allows a position to use only the tokens at or before its own position. 'sat' lies in the future of 'cat', so its weight from 'cat' is blocked.

Why Left-to-Right Generation Needs Ordering

Some LLMs read and generate text from left to right. The model produces one token, then uses what it has produced so far to produce the next one. At any position, the tokens that come later in the sequence are not yet available to the model in the way a finished sentence would be. The model is building the sequence one step at a time, so the future is genuinely not there to be read.

The causal attention mask exists to enforce this ordering inside the attention computation. Its effect is that a token's attention can only be spread over the tokens at or before its own position. The rule is a constraint on direction: information may flow from the past into the present, but not from the future into the present.

Visibility Grid for Each Query Token

A helpful way to see the causal mask is to draw a grid. Each row is a query token, the token whose attention is being computed. Each column is a token it could attend to. Following the example sentence 'The cat sat', the row for 'The' can see only 'The'. The row for 'cat' can see 'The' and 'cat'. The row for 'sat' can see all three tokens, because 'sat' is the last position and every earlier position lies at or before it.

The to ThevisibleThe to catblockedThe to satblockedcat to Thevisiblecat to catvisiblecat to satblockedsat to Thevisiblesat to catvisiblesat to satvisible
Each row is a query token and each cell is a key position. Cells at or before the query's own position are visible; cells in the future are blocked.

Read the grid row by row and the pattern is clear. Each row includes everything up to and including its own position, and nothing after it. The visible sets are nested: each row sees one more token than the row above it, so the view of the past is complete at every step and the view of the future is always empty.

How the Mask Zeroes Out Future Weights

Attention weights describe how strongly one token attends to each other token. The causal attention mask sets the weights that point toward future positions to zero, so those positions contribute nothing to the result. Positions that are not in the future remain available and keep their weights. Because the masked weights are zero, a blocked future token has no influence on the output for that query.

Before

Query: cat Weight to The: 0.40 Weight to cat: 0.35 Weight to sat: 0.25

All three positions carry weight, including the future position sat.

After

Query: cat Weight to The: 0.40 Weight to cat: 0.35 Weight to sat: 0

The future position sat is zeroed; the current and earlier positions keep their weights.

The 0.40earlier positioncat 0.35current positionsat 0.25future positionThe 0.40earlier positioncat 0.35current positionsat 0future position, blockedunchangedunchangedzeroed
Before masking, the future token sat carries weight from cat. After the causal mask, that future weight is zero while the earlier and current positions remain available.
Worked example

Masking the future for the token cat

The sentence is 'The cat sat'. While computing attention for the token cat during generation, what happens to the weight that cat would place on sat?

  1. 1
    Identify the position of the query

    The query is cat, which is at position 2 of 3.

  2. 2
    Identify which positions are in the future

    sat is at position 3, which lies after cat. It is a future position for this query.

  3. 3
    Apply the causal attention mask

    The mask sets the attention weight from cat to sat to zero.

  4. 4
    Check the remaining positions

    The weights from cat to The and from cat to cat are not in the future, so they stay available.

Result

The weight from cat to sat is zero, so sat contributes nothing to cat's attention output. The text itself is unchanged.

Context Growth During Generation

The same rule explains what the model sees as generation proceeds. At the first step, only the first token exists in the visible context. Once that token is produced, the model can use it to produce the second token. At each new step the visible context grows by exactly one token, and every token already visible remains visible. Earlier steps are never revised by later ones, and later tokens are never visible to earlier steps.

  1. 1
    Step 1: the model can attend only to The, its own position.
  2. 2
    Step 2: the model can attend to The and cat; sat has not been produced yet.
  3. 3
    Step 3: the model can attend to The, cat, and sat, because all three now exist and sat is the current position.

Adding the Dropout Mask

The causal attention mask is not the only mask in the process. A dropout mask is used in addition to causal masking, and its purpose is to reduce overfitting by randomly dropping out attention weights. The source describes the two masks as applied together rather than as alternatives. The dropout mask is layered on top of the causal masking, not used in its place.

Attention weightsstrength of eachtoken-to-token linkCausal attention maskblocks future positionsDropout maskrandomly drops some weightsAttention usedweights after both masksfirstthenresult
The causal mask first determines which future positions are blocked. The dropout mask then operates on the attention weights as an additional step.

The order matters for understanding what each mask is responsible for. The causal mask settles which positions are blocked because they lie in the future. The dropout mask then acts on the weights that remain, randomly removing some of them so the model does not depend too heavily on any single attention pattern. The source frames this as addressing overfitting, which is a concern about the model's reliance on particular patterns rather than about the direction of generation.

Comparing the Causal and Dropout Masks

A useful way to keep the two masks apart is to ask what question each one answers. The causal mask answers which tokens a position is allowed to see, given the direction of generation. The dropout mask answers how to keep the model from relying too heavily on any particular attention pattern. One concerns access and direction; the other concerns overfitting.

AspectCausal attention maskDropout mask
Main question it answersWhich tokens may this position see?How do we keep the model from over-relying on one pattern?
What it affectsSets weights toward future positions to zeroRandomly drops out attention weights
PurposeControls direction of access during left-to-right generationReduces overfitting
Relation to the other maskApplied first to block future positionsApplied in addition, on top of causal masking
Does it change the text?No; it changes which tokens each position may useNo; it changes which weights contribute during the computation

Causal attention mask versus dropout mask

Mistakes When Reading the Masks

  • Common mistake

    Thinking the causal mask deletes future words from the text

    What it looks like

    Assuming that 'sat' disappears from 'The cat sat' when 'cat' is processed.

    Why it fails

    The mask does not change which tokens are present. It changes which tokens each position is permitted to use.

    Do this instead

    Keep the full sequence in mind and ask only which positions a given query may attend to.

  • Common mistake

    Assuming a token can look ahead as long as the sentence is known

    What it looks like

    Believing 'cat' can use 'sat' during left-to-right generation because the full sentence is already written in the training text.

    Why it fails

    The model produces tokens one at a time, and later tokens are not available to earlier positions in the way a finished sentence would be.

    Do this instead

    During generation, a position may use only itself and the tokens before it.

  • Common mistake

    Treating the dropout mask as a replacement for the causal mask

    Why it fails

    The source describes the two masks as applied together. The dropout mask does not decide direction of access.

    Do this instead

    Apply the causal mask to block future positions, then apply the dropout mask on top of it for overfitting.

  • Common mistake

    Confusing the purpose of dropout with the purpose of the causal mask: dropout addresses overfitting, not which tokens are visible.

Checking Your Understanding

EASY

For the sentence 'The model reads text', list which tokens the query 'model' is allowed to attend to under a causal attention mask, and explain which weights the mask sets to zero.

Hints
  • Position the query in the sentence first. Which positions come at or before 'model'?
  • Any position after 'model' lies in the future for that query.
  • Recall that the mask zeroes the weights toward future positions and leaves the others available.
MEDIUM

A colleague says the dropout mask is what stops the model from seeing future tokens. Explain in two or three sentences why this is incorrect, using the difference between the two masks.

Hints
  • Ask which of the two masks the source associates with direction of access.
  • Ask which of the two masks the source associates with overfitting.

Key Takeaways

  • A causal attention mask is used when an LLM reads and generates text from left to right, to prevent access to future tokens.
  • The causal mask works by setting attention weights toward future positions to zero, while earlier and current positions stay available. It changes which tokens a position may use, not which tokens exist in the text.
  • During generation the visible context grows by one token per step, and the view of the past is always complete while the view of the future is empty.
  • A dropout mask is applied in addition to the causal mask. Its purpose is to reduce overfitting by randomly dropping out attention weights.
  • The causal mask controls direction of access, while the dropout mask addresses overfitting. They are layered together, not used as alternatives.

Related concepts

More concepts starting with S