At 10-Million-Token Contexts, the Indexer Becomes the New Bottleneck for Sparse Attention

    This article explains why, as context grows to ten million tokens, the indexer in sparse attention can cost more than the attention computation it is meant to reduce. It compares linear-scan and hierarchical indexing, describes how KV cache layout affects memory access, and covers how retrieval can be placed in the computation graph. It also explains how to read reported speedups and gives a step-by-step plan for evaluating these methods in production.

    1. Where the Cost of Sparse Attention Comes From

    Full attention has a prefill complexity of O(T²), where T is the sequence length. Decoding each token costs O(T). When the context grows from 1 million to 10 million tokens, the pure attention computation rises sharply, and full computation alone becomes hard to support in deployment.

    Sparse attention splits the work into two steps. The first step is selection: an indexer picks a small set of relevant keys for each query. The second step is computation: attention runs only over the selected candidates. The cost of the second step already depends on the number of candidates, k. The first step still has to decide which keys are relevant.

    If the indexer scores every historical key for every query, the selection cost is O(T) per query, and the total prefill cost adds up to O(T²). Although the attention computation is compressed, the indexer cost can become the new major part of the total.

    The table below summarizes the differences among three common approaches.

    Stage Full attention Linear-scan sparse attention Hierarchical or approximate indexing
    Candidate selection None Each query scores all keys Filters layer by layer along a hierarchical path
    Attention computation Runs over all keys Runs only over k candidates Runs only over k candidates
    Index construction None Usually done online Can be done offline
    Main bottleneck Square of sequence length Index scoring cost grows with length Index construction cost and recall error

    Indexers can reduce the constant overhead of each scoring operation through dimensionality reduction, quantization, block-level coarse filtering, and similar methods. These techniques shrink the actual runtime, but they do not necessarily change the growth trend. To judge an indexing scheme, look at both its constant factor and its growth order.

    2. Why There Is Still Room for Optimization

    Sparse attention works because a model relies on only a small amount of key information at most positions. The source article cites one experiment: at a 128k context, each query attends to only about 0.8% of the keys, most relevant to it, and the model still retains roughly 98% of full-attention performance on long-context benchmarks. The settings of this result, including the model, benchmarks, and statistical method, should be checked against the original paper before anyone relies on it.

    This shows that, in theory, finding a small set of relevant keys does not need to scale quadratically with sequence length. The problem is that existing indexers are often still designed to work like full scans.

    This optimization also has clear limits. Approximate retrieval may miss relevant keys, which lowers recall. How much lower recall affects model accuracy depends on the task. For this reason, every speedup should be paired with measurements of both recall and accuracy.

    3. Basic Approach to Hierarchical Retrieval

    The core idea of hierarchical retrieval is to separate offline construction from online querying.

    Offline, the system clusters all historical keys and builds representative vectors at several layers. Upper layers separate coarse semantic regions, and lower layers subdivide local clusters. After construction, each cluster stores a centroid vector and references to its member keys.

    Online, each query first produces a vector representation. Starting at the root layer, the system compares centroid vectors layer by layer, selects the most relevant branches, and continues until it reaches candidate clusters at the leaf layer. The candidate keys then go through the normal sparse attention computation.

    The query cost depends on the number of branches and layers. If each layer has b branches and there are h layers, each query needs roughly b·h comparisons instead of T. The actual complexity also depends on clustering quality, how evenly clusters are distributed, and implementation details, so the formula alone does not settle the question.

    There are three main risks. First, if clustering quality is poor, relevant keys may land in clusters that are hard to reach. Second, long sequences keep growing during inference, so the offline index must be maintained incrementally or rebuilt. Third, when different queries have very different attention distributions, a fixed hierarchy may work poorly for some of them.

    4. Physical Layout and I/O

    Whether hierarchical retrieval delivers real gains depends on the underlying storage. Traditional KV caches usually store data contiguously in time order. If retrieval results are scattered across many locations, each fetch becomes a random access, and read efficiency drops.

    One approach is to reorganize KV data by semantic cluster, so the keys in each cluster sit contiguously in memory. Once retrieval hits a cluster, the system can fetch all its candidates with fewer contiguous reads.

    This layout also has costs:

    • One-time data migration: Existing KV data must be rearranged by cluster before deployment. Estimate the migration time and the GPU or system memory it needs in advance.
    • Memory overhead: Each cluster stores a centroid vector and index structures, which take additional space.
    • Fragmentation risk: When cluster sizes vary widely, allocation and reclamation tend to fragment memory. Set upper and lower limits on cluster size.
    • Cache eviction: When data exceeds the memory of one machine, whole clusters can be swapped out and loaded back. Verify that cluster access frequency has enough locality. Otherwise, the cache hit rate may not improve.

    5. Changes to the Computation Graph: Consider Training and Deployment Separately

    The source article makes two points. During training, the retrieval structure takes part in end-to-end optimization. During deployment, model weights do not need to change. These apply to different scenarios, so each should be understood on its own terms.

    The first scenario is training a new model. The retrieval structure can be part of the computation graph as a trainable component. The hierarchy and centroid vectors can adjust during training, so the retrieval regions match the regions where attention focuses. The gains from end-to-end optimization apply only in this scenario.

    The second scenario is deploying a model that is already trained. Weights stay the same. The work consists of offline index construction and KV rearrangement. The model’s attention behavior does not change, and the gains come mainly from lower indexing overhead and better memory access.

    In either scenario, placing retrieval, data movement, and attention computation in one computation graph gives the compiler a chance to fuse operators. That reduces kernel launch overhead and intermediate tensor allocation. Measure the size of this benefit on real workloads rather than assuming it.

    6. How to Read Performance Numbers

    The source article reports large speedups. For one model in a 10M-token prefill scenario, a single attention layer reaches more than 22x speedup. Another model reports an even higher speedup. The article also states that on one long-context evaluation suite, accuracy is roughly on par with the original model.

    Without published settings, these numbers cannot support decisions. At minimum, confirm the following: whether the speedup is measured for a single layer or end to end, whether the baseline is original full attention or another sparse method, the hardware, batch size, and sequence length, and whether the accuracy change is an average across several tasks or the worst case.

    The source article also notes that the larger the share of total model runtime spent in the indexer, the more room there is for speedup. This judgment is more reliable than any specific multiplier, because you can verify it directly with your own profiling.

    To compare results, record them in a table like the one below.

    Model Sequence length Hardware Batch size Baseline method Speedup (single layer / end to end) Accuracy change
    To be filled in To be filled in To be filled in To be filled in To be filled in To be filled in To be filled in

    7. Steps for Engineering Adoption

    1. Profile performance. Measure the share of indexer time in both prefill and decode. If that share is low, hierarchical retrieval offers limited gains.
    2. Confirm whether retraining is allowed. If you can only deploy an existing model, consider only approaches such as offline index construction and KV rearrangement.
    3. Calculate amortized cost. Let C be the one-time cost of migration and indexing, and S the average time saved per request. About C/S requests are needed to recover that cost. If request volume is low, the change may not be worth making.
    4. Validate accuracy on long-context evaluation sets, and monitor recall separately. Stable accuracy does not mean every query is stable, especially for samples with unusual attention distributions.
    5. Monitor in production. As sequences grow, the index needs incremental maintenance or periodic rebuilding. Include this operational cost in your evaluation.