Skip to content

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.

Advanced ~12 min9 steps

What runs in CodeSplash

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

Read first

01

Attention is tempting

Read

A 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 sure?
6
2

How many of the positions are <pad>.

Width d

Heads

Mask

Without masks or positions the output rows shuffle the same way.

.51.22.16.11.00.00.27.55.07.10.00.00.02.10.59.28.00.00.26.71.02.01.00.00.09.09.69.13.00.00.42.54.03.01.00.00thecatsaton<pad><pad>thecatsaton<pad><pad>
  • Little weight
  • Most weight
  • Masked: score set to −∞

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

02

Rollout: multiply through the layers

Read

Attention 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:

A~(l)=RowNormalize(A(l)+I),R(L)=A~(L) A~(L−1)⋯A~(1).\widetilde A^{(l)}=\mathrm{RowNormalize}\big(A^{(l)}+I\big),\qquad R^{(L)}=\widetilde A^{(L)}\,\widetilde A^{(L-1)}\cdots\widetilde A^{(1)} .

Row ii of R(L)R^{(L)} estimates how much of position ii'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.
03

By hand: two layers, two tokens

Read

The reference's example uses two tokens and two layers with attention A=(0.80.20.30.7)A=\begin{pmatrix}0.8&0.2\\0.3&0.7\end{pmatrix} then B=(0.60.40.10.9)B=\begin{pmatrix}0.6&0.4\\0.1&0.9\end{pmatrix}. 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.

04

Predict: high attention, no effect

Predict

Predict first

A feature token gets high rollout weight from the class token, but removing that feature hardly changes the prediction. Is that possible?

It must matter: the model attends to itIt may barely matter: attention weights mix values, and the value may carry little
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.

05

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.
06

Attention in CodeSplash

Read

FT-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.

07

Where it stands in CodeSplash

Read
EntryIn 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
08

Check your understanding

Checkpoint

Check 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.

09

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