Abstract
Self-attention and masked self-attention are the core mechanisms behind the success of Transformer architectures. However, our mathematical understanding of their Lipschitz properties—which are critical for analyzing model robustness and expressive power—remains incomplete. This paper provides a detailed study of the Lipschitz constant of self-attention across several practical scenarios. We analyze the impact of sequence length $n$ and layer normalization on the local Lipschitz constant for both unmasked and masked self-attention. Our findings show that for inputs of length $n$ within a compact set, the Lipschitz constant is bounded by $\sqrt{n}$ up to a constant factor, a bound that proves tight for standard sequence lengths. In the "mean-field" regime, where $n$ is sufficiently large, we provide bounds independent of $n$. Furthermore, we introduce a novel mean-field framework for masked self-attention. Empirical evaluations on pretrained and randomly initialized BERT and GPT-2 models support these theoretical conclusions.
Introduction
In deep learning, the "smoothness" of a function is typically quantified by its Lipschitz constant—a value that bounds how much the output can change relative to a small change in the input. A low Lipschitz constant indicates a model is robust to noise and adversarial perturbations, whereas a high constant suggests potential instability.
While the Lipschitz properties of standard feed-forward networks are well-documented, Transformers present a unique challenge. The self-attention mechanism is highly non-linear, involving complex interactions between all tokens in a sequence. Previous research suggested that attention might lack global smoothness entirely. This study seeks to quantify the local Lipschitz constant of attention across varying sequence lengths ($n$) and token magnitudes ($R$) to determine exactly how stable these models are.
Theoretical Regimes
The analysis is conducted through three distinct mathematical lenses:
- The General Regime: Analyzing standard sequences where $n$ is finite. Here, the authors derive an upper bound for the Lipschitz constant that scales with $\sqrt{n}$.
- The Mean-Field Regime: By modeling attention as a map between probability measures (letting $n \to \infty$), the authors identify bounds that are independent of sequence length but depend exponentially on the magnitude of the input tokens.
- The Large Radius Regime: This examines the behavior when the magnitude of tokens ($R$) becomes very large while $n$ remains fixed.
Innovation in Masked Attention
A significant theoretical hurdle in this research was extending the analysis to masked self-attention, the variant used in Decoders like GPT. Masking breaks "permutation equivariance" (the property where the order of tokens does not change the nature of the operation), rendering standard measure-theory tools ineffective. The authors overcome this by utilizing conditional optimal transport, adding a temporal or positional coordinate to tokens to preserve their sequence order within the mathematical framework.
Key Insights
The $\sqrt{n}$ Scaling Law
The paper proves that the Lipschitz constant of self-attention grows at a rate of at most $\sqrt{n}$. This implies that as Transformers process longer sequences, the layers naturally become less smooth and more sensitive to perturbations. Through adversarial construction, the authors demonstrate that this $\sqrt{n}$ growth is a "tight" bound, meaning real-world-adjacent configurations can actually reach this limit.
The Role of Normalization
Layer normalization (LayerNorm) and RMSNorm are identified as critical components for stability. By constraining the magnitude of tokens ($R$), these techniques prevent the Lipschitz constant from exploding exponentially—a phenomenon the authors show occurs at a rate of $R^2 e^{R^2}$ when tokens are unconstrained.
Natural Language vs. Adversarial Cases
Interestingly, the Lipschitz constant grows more slowly on actual text data (such as Alice in Wonderland or the AG_NEWS dataset), at a rate of approximately $n^{1/4}$. This suggests that natural language rarely triggers the pathological, "worst-case" configurations that lead to extreme instability.
Empirical Results
The theoretical bounds were validated using pretrained BERT and GPT-2 models. Experiments using specifically constructed adversarial inputs perfectly matched the predicted $\sqrt{n}$ growth.
However, an anomaly was observed in pretrained GPT-2, where growth occasionally exceeded $\sqrt{n}$. The authors attributed this to learned positional encodings. These encodings increase the magnitude of tokens as the sequence length grows, effectively increasing the "radius" $R$ alongside $n$ and bypassing the standard sequence-length limit.
Conclusion and Future Work
These findings present a fundamental trade-off in Transformer design. Achieving a 1-Lipschitz (perfectly stable) model across all sequence lengths might require dividing the attention output by $\sqrt{n}$, which could significantly diminish the model's expressive power for shorter sequences.
Future research directions include refining the analysis for multi-head attention interactions and further investigating how different positional encoding schemes impact model robustness. Understanding these "obstructions" to smoothness is a vital step toward designing the next generation of robust and reliable large language models.
Summary updated