Foundations to Frontier
Tests
F. NLP and Large Language Models › F15 Interpretability of LLMs

Attention analysis and its limits

Attention weights are easy to visualise, so they were the first window into transformers. Researchers found heads that track syntax, coreference, positions and rare words. But attention shows where information is read from, not how much it matters. Weights ignore the size of the vectors moved, are mixed by residual connections and later layers, and often go to 'sink' tokens that carry nothing. Different attention patterns can also give the same output. This page covers head analysis, attention rollout and flow, norm-based and gradient-based attribution, and the 'attention is not explanation' debate. In the exercises you compute attention weights, rollout and entropy. You then analyse a one-head model with sink tokens over 3000 sequences, using leave-one-out importance as the reference. The most-attended token was the most important one only 22% of the time, because 94% of top attention went to sinks. Weighting attention by value size raised this to 75%, and gradient × input to 83%. In every sequence, a very different attention pattern gave the same output.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Where a model looks is not what it uses

Attention maps look like a heat map of what the model is ‘looking at’. That is tempting to read as an explanation. But looking is not using: a model may stare at a full stop that carries no information, the way your eyes rest somewhere while you think. To find what matters, remove a word and see whether the answer changes. The demo shows attention weights over a sentence.

Interpretability lab
Thedronethatthepilotlaunched→ landed
Attention from "landed" to earlier words at temperature 1: the largest weight is on "drone" (54%). High attention shows where information may flow, not that it was used.
Why engineers care

Attention heat maps are often shown in safety reviews of vision and language models; they should be backed by tests that remove or change inputs.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Attention weights show…
  2. 2. A better test of importance is…
  3. 3. Attention sinks are tokens that…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Head studies: BERT heads for syntax, coreference and positions (Clark et al., 2019); many heads are prunable (Michel et al., 2019).
  • Debate: “Attention is not explanation” (Jain & Wallace, 2019): attention often disagrees with gradient and leave-one-out importance, and adversarial attention can give the same prediction; “Attention is not not explanation” (Wiegreffe & Pinter, 2019): it can be informative under conditions.
  • Better attention-based scores: attention × value norm (Kobayashi et al., 2020), attention rollout and flow across layers (Abnar & Zuidema, 2020).
  • Attention sinks: heads put large weight on the first token or delimiters as a no-op (Xiao et al., 2024).
  • Alternatives: gradient × input, integrated gradients, leave-one-out and occlusion, activation patching (F15-5).
  • Use: attention maps generate hypotheses; causal tests confirm them.
📝 Quick check (Level 1)
0/3 answered
  1. 1. 'Attention is not explanation' showed…
  2. 2. Kobayashi et al. weight attention by…
  3. 3. Attention rollout combines…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Analysing attention in practice

Exercise 1.

  • Scaled dot-product attention weights.
  • Attention rollout across layers.
  • Attention entropy.

Practice.

  • Visualise heads with bertviz or circuitsvis; get weights with output_attentions=True (eager attention implementation).
  • Report entropy and sink share per head to spot no-op heads.
  • Compare attention-based scores with leave-one-out or integrated gradients (Captum) on a sample.
  • Prefer value-weighted or rollout scores to raw weights; confirm with interventions.
  • Be careful with flash attention kernels: they do not return weights.
from transformers import AutoModel, AutoTokenizer
tok = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModel.from_pretrained("bert-base-uncased", output_attentions=True, attn_implementation="eager")
out = model(**tok("The drone that the pilot launched landed.", return_tensors="pt"))
A = out.attentions[5][0, 3]          # layer 6, head 4: (tokens × tokens)
print(A[-2].detach().numpy().round(2))
🐍 Exercise 1: weights, rollout and entropy
Compute attention weights, roll attention out across layers and measure entropy.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A tool to visualise attention heads:
  2. 2. Fast attention kernels…
  3. 3. Confirm attention hypotheses with…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

What attention computes. A head outputs Σ_i α_i W_O W_V x_i. The contribution of position i is α_i times a vector whose size can vary by orders of magnitude; a large α_i on a token with a small value vector moves almost nothing.

Non-identifiability. The output depends on attention only through Σ α_i c_i. Any other distribution with the same weighted sum gives the same output: whenever the output lies between the contributions of other tokens, a mixture of just two of them reproduces it.

Mixing across layers. Residual streams and later attention layers mix information, so a token’s position in a deep layer no longer contains only that token. Rollout approximates this mixing but ignores values and nonlinearity.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A head's contribution from position i is…
  2. 2. Different attention can give the same output because the output depends on…
  3. 3. Rollout ignores…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How well do raw attention weights, value-weighted attention and gradient × input agree with leave-one-out importance?

History.

  • K. Clark et al., “What does BERT look at?” (2019).
  • S. Jain and B. Wallace, “Attention is not explanation” (2019); S. Wiegreffe and Y. Pinter, “Attention is not not explanation” (2019).
  • G. Kobayashi et al., “Attention is not only a weight” (2020).

Exercise 2. One attention head with a scalar readout.

  • Tokens: 50 types; each has an attention score s_t ~ N(0, 1) and a contribution c_t ~ N(0, 1). Two sink types have score 3 and contribution near 0; 25% of positions are sinks.
  • Data: 3000 sequences of 10 tokens; output y = Σ α_i c_i.
  • Reference: leave-one-out importance |y − y without token i|.
  • Scores: attention α_i; attention × |value contribution|; gradient × input.
  • Alternative attention: put all weight on two tokens other than the top one whose contributions bracket y.
importance score Spearman with leave-one-out top-1 matches leave-one-out
attention 0.577 0.216
attention × value norm 0.627 0.751
gradient × input 0.886 0.83
  • Sinks received the top attention in 93.7% of sequences, though they change nothing.
  • Value weighting fixed most of the top-1 errors; gradient × input ranked importance best.
  • Non-identifiability: a two-token alternative attention with the same output existed in 100% of sequences, with mean Jensen-Shannon divergence 0.63 bits from the real one.
  • Seeds 1 and 2: attention top-1 0.253 and 0.227; value-weighted 0.703 and 0.703; gradient × input 0.758 and 0.778.
  • Caveat: one head with a linear readout; in deep models, leave-one-out itself is affected by redundancy and gradient × input can saturate.
🐍 Exercise 2 (research): how well do raw attention weights, value-weighted attention and gradient × input agree with leave-one-out importance?
Compare attention weights, value-weighted attention and gradient × input with leave-one-out importance in a head with sink tokens; construct a different attention pattern with the same output.

Attention analysis in AI, robotics and aerospace

  • Vision transformers in perception: attention maps of detectors are shown to operators; backing them with occlusion tests avoids false reassurance.
  • Time-series transformers: attention over sensor channels hints at which inputs matter for fault prediction; validate with ablation.
  • Certification evidence: explanation methods used as evidence must be shown to be faithful.

References

  1. K. Clark, U. Khandelwal, O. Levy and C. Manning (2019). “What does BERT look at? An analysis of BERT’s attention.” BlackboxNLP.
  2. S. Jain and B. C. Wallace (2019). “Attention is not explanation.” NAACL.
  3. G. Kobayashi, T. Kuribayashi, S. Yokoi and K. Inui (2020). “Attention is not only a weight: analyzing transformers with vector norms.” EMNLP.
  4. G. Xiao et al. (2024). “Efficient streaming language models with attention sinks.” ICLR.