Paper Breakdowns  /  Longformer
Paper 56~11 min read2020worked math + runnable code
Paper Breakdown

Longformer,
explained.

Early transformers had a hard ceiling: about 512 tokens, roughly a couple of paragraphs. Feed them a full article and the cost exploded, because every token insisted on looking at every other — a quadratic appetite. Longformer asked a simple question: does a token really need to see the whole document at once, or just its neighborhood? Restrict attention to a sliding window, add a few tokens that see everything, and the cost drops from quadratic to linear. Here's the pattern that let transformers finally read long documents.

Video breakdown
The animated walkthrough is in production.
Read the full breakdown below in the meantime ↓
01

The quadratic wall

Self-attention's power comes from letting every token look at every other token — but that is also its curse. With a sequence of n tokens, there are pairs to score, so both compute and memory grow with the square of the length. Double the input and the cost quadruples. This is why the original BERT-style transformers were capped around 512 tokens: a full document of a few thousand tokens would blow past any reasonable memory budget.

But do you really need all-to-all attention? In language, most of what a word depends on is nearby — the phrase it's in, the sentence around it. Long-range links exist, but they are sparse. Longformer's bet is that you can get most of the benefit of full attention with a mostly-local pattern, plus a little global connectivity where it matters — and pay only linear cost.

The one-sentence version

Full attention is quadratic; let each token attend to a local window plus a few global tokens, and attention becomes linear in length — long documents become tractable.

02

Sliding-window attention

The core move: instead of attending to all n tokens, each token attends only to a fixed window of w neighbors around it — like a convolution's kernel, but for attention. That changes the arithmetic completely:

full attention = pairs  ·  sliding window = n · w pairs  →  savings = n / w

The window size w is a fixed constant, so the cost n·w grows only linearly with sequence length — double the document and the cost merely doubles. The saving over full attention is n/w, and it grows with the document: at 4,096 tokens with a 512 window, that is 8× fewer pairs; at 16,384 tokens, 32× fewer. The longer the input, the more the window helps. The runnable version below computes these costs directly.

03

Depth and dilation

A fair objection: if a token only sees w neighbors, how can the model capture a dependency spanning the whole document? The same way a convolutional network does — through depth. A token's window covers its neighbors; those neighbors' windows cover their neighbors; so with each stacked layer the receptive field widens. After enough layers, information can flow from any token to any other, even though no single layer looked far.

Longformer also borrows dilation from convolutions: in some attention heads or layers, the window has gaps, so it reaches further for the same number of connections — like looking at every other token instead of every adjacent one. A few dilated heads dramatically extend the range at no extra cost, while non-dilated heads keep the fine-grained local view. The receptive field ends up large enough for long-range reasoning while the per-layer cost stays linear.

04

Global tokens

Some tokens genuinely need to see everything, and waiting for depth to route information to them is too slow. So Longformer designates a handful of global tokens: they attend to all tokens, and all tokens attend to them. These are placed where the task needs a global summary — the [CLS] token for classification, the question tokens in question answering.

Longformer's attention, in pieces
WindowEvery token attends to w local neighbors — the bulk of the connectivity, linear cost.
DilationSome heads skip tokens to reach further for free.
GlobalA few task tokens attend everywhere (and are attended to) — cost 2·n·g.
Totaln·w + 2·n·g — still linear in n, since w and g are constants.

Because only a few tokens are global, they add just a linear term to the cost. The result is a pattern that is overwhelmingly local and cheap, with a thin backbone of global connectivity for whatever the task needs to see whole — the best of both worlds at linear price.

RUN IT YOURSELF

Quadratic vs linear, counted

The whole argument is a pair count. This compares full attention's n² pairs against the sliding window's n·w and confirms the window is linear in length (double n, double the cost), that the saving is exactly n/w (8× at 4,096 tokens with a 512 window), that global tokens add only a linear 2·n·g term, and that stacking layers grows the receptive field so distant tokens still connect. Change the sequence length, window, or global count and watch the costs scale.

CPython · WebAssembly
05

What it unlocked

By breaking the quadratic wall, Longformer let transformers operate at document scale — thousands of tokens instead of hundreds:

CapabilityWhy it followed
Long-document tasksClassification, QA, and coreference over full articles — no truncation or chunking hacks.
~4,096+ token inputs8× the standard limit at the time, at manageable cost.
Drop-in pre-trainingLongformer could continue from a pre-trained model's weights, inheriting its knowledge with the new attention.
Encoder-decoder variantLED extended the idea to summarization of long documents.

Before Longformer, handling a long document meant awkward workarounds — truncate it, split it into chunks and stitch results, or lose cross-chunk context. Longformer let a single model read the whole thing, which is qualitatively different for tasks where the answer depends on connecting distant parts.

06

Its place in the story

Longformer was part of a wave of efficient transformers (alongside Reformer, BigBird, and others) all attacking the quadratic-attention bottleneck with different sparsity patterns. Its particular combination — sliding window + dilation + global tokens — was clean, effective, and easy to adopt, and it became a go-to for long-document NLP.

The broader arc is worth noting. Sparse-attention patterns like Longformer's were one answer to long context; a different answer arrived later from the systems side — FlashAttention made full attention dramatically cheaper by reorganizing the computation, and hardware kept growing, so many modern models run full attention over long contexts after all. But the question Longformer sharpened — how much attention does a token actually need? — remains central, and sparse patterns are back in force as context windows stretch toward millions of tokens.

Worth knowing

The convolution analogy runs deep: a sliding-window attention layer is like a convolution whose weights are computed from content (via attention) rather than fixed — local receptive field, growing with depth, exactly as in a CNN.

Frequently asked

Quick answers

What does Longformer change?

It replaces full O(n²) attention with a sliding window (O(n·w)) plus a few global tokens, making attention linear in sequence length so transformers can read long documents.

How does a local window see far?

Through depth: stacked window layers compound the receptive field like a CNN, and dilated windows reach further for free — so distant tokens still connect.

What are global tokens for?

Task-important positions (like [CLS] or question tokens) that attend to everything and are attended to by everything, carrying global information in one step at linear cost.

Is sparse attention still used?

Yes — alongside efficient full-attention kernels like FlashAttention. As context windows grow very long, sparse patterns like Longformer's are increasingly relevant again.

Finished this one? 0 / 101 Paper Breakdowns done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Paper Breakdowns