Abstract
Transformers have revolutionized deep learning, yet the mathematical stability of their core component—self-attention—has remained poorly understood. We demonstrate that standard dot-product self-attention is not Lipschitz continuous, which can lead to instability in deep architectures and sensitivity to adversarial perturbations. To address this, we propose L2 self-attention and prove that it is Lipschitz continuous when query and key weights are tied. We derive a tight upper bound for its Lipschitz constant that grows sub-logarithmically with sequence length. Our findings enable the creation of provably invertible Transformers and demonstrate improved training stability.
Introduction
In deep learning, Lipschitz continuity is a desirable property that ensures a model's output changes predictably with respect to its input. This property is essential for adversarial robustness, the stability of Generative Adversarial Networks (GANs), and the development of invertible neural networks like Normalizing Flows. While Lipschitz bounds are well-established for linear and convolutional layers, the self-attention mechanism presents unique challenges due to the interaction between the dot-product kernel and the softmax function. This work investigates whether standard self-attention is Lipschitz and proposes a stable alternative.
Theoretical Framework and L2 Self-Attention
The authors analyze the Jacobian of standard dot-product attention and prove that its Lipschitz constant is unbounded on an infinite domain. Specifically, the rate of change in the output grows with the variance of the inputs, making the mechanism inherently unstable.
To rectify this, the paper introduces L2 self-attention, which replaces the dot-product kernel with one based on L2 distance: $$P_{ij} \propto \exp(-\|x_i^T W^Q - x_j^T W^K\|^2)$$ By tying the weights ($W^Q = W^K$), the authors prove that L2 self-attention becomes Lipschitz continuous. They derive a theoretical upper bound for this constant, showing it grows as $O(\log N)$, where $N$ is the sequence length.
As shown in Figure 5, empirical tests on random data confirm that the derived upper bound (UB) is a remarkably close match to the observed lower bounds (LB) across various sequence lengths, confirming the tightness of the $O(\log N)$ derivation.
Invertible Transformers
A significant application of a bounded Lipschitz constant is the construction of invertible residual blocks. According to the Banach Fixed Point Theorem, if the Lipschitz constant of a residual branch is less than 1, the entire block is a contraction and can be inverted using simple fixed-point iteration. This allows for the design of "Invertible Transformers," which are useful for memory-efficient training and generative modeling.
Experimental Results
The proposed L2-MHA was evaluated on character-level language modeling (Penn Treebank) and synthetic datasets to test both expressiveness and numerical stability.
Numerical Invertibility
The stability of L2-MHA allows for rapid convergence during inversion. In contrast, standard dot-product attention (DP-MHA) fails to converge, as its lack of Lipschitz continuity prevents it from being a reliable contraction.
As illustrated in Figure 3, L2-MHA achieves reconstruction errors as low as $10^{-5}$ within 15 iterations. Conversely, DP-MHA's error remains several orders of magnitude higher and fails to decrease, demonstrating the necessity of the Lipschitz property for invertibility.
Training Stability and Performance
L2-MHA exhibits superior training stability. While standard Transformers often require complex learning rate "warmup" schedules to prevent gradient explosions, L2-MHA can be trained with a fixed learning rate even in very deep configurations (e.g., 18+ layers). On the Penn Treebank task, L2-MHA achieved performance comparable to standard attention. Although tying $W^Q$ and $W^K$ slightly reduces per-layer capacity, the increased stability allows for deeper models that regain and eventually exceed baseline expressiveness.
Conclusion
This work provides the first rigorous analysis of the Lipschitz constant for self-attention. By identifying the instability in standard dot-product attention and proposing the L2-MHA alternative, we provide a path toward more robust and mathematically grounded Transformer architectures. Future work includes exploring alternative kernels that might offer similar guarantees without the requirement of tied weights.
Summary updated