On the Role of Attention Masks and LayerNorm in Transformers

Xinyi Wu, Amir Ajorlou, Yifei Wang, Stefanie Jegelka, Ali Jadbabaie

Abstract

While pure self-attention mechanisms are theoretically prone to "rank collapse"—a state where token representations become identical—modern Transformer architectures remain highly expressive. This paper investigates the architectural components that prevent this decay, specifically focusing on the roles of attention masks and LayerNorm. By modeling Transformers as discrete-time nonlinear dynamical systems and employing a graph-theoretic framework, we demonstrate that attention masks slow the rate of collapse and that LayerNorm, when paired with specific value matrices, enables the existence of stable, high-rank equilibria. Our findings are validated through both mathematical proof and empirical analysis of pretrained models like BERT and GPT-2.

1. Introduction

The Transformer architecture has become the standard for natural language processing, yet its internal dynamics are not fully understood. A significant concern in deep learning theory is "rank collapse" (also known as oversmoothing or token uniformity), where successive layers of self-attention cause token embeddings to converge, reducing the data matrix to a rank of one. While previous studies suggested that Transformers are inherently susceptible to this loss of expressivity, this research proves that the "plumbing" of the architecture—specifically attention masks and LayerNorm—serves as a critical defense mechanism.

2. The Problem: Rank Collapse in Self-Attention

Rank collapse occurs when the representations of different tokens in a sequence become increasingly similar until they are indistinguishable. Mathematically, this means the token matrix $X$ approaches a state where all rows are identical. In a "pure" self-attention model (bidirectional, without normalization), this collapse happens exponentially fast. Previous theoretical models often simplified Transformers by ignoring LayerNorm or using continuous-time approximations, which failed to capture the stabilizing effects of the actual discrete-time implementation used in practice.

3. Methodology: Transformers as Dynamical Systems

To bridge the gap between theory and practice, the authors model the forward pass of a Transformer as a discrete-time nonlinear dynamical system. This approach allows for a rigorous tracking of token similarity $\mu(X)$ across layers.

3.1 Graph-Theoretic Framework

Attention masks are formalized as directed graphs where nodes represent tokens and edges represent attention weights. The researchers introduce the concept of "quasi-strongly connected" graphs and use the "graph radius" to quantify information flow. This framework allows for the analysis of how different masking strategies (e.g., causal, sliding window, or global) impact the convergence of token representations.

3.2 Dynamical Analysis

By analyzing the infinite products of non-negative matrices, the study tracks how the geometry of the token space evolves. The authors also use constructive counterexamples to demonstrate that LayerNorm is not merely a training stabilizer but a fundamental component in maintaining representation rank. They mathematically construct specific value matrices ($W_V$) that allow LayerNorm to maintain full-rank representations indefinitely.

4. Key Insights

4.1 The Protective Power of Local Attention

The analysis reveals that sparse or "local" attention masks (such as sliding windows) act as a shield against rapid rank collapse. The paper proves that the rate of collapse is influenced by the radius of the attention graph; the larger the radius (i.e., the more "local" the attention), the slower the tokens converge. This suggests that restricted attention patterns inherently preserve token diversity longer than global attention.

4.2 LayerNorm and the Repulsion Mechanism

The most significant finding is the interaction between LayerNorm and the value projection matrices ($W_V$). While self-attention naturally acts as an "attractive" force pulling tokens together, LayerNorm can provide a "repulsive" counter-force. By rescaling the embeddings, LayerNorm allows the model to maintain tokens in a state that is both stable and high-rank. Theorem 3 of the study proves that with LayerNorm, a Transformer can support equilibria of any rank, from one to full rank, depending on the weight initialization.

4.3 Anisotropy vs. Rank Collapse

A common observation in pretrained models is anisotropy, where tokens cluster within a narrow cone in the vector space. The authors clarify that anisotropy does not equate to rank collapse. A model can exhibit high token similarity (anisotropy) while maintaining linear independence (full rank). This distinction explains why models like BERT and GPT-2 remain highly functional despite their "crowded" embedding spaces.

5. Empirical Validation

The theoretical predictions were tested against several benchmarks:

  • Numerical Simulations: Adding LayerNorm to randomly initialized models was shown to immediately halt the exponential decay of token similarity.
  • Pretrained Model Analysis: The internal dynamics of BERT, GPT-2, T5, and ALBERT were examined using Wikipedia excerpts. The results confirmed that these models effectively preserve full-rank representations across all layers, validating the theoretical existence of high-rank equilibria in real-world applications.

6. Conclusion and Future Work

This research refutes the myth that Transformers are doomed to rank collapse. It highlights that LayerNorm and attention masks are not just auxiliary components for training stability but are essential for the model's representational capacity. However, the authors note that while high-rank equilibria exist, they may be difficult to reach. Future research is needed to determine how Multi-Layer Perceptrons (MLPs) assist in navigating the model toward these stable regions and whether similar mechanisms exist for purely undirected, bidirectional architectures without causal constraints.

Summary updated

BibTeX
@inproceedings{wu2024role,
  author = {Xinyi Wu and Amir Ajorlou and Yifei Wang and Stefanie Jegelka and Ali Jadbabaie},
  title = {{On the Role of Attention Masks and LayerNorm in Transformers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=lIH6oCdppg},
}
Figure