On the Role of Attention Masks and LayerNorm in Transformers

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

TL;DR

This paper mathematically refutes the common belief that Layer Normalization (LayerNorm) plays no role in preventing token rank collapse in deep Transformers. By analyzing discrete-time self-attention dynamics with attention masks and LayerNorm, the authors prove that local attention masks slow down rank collapse, and LayerNorm—when paired with appropriate value matrices—can completely prevent it, sustaining full-rank, anisotropic representations.

What Problem Does This Paper Address?

As Transformer models grow deeper, they face a subtle structural threat: rank collapse (also known as token uniformity or oversmoothing). As tokens pass through successive pure self-attention layers, their representations tend to become increasingly homogeneous, eventually collapsing into a rank-one subspace where all tokens look identical. This severely limits the model's expressivity and its ability to utilize deep architectures effectively.

Previous theoretical studies concluded that architectural additions like LayerNorm do nothing to stop this collapse. However, those studies relied on highly restrictive assumptions, such as treating attention as fully bidirectional, analyzing it in continuous time, or assuming the value matrix ($W_V$) is simply the identity matrix. This paper bridges the gap between theory and practice by analyzing how realistic attention masks (like causal or sparse masks) and LayerNorm affect rank collapse in realistic, discrete-time settings.

Methods

The authors model self-attention as a discrete-time non-linear dynamical system, which closely mirrors actual implementation. Their methodology relies on two main pillars:

  1. Graph-Theoretic Formalization: They treat attention masks as directed graphs ($G$). By defining structural properties like quasi-strong connectivity and graph radius, they mathematically track how information flows between tokens.
  2. Matrix Product Theory: They leverage advanced linear algebra tools concerning the infinite products of inhomogeneous non-negative matrices and their ergodicity to bound the convergence rates of token representations.

Through these tools, they construct rigorous proofs for token dynamics both with and without LayerNorm, culminating in the design of non-trivial counterexamples that map out stable, full-rank equilibria.

Datasets

To validate their theoretical bounds, the authors ran controlled experiments using:

  • Wikipedia: 3,000 randomly selected samples of 128-token excerpts.
  • Pretrained Models: Token geometries were extracted and evaluated from standard implementations of BERT, GPT2, T5, and ALBERT via the Hugging Face Transformers library.

Key Insights and Ideas

  • Local Attention as a Speed Brake: Sparser or local attention masks (like sliding windows) increase the "graph radius" of token interactions. The math reveals that a larger radius directly slows down the exponential rate of rank collapse, explaining why local attention helps preserve expressivity at finite depths.
  • The Value Matrix is the Key to LayerNorm: LayerNorm can stop rank collapse, completely overturning prior assumptions. The trick lies in the value matrix ($W_V$). If $W_V$ is orthogonal, tokens still collapse. However, if $W_V$ is properly selected (non-orthogonal), LayerNorm introduces a "repulsion" component that perfectly counteracts the pooling effect of attention.
  • Explaining Anisotropy: The stable, full-rank equilibria discovered by the authors force tokens to preserve their distinctiveness while clustering within a narrow geometric region. This provides a elegant theoretical explanation for anisotropy—the empirical observation that LLM token embeddings in practice are full-rank yet highly directional and concentrated.

Key Results

  • Theorem 1 (Pure Attention): Establishes that for any quasi-strongly connected mask, pure self-attention collapses exponentially to a rank-one subspace at a rate of $(1 - \epsilon^r)^{t/r}$, where $r$ is the graph radius.
  • Theorem 3 (LayerNorm Expressivity): Proves that under a causal mask, there exist weight matrices that yield at least $2^k$ stable equilibria of rank $k$ (for any rank up to full rank). This means the system can avoid rank-one collapse entirely.

The geometric intuition of this phenomenon for a two-token system ($N=2, d=2$) is illustrated below:

Figure 1: Long-term behavior of tokens in the case of N = 2, d = 2. Without LayerNorm (left), both tokens collapse to the same point in R2; whereas with LayerNorm (right), such a collapse would not necessarily happen and token representations can maintain full rank in the long term (first token converges either to (0, 1) or (0, −1). Assuming convergence to (0, 1) for the first token, the second token converges to B, if it is initially located within the red segment).
  • Empirical Confirmation: Simulations on the Wikipedia dataset show that while pure self-attention networks (SANs) drop to zero rank rapidly, variants containing LayerNorm maintain stable, non-zero token similarity across hundreds of layers.

Limitations and Future Work

While the paper proves the existence of stable full-rank equilibria, the authors note that the "regions of attraction" for these equilibria can be quite small. This implies that it remains difficult for a generic input sequence to naturally hit these exact configurations, explaining why Multi-Layer Perceptron (MLP) blocks remain practically essential to provide additional non-linear expressivity.

Additionally, the authors' proof relies heavily on the directional asymmetry of causal graphs. They leave open an intriguing question for future research: Can a similar non-collapsing equilibrium exist when using completely undirected, bidirectional attention masks?

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