GlobEnc: Quantifying Global Token Attribution by Incorporating the Whole Encoder Layer in Transformers

Ali Modarressi, Mohsen Fayyaz, Yadollah Yaghoobzadeh, Mohammad Taher Pilehvar

Abstract

Transformers have revolutionized natural language processing, yet their internal mechanisms often remain opaque. While attention weights are commonly used to interpret model behavior, they frequently provide a misleading picture by ignoring the impact of residual connections and layer normalization. We introduce GlobEnc (Global Encoder attribution), a novel method that provides a faithful and computationally efficient "global" map of token importance. By incorporating the entire encoder layer—including residual connections and the final layer normalization—GlobEnc significantly improves the correlation between attribution scores and gradient-based saliency measures.

Introduction

The interpretability of Transformer models is a critical area of research. A common practice is to use attention weights to explain which input tokens drive a model's prediction. However, recent literature has highlighted significant flaws in this approach. Raw attention weights ignore the magnitude of the transformed vectors and the substantial role of residual connections, which allow information to bypass the attention mechanism entirely.

Furthermore, most interpretability techniques are "local," focusing on individual layers. To understand a model's final output, we require a "global" perspective that aggregates information across the entire network. Existing methods like "Attention Rollout" attempt this but are often hampered by their reliance on unreliable raw attention weights.

The GlobEnc Method

GlobEnc addresses these limitations by moving beyond simple attention weights to a comprehensive analysis of the encoder block. The method is built on three primary pillars:

  1. Norm-based Attribution: Instead of raw weights, GlobEnc utilizes the norm (length) of the vectors produced by attention heads, capturing the actual magnitude of the data transformation.
  2. Whole Encoder Integration: Unlike previous methods that stop at the attention block, GlobEnc incorporates the residual connections (which preserve token identity) and the second Layer Normalization (LN#2) at the end of the encoder block.
  3. Simplified Global Rollout: GlobEnc uses a modified "rollout" technique to aggregate layer-wise scores. Because the layer-wise scores already account for residual connections, the aggregation process becomes more accurate and mathematically grounded.
Architecture of the Transformer encoder block and the scopes of different attribution methods.

As illustrated in the figure above, GlobEnc (represented by the scope $\mathcal{N}_{ENC}$) encompasses the entire encoder structure, whereas traditional methods often focus only on the attention mechanism ($\mathcal{N}$).

Experimental Evaluation

The authors evaluated GlobEnc using BERT-base, BERT-large, and ELECTRA models across three diverse classification tasks:

  • SST2: Sentiment analysis.
  • MNLI: Multi-Genre Natural Language Inference.
  • HateXplain: Hate speech detection.

The performance of GlobEnc was benchmarked against gradient-based saliency scores (HTA), which serve as the "gold standard" for attribution faithfulness but are computationally prohibitive for large-scale use.

Key Results and Insights

GlobEnc demonstrates a remarkably high correlation with gradient-based saliency. On the SST2 dataset using BERT-base, GlobEnc achieved a Spearman’s rank correlation of 0.77, a stark contrast to the -0.11 observed for raw attention weights.

Spearman rank correlation between various attribution methods and gradient-based saliency across layers.

The figure above highlights how incorporating the full encoder (N-Enc) consistently outperforms weight-based (W) or partial norm-based (N, N-Res) methods as we move deeper into the model.

The Role of Layer Normalization and Residuals

A significant finding of this work is the "tug-of-war" between the two Layer Normalization steps. Outlier weights introduced in the first LN are often balanced by the second. Methods that omit the second LN produce noisy attribution maps. Additionally, GlobEnc correctly identifies that in early layers, tokens primarily "attend to themselves" via residual connections, preserving their identity before significant mixing occurs in later layers.

Efficiency and Qualitative Faithfulness

GlobEnc offers a massive leap in efficiency. While gradient-based HTA required approximately 2 hours to process a small sample, GlobEnc achieved comparable results in just 5 seconds. Qualitatively, GlobEnc visualizations show that BERT often identifies "decisive" tokens (e.g., "meaningful" or "waste") within the first few layers, with subsequent layers acting as a refinement phase rather than a discovery phase.

Conclusion

GlobEnc provides a robust framework for understanding token attribution in Transformer encoders. By accounting for the entire encoder block, it offers a faithful alternative to raw attention weights and a scalable alternative to gradient-based methods. Future work will focus on extending this methodology to non-linear Feed-Forward Networks (FFN) and broader architectures such as decoder-only models.

Summary updated

BibTeX
@inproceedings{modarressi2022globenc,
  author = {Ali Modarressi and Mohsen Fayyaz and Yadollah Yaghoobzadeh and Mohammad Taher Pilehvar},
  title = {{GlobEnc: Quantifying Global Token Attribution by Incorporating the Whole Encoder Layer in Transformers}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2022},
  doi = {10.18653/v1/2022.naacl-main.19},
}
Figure