Abstract
Transformer-based models are unable to process long sequences due to their self-attention operation, which scales quadratically with the sequence length. To address this limitation, we introduce the Longformer with an attention mechanism that scales linearly with sequence length, making it easy to process documents of thousands of tokens or longer. Longformer's attention mechanism is a drop-in replacement for the standard self-attention and combines a local windowed attention with a task motivated global attention. Following prior work on long-sequence transformers, we evaluate Longformer on character-level language modeling and achieve state-of-the-art results on text8 and enwik8. In contrast to most prior work, we also pretrain Longformer and finetune it on a variety of downstream tasks. Our pretrained Longformer consistently outperforms RoBERTa on long document tasks and sets new state-of-the-art results on WikiHop and TriviaQA. We finally introduce the Longformer-Encoder-Decoder (LED), a Longformer variant for supporting long document generative sequence-to-sequence tasks, and demonstrate its effectiveness on the arXiv summarization dataset.
Introduction
Standard Transformer models like BERT and RoBERTa are limited by a context window of typically 512 tokens. This constraint arises from the self-attention mechanism, which computes scores for every pair of tokens, leading to computational and memory requirements that scale quadratically ($O(n^2)$) with sequence length $n$. For tasks requiring the processing of long documents—such as summarizing scientific papers or answering questions based on entire book chapters—this limitation necessitates sub-optimal workarounds like text truncation or document chunking, which often result in the loss of critical cross-document context.
The Longformer addresses this bottleneck by introducing a sparse attention mechanism that scales linearly ($O(n)$) with sequence length. This allows the model to natively process thousands of tokens in a single pass, enabling more effective reasoning over long-range dependencies.
Methods
The core innovation of the Longformer is its sparse attention pattern, which replaces the dense $O(n^2)$ matrix with a combination of three components:
- Sliding Window Attention: Each token attends only to a fixed-size window of neighboring tokens. By stacking multiple layers, the model builds a large receptive field, similar to convolutional neural networks.
- Dilated Sliding Window: To further expand the receptive field without increasing computation, the sliding window can be "dilated" by skipping tokens, allowing higher layers to capture even longer-range dependencies.
- Global Attention: To support tasks requiring a holistic document view, specific tokens (e.g.,
[CLS]for classification or question tokens for QA) are granted global attention, allowing them to attend to all other tokens and vice versa.
To implement this efficiently, the authors developed a custom CUDA kernel using TVM, as standard deep learning libraries do not natively optimize the "banded matrix multiplication" required for this sparse pattern. Additionally, Longformer uses separate linear projections for local and global attention to prevent interference.
Experiments and Results
The Longformer was evaluated across a wide range of tasks, including character-level language modeling, question answering, and summarization.
Efficiency and Scaling
A key advantage of the Longformer is its efficiency. While standard self-attention models quickly exhaust GPU memory beyond 1,000 tokens, the Longformer scales linearly, comfortably handling sequences of 32,000 tokens or more.
Performance Highlights
- Question Answering: Longformer-large set new state-of-the-art (SOTA) results on long-context benchmarks, achieving 81.9 F1 on WikiHop (+3.6 points) and 77.3 F1 on TriviaQA (+4 points).
- Character-Level LM: The model achieved SOTA Bits-Per-Character (BPC) on
text8(1.10) andenwik8(1.00). - Summarization: The Longformer-Encoder-Decoder (LED) variant, initialized from BART, achieved SOTA on the arXiv summarization dataset with a 46.63 ROUGE-1 score.
Conclusion
The Longformer demonstrates that linear-scaling sparse attention is a powerful and efficient alternative to standard self-attention for long-document tasks. By combining local windowed attention with task-specific global attention, it eliminates the need for document chunking and preserves long-range context. Future work includes exploring new pretraining objectives for the LED variant and pushing sequence lengths even further.
Summary updated