Quantifying Attention Flow in Transformers

Samira Abnar, Willem H. Zuidema

Abstract

In deep Transformer models, "raw" attention weights become misleading because information from different tokens gets increasingly mixed as it moves through the layers. This paper introduces Attention Rollout and Attention Flow, two graph-based methods that track how information actually propagates from the input tokens to the final embeddings, providing a much more accurate tool for model interpretability.

The Token Identifiability Problem

Attention visualizations are frequently used to explain Transformer predictions. However, as information moves deeper into a model, embeddings become highly "contextualized." By the 10th layer, a vector representing a specific word has already absorbed information from every other word in the sentence.

If the final layer attends to this vector, it is unclear whether it is attending to the original word or the context it has absorbed. This "token identifiability" problem makes raw attention weights an unreliable probe for explanation in deeper layers, as they only represent relationships between adjacent layers rather than the relationship between the output and the original input tokens.

Methods: Modeling Transformers as Flow Networks

The authors propose modeling the Transformer as a Directed Acyclic Graph (DAG), where nodes are embeddings and edges represent attention weights. Crucially, they account for residual connections by augmenting the attention weights (effectively adding an identity matrix to the attention matrix), acknowledging that information flows both through the attention mechanism and directly from the previous layer.

Comparison of information propagation using raw embedding attentions, attention rollout, and attention flow.

Two primary methods are introduced to calculate the "flow" from the input layer to higher layers:

  1. Attention Rollout: This method assumes that identities of input tokens are linearly combined through the layers. It computes the "input attention" by recursively multiplying the attention matrices of all preceding layers. It is an intuitive, multiplicative approach to tracking proportions of information.
  2. Attention Flow: This treats the attention graph as a flow network. Using a maximum flow algorithm, it computes the maximum possible "value" that can reach an input token (the sink) from a hidden embedding (the source). Unlike Rollout, which assumes a fixed linear combination, Flow finds the most efficient path for information to have traveled.

Experimental Evaluation

The methods were evaluated using two primary benchmarks:

  • Subject-Verb Agreement Dataset: A task where the model predicts if a verb is singular or plural based on a preceding sentence (e.g., "The key to the cabinets is..."). This tests the model's ability to track long-range dependencies while ignoring "attractor" nouns like "cabinets."
  • SST-2 (Stanford Sentiment Treebank): A sentiment analysis dataset used to test a fine-tuned DistilBERT model.

Key Results and Insights

The authors compared their methods against raw attention and ground-truth importance scores calculated via input gradients and blank-out ablation.

Heatmap visualizations comparing the attribution of different attention methods across layers.
  • Higher Correlation: In the final layer (L6) of the experimental model, raw attention had a poor correlation (~0.29) with token importance. In contrast, Attention Rollout (~0.71) and Attention Flow (~0.70) showed significantly higher alignment with the tokens that actually drove the model's prediction.
  • The Uniformity Trap: In higher layers, raw attention weights often appear uniform or random. This is not due to model confusion, but because embeddings are so mixed that the model can find necessary information almost anywhere. Rollout and Flow successfully "track back" this information to the correct input tokens.
  • Rollout vs. Flow: Attention Rollout tends to produce "sharper," more focused attention maps, while Attention Flow is more "amortized," distributing importance across a set of relevant tokens. The authors find that the "relaxation" provided by the flow algorithm often makes it a more reliable diagnostic tool.

Conclusion

Quantifying information flow in Transformers requires accounting for the "highway" of residual connections. By moving beyond raw attention weights to graph-based flow methods, researchers can gain a much more accurate understanding of how Transformers utilize input tokens in their final decision-making processes.

Summary updated

BibTeX
@inproceedings{abnar2020quantifying,
  author = {Samira Abnar and Willem H. Zuidema},
  title = {{Quantifying Attention Flow in Transformers}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2020},
  doi = {10.18653/v1/2020.acl-main.385},
}
Figure