Sliding Window Attention: From Local Windows to Global Context
Borrowing from convolutions — how a sliding local window keeps attention linear in sequence length, recovers a global receptive field by stacking layers, and reaches across the whole document through a small set of task-driven global tokens.
The previous blog derived sparse factorized attention: replace the dense pattern with two structured sparse patterns of size each, reducing total cost from to . Strided and fixed factorizations preserved full reachability through two-hop paths, and the Sparse Transformer achieved state-of-the-art results on images, text, and audio.
But the Sparse Transformer has limitations. Its factorized patterns are rigid — strided attention assumes periodic structure, fixed attention designates relay positions by location. Neither adapts to the task at hand. And , while better than , still grows faster than linear.
Longformer (Beltagy, Peters, and Cohan, 2020) takes a different, simpler approach on the same axis. It combines a sliding window for local context with task-motivated global attention on a few designated tokens. The sliding window costs where is the fixed window size — linear in . The global attention adds where is the number of global tokens — also linear. The total is , strictly linear, with no growing exponents.
Even more importantly, Longformer is designed as a drop-in replacement for existing pretrained models. You can take a RoBERTa checkpoint, swap its full attention for Longformer’s sliding window attention, and continue pretraining. This makes it the first efficient attention mechanism that cleanly integrates with the pretrain-finetune paradigm that dominates modern NLP.
We will derive the entire mechanism from scratch, trace every connectivity set by hand, and verify every count numerically.
The Running Example
We use a sequence of tokens throughout the entire post, now interpreted as a short document rather than an image:
We fix the following parameters:
- Window size: (each token attends to tokens on each side)
- Dilation: (for dilated heads)
- Global tokens: (positions 0 and 8 are designated global)
For cost comparisons, the same model as always:
- , heads, , layers, fp16
We define the half-window radius as . With , we have . This is the number of positions each token can see on each side.
1. Sliding Window Attention
1.1 The connectivity set
In sliding window attention, each token attends only to tokens within a fixed distance on each side. The connectivity set for position in the bidirectional (encoder) case is:
This is the set of all positions within distance of position , clipped to the valid range .
1.2 Tracing for our running example
With , :
| Position | ||
|---|---|---|
| 0 | 3 | |
| 1 | 4 | |
| 2 | 5 | |
| 3 | 5 | |
| 4 | 5 | |
| 13 | 5 | |
| 14 | 4 | |
| 15 | 3 |
Positions 0 and 15 are at the boundary and see only tokens. Position 1 and 14 see tokens. All interior positions (2 through 13) see tokens.
1.3 Counting entries
The total number of attention entries across all positions is:
We split this into three groups: left boundary, interior, and right boundary.
Left boundary (positions ): Position has (it can look positions to the left and to the right, plus itself). So:
Simplify by factoring out :
Interior (positions ): Each has . There are such positions:
Right boundary (positions ): By symmetry with the left boundary:
Total:
Expand the second term by the distributive law:
Factor differently for clarity:
1.4 Numerical check
Substitute , :
Verify by direct summation from the table:
Interior: . Boundaries: . Total: .
1.5 Comparison to full attention and sparse factorization
For :
| Pattern | Total entries | Formula |
|---|---|---|
| Full bidirectional | ||
| Full causal | ||
| Sparse factorized (Blog 9) | 110 | |
| Sliding window () | 74 |
At , the sliding window already computes fewer entries than sparse factorization. But the real difference appears at scale.
1.6 Scaling comparison
For large , the approximate entries per pattern are:
| Full | Sparse | Window , | Window reduction vs full | |
|---|---|---|---|---|
| 1,024 | 1,048,576 | 49,152 | 525,312 | |
| 4,096 | 16,777,216 | 393,216 | 2,101,248 | |
| 16,384 | 268,435,456 | 3,145,728 | 8,404,992 | |
| 65,536 | 4,294,967,296 | 25,165,824 | 33,619,968 |
At moderate sequence lengths (), sparse factorization is more aggressive because grows slower than when is large. But at very long sequences, both are far better than full attention. The crucial difference is that the sliding window’s cost is a constant factor times — it does not grow with at all once is fixed. Sparse factorization’s cost still grows as .
1.7 Interpretation
Sliding window attention makes a strong bet: local context is what matters most. This is empirically well-supported. Kovaleva et al. (2019) showed that BERT’s attention heads overwhelmingly attend to nearby tokens — the vast majority of attention weight falls within a local window. The sliding window formalizes this observation into an architectural constraint.
But local context alone is not enough. Token 0 cannot reach token 15 in a single layer. The receptive field is limited to positions per layer. We need a mechanism for long-range information flow.
2. Receptive Field Growth Through Stacking
2.1 The key insight: layers compound the window
This is where the sliding window reveals a deep structural connection to convolutional neural networks.
Consider what happens when we stack multiple sliding window attention layers. In layer 1, token receives information from tokens within distance . In layer 2, each of those tokens has already aggregated information from its own window of radius . So token in layer 2 indirectly has access to information from tokens within distance of its original position.
2.2 Deriving the receptive field
Let denote the receptive field radius after layers. The receptive field is the set of original input positions that can influence a given token’s representation at layer .
Layer 1: Token directly attends to positions in .
Layer 2: Token attends to tokens in , each of which carries information from their own window of radius . The leftmost token carries information from as far left as . The rightmost token carries information from as far right as .
Layer : By the same argument applied inductively:
The total number of original input positions that can influence token after layers is .
2.3 Numerical check
With layers and ():
The total reachable width is positions. Since our sequence has only tokens, and , the top layer of a 12-layer model can access the entire sequence through its stacked windows.
For a practical model with and :
Total reachable width: . For a token far from the boundaries, this is enough to cover a sequence of up to 6,145 positions through local information propagation alone. Boundary tokens see less because the receptive field is clipped by the start or end of the sequence.
2.4 The CNN analogy
This is exactly how convolutional neural networks build global representations from local filters. A CNN with kernel size and layers has a receptive field of approximately . Each layer sees a small local neighborhood, but stacking many layers gives the top layer a view of the entire input.
Sliding window attention is the attention equivalent: each layer computes a local attention pattern (the “kernel”), and the receptive field grows linearly with depth. Wu et al. (2019) made this connection explicit, showing that stacked local attention layers and deep CNNs have similar representational properties.
The key difference from a CNN is that within each window, the attention weights are data-dependent — they are computed via the softmax of query-key dot products, not learned as fixed filter weights. So the model has the inductive bias of locality (from the window) combined with the flexibility of content-based retrieval (from attention).
2.5 When the receptive field is not enough
The receptive field of grows linearly with depth, but this growth relies on information propagating one window at a time through the network. For a 4,096-token document with and , an interior token’s receptive field of 6,145 positions is wide enough to cover the full document. But information from the far end still propagates one window at a time: traversing the full 4,096-token span requires about overlapping-window transfers, and the representation at each hop is lossy.
For tasks that require direct global access — classification from a [CLS] token, comparing a question to an answer span far away in the document — the receptive field argument is not sufficient. We need some tokens to have direct access to the entire sequence. That is the role of global attention, which we introduce in Section 4.
But first, we can improve the receptive field itself without changing the window size.
3. Dilated Sliding Window
3.1 Definition
A dilated sliding window introduces gaps of size (the dilation rate) between the attended positions. Instead of attending to consecutive positions within the window, each token attends to positions spaced apart.
The connectivity set for position with half-window radius and dilation is:
This is analogous to dilated convolutions (van den Oord et al., 2016), where the kernel samples inputs at regular intervals rather than contiguously.
3.2 Tracing for our running example
With , , : each token attends to positions (clipped to valid range).
| Position | ||
|---|---|---|
| 0 | 3 | |
| 1 | 3 | |
| 2 | 4 | |
| 3 | 4 | |
| 4 | 5 | |
| 5 | 5 | |
| 6 | 5 | |
| 7 | 5 | |
| 8 | 5 | |
| 9 | 5 | |
| 10 | 5 | |
| 11 | 5 | |
| 12 | 4 | |
| 13 | 4 | |
| 14 | 3 | |
| 15 | 3 |
3.3 Total entries
The total number of entries is identical to the non-dilated case in terms of count — each position still attends to at most positions. The entries have the same boundary effects. So:
The same 74 entries as the non-dilated window. The dilation does not change the number of computations — it changes which positions are attended to.
3.4 Receptive field with dilation
The reach of each layer increases by a factor of . With dilation and half-window , each token reaches positions up to away on each side.
After layers, each using dilation :
3.5 Numerical check
With , , :
Total reachable width: positions. Compare to the non-dilated receptive field of 49 — dilation doubles the reach for the same compute cost.
3.6 Multi-head mixing: some heads dilated, others not
This is a practical design choice that the paper emphasizes. In multi-head attention, different heads can use different dilation rates. Some heads use (no dilation) to capture fine-grained local context, while others use to reach distant positions.
For example, with heads: heads 1–6 use (local), heads 7–8 use (dilated). This gives the model both detailed local information and broader context, without increasing total compute.
3.7 Varying window size across layers
The paper found that varying across layers improves performance. Specifically, using small windows in lower layers and increasing window sizes in higher layers works best. The intuition is:
- Lower layers learn local features (part-of-speech, local syntax) — a small window suffices
- Higher layers learn document-level features (topic, long-range coreference) — a larger window helps
The ablation in Table 4 of the paper confirms this: increasing from bottom to top gives 1.21 BPC on text8, while decreasing from bottom to top gives 1.24 BPC, and using a fixed average gives 1.23 BPC. The ordering matters.
For the paper’s best character-level language model: the bottom layers use and the top layers use , with the window doubling every few layers across 5 training phases.
3.8 Interpretation
Dilation is a free lunch in terms of compute: same number of attention entries, larger receptive field. The price is that dilated attention skips intermediate positions — token with sees positions but not . The skipped positions’ information must be filled in by non-dilated heads or by multi-layer propagation. This is why the multi-head mixing strategy works: different heads cover different gaps.
4. Global Attention
4.1 Motivation: some tokens need to see everything
The sliding window — even with dilation and multi-layer stacking — builds global context gradually. But certain NLP tasks require specific tokens to have direct, single-layer access to the entire sequence:
- Classification: The [CLS] token must aggregate information from every position to make a prediction.
- Question answering: Question tokens must be compared to every position in the document to find the answer span.
- Summarization: Encoder tokens at the beginning of the document may need to attend to the conclusion.
For these tasks, purely local attention is not flexible enough. The model needs a way to inject global connectivity for a small number of task-relevant tokens.
4.2 The global attention mechanism
Global attention designates a small set of tokens as “global.” The attention pattern for global tokens is:
- A global token attends to all tokens in the sequence (not just its local window)
- All tokens in the sequence attend to the global token (not just tokens within its window)
This is a symmetric definition: the global token both reads from and is read by the entire sequence.
4.3 Formalizing the connectivity sets
Let be the set of global positions, with . The connectivity set for each position becomes:
For a global position :
The global token attends to all positions.
For a local position :
The local token attends to its window plus all global tokens.
4.4 Tracing for our running example
With , , (two global tokens):
| Position | Type | ||
|---|---|---|---|
| 0 | global | 16 | |
| 1 | local | 5 | |
| 2 | local | 6 | |
| 3 | local | 7 | |
| 4 | local | 7 | |
| 5 | local | 7 | |
| 6 | local | 6 | |
| 7 | local | 6 | |
| 8 | global | 16 | |
| 9 | local | 6 | |
| 10 | local | 6 | |
| 11 | local | 7 | |
| 12 | local | 7 | |
| 13 | local | 7 | |
| 14 | local | 6 | |
| 15 | local | 5 |
Note that when a global token is already in the local window (e.g., position 1’s window includes position 0, and position 7’s window includes position 8), the union does not add a new entry.
4.5 Counting entries with global attention
The total entries split into three parts:
Global tokens: Each of the global tokens attends to all positions:
Local tokens attending to their window: The local tokens each attend to their local window. We already derived this: approximately for interior tokens, with boundary corrections.
Local tokens attending to global tokens: Each local token adds at most entries for the global tokens. But some global tokens may already fall within the local window, so the exact count depends on the positions of the global tokens. In the worst case (all global tokens outside all local windows), this adds entries.
The total is:
For a clean upper bound:
4.6 Numerical check
From the table, summing all directly:
Compare to:
- Pure sliding window (no global): 74 entries
- Full bidirectional: 256 entries
- Our global + window: 126 entries
The 126 entries represent a increase over the pure sliding window (from 74 to 126) due to the 2 global tokens, but still a reduction from full attention (256). As grows, the global attention adds only , which is linear since is a small constant.
4.7 Complexity
For much larger than and :
Since both and are fixed constants independent of , the total complexity is linear in sequence length.
4.8 Which tokens get global attention?
This is task-specific, and the paper makes this an explicit design choice:
- Classification: Global attention on the [CLS] token only ()
- Question answering: Global attention on all question tokens ( = number of question tokens)
- Summarization (LED): Global attention on the first token of the encoder ()
- Language modeling: No global attention needed (purely autoregressive, left-to-right windowed attention with dilation)
The flexibility to choose different global tokens for different tasks, without retraining the model, is a practical advantage over approaches like the Sparse Transformer’s fixed relay positions.
4.9 Interpretation
Global attention plays the same role as the Sparse Transformer’s summary positions from Blog 9, but with two crucial differences. First, the global tokens are chosen by the task, not by a fixed positional rule. Second, the global attention is symmetric — the global token attends to all positions and all positions attend to the global token. In the Sparse Transformer’s fixed pattern, the summary positions only gathered information from their block; they did not explicitly broadcast back to all positions.
5. Separate Projections for Global Attention
5.1 The problem with shared projections
In standard multi-head attention, all queries, keys, and values are computed from the same projections , , . But in Longformer, local tokens and global tokens play fundamentally different roles. A local token is doing local context gathering — it only needs to attend to nearby positions. A global token is doing sequence-level aggregation — it needs to compare itself to every position in the document.
These two roles benefit from different learned projections.
5.2 The two-projection design
Longformer uses two sets of projection matrices:
- : The sliding window projections, used for computing local attention scores
- : The global projections, used for computing attention scores involving global tokens
When a global token computes its attention over the entire sequence, it uses for its query and , for the keys and values of all positions. When local tokens attend to the global token, they use for their queries but , for the global token’s key and value.
5.3 Why this matters
The paper shows through ablation (Table 10, WikiHop) that removing the separate linear projections for global attention drops accuracy from 73.8 to 72.2, a loss of 1.6 points. Removing both the separate projections and global attention entirely drops it to 65.5, a loss of 8.3 points. So the separate projections account for about of the total benefit of global attention in that ablation.
The separate projections are initialized from the values of , , so that at the start of finetuning, the global attention mechanism behaves identically to the sliding window attention. During finetuning, the global projections specialize: learns to produce queries that are effective for whole-sequence retrieval, while and learn to present information in a way that is useful for global aggregation.
5.4 Parameter count
The separate projections add one full set of , , matrices. Using the parameter count we derived in the taxonomy blog, one set of QKV projections costs parameters per layer. In our running model:
Across layers:
For a RoBERTa-base model with about 125M parameters, this is a 7.5% increase — modest for the capability it provides.
6. The Attention Matrix as a Banded Matrix
6.1 What the sliding window produces
This is the part that connects the mathematical pattern to implementation. In full attention, the score matrix is dense: every entry is computed. In sliding window attention, the score matrix is banded: only entries within distance of the diagonal are nonzero. Entries outside the band are not merely masked to — they are never computed at all.
A banded matrix is a matrix where all nonzero entries lie within a fixed number of diagonals from the main diagonal. For a window of radius , the band has width : the main diagonal plus diagonals above and diagonals below.
6.2 Visualizing the band structure
For and , the attention mask looks like (marking computed entries with and skipped entries with ):
This is the band. Standard matrix multiplication computes all entries. Banded matrix multiplication computes only the entries within the band.
6.3 Implementation challenge
The core computational challenge is that standard deep learning libraries (PyTorch, TensorFlow) do not natively support banded matrix multiplication. The operation produces a dense matrix, and there is no built-in way to say “only compute the band.”
The paper describes three implementation strategies with different trade-offs:
Longformer-loop: Compute each diagonal of the banded matrix separately in a loop. This is memory-efficient (only stores nonzero values) but extremely slow because loop iterations cannot be parallelized on GPUs.
Longformer-chunks: Split and into overlapping blocks of size with overlap . Multiply each block pair using standard dense matrix multiplication, then mask out the entries outside the band. This is fast (uses one large batched matrix multiply) but uses the memory of a perfect implementation because some entries are computed and then discarded.
Longformer-cuda: A custom CUDA kernel implemented using TVM (Chen et al., 2018) that computes exactly the banded entries. This is both fast and memory-efficient, and also supports dilation. The paper uses this implementation for the autoregressive language modeling experiments.
6.4 Memory scaling
The key advantage of all three implementations over full attention is memory. Full attention materializes an matrix, consuming memory. The sliding window implementations store at most values. Since is fixed:
This is the result shown in Figure 1 of the paper: Longformer’s memory scales linearly, while full self-attention’s memory grows quadratically and exceeds GPU capacity around tokens on a single GPU.
7. Autoregressive Language Modeling
7.1 Attention pattern for autoregressive models
For autoregressive (left-to-right) language modeling, the attention must be causal: position can only attend to positions . The sliding window becomes one-sided:
The paper uses dilated sliding window attention for the language modeling experiments, with varying dilation and window sizes across layers.
7.2 Staged training procedure
The paper adopts a staged training procedure where the window size and sequence length grow together across 5 phases:
| Phase | Sequence length | Window size | Learning rate |
|---|---|---|---|
| 1 | 2,048 | 32 → varies | 2.5e-4 |
| 2 | 4,096 | 64 → varies | 1.25e-4 |
| 3 | 8,192 | 128 → varies | 6.25e-5 |
| 4 | 16,384 | 256 → varies | 3.125e-5 |
| 5 | 23,040 | 512 → varies | 1.5625e-5 |
In each phase, the sequence length doubles (approximately) and the learning rate halves. The window size also increases, with the bottom layer starting small and the top layer receiving the largest window. This progressive schedule is motivated by the observation that the model needs many gradient updates to learn local context before it can benefit from longer context.
7.3 Results on character-level language modeling
The paper evaluates on text8 and enwik8, both standard benchmarks containing 100M characters from Wikipedia.
Small models (41M parameters, 12 layers):
| Model | text8 BPC | enwik8 BPC |
|---|---|---|
| T12 (Al-Rfou et al., 2018) | 1.18 | 1.11 |
| Adaptive Span (2019) | 1.11 | 1.02 |
| BP-Transformer (2019) | 1.11 | 1.02 |
| Longformer | 1.10 | 1.00 |
Longformer achieves new state-of-the-art on both datasets with the small model configuration: 1.10 BPC on text8 and 1.00 BPC on enwik8.
Large models (102M parameters, 30 layers):
| Model | #Param | enwik8 BPC |
|---|---|---|
| Transformer-XL (18 layers) | 88M | 1.03 |
| Sparse Transformer | 100M | 0.99 |
| Transformer-XL (24 layers) | 277M | 0.99 |
| Adaptive Span | 209M | 0.98 |
| Compressive Transformer | 277M | 0.97 |
| Longformer | 102M | 0.99 |
The large Longformer matches the Sparse Transformer’s 0.99 BPC and outperforms the comparable Transformer-XL (18 layers, 88M parameters) at 1.03 BPC. Models that achieve 0.97–0.98 BPC use more than twice the parameters.
7.4 Ablation: window arrangement matters
Table 4 of the paper shows that the arrangement of window sizes across layers is significant:
| Configuration | Dev BPC |
|---|---|
| Decreasing (512 → 32, top to bottom) | 1.24 |
| Fixed (average) | 1.23 |
| Increasing (32 → 512, bottom to top) | 1.21 |
And adding dilation on 2 heads further improves to 1.20 BPC. The principle is clear: lower layers need local detail, upper layers need broader context.
8. From Language Model to Pretrained Encoder
8.1 The pretrain-finetune gap
Every prior efficient attention method (Sparse Transformer, Adaptive Span, Compressive Transformer) was evaluated primarily on autoregressive language modeling. But the dominant paradigm in NLP is pretrain-finetune: pretrain a bidirectional model (like BERT or RoBERTa) with masked language modeling (MLM), then finetune on downstream tasks. None of the prior methods addressed this paradigm.
Longformer bridges this gap. The paper shows that you can take a pretrained RoBERTa checkpoint, replace its full self-attention with Longformer’s sliding window attention, and continue pretraining with MLM — all without changing the model architecture beyond the attention pattern.
8.2 The position embedding problem
RoBERTa uses learned absolute position embeddings with a maximum position of 512. To support longer documents (up to 4,096 tokens), new position embeddings are needed for positions 513–4,095.
The paper’s solution is simple and effective: copy the first 512 position embeddings repeatedly to fill positions 0–4,095. Specifically, position is initialized with the embedding from position .
8.3 Why copying works
This initialization works because BERT’s attention heads exhibit strong local patterns — most attention weight falls on the previous token, the next token, or the token itself. The position embedding encodes relative position within a period of 512. Since the sliding window is much smaller than 512 ( in the pretrained model), local attention patterns are perfectly preserved by the copied embeddings. The only artifacts appear at the period boundaries (positions 511, 512, 1023, 1024, etc.), where the copied pattern breaks.
Table 5 of the paper confirms this. Starting from the RoBERTa-base BPC of 1.846:
| Configuration | MLM BPC |
|---|---|
| Random position embeddings | 10.299 |
| Copied position embeddings (no training) | 1.957 |
| Copied + 2K gradient updates | 1.753 |
| Copied + 65K gradient updates | 1.705 |
Random initialization destroys the model (10.299 BPC — essentially random). Copied embeddings start at 1.957, close to the RoBERTa baseline of 1.846, and improve with continued pretraining. After 65K gradient updates, the model reaches 1.705, substantially better than the short-context RoBERTa baseline.
8.4 Frozen-weight experiment
The paper also tries freezing all RoBERTa weights and only training the new position embeddings. This achieves 1.850 BPC — almost exactly matching the RoBERTa baseline of 1.846. This confirms two things: the sliding window attention is fully compatible with the pretrained weights, and the remaining gap (1.850 vs 1.705) comes from the model learning to use longer context, not from fixing broken short-context behavior.
9. Downstream Tasks
9.1 Experimental setup
The paper evaluates Longformer on six downstream tasks spanning question answering, coreference resolution, and document classification:
| Task | Dataset | Avg. length | 95th percentile |
|---|---|---|---|
| QA | WikiHop | 1,535 | 3,627 |
| QA | TriviaQA | 6,589 | 17,126 |
| QA | HotpotQA | 1,316 | 1,889 |
| Coreference | OntoNotes | 506 | 1,147 |
| Classification | IMDB | 300 | 705 |
| Classification | Hyperpartisan | 705 | 1,975 |
All of these except IMDB have contexts that frequently exceed BERT’s 512-token limit. The baseline is RoBERTa-base, which must either truncate or chunk long documents.
9.2 Longformer-base results
| Task | RoBERTa-base | Longformer-base | |
|---|---|---|---|
| WikiHop (F1) | 72.4 | 75.0 | +2.6 |
| TriviaQA (F1) | 74.3 | 75.2 | +0.9 |
| HotpotQA (joint F1) | 63.5 | 64.4 | +0.9 |
| OntoNotes (avg F1) | 78.4 | 78.6 | +0.2 |
| IMDB (accuracy) | 95.3 | 95.7 | +0.4 |
| Hyperpartisan (F1) | 87.4 | 94.8 | +7.4 |
Longformer consistently outperforms RoBERTa-base on every task. The gains are largest on tasks with the longest contexts: WikiHop (+2.6), Hyperpartisan (+7.4, which has relatively long documents at 705 average length), and TriviaQA (+0.9, with 95th percentile at 17K tokens). The gains are smallest on OntoNotes (+0.2) and IMDB (+0.4), where most documents fit within 512 tokens.
9.3 Longformer-large results
On the QA tasks where long context matters most:
| Task | Previous SOTA | Longformer-large |
|---|---|---|
| WikiHop (F1) | 78.3 | 81.9 |
| TriviaQA (F1) | 73.3 | 77.3 |
| HotpotQA (joint F1) | 74.2 | 73.2 |
Longformer-large sets new state-of-the-art on WikiHop (+3.6 points) and TriviaQA (+4.0 points). On HotpotQA, it places second; the models that outperform it use graph neural networks, which encode an inductive bias specific to multi-hop reasoning.
9.4 WikiHop ablation: what matters
Table 10 of the paper provides a detailed ablation on WikiHop that isolates each component’s contribution:
| Configuration | Accuracy | |
|---|---|---|
| Full Longformer (seqlen 4,096) | 73.8 | — |
| RoBERTa-base (seqlen 512) | 72.4 | -1.4 |
| Longformer, seqlen 512, attention | 71.7 | -2.1 |
| Longformer, seqlen 2,048 | 73.1 | -0.7 |
| No MLM pretraining | 73.2 | -0.6 |
| No separate global projections | 72.2 | -1.6 |
| No global attention at all | 65.5 | -8.3 |
The most important finding: removing global attention entirely causes a catastrophic 8.3-point drop. This confirms that global attention is not optional — it is essential for tasks that require comparing distant parts of the document. The local sliding window builds contextual representations, but global attention is what allows the model to reason across the full sequence.
The second finding: the separate linear projections for global attention (, , ) matter. Removing them costs 1.6 points, confirming that global and local attention benefit from specialized projection matrices.
10. Longformer-Encoder-Decoder (LED)
10.1 Extending to sequence-to-sequence
The paper introduces LED, a variant that applies Longformer’s efficient attention to the encoder of an encoder-decoder Transformer. The encoder uses local+global attention (linear in input length), while the decoder uses full cross-attention to the encoded sequence and full self-attention over previously decoded tokens.
LED is initialized from BART (Lewis et al., 2020) — the same approach used for the encoder-only model with RoBERTa. Position embeddings are extended to 16K tokens by repeatedly copying BART’s 1K position embeddings.
10.2 Results on arXiv summarization
The arXiv summarization dataset (Cohan et al., 2018) contains scientific papers with long inputs (90th percentile: 14.5K tokens), making it an ideal test for LED.
| Model | Seqlen | R-1 | R-2 | R-L |
|---|---|---|---|---|
| Discourse-aware (2018) | — | 35.80 | 11.05 | 31.80 |
| Pegasus (2020) | — | 44.21 | 16.95 | 38.83 |
| BigBird (2020) | 4,096 | 46.63 | 19.02 | 41.77 |
| LED-large | 4,096 | 44.40 | 17.94 | 39.76 |
| LED-large | 16,384 | 46.63 | 19.62 | 41.83 |
At 16K tokens, LED-large achieves ROUGE scores that slightly outperform BigBird (which uses 4K tokens) — despite LED having no task-specific pretraining. It is initialized from BART with no additional pretraining, demonstrating that the efficient attention pattern alone is sufficient to process long documents effectively.
Figure 3 of the paper shows that ROUGE scores improve monotonically as the input length increases from 1K to 16K tokens, confirming that the model genuinely benefits from seeing more of the document.
11. The Unified View: Three Axis 3 Approaches
We have now seen three approaches to sparse attention, all living on Axis 3 of the taxonomy from the earlier blog. Let us place them in a single framework.
11.1 The common abstraction
Every approach defines a connectivity set for each position . The attention output is identical in all cases:
The only difference is the rule for constructing .
11.2 Comparison table
| Property | Full attention | Sparse Transformer | Longformer |
|---|---|---|---|
| per token | |||
| Total entries | |||
| Scales as | Quadratic | Superlinear | Linear |
| Reachability | 1 hop | 2 hops | hops (window) or 1 hop (global) |
| Task adaptation | None needed | None | Global token selection |
| Pretrain compatibility | Native | Train from scratch | Drop-in replacement |
| Data assumption | None | Periodic (strided) or none (fixed) | None (local + global) |
11.3 Interpretation
These three approaches represent a progression. Full attention is the starting point: maximum flexibility, quadratic cost. The Sparse Transformer was the first to show that structured sparsity can match or beat full attention quality — a surprising result that challenged the assumption that models need dense connectivity. Longformer builds on this insight with a simpler, more practical design: local windows handle most of the work, global tokens handle the rest, and the whole thing drops into existing pretrained models.
The trajectory is clear: from to to , with each step trading a small amount of per-layer connectivity for a large reduction in cost. The key realization is that full pairwise connectivity was always overkill — models need local detail and occasional global access, and that combination is achievable at linear cost.
Summary
Longformer replaces full self-attention with a combination of sliding window attention and task-motivated global attention, reducing complexity from to . The sliding window gives each token access to neighboring positions, and stacking layers grows the receptive field to positions — the attention analogue of a deep CNN. Dilated sliding windows further extend this reach by a factor of without additional cost. Global attention designates a small number of task-specific tokens (like [CLS] for classification or question tokens for QA) that attend to and are attended by every position, providing direct long-range connectivity where the task requires it. Separate projection matrices for global attention let the model specialize its global and local computations. The mechanism drops into existing pretrained models — replacing RoBERTa’s full attention with Longformer’s windowed attention and copying position embeddings to cover longer sequences — enabling continued pretraining on long documents. The result: state-of-the-art character-level language modeling (1.00 BPC on enwik8), consistent improvements over RoBERTa on document-level NLP tasks (up to +7.4 F1 on Hyperpartisan), new state-of-the-art on WikiHop and TriviaQA, and competitive summarization with LED at 16K-token inputs.
Previous: Why Full Attention Is Wasteful: Sparse Factorization from Scratch
Next: DeepSeek Sparse Attention: Learned Token Selection from Scratch
Enjoyed this post?
Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.