Manuscript Offprint

arXiv:2611.02508

AttentionIndex: Accelerating Learned Sparse Attention with a Causal Inverted-File Index

Jeongmin Brian Park, Wen-mei Hwu

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

ABSTRACT

Learned sparse attention, used in production models such as DeepSeek-V4 and GLM-5, lets each query attend to only kk keys chosen by a lightweight indexer, but the indexer still scores every key in the prefix. At one million tokens, this selection takes more than half of prefill time. Two-stage selection over contiguous blocks, as in HiSA, reduces this cost, but its recall declines as context grows at a fixed candidate budget. We present AttentionIndex, which routes each query over an inverted-file index that groups indexer keys by similarity and refines the candidates with the native indexer score. AttentionIndex builds the index during prefill, making new keys searchable immediately, reusing previous clustering to keep maintenance cheap, and exposing each query only to clusters built from its own prefix. On GLM-5.3 and DeepSeek-V4-Flash at one million tokens, AttentionIndex delivers 1.3–1.8×1.3–1.8\times the prefill throughput of exhaustive selection, and at throughput within 5% of HiSA, it recovers 17.0 and 10.0 percentage points more of the exhaustive top-kk, respectively. AttentionIndex also improves accuracy on long-context benchmarks by up to 18.4 percentage points over HiSA at the same candidate budget.

1 INTRODUCTION

Long-context inference has become a defining workload for large language models: agents (Sun et al., 2026), code assistants (Rando et al., 2025), and document analysis (Bai et al., 2025; Zhang et al., 2024) routinely process hundreds of thousands to millions of tokens per request (Gemini Team, 2024). At these lengths, dense attention is prohibitively expensive, and production models have adopted learned sparse attention. DeepSeek Sparse Attention (DSA) (DeepSeek-AI, 2025), used by DeepSeek-V3.2, DeepSeek-V4, and GLM-5.3 (DeepSeek-AI, 2026; Z.ai, 2026), adds a lightweight learned indexer to the model's sparse attention layers. The indexer scores the keys in the prefix, and the main attention operates only on the kk highest-scoring entries, which bounds its per-query cost regardless of context length.

However, the indexer itself still scores every key. As a result, the computation cost of selection still grows linearly with context length per query while the cost of the attention stays constant. At one million tokens, indexer scoring and top-kk selection take 53–58% of prefill time on DeepSeek-V4-Flash and GLM-5.3, more than the rest of the model combined, even with key compression and cross-layer index reuse (Section 3.1). At long context, choosing which keys to attend to has become the bottleneck of sparse attention.

A natural remedy is two-stage selection: route each query to a small set of candidate keys, then apply the native indexer only to those candidates. HiSA (Xu et al., 2026) routes

to contiguous blocks of keys ranked by their mean-pooled representatives. However, keys of contiguous token entries in the context need not be similar in indexer space or equally relevant to a query, so a block holding a few important keys can rank low and be discarded. As context length grows, the probability of entries relevant to a query falling into unselected blocks grows. In our LongBench-v2 measurements on GLM-5.3, HiSA's recall with 8K candidate keys falls from 83.9% to 41.8% as context grows from 16K to 256K tokens. We observe a similar decline on DeepSeek-V4-Flash with a budget of 4K compressed entries (Section 3.2).

We observe that the native indexer performs exhaustive search within each selected block, so any approximation lies in which keys reside in the selected blocks. Grouping keys by similarity rather than by their positions in the context allows the exhaustive top-kk search to be applied to fewer groups that contain a higher number of promising candidates, thus reducing the selection process with limited time budget to reach higher quality top-kk outcome. This is the basic idea behind inverted-file (IVF) indexes for approximate nearest-neighbor search (Jegou et al., 2011; Johnson et al., 2021).

However, three constraints in a learned indexer environment make application of IVF more challenging. First, since pre-fill and decode generate new tokens that must be added into the candidate groups, updating the IVF index to include these new tokens must cost less than the scoring work it saves. Second, in order to maintain inference causality, every query must see an IVF index built only from its own prefix, including the centroids that guide routing. Third, since the indexer score, i.e. similarity measure, is not generated as a simple inner product, the score for a centroid that is the mean of the members of a cluster is a potentially poor approximate proxy for how the indexer scores its members.

We present AttentionIndex, which applies the IVF principle with three novel ideas. First, it routes each query over IVF clusters of keys and refines the candidates with the native indexer score, leaving the sparse attention kernel unchanged. Second, it incrementally builds and maintains the index during prefill to limit maintenance overhead. New keys become available to subsequent queries immediately and are clustered in small batches. Periodic refits regroup the prefix while reusing the previous clustering and selectively reassessing key assignments. To preserve causality, each query uses only clusters and representatives formed from keys in its own prefix. Third, it adjusts the cluster representatives to better predict the native indexer score for its cluster members using an encoder trained offline and shared across the clusters of each layer.

We implement AttentionIndex in SGLang (Zheng et al., 2024) and evaluate it on GLM-5.3 and DeepSeek-V4-Flash with contexts of up to one million tokens. At one million tokens and almost matched prefill throughput, AttentionIndex improves recall over HiSA by 17.0 and 10.0 percentage points on GLM-5.3 and DeepSeek-V4-Flash, respectively. Across the evaluated budgets, AttentionIndex achieves 1.3–1.8× the prefill throughput of exhaustive selection at the same context length. AttentionIndex also improves accuracy on long-context benchmarks by up to 18.4 percentage points over HiSA at the same candidate budget.

In summary, this paper makes the following contributions:

  • We show that in production learned sparse attention, key selection rather than attention dominates long-context prefill, and that HiSA's recall declines as context grows at a fixed candidate budget (Section 3).

  • We design a causal IVF index for learned indexers that is built and maintained during prefill through immediate insertion and incremental refits (Section 4).

  • We evaluate AttentionIndex on two production models with up to one million tokens, measuring selection recall, prefill throughput, and task accuracy against exhaustive selection and HiSA (Section 6).

References (39)