A mathematical perspective on Transformers

Borjan Geshkovski, Cyril Letrouit, Yury Polyanskiy, Philippe RigolletarXiv 2023 · DOI

Abstract

This paper provides a rigorous mathematical framework for understanding Transformers by treating them as interacting particle systems evolving on a sphere. The authors demonstrate that the self-attention mechanism acts as an attractive force, inevitably leading to "clustering"—a phenomenon where tokens collapse toward a single point. This framework explains empirical observations like over-smoothing and rank collapse in deep models while identifying a metastable phase that preserves representational diversity in shallower architectures.

Introduction: The Black Box Problem

Despite their state-of-the-art performance across diverse domains, Transformers are often treated as "black boxes." A fundamental theory explaining how data is transformed as it passes through dozens of layers has remained elusive. Specifically, researchers have noted that in very deep Transformers, tokens tend to become identical (over-smoothing) or the representation space collapses into a low-rank subspace (rank collapse). This work seeks to move beyond empirical observation to mathematical proof, asking: What are the long-time dynamics of these models, and can we describe the flow of information using the tools of statistical physics and geometry?

Methodology: Transformers as Neural ODEs

The authors simplify the standard Transformer architecture into a mathematical model that retains core components: self-attention and layer normalization. By treating the discrete layers as a continuous time variable ($t$), they transform the network into a Neural Ordinary Differential Equation (Neural ODE).

In this framework, layer normalization is modeled as a constraint that keeps tokens (particles) on the unit sphere ($S^{d-1}$). Self-attention is viewed as a "mean-field" interaction where every particle's velocity depends on the positions of all other particles. A key contribution is the proof that these dynamics constitute a Wasserstein gradient flow, meaning tokens essentially "roll downhill" to maximize a specific interaction energy functional.

The Clustering Phenomenon: Theoretical and Empirical Evidence

The paper proves that for both very small and very large "inverse temperature" parameters ($eta$), the system generically converges to a single cluster. This theoretical prediction is corroborated by analyzing the inner products of tokens across layers in a pre-trained ALBERT XLarge v2 model using Wikipedia data.

Evolution of token inner products in early layers (0-24) of ALBERT XLarge v2.

As shown in the early layers (Figure 2), the distribution of inner products begins to shift away from zero. As the depth increases further, the collapse becomes more pronounced.

Evolution of token inner products in deep layers (25-48) showing eventual rank collapse.

In the deepest layers (Figure 1), the inner products concentrate heavily near 1.0, indicating that the tokens have become nearly identical. This empirical evidence supports the "Universal Clustering" theorems (Theorem 4.1, 5.1) presented in the paper.

Key Insights: Metastability and the Kuramoto Connection

  • The Metastability Paradox: If tokens eventually collapse to a single point, why are Transformers useful? The authors identify a two-time-scale phenomenon: tokens quickly form a few distinct clusters representing different concepts and remain in this "metastable" state for a significant duration before eventually merging into a single cluster.
  • The Kuramoto Connection: In the special case of 2D vectors, the Transformer equations simplify into a version of the Kuramoto model, a famous mathematical model used to describe synchronization in biological and physical systems.
  • High-Dimensional Efficiency: The paper proves that in high dimensions ($d \ge n$), the collapse to a single cluster happens exponentially fast, explaining why rank collapse is particularly prevalent in wide models.

Limitations and Future Work

While providing a robust foundation, the model currently omits Feed-Forward Networks (FFN) and focuses on the "forward pass" with fixed weights. Future work is required to account for how weights change during training and to understand how to tune models to remain in the useful metastable phase without falling into the rank collapse trap.

Summary updated

BibTeX
@misc{geshkovski2023mathematical,
  author = {Borjan Geshkovski and Cyril Letrouit and Yury Polyanskiy and Philippe Rigollet},
  title = {{A mathematical perspective on Transformers}},
  year = {2023},
  eprint = {2312.10794},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2312.10794},
}
Figure