NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Multi-Layer Transformers Gradient Can be Approximated in Almost Linear Time

Yingyu Liang, Zhizhou Sha, Zhenmei Shi, Zhao Song, Yufa Zhou · DOI

Introduction

The expansion of the "context window" has become a primary objective in the development of large language models (LLMs). Modern architectures now routinely support sequences of 128,000 tokens or more. However, the computational cost of the self-attention mechanism grows quadratically ($O(n^2)$) with the sequence length ($n$), creating a significant bottleneck. While various techniques have been developed to accelerate the forward pass (inference), the backward pass—specifically the computation of gradients for training—has remained computationally expensive. This research provides a theoretical breakthrough by proving that gradients for multi-layer Transformers can be approximated in "almost linear" time ($n^{1+o(1)}$) without sacrificing the accuracy required for effective optimization.

The Quadratic Bottleneck in Gradient Computation

In standard Transformer architectures, the self-attention mechanism requires computing an $n \times n$ attention matrix. During the backward pass, calculating the gradients of the loss function with respect to the model parameters involves manipulating these large matrices. As $n$ increases, the $O(n^2)$ complexity makes training on extremely long sequences prohibitively expensive.

Previous theoretical attempts to mitigate this complexity were often limited to single-layer models, specific loss functions (such as $\ell_2$ loss), or simplified architectures that omitted essential components like residual connections and multi-head attention. This work bridges the gap between theoretical complexity and the practical realities of modern Transformer architectures.

The ALT Gradient Approximation Method

The authors introduce a novel framework called ALT (Almost Linear Time) gradient approximation. The core of this method relies on polynomial kernel approximation to bypass the need for full matrix materialization.

1. Low-Rank Representation

Instead of computing the full $n \times n$ attention matrix, the ALT method uses polynomial techniques to decompose the matrix into a product of low-rank matrices ($n \times k$, where $k \ll n$). This reduction is the primary driver of the speedup.

2. Matrix Deconstruction and Chain Rule

The gradient of the loss function is deconstructed into five distinct matrix components. By applying low-rank approximations to each component individually, the algorithm avoids the quadratic cost. These approximations are then propagated back through the network's multiple layers using the chain rule.

3. Addressing Architectural Constraints

A significant contribution of this research is its ability to handle "real-world" Transformer features:

  • Causal Masking: Causal masks, which prevent tokens from attending to future tokens, typically break low-rank structures. The ALT method leverages specific properties of the mask to maintain almost linear complexity.
  • Residual Connections: The framework accounts for the additive nature of residual connections, ensuring that the gradient flow remains accurate across many layers.

Key Theoretical Insights

Controllable Error Bounds

A critical concern in gradient approximation is the accumulation of error during backpropagation. The authors prove that the approximation error remains "polynomially small" ($1/\text{poly}(n)$). This ensures that the approximate gradient maintains a direction useful for optimization, preventing the training process from diverging even in deep, multi-layer models.

Generality of Loss Functions

Unlike prior work restricted to mean-squared error, this theory is applicable to general loss functions, including the cross-entropy loss used in the vast majority of modern LLMs.

Complexity Breakthrough

The algorithm achieves a runtime of $n^{1+o(1)}$. In practical terms, this means the computational cost of training on 100,000 tokens becomes theoretically comparable to training on 10,000 tokens (scaled linearly), rather than being 100 times more expensive. The theory also extends to $m$-layer models where the number of layers $m$ can scale with the sequence length $n$.

Limitations and Future Directions

While this work provides a rigorous mathematical foundation for linear-time training, several hurdles remain for practical adoption:

  • Implementation Challenges: Current deep learning frameworks and GPU kernels are highly optimized for dense matrix multiplications. Implementing the specific low-rank tensor operations required by ALT will require significant engineering effort to achieve real-world speedups.
  • Hardware Constants: The $n^{1+o(1)}$ notation hides potential overhead constants. The actual efficiency of the algorithm in a production environment will depend on how well these operations can be parallelized in CUDA or similar environments.
  • Empirical Validation: As a theoretical paper, the primary results are mathematical theorems. The next step for the research community is to implement ALT gradients in a training library to validate these findings during the pre-training of multi-billion parameter models.

Conclusion

By breaking the quadratic barrier for the backward pass, this research provides a path toward training models on significantly longer contexts than previously possible. The proof that multi-layer Transformer gradients can be computed in almost linear time with negligible error represents a major step forward in the algorithmic efficiency of deep learning.

Summary updated

BibTeX
@inproceedings{liang2024multi,
  author = {Yingyu Liang and Zhizhou Sha and Zhenmei Shi and Zhao Song and Yufa Zhou},
  title = {{Multi-Layer Transformers Gradient Can be Approximated in Almost Linear Time}},
  booktitle = {OPT @ NeurIPS},
  year = {2024},
  url = {https://openreview.net/forum?id=1LJIPZ4SvS},
}
Figure