Skip to content

Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP015 · Diagnostic

Attention rollout

Combine attention matrices across layers to approximate how token mixing propagates from input tokens to later representations.

Partly runs in CodeSplash (Experimental) Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method covers part of this entry; some options or variants aren't built yet. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Partly runs in CodeSplash (Experimental)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through learn.ft_transformer. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Combine attention matrices across layers to approximate how token mixing propagates from input tokens to later representations.

Question / hypotheses

Under a simplified attention-as-mixing interpretation, which input positions receive weight through multiple attention layers?

Architecture / computational structure

Attention heads are aggregated under a chosen rule. Residual identity connections are added and rows normalized. Multiplying the adjusted attention matrices in layer order yields a rollout matrix connecting later positions to earlier input positions.

Assumptions

The attention matrices, direction convention and residual handling must be known. Attention is only one part of a transformer: value projections, MLPs, normalization and output heads also matter. Rollout is a descriptive approximation rather than a complete attribution of the output.

Formula / computational definition

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

Parameters and notation

The original attention tensors are learned-model states, not newly fitted explanation parameters. Choices include head aggregation, residual weighting, layer range, token of interest, padding/causal masks and visualization normalization. Matrix multiplication order depends on the declared row-source/column-target convention. Here A^(l) is a head-aggregated token-attention matrix at layer l, I the token identity matrix, and R^(L) the accumulated mixing from input to layer L under the stated multiplication convention.

Fitting, fine-tuning, and adaptation

Recompute after adaptation and preserve attention extraction settings. A model with a different architecture or residual scheme needs a corresponding definition. Do not compare heatmaps from different tokenizations as if token positions were identical.

How to use and calibrate

Verify row normalization, mask handling and identity cases on tiny matrices. Corroborate with controlled input or activation interventions. Present attention-path visualization separately from gradient or Shapley attributions rather than treating them as interchangeable.

Example application

Trace a classification token’s approximate attention paths across two layers and compare with a single-layer attention display.

Interpretation / pitfalls

Watch out

High attention weight does not establish causal importance. Layer/head averaging can erase specialized behavior. A graph of mixing weights omits value content and subsequent nonlinear computation; appealing token highlights can therefore mislead.

Python and R code examples

Scope: Complete two-layer residual-adjusted rollout from supplied matrices. Python: NumPy; R: base R.

Validation: Python executed successfully on the supplied fixture; R reviewed but unexecuted. Full implementations and supported operation details are provided in the linked primary/official sources.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Complete two-layer residual-adjusted rollout from supplied matrices. Python: NumPy; R: base R. · needs numpy

import numpy as np
A=np.array([[.8,.2],[.3,.7]]);B=np.array([[.6,.4],[.1,.9]])
adjust=lambda M:(M+np.eye(len(M)))/(M+np.eye(len(M))).sum(1,keepdims=True)
R=adjust(B)@adjust(A);assert np.allclose(R.sum(1),1);print(R)
R

Component demo, not a trained model — Complete two-layer residual-adjusted rollout from supplied matrices. Python: NumPy; R: base R.

A<-rbind(c(.8,.2),c(.3,.7));B<-rbind(c(.6,.4),c(.1,.9))
adjust<-function(M) {Q<-M+diag(nrow(M));Q/rowSums(Q)}
R<-adjust(B)%*%adjust(A);stopifnot(all(abs(rowSums(R)-1)<1e-12));print(R)

Sources

From the CodeSplash reference, entry EXP015.