The Shaped Transformer: Attention Models in the Infinite Depth-and-Width Limit

Lorenzo Noci, Chuning Li, Mufan Bill Li, Bobby He, Thomas Hofmann, Chris Maddison, Daniel M. Roy

TL;DR

The paper introduces the Shaped Transformer, a novel modification to the Transformer architecture that ensures stability and trainability in the proportional infinite depth-and-width limit. By centering the Softmax attention mechanism around the identity matrix and scaling the temperature parameter, the authors prevent the notorious issue of rank collapse and derive a stable Stochastic Differential Equation (SDE) that accurately describes the network's behavior at initialization.

What Problem Does This Paper Address?

As deep learning models scale, training deeper and wider Transformers becomes increasingly difficult due to numerical instabilities. A major culprit is rank collapse (or token uniformity), where token representations across the network tend to perfectly align as depth increases. This causes the covariance matrix of the representations to become poorly conditioned, leading to vanishing or exploding gradients at initialization.

While theoretical frameworks like the Neural Tangent Kernel (NTK) limit help analyze wide networks, they treat depth as fixed and fail to capture the random fluctuations that accumulate over many layers. To understand and fix these instabilities, this paper analyzes Transformers in the proportional infinite depth-and-width limit, where both depth ($d$) and width ($n$) go to infinity while their ratio ($d/n$) remains constant.

Methods

The authors propose the Shaped Transformer, which introduces three key modifications to the standard attention block to ensure a well-behaved continuous limit:

  1. Centering the Softmax: They subtract the zero-order term ($\frac{1}{m}\mathbf{1}\mathbf{1}^\top$) from the Taylor expansion of the Softmax function to eliminate non-infinitesimal drift.
  2. Biasing Towards Identity: They add the identity matrix ($I$) back to the attention matrix, forcing each token to primarily attend to itself at initialization, making the attention block a small perturbation of the identity.
  3. Width-Dependent Temperature Scaling: They scale the Softmax logits by a temperature parameter $\tau = \tau_0\sqrt{nn_k}$ (where $n_k$ is the key/query dimension), which linearizes the Softmax and prevents saturation.

Using these modifications, the authors apply the mathematical framework of Markov chain convergence to derive a system of Stochastic Differential Equations (SDEs) that fully characterizes the initial output and covariance distribution of the network.

Datasets

For the empirical validation and preliminary training experiments, the authors utilized:

  • Pre-training: A subset of the English Wikipedia (20220301.en) and the English BookCorpus datasets for a masked language modeling task.
  • Evaluation: The trained models were fine-tuned and evaluated on three datasets from the GLUE benchmark: COLA, MRPC, and RTE.

Key Insights and Ideas

  • Attention as a Perturbation: For a network to have a stable SDE limit in the proportional depth-and-width regime, layer updates must be infinitesimal. Standard Softmax attention fails this because its average output introduces a massive drift that destroys representation rank. Biasing attention toward the identity fixes this.
  • ResNets as a Time Change: The authors show that residual connections damp both the drift and diffusion terms by a factor of $\gamma^2$. At initialization, adding skip connections is mathematically equivalent to reducing the effective depth-to-width ratio of the network.
  • SDEs as Faithful Proxies: The derived SDEs are not just abstract math; they provide a surprisingly accurate description of actual finite-sized neural networks, matching their empirical correlation distributions tightly.

Key Results

  • Prevention of Rank Collapse: Standard and Pre-LN Transformers see their token correlations rapidly converge to 1 (complete rank collapse). The Shaped Transformer keeps token correlations well-behaved and bounded away from 1 across 150 layers.
Our shaped Transformer prevents token representations from becoming perfectly aligned (rank collapse).
  • Stable Gradients: Standard Post-LN architectures suffer from rapidly vanishing query gradients. The Shaped Transformer maintains stable gradient norms for both value and query weights across all depths.
Comparing gradient norms at initialization as a function of depth.
  • Downstream Performance: In practical training, the Shaped Transformer (without LayerNorm) achieved comparable pre-training loss to standard Pre-LN Transformers and even outperformed the baseline on GLUE downstream tasks (e.g., achieving a COLA score of 0.221 vs 0.139 for the baseline at $d=18$).
  • Mitigating Entropy Collapse: Standard Transformers often suffer from training instability due to attention entropy collapse (where attention focuses entirely on a single token). The Shaped Transformer remains stable and avoids this collapse even at higher learning rates.

Limitations and Future Work

While the theoretical foundation is robust, the authors note that their practical training experiments are preliminary due to limited computing resources. They only tested masked language modeling on relatively small models ($d=18$ and $d=24$).

Future work includes extending this framework to fully characterize training dynamics and generalization in the proportional limit, as well as scaling up the Shaped Transformer to much larger datasets and model sizes to confirm its utility in massive production-scale LLMs.

Summary updated

Abstract

In deep learning theory, the covariance matrix of the representations serves as a proxy to examine the network's trainability. Motivated by the success of Transformers, we study the covariance matrix of a modified Softmax-based attention model with skip connections in the proportional limit of infinite-depth-and-width. We show that at initialization the limiting distribution can be described by a stochastic differential equation (SDE) indexed by the depth-to-width ratio. To achieve a well-defined stochastic limit, the Transformer's attention mechanism is modified by centering the Softmax output at identity, and scaling the Softmax logits by a width-dependent temperature parameter. We examine the stability of the network through the corresponding SDE, showing how the scale of both the drift and diffusion can be elegantly controlled with the aid of residual connections. The existence of a stable SDE implies that the covariance structure is well-behaved, even for very large depth and width, thus preventing the notorious issues of rank degeneracy in deep attention models. Finally, we show, through simulations, that the SDE provides a surprisingly good description of the corresponding finite-size model. We coin the name shaped Transformer for these architectural modifications.
BibTeX
@inproceedings{noci2023shaped,
  author = {Lorenzo Noci and Chuning Li and Mufan Bill Li and Bobby He and Thomas Hofmann and Chris Maddison and Daniel M. Roy},
  title = {{The Shaped Transformer: Attention Models in the Infinite Depth-and-Width Limit}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=PqfPjS9JRX},
}
Figure