Longformer
Longformer replaces dense full attention with sliding-window local attention plus task-motivated global attention so Transformers can process long documents.
Metadata
| Field | Value |
|---|---|
| Paper | Longformer: The Long-Document Transformer |
| Authors | Iz Beltagy, Matthew E. Peters, Arman Cohan |
| Year | 2020 |
| Venue | arXiv preprint |
| arXiv | 2004.05150 |
| Code | allenai/longformer |
| Status | full note started |
One-Line Takeaway
Longformer makes the attention pattern sparse:
where is sequence length, is local window size, and is the number of global tokens.
Question
Standard self-attention compares every token to every other token:
This gives:
For long documents, this is the limiting factor:
The paper asks:
Longformer answers by making most attention local and reserving full-range attention for selected global tokens.
Main Claim
The durable architecture claim is:
The method is a drop-in replacement for full self-attention:
The rest of the Transformer block can remain familiar: residual stream, normalization, feed-forward network, and stacking.
Architecture Contract
| Item | Contract |
|---|---|
| Input | long token sequence, usually documents |
| Main operator | sparse self-attention |
| Local pattern | each token attends to a fixed-size sliding window |
| Global pattern | selected task tokens attend broadly and can be attended by all tokens |
| Complexity target | linear in sequence length for fixed window and global-token count |
| Main use | long-document classification, QA, language modeling, summarization with LED |
| Main risk | attention pattern must match the task’s needed information flow |
Longformer is not a new modality. It is a Transformer attention pattern for long sequence inputs.
Sliding-Window Attention
For token position , define a local window radius :
The window size is:
Instead of attending to all keys:
Longformer attends to:
The sparse attention output is:
For fixed , local attention cost scales as:
This is linear in if is treated as a constant.
Global Attention
Local windows alone make long-range information flow slow. A token can only pass information across the sequence after many layers:
for layers.
Longformer adds global tokens:
For a global token :
For ordinary token :
This creates a route for task-specific long-range aggregation:
Mask View
Longformer can be read as a structured attention mask. Let:
Then:
This places Longformer inside the same general attention contract as Attention:
Relation to Dense Attention
| Attention Pattern | Each Token Reads | Complexity Sketch | Typical Use |
|---|---|---|---|
| dense | all tokens | short sequence Transformer | |
| local window | nearby tokens | long sequence locality | |
| dilated window | spaced nearby tokens | larger receptive field | |
| global attention | selected full-range tokens | task aggregation | |
| Longformer | local plus global | long documents |
Dense attention is more flexible, but it pays for all pairwise interactions. Longformer pays for a chosen communication graph.
Relation to Other Architecture Notes
| Paper | Relation |
|---|---|
| Attention Is All You Need | baseline dense Transformer attention |
| Transformer-XL | long context through recurrence and segment memory |
| BigBird | long context through local, global, and random sparse attention |
| Reformer | long sequence efficiency through LSH attention and reversible layers |
| ALiBi | length extrapolation through attention logit bias |
| Performer | approximate softmax attention with linear complexity |
| FlashAttention | exact dense attention with IO-aware kernel optimization |
Longformer is a sparse-pattern route, not an exact dense attention kernel route and not a positional extrapolation route.
Evidence to Read
| Claim | Evidence Type | What It Supports | Caveat |
|---|---|---|---|
| sparse attention scales to long documents | complexity and implementation experiments | long sequences can be processed without full attention | sparse pattern must match the task |
| local plus global works for NLP tasks | downstream long-document tasks | global tokens help task-level aggregation | global-token choice is task-dependent |
| pretraining matters | Longformer vs RoBERTa comparisons | long-context architecture benefits from pretraining | data and initialization matter |
| LED supports seq2seq long input | summarization experiments | sparse encoder attention can support long document generation | decoder and cross-attention still need careful cost accounting |
Implementation Reading
Check:
- What is the local window size ?
- Are attention windows one-sided, two-sided, or dilated?
- Which tokens receive global attention?
- Does each task define global tokens consistently?
- Are padding and document boundaries masked correctly?
- Does the implementation materialize dense masks anyway?
- Are results compared against truncation, chunking, and dense baselines at the same length?
The main failure mode is thinking the architecture “sees everything” because the document fits. In Longformer, each ordinary token sees a sparse neighborhood plus global routes.
Limitations
- Sparse attention encodes a communication pattern; if the task needs arbitrary pairwise interactions, local windows may miss them.
- Global token selection is task-dependent and can become a hidden design choice.
- Linear scaling holds when window size and global token count are controlled.
- Long input support does not remove memory costs from embeddings, activations, FFNs, or decoder-side generation.
- Long-document benchmarks can reward input length rather than better reasoning if truncation baselines are weak.
Common Misreadings
| Misreading | Correction |
|---|---|
| ”Longformer is just a bigger BERT.” | Its key architecture change is the sparse local/global attention pattern. |
| ”Linear attention means the same thing as Performer.” | Longformer is sparse exact attention over selected positions; Performer approximates softmax attention. |
| ”Every token attends globally.” | Only selected global tokens attend broadly; ordinary tokens use local windows plus global connections. |
| ”Longer input alone proves better architecture.” | Compare against truncation, chunking, retrieval, and dense baselines when possible. |
What to Remember
Longformer is the canonical sparse-attention paper for long documents:
For this wiki, keep it as the anchor for windowed sparse attention, separate from ALiBi/RoPE position mechanisms, Performer-style attention approximation, and FlashAttention kernel optimization.