Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path →
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.
What changes against Transformer-XL: one long pass instead of remembering
Rung 1 keeps the window small and remembers across windows. This rung makes the window itself long, by attacking the reason it could not be: comparing every position with every other is quadratic, so doubling the sequence quadruples the cost, and a few thousand tokens is where that becomes unaffordable.
The change is to stop attending to everything. Each token attends only to a sliding window of its neighbours, which makes the cost linear in sequence length rather than quadratic. Information still travels further than the window, because stacking layers composes overlapping windows the way stacked convolutions grow a receptive field, and dilating some windows — attending to every kth neighbour instead of every one — can widen the reach at no extra cost (the paper uses it only in its character-level model).
The piece that makes it work on real tasks rather than just on perplexity is small and important: a handful of global tokens that attend to the whole sequence and are attended by all of it. A classification token or the tokens of a question need to see everything, and a pure window pattern would deny them that. So the pattern is task-aware by design — you choose which positions get global attention based on what the model is for, which is a deliberate departure from the uniformity of rung 2 on the transformer ladder.
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 n² 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.
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.
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:
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.
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: a dilated window has gaps, so it reaches further for the same number of connections — like looking at every other token instead of every adjacent one. In the paper this is used sparingly: only the character-level language model dilates (a small dilation on a couple of heads in upper layers), and the widely used RoBERTa-based Longformer does not dilate at all. Depth alone builds the receptive field there, with the per-layer cost staying linear.
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.
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.
How Longformer attention is built and trained, step by step
Longformer, from Beltagy, Peters and Cohan at the Allen Institute for AI, replaces each self-attention layer with a sparse pattern that a pretrained model can adopt without changing anything else. The paper uses it in two settings: a character-level language model trained from scratch, and a RoBERTa model extended to 4,096 tokens and fine-tuned on long-document tasks.
- Sliding window. Each token attends to w/2 tokens on each side, so a layer costs O(n × w). The pretrained Longformer uses w = 512 in every layer.
- Dilation, only where it helps. A window with gaps of size d covers d × w positions for the same cost. In the character model the lower layers use no dilation, and the higher layers apply a small dilation on only two heads, so most heads keep fine local detail while a few look further.
- Windows that grow with depth. In the character model, windows are small in the bottom layers and larger at the top: local patterns first, wider context later.
- Global attention on chosen tokens. A few positions, chosen per task, attend to every token, and every token attends to them. These are the classification token for classification, the question for question answering, and the question plus the candidate answers for WikiHop.
- Separate projections for global attention. Global attention gets its own query, key and value projections, initialized from the local ones, so the two kinds of attention can learn different behaviour.
- A kernel that makes sparsity pay. A masked dense matrix multiply costs as much as full attention, so the paper provides three implementations: a loop, which is memory-efficient but slow; a chunked version that splits the sequence into overlapping blocks of size w and uses ordinary matrix multiplies, without dilation, used for pretraining and fine-tuning; and a custom CUDA kernel written with TVM that supports dilation, used for the character model.
- Staged training for the character model. Training ran in five phases, starting at sequence length 2,048 with a window of 32, roughly doubling both each phase while halving the learning rate, and ending at 23,040 characters with a window of 512.
- Continue pretraining from RoBERTa. For downstream tasks the authors start from RoBERTa's weights and set the window to 512. They initialize 4,096 position embeddings by copying RoBERTa's 512 learned ones eight times, then continue masked-language-model pretraining for 65,000 updates at length 4,096 on a mix of books, Wikipedia, and long news and story documents.
- Fine-tune with global tokens, and LED for generation. Fine-tuning puts global attention on the task tokens. The Longformer-Encoder-Decoder (LED) uses the same pattern in the encoder of a BART-initialized model, for inputs up to 16,384 tokens.
Why copying the position embeddings works
Within any 512-position stretch, the copied embeddings are exactly RoBERTa's, so the local relative structure that attention learned is preserved almost everywhere. Only the seams every 512 positions are new. Training therefore starts close to RoBERTa's quality, instead of relearning position from scratch with a random initialization.
Longformer attention with window, dilation and global tokens
oi = Σj ∈ 𝒩(i) ∪ 𝒢 softmaxj( qi^s · kj^s / √d ) vj^s for ordinary tokens i
og = Σj = 1…n softmaxj( qg^g · kj^g / √d ) vj^g for global tokens g ∈ 𝒢
reach after ℓ layers ≈ ℓ · δ · w
w— the window size, the number of neighbours each token attends to; 512 in the pretrained model.δ— the dilation, the gap between attended positions; 1 means a contiguous window.𝒢— the global tokens, picked per task. Ordinary tokens add them to their own window.s,g— the two projection sets: the local projections used by ordinary tokens, and the separate projections used to compute the global tokens' own outputs.ℓ · δ · w— the receptive field at the top of a stack of ℓ such layers.
At the extremes: with w = n every token sees every other and the layer is ordinary full attention, so BERT is a special case. Making every token global also gives back full attention, at full cost. With w small and no global tokens, information needs many layers to cross a document: 12 layers of a 512 window reach about 6,000 tokens. A large δ widens reach but skips neighbours, which is why the paper keeps most heads undilated.
What Beltagy, Peters and Cohan ran, and what Longformer showed
Two kinds of experiment. First, character-level language modeling on text8 and enwik8, to test the attention pattern by itself: a small 12-layer model reached 1.10 bpc on text8 and 1.00 on enwik8, state of the art for models of that size, and a large 30-layer model with 102M parameters reached 0.99 on enwik8, matching larger models. Second, a RoBERTa-based Longformer at 4,096 tokens, fine-tuned and compared with RoBERTa, which has to cut or chunk anything past 512 tokens.
| Task (development set) | RoBERTa-base | Longformer-base |
|---|---|---|
| WikiHop (QA) | 72.4 | 75.0 |
| TriviaQA (QA) | 74.3 | 75.2 |
| HotpotQA (QA) | 63.5 | 64.4 |
| OntoNotes (coreference) | 78.4 | 78.6 |
| IMDB (classification) | 95.3 | 95.7 |
| Hyperpartisan (classification) | 87.4 | 94.8 |
The pattern of gains is the finding. They are largest where documents are long and the answer depends on connecting distant parts, such as Hyperpartisan and WikiHop, and smallest where most inputs already fit in 512 tokens, such as IMDB and OntoNotes. Longformer-large set new state-of-the-art results on WikiHop and TriviaQA, and the encoder-decoder version, LED, did the same for arXiv summarization.
The ablations support the design. On text8, windows that grow from the bottom layer to the top beat both fixed and shrinking windows, and dilation on two heads added a small further gain. On WikiHop, removing the separate global projections hurt, and removing global attention as well hurt much more. In pretraining, copying RoBERTa's position embeddings gave a far better starting loss than random initialization.
The negative result is in the engineering: the saving only appears with the custom chunked or CUDA implementations, because a masked dense multiply costs as much as full attention.
What the authors admit, and what happened next
The honest admission is in the engineering rather than the method: realising the theoretical saving requires a custom kernel, because a sparse attention pattern expressed as a masked dense matrix multiply costs exactly what dense attention costs. Sparsity on hardware built for dense arithmetic is not free, and the paper had to write the kernel to get the numbers.
The methodological cost is that no two distant tokens interact directly — any long-range relationship must be composed through depth, which is strictly weaker than one attention operation seeing both ends, and the global tokens are a hand-placed exception to a limitation rather than a removal of it.
How it held up: this is the most instructive reversal on the ladder. The field largely abandoned approximate attention, and the reason is that the diagnosis was wrong about which resource was scarce. Attention's practical bottleneck at these lengths turned out not to be arithmetic but memory traffic — writing an enormous intermediate matrix out to slow memory and reading it back. Once that was fixed by never materialising the matrix, exact attention became fast enough that giving up exactness to save operations stopped paying. Sliding windows did survive, but as one component inside hybrid designs rather than as the answer, and the lesson — profile which resource is actually binding before redesigning the mathematics — is the transferable one.
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.
Write the body of receptive_field. Everything else in the file is
already written; this is the function the paper is about. Press ▶ Run to check it against
the 5 assertions below — they start red.
- Full attention is quadratic
- Windowed attention is linear in n
- Windowing saves a factor of n/w
- Global tokens add only a linear term
- The receptive field grows linearly with depth
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 + global tokens, with dilation in the character model — 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.
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.
Questions people ask about Longformer
How does global attention work in Longformer?
A few positions chosen for the task get global attention: they attend to every token in the sequence, and every token attends to them. For classification that is the classification token, for question answering it is the question tokens, and for WikiHop it is the question and the candidate answers. Ordinary tokens simply add the global tokens to their sliding window. The global tokens' own outputs are computed with a separate set of query, key and value projections, initialized from the local ones, so they can learn a different kind of attention. Because there are only a handful of global tokens, they add a cost of about 2 times n times g, which stays linear in sequence length.
What is sliding window attention, and why does it scale linearly?
In sliding window attention each token attends only to a fixed number of neighbours, w/2 on each side, instead of to the whole sequence. The work per layer is then n times w rather than n squared, and since w is a constant, doubling the document only doubles the cost. Information still travels further than one window because layers stack: after l layers a token can be influenced by positions about l times w away, like the growing receptive field of a convolutional network. Longformer's pretrained model uses w = 512 in every layer. Mistral 7B later used the same idea in a decoder, with a 4,096-token window.
How was Longformer created from RoBERTa?
The authors took RoBERTa's pretrained weights, replaced full attention with a 512-token sliding window plus global attention, and extended the input from 512 to 4,096 positions. The new position embeddings were initialized by copying RoBERTa's 512 learned position embeddings eight times, so every local stretch looks exactly like what RoBERTa already knew; random initialization started from a much worse loss. They then continued masked-language-model pretraining for 65,000 updates at sequence length 4,096, on a corpus of books, Wikipedia and long news and story documents. The result is a drop-in long-document encoder that is fine-tuned exactly like RoBERTa, with global attention placed on the task's key tokens.
What is dilated sliding window attention in Longformer?
A dilated window attends to every d-th token instead of every adjacent one, so with the same number of attended positions it covers d times as much of the sequence. It is borrowed from dilated convolutions. Longformer uses it sparingly: in its character-level language model, the lower layers have no dilation and the upper layers apply a small dilation on just two heads, keeping fine local detail in most heads while a few reach further. The paper's ablation found this gave a small improvement. The RoBERTa-based Longformer that most people download uses no dilation at all, because the efficient chunked implementation it was trained with does not support it.
Why did Longformer need a custom CUDA kernel?
Because a sparse pattern computed the obvious way saves nothing. If you compute the full n by n score matrix and then mask out everything outside the window, you pay the full quadratic cost in time and memory. Deep-learning libraries at the time had no banded matrix multiply, so the authors wrote their own. The paper offers three versions: a simple loop that saves memory but is slow, a chunked version that splits the sequence into overlapping blocks of size w and uses ordinary dense multiplies, which they used for pretraining and fine-tuning, and a custom CUDA kernel built with TVM that also supports dilation, used for the character-level experiments.
4 questions to test what you just read
Work each one out before you open it — a number, a prediction or a reason, not a definition. Producing the answer is what makes it stick. Your progress is kept on this device.
For n = 4,096, w = 512 and two global tokens, how many query-key pairs does Longformer compute against full attention, and what is the saving?
Show answer
Window: 4,096 × 512 = 2,097,152. Global: 2 × 4,096 × 2 = 16,384. Total ≈ 2.11 million, against 4,096² ≈ 16.8 million for full attention, about 8× fewer. At 16,384 tokens the same window would save about 32×.
In a 12-layer model with w = 512 (256 on each side) and no global tokens, can a question at position 0 influence a token at position 4,000, and how many layers would it need?
Show answer
Each layer moves information at most 256 positions, so reaching 4,000 takes ⌈4,000 / 256⌉ = 16 layers. Twelve layers reach only about 3,072 positions, so no. A global token covers that distance in one layer, which is why the question gets global attention.
If 512 position embeddings are copied eight times to fill 4,096 positions, which original embedding does position 1,000 receive?
Show answer
1,000 mod 512 = 488, the embedding RoBERTa learned for position 488. Every stretch between seams is an exact copy, so local relative structure is unchanged; only the boundaries at 512, 1,024 and so on are new to the model.
Why do Longformer's gains over RoBERTa come out large on Hyperpartisan and WikiHop but tiny on IMDB?
Show answer
RoBERTa only loses when inputs exceed 512 tokens and the answer needs the parts it cuts off. Most IMDB reviews fit in 512 tokens, so there is little to gain (95.3 to 95.7). Hyperpartisan articles and WikiHop's multi-document contexts are long and need distant evidence connected, so reading the whole input pays (87.4 to 94.8, and 72.4 to 75.0).
Making the context long
Attention costs quadratic in sequence length. Six attempts to escape that, ending with two 2026 papers that give the model a memory instead.
- 1Transformer-XL2019
- 2Longformer2020
- 3Ring Attention2023
- 4Mamba2023
- 5Titans2025
- 6Engram2026
Explore the topic
See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.
More Paper Breakdowns
- Attention Is All You NeedThe paper that introduced the Transformer — the architecture underneath GPT, BERT, Claude, and Gemini. Self-attention, Query/Key/Value, multi-head attention, positional encoding, and the real BLEU results, explained in depth with analogies and animated visuals, not just equations.Read →
- BERTThe paper that made "pre-train once, fine-tune anywhere" the default for NLP. Bidirectional context, masked language modeling, next-sentence prediction, the [CLS]/[SEP] tokens, and the GLUE sweep — explained with analogies, not just equations.Read →
- Chain-of-Thought PromptingShow a model a few worked examples with their reasoning and it starts solving problems it used to fail. Why reasoning emerges only at scale, the GSM8K jump, zero-shot "Let's think step by step", and how CoT seeded modern reasoning models.Read →
- LoRAThe paper that made fine-tuning affordable. Freeze the giant pretrained model and train two tiny low-rank matrices instead — up to 10,000x fewer trainable parameters, no extra inference latency, and swappable per-task adapters. The road to QLoRA and PEFT.Read →
- Direct Preference OptimizationRLHF without the RL. Align a model to human preference pairs with one simple classification-style loss — no separate reward model, no unstable reinforcement learning. Why "your LM is secretly a reward model", and how DPO became the default alignment method.Read →
- ChinchillaThe compute-optimal scaling law that said models were too big. Parameters and training tokens should scale together — ~20 tokens per parameter — so a 70B model trained on 1.4T tokens beat the 280B giants. Why every model since trains on trillions of tokens.Read →