Subquadratic Attention with a Hierarchical Tree-Structured Indexer
Organizations: Zyphra Research
Abstract
The quadratic scaling of attention is a fundamental bottleneck limiting the long-context capabilities of large language models (LLMs). A variety of approaches have attempted to circumvent this, but they either fail to match attention's expressiveness (e.g., sliding-window or linear attention variants with fixed-size state) or only achieve constant-factor speedups (e.g., sparse attention methods whose indexers themselves scale quadratically). In this paper, we derive an asymptotically subquadratic sparse attention method based on a hierarchical, tree-structured indexer, which we call Zyphra Hierarchical Attention (ZHA). ZHA constructs a tree of recursively compressed key summaries over the KV cache, which future queries traverse from the top down to retrieve a sparse subset of keys. Crucially, by iteratively growing the tree's depth, the total cost including routing and final attention grows sublinearly in sequence length, resulting in overall subquadratic (but superlinear) scaling for attention. We demonstrate that ZHA can be cheaply applied to existing pretrained models with only a short KL distillation training stage over a few billion tokens, and maintains performance on long-context retrieval and challenging reasoning, mathematical, and agentic coding evaluation tasks. Moreover, we show that our method is highly robust to tree geometry including very deep trees of up to 6 levels. Finally, we develop a detailed performance model and show that our approach scales subquadratically, dramatically outperforms competing methods beyond 1 million context, and ultimately has the potential to extend effective context windows to the hundreds of millions or billions of tokens.
Figures & tables
Appendix figures & tables34 assets
Supplementary material from the paper’s appendix.
Appendix
| Qwen ZHA | Nemotron ZHA | Qwen MSA | |||
| Deep | Wide | Deep | Wide | Index | |
| Trained module | ref. queries | ref. queries | ref. queries | ref. queries | index proj. + norms |
| Geometry | C4 F4 L6 | C256 L1 | C4 F4 L6 | C256 L1 | B128 top-16 |
| Sparse-attention layers | 10 / 40 | 10 / 40 | 6 / 52 | 6 / 52 | 10 / 40 |
| Trainable params | 81,920 | 81,920 | 24,576 | 24,576 | 7.87M |
| Frozen backbone params | 34.66B | 34.66B | 31.58B | 31.58B | 34.66B |
| Router | L0 | L1 | L2 | L3 | L4 | L5 |
|---|---|---|---|---|---|---|
| Qwen deep | 0.021 | 0.097 | 0.180 | 0.253 | 0.302 | 0.319 |
| Qwen wide | 0.242 | – | – | – | – | – |
| Nemotron deep | 0.016 | 0.069 | 0.137 | 0.192 | 0.217 | 0.221 |
| Nemotron wide | 0.179 | – | – | – | – | – |
| Benchmark | Evaluated subset | Reported metric | Generation budget | Decoding / harness |
|---|---|---|---|---|
| AIME 2026 [ 32 ] 1 | 30 problems, 16 samples each | avg@16 (%) | 81,920 output tokens | Thinking; temperature 1.0, top- 0.95, top- 20, presence penalty 1.5 |
| SWE-bench Verified [ 22 , 5 ] | 500 tasks, 1 run each | Resolved tasks (%) | 32,768 tokens/turn; 204,800-token context; 250 steps | mini-swe-agent 2.4.6; bash text actions; thinking retained across turns; temperature 1.0, top- 0.95, top- 20 |
| Terminal-Bench 2.1 [ 40 ] 2 | All 89 tasks, 5 attempts each | Mean per-task success rate (%) | 81,920 tokens/turn; 262,144-token context; 3 h per trial | Harbor 0.24 / Terminus-2; thinking retained across turns; temperature 1.0, top- 0.95, top- 20 |
| MMLU-Pro [ 43 ] | Full test split: 12,032 questions; zero-shot CoT; 1 sample each | Accuracy (%) | 81,920 output tokens | Thinking; temperature 1.0, top- 0.95, top- 20 |
| GPQA Diamond [ 36 ] | All 198 questions, 8 samples each | avg@8 (%) | 81,920 output tokens | Thinking; temperature 1.0, top- 0.95, top- 20 |
| RULER-13 [ 16 ] | 13 tasks 100 examples at 256K; 64K and 128K in appendix | Mean task score (%) | Official per-task caps: 30–128 tokens | Greedy; thinking disabled |
| Variable | Definition |
|---|---|
| Batch size. | |
| Context and query lengths; in decode and in full prefill. | |
| Query and KV head counts, and GQA group size . | |
| Key and value dimensions per head. | |
| Bytes per stored Q/K/V or output element, and per accumulator or softmax statistic. | |
| Kernel stage: attention, split-KV partials, or merge. |
| Mode | Storage | Median | Median | th percentile |
|---|---|---|---|---|
| Causal prefill | BF16 | |||
| Causal prefill | FP16 | |||
| Decode | BF16 | |||
| Decode | FP16 |
| Variable | Definition |
|---|---|
| Batch size. | |
| Context and query lengths; in decode and in full prefill. | |
| Numbers of query heads and key/value heads, respectively. | |
| Query/key and value dimensions per head, respectively. | |
| Query heads per KV head (GQA group size): . | |
| Bytes per stored Q/K/V, compressed-key, reference-bias, or routing-score element; BF16 baseline: . |
| Variable | Definition |
|---|---|
| Kernel label: routing, scan, attention, split partial/merge, fusion, or tree summarization/merge. Sparse stage quantities also carry when level-specific. | |
| Sparse-stage execution latency, excluding kernel launches. | |
| Worker-wave contribution to sparse-stage execution latency. | |
| Sparse-stage operation count and memory traffic (bytes). | |
| Product of launch-grid dimensions, not inner-loop tiles. In 1-D: . Unsplit attention is a deterministic special case. | |
| Work-item count for a generic 1-D partition. |
| Quantity | A100 SXM4 | H100 SXM5 | MI300X | MI300A |
|---|---|---|---|---|
| Workers | 108 SMs | 132 SMs | 304 CUs | 228 CUs |
| Hardware bound on |
| Variable | Definition |
|---|---|
| Full nodes per sequence; the balanced full-node count at level is . | |
| Deterministic launch-grid size for a tree kernel processing full nodes per sequence. | |
| Deterministic operation count and memory traffic for that tree kernel. | |
| Leaf-summarization and parent-merge latencies, respectively, including launches. | |
| Cost of constructing all full nodes in prefill. | |
| Tree-maintenance cost per decoded token, amortized over full-node completions. |
| Variable | Definition |
|---|---|
| Tokens per block and maximum selected blocks per KV group, including the local block. | |
| Visible block count and selected block count at context length . | |
| Tokens in the final, local block and total retained tokens per KV group. | |
| Index-head width and model hidden width. | |
| Combined index-projection width: . | |
| Index-projection output channels per tile. |
| Stage | |||
|---|---|---|---|
| Index projections | |||
| Index + max-pool | |||
| Top- | — |
Efficient Attention
Efficient attention reduces the compute, memory traffic, and cache costs of Transformer attention while preserving useful long-range interactions.
See all 142 papers