Efficient Long-Range Transformers: You Need to Attend More, but Not Necessarily at Every Layer

Qingru Zhang, Dhananjay Ram, Cole Hawkins, Sheng Zha, Tuo Zhao

TL;DR

MASFormer (Mixed Attention Span transFormer) is a simple yet effective architectural tweak that enables Transformers to handle long sequences (8k+ tokens) efficiently. By using full attention in only a few bottom layers and sparse block attention elsewhere, it achieves performance comparable to standard Transformers while slashing computational costs by up to 75%.

What Problem Does This Paper Address?

The "Achilles' heel" of the standard Transformer is the self-attention mechanism. Its computational complexity grows quadratically ($O(n^2)$) with the sequence length $n$. While this is manageable for short snippets, it becomes a massive bottleneck for long-context tasks like summarizing entire academic papers or maintaining multi-turn chatbot histories. A model with just 250M parameters can hog over 80GB of GPU memory when processing 8k tokens.

Existing solutions like sparse attention (e.g., sliding windows or blocks) reduce costs but often struggle to capture the infrequent, complex long-range dependencies that give a document its global structure. Other alternatives, like State Space Models (SSMs), can be difficult to implement and unstable to scale. The authors sought a middle ground: a model that maintains the power of full attention where it matters most, without the prohibitive cost of applying it everywhere.

Methods

The core proposal is MASFormer, which challenges the convention that every layer in a Transformer should use the same attention mechanism.

  1. Mixed Attention Spans: MASFormer uses full attention in a small subset of layers ($l$) and block sparse attention in the remaining layers ($L-l$).
  2. Strategic Placement: Through empirical testing, the authors found that placing full attention at the bottom layers (closest to the input) is most effective. The logic is that long-range signals need to be captured and reinforced early before being propagated through the rest of the network.
  3. Continual Training: Rather than training from scratch, the authors take a pre-trained 1.3B parameter GPT-3 XL model, modify its attention patterns, duplicate its positional embeddings to handle longer sequences, and perform "continual training" on the PILE corpus. This allows the model to adapt to the new attention structure and longer context windows (8k tokens) without the massive expense of a full pre-training run.
Comparison of attention patterns: (a) Block Sparse, (b) Sliding Window, and (c) the proposed MASFormer which mixes full and block attention.

Datasets

The authors evaluated MASFormer across several demanding long-context benchmarks:

  • Continual Pre-training: The PILE corpus (300 billion tokens).
  • Natural Language Modeling (NLM): ArXiv and PubMed scientific paper datasets, used to measure perplexity.
  • Natural Language Generation (Summarization): ArXiv, QMSUM (meeting summaries), and GovReport (government reports) from the SCROLLS benchmark.

Key Insights and Ideas

  • Locality of Reference: The authors observe that most information in NLP is local—a token's meaning is usually derived from its immediate neighbors. Long-range dependencies are "sparse and infrequent." Therefore, a few "global eyes" (full attention layers) are sufficient to capture the big picture.
  • The Perplexity Trap: A major insight is that perplexity (NLM) is a poor proxy for long-range capability. Increasing context length might only slightly improve a model's ability to predict the next token, but it can lead to massive gains in downstream tasks like summarization. This suggests that researchers should prioritize downstream evaluation over simple language modeling metrics.
  • Bottom-Heavy is Better: If you put sparse attention at the bottom, the model "blurs" long-range signals before they ever reach a full attention layer. By putting full attention at the bottom, the model captures these signals clearly and passes them up, where sparse layers can then process them locally.

Key Results

  • Efficiency vs. Performance: MASFormer with just 4 layers of full attention (out of 24) achieved comparable perplexity to a full-attention model while reducing attention costs by 72%.
  • Summarization Prowess: On the QMSUM dataset, MASFormer achieved a similar ROUGE-2 score to the full-attention baseline while using only 27% of the attention computation.
  • Scaling with Length: The researchers showed that increasing sequence length from 2k to 8k tokens consistently improved summarization performance, provided the model had the architectural capacity (like MASFormer) to actually use that extra information.
Performance comparison on summarization tasks. MASFormer (red line) consistently outperforms sparse baselines and approaches full attention performance at a fraction of the cost.

Limitations and Future Work

  • Architecture Scope: The study focused primarily on decoder-only (GPT-style) models. The effectiveness of this approach in encoder-decoder models (like T5) remains to be fully explored.
  • Fixed vs. Dynamic: MASFormer uses a fixed number of full-attention layers. Future research could investigate dynamic attention allocation, where the model decides which layers need full attention based on the specific input.
  • Linear Components: While MASFormer solves the quadratic attention bottleneck, other parts of the Transformer (like Feed-Forward Networks) still scale linearly. For extremely long sequences (e.g., 100k+ tokens), these other components may eventually become the new bottleneck.

Summary updated

BibTeX
@inproceedings{zhang2023efficient,
  author = {Qingru Zhang and Dhananjay Ram and Cole Hawkins and Sheng Zha and Tuo Zhao},
  title = {{Efficient Long-Range Transformers: You Need to Attend More, but Not Necessarily at Every Layer}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  url = {https://openreview.net/forum?id=rI7ebWPRLr},
}
Figure