Long Context · July 2023
LongNet — Dilated Attention
advanced
long-contextefficiency
Cover very long sequences via dilated attention — multiple attention heads operating at exponentially-increasing stride patterns, so the receptive field grows multiplicatively in depth and head count rather than linearly in either.
§ 1 · Premise
Quadratic attention versus billion-token sequences
Dense self-attention’s compute scales as in sequence length and model dim . At , that is FLOPs per layer per head — already beyond what a single accelerator can hold the activations for; at the per-layer attention matrix alone is entries, six orders of magnitude past any plausible HBM budget. Ding et al. (arXiv 2307.02486) set the deliberately extreme target of training a Transformer at , and the question they ask is: which existing sparse pattern actually composes to that regime?
The 2020-era sparse-attention literature offered several patterns, each with a known limitation at the billion-token scale:
- Sliding window (Longformer, Beltagy et al. 2020, arXiv 2004.05150) attends to the last keys per query. compute, but the receptive field per layer is just ; reaching distance requires layers — for , this is layers.
- Sparse Transformer (Child et al. 2019, arXiv 1904.10509) interleaves a local window with a global stride- pattern: compute, two-hop reach. Better, but ops is still painful.
- BigBird (Zaheer et al. 2020, arXiv 2007.14062) combines window + random + global tokens for compute but inherits the same multi-hop reach problem and adds a heavy random-routing overhead.
LongNet’s wager is that the exponential generalization of stride-based attention — Sparse Transformer’s stride extended across a logarithmic number of dilation rates per head — is the right primitive: per-layer compute , single-layer reachability of any past position at some resolution, and a distributed-training pattern that decomposes along the dilation axis. The contribution is the dilated-attention construction with its mixture-of-dilations softmax recombination, the proof of linear distributed scaling along the sequence dimension, and a demonstration at tokens.
§ 2 · Derivation
From local windows to exponentially-dilated mixture attention
Starting point — local-window attention. With window size and stride , the per-query computation is
Compute per layer: . Receptive field per layer: .
Step 1 — dilated indexing. Replace contiguous keys with a strided set. For dilation rate and segment size , the keys attended to by query are
The query sees keys spaced apart, covering a span of positions but sampling only of them. Mathematically this is gather-then-attend (§2.2, Eq. 2):
A single dilated head at rate has compute per query — same as local attention — but a per-layer receptive field of .
Step 2 — segment-and-shuffle implementation. A naive gather is hardware-hostile (strided loads break GPU memory coalescing). Ding et al. instead realize dilated attention by segment-and-shuffle (§2.2, Figure 3): split the sequence into segments of length , reshape each segment as a matrix, and transpose so the resulting blocks naturally group together the indices a query at stride would attend to. Standard dense attention then runs within each shuffled segment at length . After attention, the inverse shuffle restores the sequence ordering. The overall pattern is mathematically equivalent to the gather formulation but executes as a sequence of contiguous-memory dense-attention kernels — fully Flash-Attention-compatible.
Step 3 — mixture of dilation rates. A single rate either covers too little reach (small ) or too coarsely (large ). LongNet mixes heads with geometrically-growing rates (§2.3):
with (Ding et al. use ) and segment sizes that may also grow. The configuration in Table 1 is — five dilated patterns covering distances positions per layer.
The mixture is not a simple sum of softmaxes — that would over-count positions that fall in multiple patterns. LongNet’s combination weighs each pattern’s softmax denominator proportionally to its participation (, Eq. 4):
The are exactly the softmax denominators of each pattern’s local computation; the mixing weight is each pattern’s mass relative to the total. This is the standard online log-sum-exp recombination used in distributed attention, applied across dilation patterns rather than across sequence shards.
Why exponential dilations. Two reasons. First, one-layer reachability of every distance: for any distance , there exists a rate with , so some head’s window covers position . Second, logarithmic head count to cover linear range: patterns cover the full sequence; for , , that is patterns — feasible to allocate across heads of a normal multi-head layer (Ding et al. use in their experiments, overloading some heads with multiple patterns).
Step 4 — cost accounting. Per layer, summing over patterns:
With constant, this is — sub-quadratic by a factor of . With growing as in Table 1, the cost becomes , which is back to quadratic when . Ding et al.’s configuration uses which formally is but with a leading constant; the practical scaling at the they study is sub-quadratic because of the tiny constant and the distributed-training shape.
Memory: each pattern’s attention matrix is ; the maximum is the largest , giving an attention-matrix footprint of entries per head — for this requires sequence-parallel splitting across many accelerators, which is exactly what the distributed-training section ( of the paper) addresses.
Step 5 — distributed training along the dilation axis. Dilated attention’s special property is that each pattern is local within its shuffled segment (segment size ). This means the attention computation parallelises naturally along the sequence dimension: shard the sequence across devices, and each device runs dense attention within its own shard for the local-rate pattern, with cross-device communication only for the coarser-rate patterns. Ding et al. report near-linear scaling to devices at (Figure 5).
Parameter count. No new parameters introduced; dilated attention reuses standard . The only added “config” is the choice of per layer.
§ 3 · Reference implementation
Segment-and-shuffle dilated attention in pseudocode
# q, k, v: [B, N, H, d_h]
# patterns: list of (w_i, r_i) tuples assigned to subsets of heads
def longnet_layer(q, k, v, patterns, head_assign):
# head_assign: list of head indices per pattern
outs, Zs = [], []
for (w, r), heads in zip(patterns, head_assign):
qh, kh, vh = q[..., heads, :], k[..., heads, :], v[..., heads, :]
# Segment-and-shuffle: reshape so that stride-r positions become contiguous
N = q.shape[1]
# 1) Pad to multiple of w*r
# 2) Reshape [B, N/(w*r), w*r, H', d_h] -> [B, N/(w*r), r, w, H', d_h] (transpose)
# 3) Flatten the r dimension into the batch: [B * N/(w*r) * r, w, H', d_h]
qs, ks, vs = segment_shuffle(qh, w, r), segment_shuffle(kh, w, r), segment_shuffle(vh, w, r)
# Standard dense attention within each shuffled block (Flash-Attn under the hood)
out_i, lse_i = flash_attention(qs, ks, vs, return_lse=True) # lse = log Z_i
# Inverse-shuffle back to [B, N, H', d_h]
out_i = inverse_shuffle(out_i, w, r)
lse_i = inverse_shuffle(lse_i, w, r)
outs.append(out_i)
Zs.append(exp(lse_i)) # [B, N, H', 1]
# Mixture combination across patterns (log-sum-exp recombination)
Z_total = sum(Zs)
o = sum(Z_i * o_i for Z_i, o_i in zip(Zs, outs)) / Z_total
# Re-merge heads back to [B, N, H, d_h]
return scatter_heads(o, head_assign)
The sketch elides three production concerns: (1) causal masking — dilated indices need a causal-respecting gather that drops keys with index ; (2) padding handling when is not a multiple of ; (3) the actual heads-to-patterns assignment, which Ding et al. keep as a fixed configuration per layer rather than learned.
§ 4 · Empirical evidence
What is and isn’t known
Introducing paper (Ding et al. 2023).
- Sub-quadratic scaling demonstrated. Trained MAGNETO Transformer backbones (Wang et al. 2022) with LongNet attention at sequence lengths K, K, K, K, K, and M, reporting near-flat wall-clock per token vs. growing-quadratically wall-clock for dense attention (Figure 4). Dense attention OOMs at K on their hardware; LongNet continues smoothly.
- Distributed scaling. The distributed implementation reaches near-linear throughput as training devices scale from 1 to 32 GPUs at fixed K (Figure 5), and the paper describes (though does not benchmark at full hardware) the procedure for scaling to tokens.
- Language modeling perplexity. On the Stack (Kocetkov et al. 2022, a code corpus) and Pile (Gao et al. 2020), LongNet at K trained for B tokens reaches lower perplexity than dense attention at K trained for the same number of tokens (Table 2, Table 3). The improvement is modest ( bpb on the Pile, bpb on the Stack) and is the result of the extended context, not an intrinsic property of the dilation pattern.
- Receptive-field ablation. Removing the larger dilation rates (keeping only ) degrades perplexity by bpb on long-document subsets of the Pile (Table 4), confirming that the mixture’s long-range patterns contribute beyond what the small- patterns alone provide.
Independent follow-up.
- Survey coverage. LongNet appears in the standard long-context surveys (Pawar et al. 2024, arXiv 2402.02244, §3.4; Liu et al. 2024 “Understanding LLMs’ Long-Context Capabilities”, arXiv 2404.02060) as the canonical dilated-attention design.
- YOCO / FocusLLM (Sun et al. 2024, arXiv 2405.05254, §2) compares against LongNet as a representative sub-quadratic attention baseline and reports that on RULER-style long-context recall benchmarks, fixed-pattern sparse attention (LongNet, BigBird) underperforms the cache-compression family on multi-hop tasks despite competitive language-modeling perplexity — an extension of the dense-vs-sparse-attention quality gap Hsieh et al. document.
- Mixture-of-Sparse-Attention work. Subsequent work has refined the dilated pattern with learned routing rather than fixed strides — e.g., Native Sparse Attention (Yuan et al. 2025, arXiv 2502.11089) cites LongNet as the fixed-stride predecessor that motivates learned-routing alternatives.
Sensitivity studies — what is not publicly known. The introducing paper does not study (a) how the dilated-attention model behaves on needle-in-a-haystack benchmarks like RULER (Hsieh et al. 2024) or the LongBench / NIAH lineage — the paper’s evaluation is language-modeling perplexity, not retrieval accuracy; (b) how LongNet composes with subsequent positional-encoding rescaling techniques (the paper uses xPos / MAGNETO’s positional encoding, not RoPE); (c) whether the configuration optimal at K transfers to the regime — the billion-token result is a system demonstration, not a downstream-task evaluation. I don’t know of an independent reproduction at B model parameters; published LongNet results sit at M–B MAGNETO scales.
Production adoption. None recorded in this knowledge base. No frontier dense or MoE production decoder ships pure dilated attention as its long-context primitive — the production-frontier choice in the 32K–1M regime has converged on interleaved SWA + global attention (Gemma 3, Mistral) with positional-encoding rescaling on top (YaRN, NTK-aware extension). LongNet’s specific value is asymptotic: at the billion-token regime where heads materially outperform patterns; that regime is not yet the production frontier.
Lineage
- Predecessors
- Sparse TransformerSparse Transformer
Cite
BibTeX entry for the original paper
@article{arxiv2307_02486,
title = {LongNet: Scaling Transformers to 1,000,000,000 Tokens},
author = {Jiayu Ding and others (Microsoft Research)},
year = {2023},
eprint = {2307.02486},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2307.02486}
} Or cite the paper directly: arXiv:2307.02486.
Export
BibTeX
@article{arxiv_2307_02486,
title = {LongNet: Scaling Transformers to 1,000,000,000 Tokens},
author = {Jiayu Ding et al. (Microsoft Research)},
year = {2023},
eprint = {2307.02486},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2307.02486}
} CSL JSON
{
"id": "arxiv_2307_02486",
"type": "article-journal",
"title": "LongNet: Scaling Transformers to 1,000,000,000 Tokens",
"author": [
{
"literal": "Jiayu Ding et al. (Microsoft Research)"
}
],
"issued": {
"date-parts": [
[
2023
]
]
},
"URL": "https://arxiv.org/abs/2307.02486",
"number": "2307.02486",
"source": "arXiv"
} RIS
TY - JOUR
TI - LongNet: Scaling Transformers to 1,000,000,000 Tokens
AU - Jiayu Ding et al. (Microsoft Research)
PY - 2023
JO - arXiv
AN - arXiv:2307.02486
UR - https://arxiv.org/abs/2307.02486
ER -