Attention Flows for General Transformers

Niklas Metzger, Christopher Hahn, Julian Siber, Frederik Schmitt, Bernd Finkbeiner

TL;DR

This paper extends Attention Flow—a post-processing interpretability method that models attention weights as maximum flow networks—from encoder-only architectures to general Transformers, including decoder-only (e.g., GPT-2) and encoder-decoder models. By introducing a positional normalization scheme to account for causal masking, the authors prove a formal connection between per-token max-flow and game-theoretic Shapley values, enabling fine-grained attribution and bias analysis across arbitrary Transformer models.

What Problem Does This Paper Address?

Transformer explainability methods often struggle to track how information compounds across multi-head, multi-layer architectures. While raw attention weights provide local hints, they do not measure the cumulative, end-to-end influence of an input token on a final prediction.

Earlier work introduced Attention Flow (Abnar & Zuidema, 2020) for encoder-only models (like BERT) by treating self-attention matrices as directed flow networks and computing maximum flow from input tokens to final representations. However, two major gaps remained:

  1. Architecture limitations: It did not support auto-regressive decoders, where causal masking introduces structural bias toward later tokens (which naturally accumulate more incoming edges).
  2. Theoretical ambiguity: Prior claims linking attention flow to cooperative game theory (Shapley values) relied on incorrect assumptions about network flow independence across tokens.

Methods

The authors formalize attention flow across three architectures and reconcile its game-theoretic properties:

  • Decoder and Encoder-Decoder Flow Networks: The authors build layered directed acyclic graphs representing causal self-attention and cross-attention. For decoder token predictions at position $n$, flow is routed from candidate tokens to the $n$-th prediction embedding.
  • Positional Normalization: Because auto-regressive models give later tokens structurally more incoming capacity, the authors enforce positional independence (ensuring constant attention tensors yield equal attribution) by scaling the max-flow between token $o_m$ and $o_n$ by $\frac{1}{1 + (|O| - (n - m)) - m}$.
  • Game-Theoretic Formulation: Previous literature claimed attention flow directly mirrored Shapley values across subsets of nodes, overlooking downstream edge-capacity competition. The authors fix this: by computing independent max-flows per token and defining coalition value as the sum of these individual flows, the attribution values strictly satisfy the four core Shapley axioms (efficiency, symmetry, null player, additivity).
  • Algorithmic Optimizations: Flows are solved using Edmonds–Karp ($O(VE^2)$ per pair). For long sequences, the authors propose layer skipping/averaging or grouping multi-token subwords to compress graph dimensions without sacrificing explanatory quality.
[Encoder Graph]  ===(Cross-Attention)===► [Causal Decoder Graph]
(All-to-all)                               (Triangular Mask + Normalized)
       │                                                 │
       └────────────────► Edmonds-Karp ◄─────────────────┘
                               │
                    Shapley Value Attribution

Datasets & Models

The authors evaluate their approach on existing models and benchmarks rather than creating new datasets:

  • Sentiment Analysis: RoBERTa fine-tuned on the TweetEval benchmark.
  • Machine Translation: OPUS-MT-EN-DE (English to German) on its test set.
  • Conversational AI & Generation: GPT-2 and DialoGPT-medium (trained on Reddit dialogue).
  • Formal Reasoning: Custom encoder-decoder Transformers trained on propositional satisfiability (PropSAT) and linear temporal logic trace generation (LTLSat).

Key Insights and Ideas

  • Decoupling Coalition Competition: In a network flow, two tokens in the same layer compete for downstream capacity even if they aren't directly linked. Treating coalitions as the additive sum of individual token-to-output max-flows provides a mathematically valid Shapley value while avoiding exponential coalition evaluations.
  • Dissecting Head Specialization: Running the flow network on individual attention heads reveals distinct functional roles. For example, in logical temporal formulas (LTL), individual heads distinctly specialize in either temporal operators ($\mathcal{U}$, $\bigcirc$) or propositions ($a, b$).
  • Root-Cause Analysis of Translation Bias: In translating ambiguous sentences (e.g., "The pilot lost her suitcase"), flow analysis pinpoints the exact heads assigning attention to gendered pronouns versus neutral sentence syntax, revealing where algorithmic gender stereotyping enters the pipeline.

Key Results

  • Detection of Vacuous Logic: On propositional logic tasks ($b \lor (a \land \neg a)$), the network correctly predicts satisfying truth assignments while attention flow assigns near-zero attribution to the unsatisfiable disjunct $(a \land \neg a)$, identifying it as non-influential regardless of position.
  • Adversarial Token Vulnerabilities: In RoBERTa sentiment classification, adding a smiley emoji shifts "John is a good killer" from negative ($0.8949$) to positive ($0.5853$). While total flow still highlighted "killer", single-head flow isolated an individual head routing maximal ($1.0$) flow exclusively to the emoji.
  • First-Token Bias in Decoders: Across 500 samples of the OPUS-MT test set, DialoGPT and GPT-2 displayed a massive attribution bias toward the first input token ($>2\times$ the attention flow of subsequent tokens), reflecting conversational artifacts from Reddit training corpora.
  • Efficiency: Calculating the flow attribution on GPT-2 during interactive text decoding required between $1.38$s and $2.09$s per step.

Limitations and Future Work

  • Ignores Feed-Forward Blocks: While attention weights route information, non-linear Multi-Layer Perceptrons (MLPs) perform significant feature transformation and factual storage, which attention flow cannot account for.
  • No Cross-Model Comparability: Flow numbers are relative within a specific model and layer; their absolute scale cannot be benchmarked across different architectures.
  • Scaling on Long Sequences: While heuristic pruning helps, computing Edmonds–Karp per token pair on very long contexts remains computationally expensive.

Summary updated

BibTeX
@inproceedings{metzger2023attention,
  author = {Niklas Metzger and Christopher Hahn and Julian Siber and Frederik Schmitt and Bernd Finkbeiner},
  title = {{Attention Flows for General Transformers}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=pcBJT4bgbpH},
}
Figure