Attention Mechanisms · April 2019
Sparse Transformer
intermediate
long-contextefficiency
Replace dense O(T²) attention with structured sparsity — two fixed patterns (strided and fixed) that together let any pair of tokens reach each other in at most two hops while attending to only O(T·√T) total keys.
§ 1 · Premise
Dense attention is quadratic — and mostly wasted
At sequence length (the longest configuration in the Sparse Transformer paper), dense self-attention requires a float logit matrix per head per layer. With 8 heads and 30 layers — the GPT-2-class configurations of 2019 — that is logits per forward pass, dominating both compute and on-device memory. On a single 16 GB V100, this configuration is infeasible at any usable batch size. The quadratic bottleneck made long-sequence generative modeling (audio, image, long-form text) impractical with dense transformers.
The structural observation that motivated the design (Child et al. 2019, arXiv 1904.10509, §3.1): inspecting trained dense transformer attention matrices shows most queries place ~90% of their probability mass on a small subset of keys. The matrix is empirically near-sparse. Yet computing the full matrix is required because (a) the model has no prior on which keys will matter and (b) standard attention’s softmax normalization requires the full row sum.
Two design strategies were available in 2019. One was to discover sparsity post-hoc and prune at inference (a route that produced no clean wins — pruning the attention matrix without retraining hurt quality, and discovering it during training was an open optimization problem). The other was to commit to a structured sparse pattern at training time. This is the strategy Sparse Transformer chose: fix a deterministic, position-conditioned sparse attention graph; train the model from scratch with that graph; pay the cost of a specific pattern in exchange for the asymptotic compute savings.
The contribution in one sentence: a pair of structured sparse-attention patterns — strided and fixed — together giving attention compute per layer and full token-pair reachability in at most two hops, demonstrated to train stably at character-level language modeling, pixel-level image modeling, and raw audio at sequence lengths up to 12288.
§ 2 · Derivation
Two patterns, two-hop reachability, cost
Let be the sequence length and a stride parameter. For each attention head we choose one of two sparse attention patterns indexed by row position (the query) and column position (the key); both patterns respect causality ().
Pattern A — strided. Each query attends to two index sets:
The first set is the local window of the most recent tokens — same form as SWA. The second set is the strided long-range view: every -th token going backward from . Total key count: . Summed across all queries the cost is , minimized at with cost .
Pattern B — fixed. Each query attends to:
The first set is the local block of contiguous keys containing . The second is a fixed summary set specifying positions within each -block whose tokens are designated “summary” tokens — keys that every query in later blocks can attend to. is shared across all positions and all heads of this kind. Setting to a small constant (the paper uses ) gives , same total.
Two-hop reachability. Both patterns connect any pair of tokens with via at most two attention hops in the same head.
For pattern A (strided), if is within of , it is in directly. If not, find the largest multiple of below — that is some — and then via the local window starting at . Total: 2 hops.
For pattern B (fixed), if is in the same -block as , it is in directly. Otherwise let be a summary token in the block containing (an element of within ). Then (every query attends to summary tokens) and (the summary token attends to its own block). Total: 2 hops.
Two-hop reachability is the key formal property. Through a stack of Sparse- Transformer layers, every token’s representation can incorporate information from every prior token — meaning the model retains the expressivity of full attention for sequences of any length up to , at cost per layer instead of .
Why and not constant? The cost has a unique minimum at , balancing the local-window contribution () against the strided contribution (). Choosing a small constant gives for the local term but for the strided term — same as dense. Choosing gives for the local term. The choice is where the two terms become equal in scaling.
How the two patterns are combined. Sparse Transformer assigns pattern A to some attention heads and pattern B to the rest, in equal proportion (Child et al. 2019, §3.2). Both patterns preserve two-hop reachability on their own; using both in parallel diversifies the heads’ inductive biases without changing the asymptotic cost. The paper also experiments with alternating the patterns across layers (odd layers use A, even use B) which gives similar quality.
Sparse attention as an unnormalized form. With masked attention, the softmax over a sparse mask is computed by setting masked logits to before the softmax, giving
where is the per-query key set (here or ). The normalization is over the sparse only — the missing keys do not contribute mass.
Per-layer cost. Compute per head per layer is . Memory for the per-query attention probabilities is . The block structure of both patterns means the actual implementation is a sparse block-matmul rather than a dense matmul with a mask — see Child et al. 2019 §4 for the block-sparse kernels and the released CUDA kernels that exploit them.
§ 3 · Reference implementation
Strided mask sketch
def strided_mask(T, stride):
# Strided pattern A: local window + strided long-range
mask = torch.zeros(T, T, dtype=torch.bool)
for t in range(T):
# A^(1): local window of last `stride` tokens
for s in range(max(0, t - stride + 1), t + 1):
mask[t, s] = True
# A^(2): every `stride`-th token back to position 0
for s in range(t, -1, -stride):
mask[t, s] = True
return mask # [T, T], causal & sparse, ~O(T*sqrt(T)) nonzeros if stride = sqrt(T)
def fixed_mask(T, stride, summary_set):
# Fixed pattern B: local block + global summary tokens
mask = torch.zeros(T, T, dtype=torch.bool)
for t in range(T):
block = t // stride
# B^(1): same-block tokens at or before t
for s in range(block * stride, t + 1):
mask[t, s] = True
# B^(2): summary tokens in any earlier block
for b in range(block + 1):
for offset in summary_set:
s = b * stride + offset
if s <= t:
mask[t, s] = True
return mask
For a production implementation, the per-query key indices are precomputed and the attention is a block-sparse matmul. The reference OpenAI implementation provides the CUDA kernels exploiting the block structure.
§ 4 · Empirical evidence
What Sparse Transformer demonstrated and what came next
Character-level language modeling on enwik8. Child et al. 2019 Table 1 reports Sparse Transformer at achieving 0.99 bits-per-byte, vs 1.03 bpb for a dense Transformer- XL baseline at . This was state-of-the-art on enwik8 at the time of release and the first transformer-based result to break the 1.0 bpb threshold. The same model trained on a single V100 GPU — explicitly impossible for dense attention at that sequence length on that hardware.
Image generation (pixel-level). Sparse Transformer at on ImageNet 64×64 achieved 3.44 bits-per-dim (Child et al. 2019, Table 2), narrowly beating contemporary CNN baselines (PixelCNN++, Glow). This was the first credible transformer-based result on unconditional image generation; the architecture was later adapted into the Image GPT (Chen et al. 2020, arXiv 2006.16236) recipe at scale.
Raw audio. §6.3 reports the same architecture at (raw audio waveform tokens) achieving competitive likelihood on classical music — extending the sequence length 5× beyond the language-modeling experiments. The result was less polished than the language or image results but demonstrated the cost asymptotic translated cleanly to other modalities.
Pattern A vs pattern B. Child et al. 2019 §6.1 ablates the strided and fixed patterns separately. On enwik8 the strided pattern achieves 1.01 bpb and the fixed pattern 1.00 bpb; combining heads with both patterns gives 0.99 bpb. The two patterns are roughly equally effective alone — the gain from combining them is small but consistent. This is the empirical case for not choosing one over the other.
Why the design did not survive into production decoder LLMs. Three reasons:
- Hardcoded patterns are inflexible. The strided and fixed masks are fixed at training time. A different head cannot learn a slightly different sparsity pattern; the model commits to the same mask shape across all heads of the same pattern type.
- SWA + occasional global attention is conceptually simpler and empirically equivalent. Mistral 7B (Jiang et al. 2023, arXiv 2310.06825) and Gemma 3 (Gemma Team 2024, arXiv 2503.19786) replaced strided with pure local windows and replaced fixed with interleaved global-attention layers. The receptive field grows linearly with depth — see SWA — which makes the explicit “strided” pattern redundant once depth is stacked sufficiently.
- BigBird’s theoretical analysis (Zaheer et al. 2020, arXiv 2007.14062) showed that random+window+global is a universal approximator — but the corresponding analysis for strided + fixed is absent. The Sparse Transformer patterns work empirically without a corresponding theory of why; this made them a less attractive starting point for the post-2020 theoretical sparse-attention line.
Influence on the sparse-attention research line. Child et al. 2019’s framing — ” is enough; two hops reach everything; commit to the pattern at training time” — became the conceptual foundation for every subsequent structured-sparse-attention design: Longformer (Beltagy et al. 2020) added task-specific globals; BigBird added the random pattern with theory; Reformer (Kitaev et al. 2020, arXiv 2001.04451) replaced fixed patterns with LSH-driven learned sparsity. The lineage from Sparse Transformer through SWA + global to modern long-context decoders is direct; the specific strided + fixed masks did not survive but the framing did.
Independent reproduction. The OpenAI block-sparse kernels have been used and verified by independent teams (the Image GPT paper, Chen et al. 2020, explicitly builds on this codebase). The enwik8 character-level result has been reproduced by community implementations; the raw-audio result less so.
What was not measured. I don’t know of a public ablation isolating the contribution of the strided pattern at fixed total nonzero count — e.g., a version of Sparse Transformer where strided is replaced by random samples at the same density. Such an ablation would clarify how much of the gain comes from the structured nature of the strided pattern vs just having enough total connectivity. BigBird’s later work suggests the answer is “mostly the connectivity matters, not the structure,” but the head-to-head experiment within the Sparse Transformer framework has not been published.
Lineage
- Successors
- Sliding Window AttentionSWA
Cite
BibTeX entry for the original paper
@article{arxiv1904_10509,
title = {Generating Long Sequences with Sparse Transformers},
author = {Rewon Child, Scott Gray, Alec Radford, Ilya Sutskever (OpenAI)},
year = {2019},
eprint = {1904.10509},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1904.10509}
} Or cite the paper directly: arXiv:1904.10509.
Export
BibTeX
@article{arxiv_1904_10509,
title = {Generating Long Sequences with Sparse Transformers},
author = {Rewon Child and Scott Gray and Alec Radford and Ilya Sutskever (OpenAI)},
year = {2019},
eprint = {1904.10509},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1904.10509}
} CSL JSON
{
"id": "arxiv_1904_10509",
"type": "article-journal",
"title": "Generating Long Sequences with Sparse Transformers",
"author": [
{
"literal": "Rewon Child"
},
{
"literal": "Scott Gray"
},
{
"literal": "Alec Radford"
},
{
"literal": "Ilya Sutskever (OpenAI)"
}
],
"issued": {
"date-parts": [
[
2019
]
]
},
"URL": "https://arxiv.org/abs/1904.10509",
"number": "1904.10509",
"source": "arXiv"
} RIS
TY - JOUR
TI - Generating Long Sequences with Sparse Transformers
AU - Rewon Child
AU - Scott Gray
AU - Alec Radford
AU - Ilya Sutskever (OpenAI)
PY - 2019
JO - arXiv
AN - arXiv:1904.10509
UR - https://arxiv.org/abs/1904.10509
ER -