On the Expressivity Role of LayerNorm in Transformers' Attention

Shaked Brody, Uri Alon, Eran Yahav

Abstract

While Layer Normalization (LayerNorm) is ubiquitously employed in Transformer architectures to stabilize training and improve gradient flow, its impact on the model's representational capacity is often overlooked. This paper investigates the geometric role of LayerNorm, demonstrating that it is essential for the expressivity of the attention mechanism. We show that LayerNorm's projection and scaling operations solve fundamental geometric hurdles, specifically the "Uniformity Problem" and the "Unselectable Key Problem," ensuring that all tokens in a sequence can be effectively attended to.

Introduction

In contemporary Deep Learning, LayerNorm is typically treated as a utility layer—a component designed to prevent activations from exploding and to facilitate backpropagation. However, the interaction between LayerNorm and the subsequent Multi-Head Attention (MHA) layer suggests a deeper functional purpose. By analyzing the geometric transformations imposed by LayerNorm, we reveal that it fundamentally alters the space in which attention operates, enabling logical operations that would otherwise be difficult or impossible for the model to represent.

Geometric Decomposition of LayerNorm

The standard LayerNorm transformation, $y = \frac{x - \mu}{\sigma}$, can be decomposed into two distinct geometric operations:

  1. Projection: The numerator ($x - \mu$) projects the input vector $x$ onto a $(d-1)$ dimensional hyperplane orthogonal to the "ones" vector ($\vec{1} = [1, 1, ..., 1]$).
  2. Scaling: The denominator ($\sigma$) rescales the resulting vector such that the output has a fixed $\ell_2$ norm of exactly $\sqrt{d}$.

These operations are not merely for numerical stability; they provide the geometric scaffolding necessary for flexible attention distributions.

The Uniformity Problem and Projection

A critical requirement for many sequence tasks is the ability to assign equal weight to all tokens—for instance, when a model must compute a simple average of its inputs. In standard dot-product attention, achieving a perfectly uniform distribution requires the query to produce identical dot products with every key in the sequence.

LayerNorm simplifies this significantly. Because the projection step ensures all keys are orthogonal to the $\vec{1}$ vector, the model can achieve a uniform distribution by simply aligning the query vector with $\vec{1}$. This results in a dot product of zero for all keys, creating a perfectly balanced softmax output. In synthetic "Majority" tasks, where identifying the most frequent token is required, models utilizing this projection property converged three times faster than those without it.

The Unselectable Key Problem and Scaling

A more complex issue arises from the "Unselectable Key Problem." In dot-product attention, a token is considered "selectable" if there exists at least one query vector that would make that token the maximum value in the attention distribution.

Mathematically, we prove that any key vector trapped inside the convex hull of other key vectors is effectively invisible; no query can ever make it the top choice for the attention head. Without the scaling component of LayerNorm, vectors can easily fall into this "convex hull trap."

LayerNorm acts as a "sphere-ifier." By scaling all vectors to the same $\ell_2$ norm, it pushes every token to the surface of a hypersphere. Geometrically, no point on the surface of a sphere can reside within the convex hull of other points on that same surface. Consequently, LayerNorm ensures that every single token in the sequence remains potentially selectable by the attention mechanism.

Empirical Results

To validate these geometric insights, we conducted experiments across synthetic and real-world datasets:

  • Language Modeling: In GPT-2 style models trained without the scaling component, we observed that 32% to 51% of tokens were unselectable across various layers. These tokens were mathematically incapable of being the primary focus of attention.
  • Sentiment Analysis: In BERT-style encoders trained on the Stanford TreeBank (SST), the absence of LayerNorm caused critical, high-nuance words like "talent," "power," and "fun" to become unselectable in certain layers, directly degrading classification performance.
  • The LayerNorm Solution: Upon applying LayerNorm's scaling, the fraction of unselectable keys dropped to exactly 0% across all tested configurations.

Conclusion

Our findings suggest that LayerNorm is a vital component of Transformer expressivity, rather than just an optimization trick. By projecting keys to allow for easy averaging and scaling them to ensure total selectability, LayerNorm provides the geometric flexibility required for complex linguistic reasoning. While these constraints are most impactful in models with lower hidden dimensions, the fundamental role of "sphere-ification" remains a cornerstone of why the Transformer architecture is so effective at capturing sequence relationships.

Summary updated

BibTeX
@inproceedings{brody2023expressivity,
  author = {Shaked Brody and Uri Alon and Eran Yahav},
  title = {{On the Expressivity Role of LayerNorm in Transformers' Attention}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.findings-acl.895},
}
Figure