Recursive Sparse Attention: Million-Token Length Extrapolation at Logarithmic Cost
Summary (Overview)
- RecSA (Recursive Sparse Attention) is a novel sparse attention mechanism that builds a tree over the KV cache, enabling context length extrapolation to 2M tokens with only O(log L) selection cost and read complexity per query.
- The method uses candidate-local RoPE, where each entry's position is its index within its level's candidate list, ensuring all relative distances stay within the training range (below 4,096 with default k=256) regardless of context length.
- RecSA is trained only at 32K tokens (0.62B tokens total) using a two-stage approach: pooler distillation from dense attention, followed by backbone adaptation—no training at target lengths is needed.
- On RULER benchmark, RecSA outperforms all baselines at every length from 131K to 2M, scoring 60.5 at 1M vs. 40.0 for the best baseline (Dense + DCA), while using only 1.16% of the context as token budget.
- Custom Triton/CUDA kernels provide 3.2× prefill and 3.8× decoding speedup at 524K on one H200 GPU compared to dense FlashAttention, with decoding latency staying nearly constant from 131K to 524K.
Introduction and Theoretical Foundation
Background and Motivation
State-of-the-art language models now support context windows of ~1M tokens, but face two fundamental challenges:
- Computational cost: Dense attention grows quadratically in prefill and reads the entire KV cache per token in decoding.
- Data scarcity: Training at target lengths requires sufficiently long documents, which become scarcer as length increases.
Limitations of Existing Approaches
Most sparse attention methods (MoBA, NSA, DSA) score all preceding blocks/tokens, making selection cost O(L) per query. While hierarchical selection (HSA, HiLS, InfLLM, InfiniteHiP) reduces this to O(log L), prior trainable hierarchical methods report accuracy only within their training length. Methods like HiP and PISA that keep original key positions suffer from out-of-distribution relative distances beyond training length.
Key Theoretical Insight
The paper introduces candidate-local RoPE: by assigning each entry its index within its level's candidate list (rather than absolute position), every relative distance to the query stays below the maximum candidate list size (C·k = 4,096 with defaults). This eliminates the need for context extension methods like YaRN or DCA.
Methodology
Tree Construction
Each attention layer maintains a KV tree per KV head. Level 0 holds exact token keys/values; groups of consecutive entries form parent nodes at higher levels. Each node's key is computed by a learnable pooler, and its value is the mean of children's values:
Hierarchical Selection
Selection proceeds top-down. At each level , the query scores all candidates and selects the current node plus the highest-scoring others (k=256 default). The children of selected nodes form the next candidate list:
Unified Softmax
Summaries of unselected candidates and exact tokens share one softmax:
Pooler Distillation
The pooler computes node keys from children's keys with a gated MLP architecture:
Training uses KL divergence between student importance distribution and teacher (dense attention mass) distribution:
A shift penalty prevents the MLP from learning positional biases:
Training Stages
- Stage 1 (Pooler): Freeze backbone, train pooler with k=64 for deeper trees. Two parts: (a) train w, τ, g with MLP off; (b) train MLP parameters with KL + shift penalty.
- Stage 2 (Backbone): Freeze pooler, train backbone under RecSA with k=256 using language modeling loss only.
Empirical Validation / Results
RULER Benchmark Results
Table 1: RULER score by context length
| Method | 32K | 131K | 262K | 524K | 1M | 2M |
|---|---|---|---|---|---|---|
| Token budget | 5,888 | 8,192 | 8,704 | 9,728 | 11,575 | 11,990 |
| % of context | 18.0 | 6.25 | 3.32 | 1.86 | 1.16 | 0.60 |
| Training-free | ||||||
| InfLLM | 45.0 | 29.3 | 29.2 | 27.4 | 25.7 | 24.2 |
| InfiniteHiP | 79.6 | 63.7 | 57.7 | 49.4 | 36.6 | 34.0 |
| Trained at 32K | ||||||
| Dense + YaRN | 94.5 | 80.0 | 48.3 | 3.6 | 1.6 | 1.1 |
| Dense + DCA | 94.6 | 78.5 | 68.4 | 58.0 | 40.0 | 27.6 |
| MoBA + YaRN | 81.6 | 31.9 | 5.1 | 0.9 | 1.0 | 0.6 |
| MoBA + DCA | 81.8 | 37.9 | 23.8 | 18.1 | 12.5 | 7.2 |
| DSA + YaRN | 95.3 | 80.5 | 57.1 | 18.8 | 6.0 | 1.4 |
| DSA + DCA | 95.2 | 77.5 | 65.7 | 55.2 | 36.8 | 22.6 |
| RecSA (ours) | 94.7 | 83.3 | 75.9 | 69.7 | 60.5 | 49.3 |
Perplexity Results
Table 3: Perplexity on PG-19 books
| Method | 32K | 131K | 262K | 524K | 1M |
|---|---|---|---|---|---|
| Dense + YaRN | 11.17 | 11.81 | 16.96 | 60.94 | 211.33 |
| Dense + DCA | 11.17 | 11.27 | 12.20 | 13.39 | 14.57 |
| MoBA + DCA | 12.11 | 12.87 | 14.85 | 17.12 | 19.18 |
| DSA + DCA | 11.15 | 11.32 | 12.35 | 13.67 | 15.01 |
| RecSA (ours) | 11.24 | 10.87 | 11.00 | 11.08 | 11.12 |
RecSA maintains nearly constant perplexity (10.87–11.12) from 131K to 1M, while all baselines degrade significantly.
Key Findings from Ablations
- Summaries improve average score from 70.4 to 72.3 (131K–1M average)
- Candidate-local RoPE is critical: standard RoPE with YaRN scores 1.9 at 524K vs. RecSA's 69.7
- Distillation + shift penalty + MLP term each contribute to long-context performance
- Increasing k to 512 improves scores at every length (with 1.7×–1.9× token budget)
Efficiency Results
- Prefill: 3.2× speedup at 524K (80s vs. 257s for Dense)
- Decoding: 3.8× speedup at 524K (6.1ms vs. 23.4ms per token); nearly constant latency from 131K–524K
- At 1M/2M: 8.35/10.7 ms per token via KV offloading to host memory
Theoretical and Practical Implications
Theoretical Contributions
-
Logarithmic complexity: RecSA achieves O(log L) selection cost and read complexity per query, a fundamental improvement over the O(L) cost of most sparse attention methods.
-
Length extrapolation without context extension: Candidate-local RoPE eliminates out-of-distribution relative distances entirely, removing the need for YaRN, DCA, or similar techniques.
-
Training efficiency: Only 0.62B tokens at 32K length are needed—no long-document training data required.
Practical Implications
-
Scalable inference: The fixed launch grid and CUDA graph capture in decoding make latency nearly constant across a wide range of context lengths.
-
Memory efficiency: KV offloading keeps GPU memory usage bounded (only levels above level 0, <7% of full KV cache, plus gathered exact tokens).
-
Model-agnostic: The approach works with pretrained models (demonstrated on Qwen3-4B) and can be extended to larger models.
Conclusion
RecSA demonstrates that hierarchical sparse attention with distilled poolers and candidate-local positional encoding can achieve million-token context extrapolation with logarithmic cost, trained only at short lengths. Key takeaways:
- Tree-based hierarchical selection with summaries sharing one softmax with exact tokens provides both accuracy and efficiency
- Candidate-local RoPE eliminates the need for context extension methods by keeping all relative distances within training range
- Two-stage training (pooler distillation + backbone adaptation) enables effective length extrapolation from 32K training data
- Custom kernels with slot-based rotation splitting enable efficient prefill and decoding implementations
Future Directions
- Applying RecSA to larger models and other architectures
- Exploring adaptive k values and branching factors
- Investigating the approach for other attention variants (e.g., sliding window combinations)
- Extending to even longer contexts (beyond 2M tokens)
Related papers
- MetaCurate: Evolving Curation Filters for High-Quality Agentic Coding Data
MetaCurate evolves executable curation filters via pairwise LLM feedback, achieving state-of-the-art SWE-bench scores without model training and at 3.5x lower search cost.
- Dory1.0-4B: A Looped Normalized-GPT
Dory1.0-4B, a looped 4B model, outperforms models up to 9x larger by scaling compute per token in latent space rather than generating longer outputs.
- Audit Where Hacks Pay: Budgeted Strong-Verifier Audits Against Reward Hacking in GRPO
Low-pass budgeted audits with reflected penalties suppress reward hacking in GRPO, while unbiased random audits fail because update randomness, not just expected gradient, determines held-out accuracy.