LongNet: Scaling Transformers to 1, 000, 000, 000 Tokens

Jiayu Ding, Shuming Ma, Li Dong, Xingxing Zhang, Shaohan Huang, Wenhui Wang, Nanning Zheng, Furu WeiarXiv 2023

TL;DR

This paper introduces LongNet, a Transformer architecture capable of processing sequences up to 1 billion tokens, a significant leap from previous limitations. It achieves this by employing a novel "dilated attention" mechanism that offers linear computational complexity and logarithmic token dependency, enabling efficient long-context modeling without sacrificing performance on shorter sequences.

What Problem Does This Paper Address?

The era of large language models (LLMs) is characterized by an insatiable demand for longer context windows. This is crucial for tasks requiring understanding complex relationships, reasoning over extended narratives, and enabling in-context learning. However, standard Transformer architectures face a fundamental bottleneck: their self-attention mechanism has a computational complexity that scales quadratically with sequence length ($O(N^2d)$). This makes processing very long sequences computationally prohibitive, limiting models to sequence lengths of a few thousand tokens at best. Existing solutions like sparse attention or state-space models have their own trade-offs, either sacrificing expressivity or struggling with performance on shorter sequences.

Methods

The core innovation of LongNet is its dilated attention mechanism. Instead of attending to all tokens, dilated attention selectively attends to tokens at exponentially increasing distances. This is achieved by:

  • Segmenting the input: The input sequence is divided into segments.
  • Dilated sampling: Within each segment, attention is applied to tokens with a specific "dilation rate," effectively skipping tokens. This creates an exponentially expanding receptive field.
  • Mixture of Dilated Attentions: To balance short-range and long-range dependencies, LongNet uses a combination of dilated attention mechanisms with different segment sizes and dilation rates. This allows for fine-grained attention to nearby tokens and broader attention to distant ones.
  • Linear Complexity: This dilated attention strategy reduces the computational complexity to $O(Nd)$, a significant improvement over the quadratic complexity of standard attention.
  • Logarithmic Token Dependency: The dilated attention also ensures that the dependency between any two tokens in the sequence grows logarithmically with distance, facilitating information flow across very long contexts.
  • Distributed Training: LongNet is designed to be easily integrated into existing Transformer optimization frameworks and can be efficiently trained using distributed systems by partitioning the sequence dimension. This allows it to scale to extremely large sequence lengths that would be impossible on a single device.

Datasets

The paper primarily uses The Stack dataset, a large collection of code from over 300 programming languages, for language modeling experiments.

Key Insights and Ideas

  • Dilated Attention as a Game Changer: The core insight is that by strategically sparsifying attention with an exponentially increasing receptive field, one can achieve linear complexity while maintaining the expressivity of Transformers. This is a crucial step towards truly long-context models.
  • Scalability Beyond Quadratic: The paper demonstrates that the quadratic bottleneck of Transformers is not an inherent limitation for scaling sequence length, but rather a consequence of the standard attention mechanism. LongNet provides a viable alternative.
  • Distributed Training is Key for Extreme Lengths: Scaling to 1 billion tokens is not just about algorithmic innovation but also about leveraging distributed computing effectively. LongNet's linear complexity makes it amenable to sequence parallelism, enabling training on massive datasets.
  • Longer Context = Better Models: The experiments strongly suggest that training with longer contexts leads to improved language modeling performance, even when compared to models that might extrapolate to longer sequences during inference.

Key Results

  • 1 Billion Token Sequences: LongNet successfully scales to process sequences of 1 billion tokens, a feat unprecedented for Transformer architectures.
  • Linear Runtime: Figure 5 shows that LongNet's runtime scales linearly with sequence length, even when equipped with FlashAttention, while vanilla attention's runtime explodes quadratically. For example, at 128M sequence length, LongNet is significantly faster than vanilla attention.
  • Competitive Perplexity: Table 2 and Figure 6 show that LongNet achieves competitive or superior perplexity compared to vanilla and sparse Transformers across various sequence lengths, demonstrating its effectiveness in language modeling. For instance, at 32K sequence length, LongNet achieves a perplexity of 3.01, outperforming sparse Transformers.
  • Efficient Scaling: Figure 7(a) demonstrates that LongNet follows similar scaling laws to vanilla Transformers with increasing model size, but with significantly less compute for longer contexts. Figure 7(b) shows that longer context windows consistently yield better language modeling performance.

Limitations and Future Work

  • Limitations: While LongNet achieves linear complexity, the paper notes that the computation and communication costs in the distributed setting are "nearly constant" as the number of devices grows, implying that there are still some overheads. The paper also mentions that for extremely long sequences, the batch size needs to be reduced to keep the number of tokens per batch constant, which might impact training efficiency in some scenarios.
  • Future Work: The authors propose extending LongNet to support more tasks, including multimodal large language modeling, BEiT pretraining, and genomic data modeling. They also highlight the potential for further exploration of the flash attention kernel.

Summary updated

BibTeX
@misc{ding2023longnet,
  author = {Jiayu Ding and Shuming Ma and Li Dong and Xingxing Zhang and Shaohan Huang and Wenhui Wang and Nanning Zheng and Furu Wei},
  title = {{LongNet: Scaling Transformers to 1, 000, 000, 000 Tokens}},
  year = {2023},
  eprint = {2307.02486},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2307.02486},
}
Figure