AI State of the Art
SOTA/Advanced Attention Mechanisms/22 min
Section 79

Sparse Attention: Longformer, BigBird, and the DeepSeek V4 Comeback

Listen as a podcast →

The observation: most of the attention is zero

Researchers looked at the attention matrices of trained Transformers. Surprise: the overwhelming majority of attention weights are close to zero.

Each token really listens to only a small fraction of the others. The rest of the computation? Wasted. It's as if, in a room of 1000 people, each person only listened to a few dozen voices. Why compute the interaction of each person with all 999 others?

Remember the complexity of standard attention: O(L2)O(L^2) in memory and compute, where LL is the sequence length. For 4096 tokens, that's ~16 million interactions (4096 × 4096). For 16K tokens, ~256 million. For 1M tokens, a trillion. Sparse attention (a matrix that is mostly zeros you never compute) starts from a simple observation: why compute millions of interactions if most are useless?

The idea: connect strategically

Instead of O(L^2) computed pairs, compute just the relevant ones. But which ones? You need predefined connectivity patterns.

Three patterns emerge from empirical observations:

  • Local: a token looks at its close neighbors. This is natural (in language, nearby words often have the most interaction ("the black cat") "black" mostly needs to see "cat"). Think of it as conversations between neighbors in a neighborhood.

  • Global: certain special tokens (such as CLS and SEP, the classification and separator markers that BERT inserts into its inputs) have the right to see and be seen by everyone. These are the "megaphones" of the sequence, they aggregate and broadcast global information. Without them, distant tokens could never communicate.

  • Random: a few long-range connections drawn at random. This might seem weird, but it's crucial to ensure information can travel from one end of the sequence to the other. It's the "small world" principle from graph theory: a few random shortcuts are enough to connect a network.

Longformer: three types of attention combined

Longformer, developed by Allen AI, combines three attention mechanisms in each layer:

Longformer: The Long-Document Transformer

Local attention (sliding window): 512-token window. Each token sees its 256 neighbors before and 256 after. This is the foundation, most useful interactions are local.

Dilated attention: in addition to the contiguous window, the token skips ahead, keeping only one token out of every few, over a larger window. Like looking into the distance with binoculars: you don't see everything, but important distant elements become accessible without excessive cost.

Global attention: certain designated tokens (the CLS token for classification, question tokens in QA) have full attention, they see everything and are seen by everyone.

Four square grids of equal size, each representing an attention matrix, with green cells marking the pairs that get computed and white cells the ones skipped. (a) Full n squared attention: the whole grid is green. (b) Sliding window attention: only a thick green band follows the diagonal. (c) Dilated sliding window: the diagonal band is perforated, every other cell is white, which spreads the band over a wider span. (d) Global plus sliding window: the diagonal band is completed by two green horizontal rows and two green vertical columns crossing the entire grid.
Longformer's three building blocks, next to full attention. Green is what actually gets computed. In (b) the local window; in (c) the same window but perforated, which covers twice the span for the same cost; and in (d) the addition of global tokens: the rows and columns crossing the whole grid are the tokens that see everything and are seen by everything. Longformer stacks all three patterns inside the same layer.Source : Longformer paper (Beltagy et al., figure 2), via Aman Arora

Result: complexity O(L * (W + G)) where W = window size, G = global tokens. If both are constant relative to L, you get linear complexity O(L). You go from quadratic to linear, that's a fundamental change.

Longformer results

Longformer processes 4K tokens (8x more than BERT-512) with comparable memory. On long document tasks (TriviaQA, WikiHop), it significantly outperforms baselines that truncate documents. Proof that seeing more context helps, even with restricted attention.

BigBird: rigorous theory

BigBird, developed by Google Research, formalizes sparse attention with random graph theory. The idea is to mathematically prove that sparse attention can be as expressive as full attention.

Big Bird: Transformers for Longer Sequences

Three components:

  1. Local: window of size W around each token
  2. Global: designated tokens that attend to everyone
  3. Random: each token attends to R randomly drawn tokens in the sequence
Figure 1 from the BigBird paper. Four square grids representing attention matrices, with white indicating absence of attention. (a) Random attention: orange cells scattered at random across the grid. (b) Window attention: a blue band of width 3 along the diagonal. (c) Global attention: two green rows at the top and two green columns on the left, the rest white. (d) BigBird: the three superimposed, blue diagonal band plus green global rows and columns plus orange random cells.
BigBird's three building blocks, isolated then combined. Orange: the randomly drawn connections. Blue: the local window. Green: the global tokens, which occupy entire rows and columns. Panel (d) is the pattern actually used. Compare it with the Longformer figure above: same family of ideas, but BigBird swaps dilation for randomness, and it is that randomness that makes the theoretical proof possible.Source : BigBird paper (Zaheer et al., figure 1), via Linsight

Key theorem: this combination forms an "expander graph", a graph with the property that information can propagate from any node to any other in very few hops. Concretely, this means that despite sparsity, the model retains the same theoretical power as full attention for approximating any sequence function.

BigBird vs Longformer

AspectLongformerBigBird
LocalYesYes
GlobalFixed tokensFixed tokens
RandomNoYes
DilatedYesNo
TheoryEmpiricalRigorous (expanders)
Randomness helps

Adding randomness seems counterintuitive, but it's mathematically smart. In graph theory, a few random connections transform a local network into a "small world" network where each node can reach every other node in few hops. It's the same phenomenon as the "six degrees of separation" between humans: a few random links are enough to connect everyone.

