Attention rollout: following attention through the layers
One layer's attention weights say little about what a deep transformer used. Attention rollout multiplies residual-adjusted attention matrices through the layers. Learn to compute it, and why even rollout is mixing, not explanation.
What runs in CodeSplash
- EXP015Attention rollout Partly runs in CodeSplash (Experimental)
- FT-Transformer Experimental
You will be able to
- Compute attention rollout from per-layer attention matrices with the residual adjustment
- Explain why a single layer's attention is a poor guide in a deep model
- Say what attention ignores (values, MLPs, normalization) and how to corroborate rollout
Attention is tempting
ReadA transformer's attention weights look like an explanation already: for each position, a row of numbers summing to one, saying how much it "looks at" every other position. Plot the last layer's weights from the class token and you get a ready-made importance chart.
But in a deep model, by the last layer each position already mixes information from every other through the earlier layers, plus residual connections that carry each position's own value forward. The last layer's attention describes the final mix of already-mixed tokens.
See one layer's attention scores, masks and heads, the raw material rollout multiplies:
DExplorable · Attention: scores, masks and heads
Where does each word look, and what do the scaling and the masks change?
With this mask, how much of each word's attention lands on <pad>?
How many of the positions are <pad>.
Width d
Heads
Mask
Without masks or positions the output rows shuffle the same way.
- Little weight
- Most weight
- Masked: score set to −∞
Commit your prediction above to see the result
Make your prediction to see the result.
Rollout: multiply through the layers
ReadAttention rolloutAttention rolloutMultiplies a transformer's attention maps across blocks (with half the weight on the skip path) to see which inputs the output token reads. follows the mixing back to the input tokens. Average the heads in each layer, add the identity for the residual connection, renormalize each row, then multiply in layer order:
Row of estimates how much of position 's final representation traces back to each input position, if mixing were the whole story.
Reference · EXP015 · Partly runs in CodeSplashAttention rolloutCombine attention matrices across layers to approximate how token mixing propagates from input tokens to later representations.By hand: two layers, two tokens
ReadThe reference's example uses two tokens and two layers with attention then . After the residual adjustment and the product, the first token's row of the rollout is 0.75 and 0.25; the second token's is 0.1875 and 0.8125. Each row still sums to one, a quick check worth keeping in any implementation, along with masked positions getting zero.
Predict: high attention, no effect
PredictPredict first
A feature token gets high rollout weight from the class token, but removing that feature hardly changes the prediction. Is that possible?
Reveal
Yes. Attention weights how values are mixed; if that token's value vector carries little, or later MLPs ignore it, high attention has little effect. Redundant features also substitute for each other. That's why rollout needs corroboration by interventions: ablations, gradient attributions or activation patching.
What rollout leaves out
Read- Values and MLPs. Rollout uses only attention weights. Value projections, feed-forward layers, normalization and the output head do much of the computation.
- Head averaging. Averaging heads erases specialized heads that do the real work.
- Direction conventions. Rows versus columns, and how masks and the residual are handled, change the result; verify on tiny matrices.
- Not an attribution. Rollout doesn't add up to the prediction and isn't signed. Present it beside gradient or Shapley attributions, never as interchangeable with them.
Attention in CodeSplash
ReadFT-Transformer turns every feature into a token, adds a [CLS] token and reports the attention [CLS] pays to each feature in its last block as a rough importance. On the Predict Explain tab, the explainer Attention rollout follows that attention through every block with the residual adjustment, and Integrated Gradients gives a signed attribution to compare it with.
Where it stands in CodeSplash
Read| Entry | In CodeSplash |
|---|---|
| Attention rollout (EXP015) | Partly runs in CodeSplash: the Attention rollout explainer on the Predict Explain tab for FT-Transformer runs; no Analyze method, and no rollout for language models |
Check your understanding
CheckpointCheck yourself
Why does attention rollout add the identity matrix to each layer's attention before multiplying?
Show the answer
Answer: b
Without the identity, rollout would ignore the path through the residual stream, which carries much of each token's information.
Check yourself
Why is the last layer's attention alone a poor guide to which input tokens mattered?
Show the answer
Answer: b
Rollout traces the mixing back through all layers, though it still ignores values and MLPs.
Check yourself
How should you check whether a token with high rollout weight actually matters?
Show the answer
Answer: b
Rollout describes mixing. Interventions measure effect.
Recap
Recap- One layer's attention describes mixing of already-mixed tokens.
- Rollout multiplies residual-adjusted, row-normalized attention through the layers.
- It ignores values, MLPs and normalization, and isn't signed or additive.
- Corroborate with interventions or attributions; CodeSplash offers it for FT-Transformer on the Explain tab.
Concepts in this lesson