Longformer

Longformer replaces dense full attention with sliding-window local attention plus task-motivated global attention so Transformers can process long documents.

Metadata

FieldValue
PaperLongformer: The Long-Document Transformer
AuthorsIz Beltagy, Matthew E. Peters, Arman Cohan
Year2020
VenuearXiv preprint
arXiv2004.05150
Codeallenai/longformer
Statusfull 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

ItemContract
Inputlong token sequence, usually documents
Main operatorsparse self-attention
Local patterneach token attends to a fixed-size sliding window
Global patternselected task tokens attend broadly and can be attended by all tokens
Complexity targetlinear in sequence length for fixed window and global-token count
Main uselong-document classification, QA, language modeling, summarization with LED
Main riskattention 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 PatternEach Token ReadsComplexity SketchTypical Use
denseall tokensshort sequence Transformer
local windownearby tokenslong sequence locality
dilated windowspaced nearby tokenslarger receptive field
global attentionselected full-range tokenstask aggregation
Longformerlocal plus globallong 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

PaperRelation
Attention Is All You Needbaseline dense Transformer attention
Transformer-XLlong context through recurrence and segment memory
BigBirdlong context through local, global, and random sparse attention
Reformerlong sequence efficiency through LSH attention and reversible layers
ALiBilength extrapolation through attention logit bias
Performerapproximate softmax attention with linear complexity
FlashAttentionexact 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

ClaimEvidence TypeWhat It SupportsCaveat
sparse attention scales to long documentscomplexity and implementation experimentslong sequences can be processed without full attentionsparse pattern must match the task
local plus global works for NLP tasksdownstream long-document tasksglobal tokens help task-level aggregationglobal-token choice is task-dependent
pretraining mattersLongformer vs RoBERTa comparisonslong-context architecture benefits from pretrainingdata and initialization matter
LED supports seq2seq long inputsummarization experimentssparse encoder attention can support long document generationdecoder 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

MisreadingCorrection
”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.