Analyzing Feed-Forward Blocks in Transformers through the Lens of Attention Maps

Goro Kobayashi, Tatsuki Kuribayashi, Sho Yokoi, Kentaro Inui

Introduction

While attention mechanisms usually steal the spotlight in Transformer interpretability, this research brings the parameter-heavy Feed-Forward (FF) blocks into the fold. In the quest for "mechanistic interpretability"—understanding exactly how a model processes information—researchers have leaned heavily on attention maps. These grids show how much each token in a sentence influences every other token. However, these maps typically ignore the FF blocks, which actually contain about two-thirds of the parameters in models like BERT or GPT.

The challenge is that FF blocks are non-linear and operate on tokens independently. On the surface, it seems like they don't "mix" information between tokens. However, because the input to an FF block is already a mixture of tokens (thanks to the preceding attention layer), the FF block can effectively change the "weight" of those tokens. Until now, there wasn't a mathematically sound way to include these non-linear transformations in the same "norm-based" attention maps used for the rest of the architecture. By using Integrated Gradients to "linearize" non-linear activations, the authors visualize how FF blocks amplify specific linguistic patterns—only to find that surrounding residual and normalization layers often "cancel out" these effects.

Methodology: Expanding the Attention Map

To handle the non-linear activation function (like GELU) inside the FF block, the authors extend norm-based analysis using Integrated Gradients (IG). IG allows the authors to attribute the output of a non-linear function back to its inputs. By treating the FF block as a sum of these attributed contributions, they can calculate a "refined" attention map that covers the entire Transformer layer, not just the attention mechanism.

Transformer layer architectures and analysis scopes

The study compares four different "windows" or scopes of the model to isolate component effects:

  1. ATB: The Attention Block alone.
  2. ATBFF: The Attention Block plus the Feed-Forward network.
  3. ATBFFRES: The Attention Block, FF network, and the residual connection.
  4. ATBFFRESLN: The full layer including Layer Normalization.

This methodology was evaluated across 11 variants of masked language models (BERT, RoBERTa) and two causal language models (GPT-2, OPT) using Wikipedia excerpts and the Stanford Sentiment Treebank (SST-2).

Key Insights: Amplification and Cancellation

The expanded attention maps reveal several surprising dynamics within the Transformer layer.

FF Blocks as Linguistic Amplifiers: FF networks actively modify contextualization rather than just acting as static knowledge memories. They tend to amplify specific linguistic compositions, such as merging subwords into words (e.g., "##night" + "week") or identifying compound nouns ("opera" + "soap"). In BERT, FF blocks in the 3rd and 9th–11th layers have the highest impact on token relationships, whereas in GPT-2, the impact is strongest in earlier layers.

The "Cancellation" Phenomenon: Perhaps the most striking finding is the internal redundancy of the layer. While the FF block might drastically change the attention map, the subsequent Residual Connection and Layer Normalization often undo that work.

Vector additions illustrating the cancellation effect

The vectors bypassed via the residual connection are often more than twice as large as the output vectors from the FF block. Consequently, the residual connection adds back a massive original vector, and the Layer Norm squashes the changes. The fact that the model's components often work at cross-purposes suggests significant redundancy in the Transformer architecture, supporting recent trends in model pruning and layer dropping.

Communication via Outliers: The authors found that FF blocks "suggest" changes by producing high-magnitude values in specific dimensions (outliers). Layer Normalization acts as a gatekeeper, specifically shrinking these outlier dimensions to maintain stability. There is a strong negative correlation (between -0.45 and -0.78) between Layer Norm weights and the magnitude of FF outputs, confirming that LN systematically suppresses the "loudest" signals from the FF block.

Limitations and Future Directions

While this method provides a breakthrough in visualizing FF blocks, using Integrated Gradients is computationally expensive. The time required to generate these maps scales sharply with model size and sequence length, making it difficult to apply to massive Large Language Models (LLMs) without further optimization. Furthermore, the study focuses on how FF blocks modify existing mixtures of tokens, leaving room to explore how they might generate entirely new information (the bias term). Future work may apply this lens to Mixture-of-Experts (MoE) models and investigate inter-layer dynamics to see how these localized cancellations propagate through the entire depth of the network.

Summary updated

BibTeX
@inproceedings{kobayashi2024analyzing,
  author = {Goro Kobayashi and Tatsuki Kuribayashi and Sho Yokoi and Kentaro Inui},
  title = {{Analyzing Feed-Forward Blocks in Transformers through the Lens of Attention Maps}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=mYWsyTuiRp},
}
Figure