Manuscript Offprint

arXiv:2610.42112

Subquadratic Attention with a Hierarchical Tree-Structured Indexer

Leon Lufkin, Cutter Dawes, Alexander Speicher, Kamesh Krishnamurthy, Tomás Figliolia, Beren Millidge

Leon Lufkin*, Cutter Dawes*, Alexander Speicher, Kamesh Krishnamurthy†, Tomás Figliolia†, Beren Millidge†

¹ Zyphra Research *Equal contribution †Senior Author

Correspondence: {leon, tom}@zyphra.com

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.

I. INTRODUCTION

Attention is the fundamental sequence-mixing primitive in modern transformer architectures [41], and it is the basis for the highly flexible and expressive sequence modeling that underpins the recent rapid and dramatic advances in AI. Attention expresses an all-to-all mapping between sequence elements, where each token in the sequence is compared to every other token to identify those which are relevant. It is highly expressive and unstructured, yet parallelizable and efficiently implementable on GPUs. These properties have made it the predominant sequence-mixing architecture and enabled massive scaling of model size and sequence lengths.

However, the all-to-all nature of attention leads to fundamentally unfavorable scaling in computational and memory costs. In prefill, its quadratic computational cost dominates other operations, while during decode, re-reading the whole key-value (KV) cache per new token imposes memory bandwidth and storage costs which linearly increase in the sequence length. This has prohibited extremely long LLM contexts, despite

continuing gains from longer reasoning and input lengths across a wide range of increasingly challenging tasks [2, 9].

This has motivated a variety of approaches that reduce the computational cost of attention [38]. State-space models and sliding-window attention [6, 12, 15, 24] replace the full KV cache with a fixed-size state that is updated along the sequence. While these eliminate the asymptotic cost of attention with linear prefill and constant decode cost, their fixed-size state saturates at long contexts, leading to worse performance [21]. Practical “hybrid” methods [13, 14, 26, 39] thus often interleave a fixed-state mixer with full attention, reaping the computational benefits in most layers while restricting the attention costs to only a few, necessary layers. However, at sufficiently long contexts, the few layers using full attention still dominate.

Other approaches aim to compress the sequence directly by online-compressing multiple tokens into a single token during prefill or decode (e.g., CSA [8], NSA [46], HNet [20]), or by compressing the KV cache by reducing the number of independent heads [1, 11, 28]. A related set of methods focuses on offline KV cache eviction, where tokens deemed to be of little value are evicted periodically from the KV cache, reducing the cost of all future queries [23]. However, although head-compression methods such as GQA and MLA have become ubiquitous, they are fundamentally limited in possible compression rate by the number of heads. Meanwhile, sequence-compression and KV-eviction methods theoretically enable large compression rates but have fared less well empirically. Many of these methods both only obtain a constant factor speedup, and also are usually brittle and scale poorly in practice. A core reason for this brittleness is that it is practically impossible to know ahead of time, or on average, whether a token is useful or not.

This leads directly to a family of fine-grained sparse attention methods that enable each query to retrieve its own sparse subset of keys and values from the context (e.g., DSA [29], MSA [25]). In these methods, a lightweight indexer attention scores every KV pair's relevance to each query, and full attention is performed only on the top-KK most relevant pairs. Since KK is fixed, the full attention has a constant cost. Crucially, this approach still permits each query to select an independent subset of keys most important to it, which enables higher precision for retrieval than compressing the same set of keys for all queries. However, this flexibility comes with two costs. First, the full KV cache must be stored, even if not computed over every token. Second, and most importantly, it relocates the cost of attention into the indexer. In practice, one can obtain significant speedups by making the indexer cheap with fewer heads or lower precision, but at contexts near 1M tokens, 60-90% of time in these layers is still spent on indexing [3, 47]. A subfamily of chunk-sparse attention methods [18, 19, 46] attempts to address this by compressing the indexer itself, and sparsely selecting chunks from the KV cache over which to perform attention. Of interest is Huang et al. [19] which uses an adaptive sparsity function α\alpha-entmax, similar to our top-p routing in spirit, and Hu et al. [18], which constructs chunk summaries as an attention-weighted sum, similar to our own key construction method. However, these methods tend to be empirically brittle and only perform chunking over a single level, thus also only obtaining constant factor speedup.

This leads to the conclusion that to construct an effective attention replacement yielding better than constant-factor speedups, two properties must be satisfied. The first property, derived from sparse attention, is query-dependent sparsity, since different queries can require substantially different information from the KV cache. The second property is a growing but sublinear active state size. New sequence elements add predictive information indefinitely, so a fixed-size state will inevitably reach capacity and degrade in performance. By contrast, full attention can be viewed as a non-parametric state that grows linearly with sequence length. This linear growth is maximally expressive but highly redundant [4, 10, 30, 48], since the predictive information of each new token diminishes: the millionth token contains information, but likely less than the first. The challenge is to design a state that can grow dynamically, but sublinearly, at the rate that predictive information accumulates in the sequence.

Motivated by these considerations, we introduce a novel hierarchical sparse attention method, which we call Zyphra Hierarchical Attention (ZHA), that can achieve better than constant-factor speedups over dense attention. By analyzing the structure of attention's representations, we find that the size of its effective state is sublinear and that it is amenable to a hierarchical indexer. Moreover, we show that we can adapt pretrained models to use our hierarchical indexer with distillation training of only a few billion tokens and very few additional parameters, without changing any of the base model's weights. We convert models up to 35B parameters and observe virtually no degradation on challenging evaluations covering mathematical reasoning, agentic coding, and long-context retrieval. We also perform a detailed study of various indexer geometries and demonstrate that ZHA is highly robust to such variations and, crucially, can maintain performance at large depths.

Finally, we develop a detailed theoretical performance model of our method as well as comprehensive empirical sparsity results for a variety of indexer geometries. Our results demonstrate that ZHA can remain asymptotically subquadratic, subject to careful top-p scaling, when extrapolated to extremely long sequences of tens of millions to billions of tokens, and can thus theoretically strongly outperform sparse attention methods at these context lengths in our performance model. We model ZHA's speedups against dense attention and MSA with a roofline model [45], which predicts a crossover point against the latter around 0.5M tokens for a performant geometry with p = 0.75.

References (43)