Other notable approaches

Sparse Transformer (OpenAI, 2019): the pioneer. Combines local + strided attention (every L\sqrt{L} tokens). Complexity O(L * sqrt(L)). The first demonstration that you could break the quadratic barrier without losing quality.

Three blocks side by side. At the top of each block, small grids showing the connectivity subsets; below, the full self-attention connectivity matrix, with input indices on the horizontal axis and output indices on the vertical axis. (a) Transformer: the entire lower triangle is blue. (b) Sparse Transformer with strided attention: two superimposed patterns, a blue diagonal band plus regularly spaced light cells forming secondary diagonals. (c) Sparse Transformer with fixed attention: a blue diagonal band plus light vertical columns placed at fixed intervals.
The original 2019 pattern. In (a) full attention. In (b) the strided variant: you keep the immediate neighbours and, on top of that, one token every square root of L, which produces the secondary diagonals. In (c) the fixed variant: instead of skipping at regular intervals, you designate fixed columns that act as meeting points for the whole sequence. These are the direct ancestors of Longformer's and BigBird's global tokens.Source : Sparse Transformer paper (Child et al.), via The Transformer Family by Lilian Weng

ETC (Extended Transformer Construction): introduces auxiliary "global" tokens that serve as communication hubs. Local tokens talk to globals, globals talk among themselves, and redistribute information, like a telephone relay network.

LongT5: local attention combined with "transient global attention" with dynamic segment summaries. Particularly effective for summarization tasks.

The big problem: GPU implementation

Theoretically, sparse attention should be ultra-fast. In practice? It's much more complicated.

Irregular patterns = chaotic memory accesses = GPUs hate them. GPUs are massive parallel compute machines, optimized for regular and predictable operations. When you tell the GPU "read positions 3, 17, 42, 891," it has to make lots of small scattered memory accesses, which kills performance.

The paradoxical result: Flash Attention on dense attention is often faster than poorly implemented sparse attention, even on sequences of several tens of thousands of tokens. Flash Attention avoids materializing the attention matrix in memory and perfectly exploits the GPU's memory hierarchy, which compensates for computing "too many" pairs.

The theory-practice gap

GPUs are optimized for dense, regular operations. Sparse attention fragments operations into small irregular pieces. Without specialized, optimized kernels, sparse attention can be SLOWER than dense attention. It's a classic case where algorithmic complexity (O(L)O(L) vs O(L2)O(L^2)) doesn't tell the whole story, hidden constants and hardware efficiency matter enormously.

Why it stopped dominating... until the 2026 comeback

With Flash Attention (hyper-optimized dense attention) and SSMs like Mamba (native linear complexity, no sparse pattern), sparse attention lost relevance for general-purpose LLMs for several years.

Niches where sparse attention stayed relevant:

  • Encoders for long documents: classification or information extraction on documents of hundreds of pages
  • Vision: images have natural 2D structure (a pixel mostly interacts with its spatial neighbors)
  • Specialized domains: proteins (local interactions in the chain + 3D contacts), molecules, graphs

The comeback: DeepSeek V4 and hybrid compressed attention (April 2026)

Then, in April 2026, DeepSeek V4 resurrects sparse attention at the largest scale. The architecture alternates two kinds of layers:

  • Compressed Sparse Attention (CSA): the K/V of every group of mm consecutive tokens are compressed into a single entry by a learned compressor, and each query then consults only the entries ranked most relevant by an indexer. It's sparse attention, but where the sparsity is learned rather than frozen into a pattern (local + global + random).
  • Heavily Compressed Attention (HCA): much more aggressive compression, alternated with CSA layer by layer.
DeepSeek-V4: Towards Highly Efficient Million-Token Context Intelligence

The result at 1M tokens, per the technical report: only 27% of the inference FLOPs per token and 10% of the KV cache compared to V3.2 (which used dense attention with MLA). A top-tier open model (1.6T parameters, mixture of experts) runs with sparse attention at the heart of its architecture, and claims the level of the best dense models at a fraction of the cost.

Learned sparse > fixed sparse

The classic sparse pattern (local + global + random) was frozen in advance. CSA learns where to compress during training. It's the same evolution computer vision went through, from the hand-crafted SIFT descriptor (Scale-Invariant Feature Transform, interest points defined by engineers) to features learned by convolutional networks: when the pattern becomes a model parameter instead of an engineering choice, quality leaps. Sparse attention may have lost the 2020-2025 battle of fixed patterns, but it is winning the 2026 one of learned compression.

Legacy

Sparse attention showed that you don't need the full attention matrix, a revolutionary idea at the time. This intuition directly paved the way for Flash Attention (which never materializes the matrix, just blocks), SSMs like Mamba (implicit linear attention with fixed memory), and modern hybrid architectures. Sparse attention may have lost the practical battle, but it won the intellectual war.

The movement continues in September 2026 with DeepSeek V4.1-Flash, where sparse attention indices are shared across layers and the cache is projected once from a causal encoder: see the dedicated lesson.

Check your understanding0 / 4

1. BigBird adds a few randomly drawn connections on top of the local and global patterns. What are they for?

2. Why can sparse attention, which computes far fewer pairs, end up slower than dense attention like Flash Attention?

3. Longformer goes from quadratic to linear complexity. Thanks to what?

4. What sets DeepSeek V4's Compressed Sparse Attention apart from the classic sparse patterns of 2020?