- Introduced by
- Beltagy, Peters & Cohan (Longformer); Zaheer et al. (BigBird)
- Year
- 2020
- Type
- Sparse-attention Transformer for long sequences
What does it do?
Longformer (Beltagy, Peters & Cohan, 2020) and BigBird (Zaheer et al., 2020) are Transformer variants that replace the dense attention of a standard Transformer with sparse attention patterns.
For a fixed window size and a fixed number of global tokens, the attention cost grows linearly (O(n)) rather than quadratically (O(n²)) with sequence length, so these models can take inputs of thousands of tokens, such as whole documents, that a conventional Transformer cannot process in one pass.
The intuition behind the method
A standard Transformer compares every token with every other token, so doubling the input length roughly quadruples the attention cost. This is why standard models are usually limited to about 512 tokens. Long-sequence Transformers rest on the observation that much of the useful information in text is local, so most tokens attend mainly to their neighbours within a sliding window. A few designated global tokens can see the whole sequence, which lets information travel across long distances. The cost then grows in step with text length, so long documents become practical to process.
History
Both methods appeared in 2020 as responses to the quadratic cost of dense attention, which had limited Transformers to short inputs. Beltagy, Peters and Cohan introduced Longformer, combining sliding-window attention with global attention for long documents. Zaheer et al. introduced BigBird, which adds random attention links to its sparse pattern. Both works made sparse attention a practical route to processing long sequences.
When to use it
Choose a long-sequence Transformer when your inputs are long documents, substantially longer than the roughly 512 tokens a standard Transformer accepts, and the task needs the full text rather than an excerpt. Do not use it when inputs already fit within a standard Transformer window, since the added complexity then brings no benefit. Training is computationally demanding and needs a substantial labelled collection. The size required depends on the task and should be judged by performance on held-out data rather than by a fixed minimum. These models are normally initialised from pretrained checkpoints and fine-tuned on the target task. Fine-tuning on a small labelled set can still be unstable and overfit, so with little data a classical baseline such as Random Forest or XGBoost is a sensible reference point. Compare the approaches on your own validation split before choosing. Global token positions must also be set deliberately.
Limitations and considerations
- Training and inference are computationally heavy; in practice a GPU is strongly recommended.
- The model overfits on small document collections, so results obtained from a small labelled set are unreliable.
- Performance depends on choosing global token positions correctly; poor choices weaken the long-range pathways the design relies on.
- Linear scaling refers to the attention cost; the remaining network layers still add computation for long inputs.
- The models produce predictions only. They do not explain their decisions, and any explanation of which parts of a document drove a prediction requires a separate attribution method.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Accepts inputs of thousands of tokens, so whole documents can be processed where a standard Transformer window would truncate them.
- Attention cost grows linearly with sequence length rather than quadratically, for a fixed window size and number of global tokens.
- Combines local sliding-window context with global tokens that carry long-range information across the document.
- Designed for long-document tasks, so the model sees the full text rather than a truncated excerpt.
Applications
- Classification of long documents such as reports and articles.
- Analysis of long legal texts that exceed a standard Transformer window.
- Document-level prediction over full-length text rather than truncated excerpts.
How it works
How it works
The core change is to replace the dense, all-pairs attention of the standard Transformer, whose cost is O(n²) in the sequence length n, with a sparse pattern whose cost is O(n). Attention is the bottleneck for long inputs, so this change is what makes inputs of thousands of tokens feasible.
Formula
O(n^2) \;\rightarrow\; O(n)
Most tokens attend only to a fixed-width sliding window of nearby tokens rather than to the whole sequence. Local context carries much of the signal in text, and because each token attends to a bounded number of neighbours, the cost grows linearly with length. BigBird additionally adds random attention links to this local pattern.
A small set of designated global tokens attends to, and is attended by, the entire sequence. These tokens provide the main channel for long-range information in the windowed pattern. Their positions must be chosen deliberately, for example a classification token or question tokens, because a poor choice weakens the paths that connect distant parts of the text.
A task-specific head sits on top of the sparse-attention encoder and produces the final prediction, such as a document-level classification. The model therefore takes the full-length document rather than a truncated excerpt. The head outputs predictions only and does not by itself explain them.
Common pitfalls
- Using the model on short texts that fit a standard 512-token Transformer, paying the extra complexity for no benefit.
- Setting global token positions carelessly, which weakens the long-range information flow the design depends on.
- Training on a small document set and treating the overfit result as a reliable model.
- Abandoning the approach because no GPU is available, without considering a smaller configuration or a classical baseline.
Frequently asked questions
How is this different from a standard Transformer like BERT?
A standard Transformer uses dense attention, comparing every token with every other token at O(n²) cost, which in practice limits inputs to about 512 tokens. Longformer and BigBird use sparse attention, mostly local sliding windows plus a few global tokens, so the cost grows linearly and inputs of several thousand tokens become feasible.
When should I prefer Longformer or BigBird over classical ML?
Use them when your texts are genuinely long and you have a substantial labelled collection, ideally starting from a pretrained checkpoint. With small collections, fine-tuning can be unstable and the model can overfit, and a classical method such as Random Forest or XGBoost is often the safer starting point. Make the decision on your own validation split.
What are global tokens and why do they matter?
Global tokens are a small set of positions allowed to attend to, and be attended by, the entire sequence. They are the main channel through which long-range information moves across the document, so choosing their positions correctly matters for performance.
Do I need special hardware?
A GPU is strongly recommended. Even with linear-scaling attention, these models use sizeable networks on long inputs, so training and inference are computationally demanding.
Related methods
Sources
Beltagy, I., Peters, M. E. & Cohan, A. (2020). Longformer: The Long-Document Transformer. arXiv.
Open publication referenceZaheer, M. et al. (2020). Big Bird: Transformers for Longer Sequences. NeurIPS.
Open publication reference