Understanding the Regularity of Self-Attention with Optimal Transport

Valérie Castin, Pierre Ablin, Gabriel Peyré · DOI

Introduction

Despite the ubiquity of Transformer architectures in modern artificial intelligence, our theoretical understanding of their mathematical stability remains incomplete. A critical metric for assessing this stability is the Lipschitz constant, which quantifies the maximum sensitivity of a model's output to changes in its input. A high Lipschitz constant indicates a "jittery" model, where minor perturbations—such as noise or adversarial attacks—can lead to disproportionately large changes in output. This paper provides a rigorous analysis of the local Lipschitz continuity of the self-attention mechanism, establishing how its regularity scales with sequence length and input magnitude.

The Problem of Stability in Attention

Previous research has suggested that self-attention is not globally Lipschitz continuous, meaning its sensitivity can theoretically explode under specific conditions. For practitioners, the vital question is how this sensitivity behaves within the operational regimes of large-scale models. Specifically, how does the Lipschitz constant scale with the sequence length ($n$) and the magnitude of input tokens ($R$)? This research fills this gap by providing tight upper and lower bounds for the Lipschitz constant across different mathematical regimes.

Methodology: Three Regimes of Analysis

The authors employ a multi-faceted approach to analyze the "smoothness" of attention, focusing on the local Lipschitz constant—the rate of change in the neighborhood of a specific input—rather than a single global value. Their analysis spans three distinct scenarios:

  1. The General Regime: Inputs are restricted to a compact set with bounded magnitude.
  2. The Mean-Field Regime: Attention is modeled as a map between probability measures as the sequence length $n$ approaches infinity. This utilizes tools from Optimal Transport, specifically the Wasserstein distance, to understand the limit behavior of the mechanism.
  3. The Large Radius Regime: The analysis examines the behavior of the mechanism as the magnitude of input tokens ($R$) becomes very large.

Bridging the Gap: Masked Attention

Traditional mean-field limits often assume "permutation equivariance," where the order of tokens does not matter. While this holds for Encoders, it is false for Decoders (masked attention). The authors introduced a novel framework that encodes token position as an extra coordinate, allowing for the first rigorous mathematical study of Decoder-style attention regularity in a mean-field context.

Key Insights: The $\sqrt{n}$ Scaling Law

The central finding of the paper is that the sequence length itself is an inherent source of instability. The authors established that the local Lipschitz constant typically grows with the square root of the sequence length ($\sqrt{n}$). This implies that, even with identical weights, a Transformer processing 1024 tokens is theoretically $\sqrt{2}$ times more sensitive than one processing 512 tokens.

Crucially, the research proves that this $\sqrt{n}$ bound is "tight," meaning it represents the true worst-case sensitivity without making additional restrictive assumptions. Furthermore, the study demonstrates that the sequential nature of Decoders does not inherently make them smoother than Encoders; both architectures are subject to the same scaling laws.

Empirical Validation and the "Real-World" Gap

The researchers validated these theoretical bounds using pretrained models (BERT and GPT-2) and real-world datasets, including the AG NEWS dataset and extracts from the NLTK Gutenberg corpus (Alice in Wonderland).

An intriguing discovery emerged from the empirical data: while adversarial inputs designed to trigger worst-case sensitivity perfectly matched the predicted $\sqrt{n}$ growth, real-world text followed a slower growth rate of approximately $n^{1/4}$. This suggests that natural language occupies a "smoother" subspace of the possible input space than the theoretical worst-case scenarios, providing a buffer of stability for practical applications.

The Role of Normalization

The analysis highlights that normalization layers, such as LayerNorm and RMSNorm, are essential for more than just training convergence. They play a vital role in keeping input magnitudes ($R$) in check. Without these mechanisms, the Lipschitz constant could grow exponentially rather than polynomially, leading to extreme model instability.

Conclusion and Future Directions

This work provides a foundational mathematical framework for understanding why Transformers behave the way they do as they scale. However, several avenues for future research remain:

  • Multi-head Interaction: While the paper provides basic bounds for multi-head attention, a more granular estimation of how different heads interact to affect overall regularity is needed.
  • The $n^{1/4}$ Phenomenon: Understanding why natural language data is more "well-behaved" than the theoretical $\sqrt{n}$ limit could lead to the development of more efficient and specialized robustness techniques.
  • Neural PDEs: The new mean-field framework for masked attention paves the way for treating Decoders as "Neural Partial Differential Equations," viewing deep networks as continuous dynamical systems.

By establishing these bounds, the authors provide a roadmap for building more robust and predictable large-scale models, grounded in the rigorous mathematics of Optimal Transport.

Summary updated

BibTeX
@inproceedings{castin2024understanding,
  author = {Valérie Castin and Pierre Ablin and Gabriel Peyré},
  title = {{Understanding the Regularity of Self-Attention with Optimal Transport}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=aP0H8A1ywk},
}
Figure