Various Lengths, Constant Speed: Efficient Language Modeling with Lightning Attention

Zhen Qin, Weigao Sun, Dong Li, Xuyang Shen, Weixuan Sun, Yiran Zhong

Abstract

Standard Transformer architectures face significant scaling challenges due to the quadratic computational complexity of Softmax attention. While linear attention alternatives offer theoretical efficiency, they often suffer from performance degradation and practical training bottlenecks on modern hardware. This paper introduces Lightning Attention, a novel implementation that achieves constant training speed and fixed memory consumption regardless of sequence length. By utilizing a "divide and conquer" tiling strategy and the TransNormerLLM (TNL) architecture, we demonstrate a model that matches state-of-the-art Transformer performance while providing up to 11x higher inference throughput.

Introduction

The "holy grail" of sequence modeling is an architecture that maintains high accuracy while scaling linearly with sequence length. Standard Transformers rely on Softmax attention, which possesses $O(n^2)$ complexity, making long-context applications prohibitively expensive. Linear attention models ($O(n)$) have been proposed as a solution, but they have historically struggled with two primary issues: a performance gap compared to Softmax models and a training bottleneck caused by the sequential nature of cumulative sum (cumsum) operations in causal modeling. On modern GPUs, these sequential operations prevent linear attention from realizing its theoretical speed advantages over optimized Softmax implementations like FlashAttention.

Methods: Lightning Attention and TransNormerLLM

The Lightning Attention Mechanism

To overcome the cumsum dilemma, Lightning Attention employs a "divide and conquer" strategy that balances computational efficiency with hardware utilization. The input sequence is partitioned into blocks, and the attention calculation is split into two distinct components:

  1. Intra-block (Left Product): Inside each block, the model utilizes conventional attention mechanisms.
  2. Inter-block (Right Product): Between blocks, the model leverages the linear attention "kernel trick" to maintain efficiency.

This approach is mathematically equivalent to a linear recurrence, allowing the model to be trained in parallel like a Transformer while performing inference with the constant memory overhead of a Recurrent Neural Network (RNN). To further optimize performance, the authors implement an IO-aware tiling technique inspired by FlashAttention, which manages data movement between high-bandwidth memory (HBM) and fast on-chip SRAM.

TransNormerLLM (TNL) Architecture

To close the accuracy gap with standard Transformers, the authors developed the TransNormerLLM (TNL) architecture, which incorporates several key innovations:

  • LRPE-d: A linearized relative positional encoding with exponential decay. This mechanism prevents "attention dilution" in long sequences by prioritizing more recent tokens while maintaining a global receptive field.
  • Gated Linear Attention (GLA): A gating mechanism designed to smooth the training process and improve stability.
  • SRMSNorm: A simplified version of Root Mean Square Layer Normalization (RMSNorm) specifically optimized for GPU execution speed.

Experimental Setup

The TNL models were pre-trained on a massive scale using a self-collected, cleaned corpus of 2 trillion tokens derived from 700TB of raw web data. Evaluation was conducted across a diverse suite of benchmarks, including Wikitext-103, MMLU, C-Eval, and various Commonsense Reasoning tasks (BoolQ, PIQA, HellaSwag). Long-range dependency capabilities were specifically tested using the SCROLLS benchmark.

Key Results

Efficiency and Throughput

The most significant result of Lightning Attention is its ability to maintain a constant training speed across sequence lengths ranging from 1k to 128k tokens. In contrast, standard implementations like LLaMA (using FlashAttention-2) show significant slowdowns as sequence length increases. At the 7B parameter scale, TNL achieves an inference throughput 11x higher than standard Transformer models.

Model Performance

TNL-7B and TNL-15B demonstrate competitive accuracy, performing on par with or better than LLaMA-2 and other efficient architectures such as RWKV and HGRN across standard benchmarks. The inclusion of exponential decay in the positional encoding proved essential for stabilizing training and ensuring the model could handle long-context dependencies effectively.

Discussion and Limitations

While Lightning Attention represents a major step forward, some limitations remain. The authors noted that making the decay parameter ($\lambda$) learnable led to numerical instability; consequently, the current version uses a fixed decay schedule based on head and layer indices. Additionally, while the model excels at the 15B parameter scale, its behavior at the "frontier" scale (100B+ parameters) is yet to be explored.

Future work will focus on further optimizing Triton kernels and investigating more complex gating mechanisms to further bridge the remaining performance gap with the most powerful Softmax-based models. Lightning Attention proves that with the right hardware-aware algorithmic design, the trade-off between sequence length and computational speed can be effectively eliminated.

Summary updated

BibTeX
@inproceedings{qin2024various,
  author = {Zhen Qin and Weigao Sun and Dong Li and Xuyang Shen and Weixuan Sun and Yiran Zhong},
  title = {{Various Lengths, Constant Speed: Efficient Language Modeling with Lightning Attention}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=Lwm6TiUP4X},
}
Figure