Sparse Attention at 10M Tokens: Breaking the Indexer Bottleneck

    Sparse attention reduces main-attention cost from O(T²) to O(T·k), but the indexer that scores all keys still runs in O(T²·d) and dominates prefill at multi-million-token contexts. This article explains where the indexer's cost comes from, the limits of four common lightweight optimizations, and how hierarchical pre-screening, progressive refinement with early stopping, and cache reuse can change the search structure itself and cut scoring operations to a tiny fraction of a full scan.

    1. The Cost of Attention in Long Contexts

    The context windows of large language models have grown from tens of thousands of tokens to the millions. Some scenarios push even further—tens of millions of tokens, such as an entire code repository or long-term conversation history. In standard full attention, every query computes similarity against every key. When the sequence length T is 10 million, the number of query–key pairs is roughly T², or 10¹⁴. Each pair requires a d-dimensional dot product, so total computation multiplies by d on top of that.

    Sparse attention keeps only a small number of the most relevant keys. The cost of the main attention step drops from O(T²) to O(T·k), where k is the number of keys retained per query. But before those k keys can be selected, the system must score all keys first. That job belongs to the indexer, and its cost is often overlooked.

    2. Where the Indexer’s Cost Comes From

    Sparse attention runs in two stages. In the first stage, the indexer scores every query against all T keys. Each score is a d-dimensional dot product, for a total cost of O(T²·d). In the second stage, the main attention computes only over the selected k keys, at a cost of O(T·k·d).

    The ratio between the two stages is roughly T/k. With T at 10 million and k at 512, that ratio is about 20,000. In other words, the indexer’s computation far exceeds the main attention’s. The total prefill cost remains quadratic; sparse attention merely moves the quadratic term to a different stage rather than eliminating it.

    The original text also stated that at 128k length, 512 keys account for about 0.8% of all keys. That figure is incorrect: 512 divided by 131,072 is about 0.4%; 0.8% corresponds to a 64k length. The discussion below uses the actual ratio.

    3. The Ceiling of Four Lightweight Approaches

    Common ways to lighten the indexer fall into four categories:

    1. Lower scoring precision. Switch floating-point scoring to half precision or integer quantization so each operation is cheaper. Excessive quantization flattens the relevance distribution, and Top-k recall drops with it.
    2. Reduce projection dimensions. Project keys and queries into a lower-dimensional space—halving d halves the scoring work. Too few dimensions weakens discrimination, and the quality of the selected keys degrades.
    3. Key compression and grouping. Merge adjacent keys into groups, score groups first, then refine within the selected groups. Group boundaries can split correlated adjacent keys; a common fix is overlapping windows, but overlap adds extra scoring.
    4. Approximate nearest neighbor search. Replace exhaustive scoring with locality-sensitive hashing or clustered indexes. Effectiveness depends on the data distribution, and in extreme cases index construction itself becomes slow.

    All four approaches compress the constant factor. Suppose scoring one key takes 1 microsecond: scanning 10 million keys takes 10 seconds per query. Halving the constant still leaves 10⁷-scale work. To change the growth rate, you have to change the indexer’s search structure.

    4. Hierarchical Pre-screening: Changing the Search Structure

    The core premise is that relevance has a hierarchical structure. In a long document, a query is typically relevant to only a few sections, and within each section only to a few paragraphs. The system can first locate candidate regions at a coarse granularity, then refine level by level, avoiding a full scan of all keys every time.

    4.1 Building a Multi-granularity Index

    The index has three levels. The first is blocks: every B adjacent keys are averaged into one block vector. The second is regions: every F blocks are averaged into one region vector. The third is super-regions, aggregated the same way once more. The three-level structure can be built offline or during prefill.

    At query time, score the super-regions first and keep the top m; then score the sub-regions of those super-regions and keep m; continue downward until fine ranking within blocks produces the final k keys.

    With a fixed block size B, the number of vectors at the first level is still T/B, which grows linearly with T. To achieve sublinear growth, there are two options:

    • Fix the branching factor F and increase the number of levels. The number of levels is about log_F T, and each level scores only m·F nodes, giving a per-query scoring count of O(m·F·log T).
    • Let block size grow with T, for example B≈√T. A two-level structure then scores about O((m+1)·√T) times.

    Take 10 million tokens with a branching factor of 64 and three levels: about 156,250 blocks, 2,442 regions, and 39 super-regions. Setting m to 8, 8, and 16 respectively, the four steps require 39, 512, 512, and 1,024 scoring operations—about 2,087 in total, roughly 0.02% of a full 10-million-key scan. This compares operation counts only; it excludes vector reads and kernel overhead and does not multiply by dimension d, but the relative scale shows what structure can do.

    Hierarchical methods carry an obvious risk: region vectors come from averaging, and averaging can mask locally strong relevant keys. If the upper level selects wrong, no amount of lower-level precision can recover the loss. Each level’s selection quality must therefore be validated with recall metrics.

    4.2 Progressive Refinement and Early Stopping

    The number of candidates kept decreases at each level. The stopping condition should reflect the confidence of the selection. Let the m-th score be sm and the (m+1)-th score be s{m+1}. If the gap between them exceeds a threshold δ, the ranking at that level is fairly stable, further refinement yields little benefit, and the process can stop early.

    The variance of candidate scores should not be used as the stopping condition. Small variance only means the candidates are hard to distinguish—in that case, continuing refinement may actually recover missed keys. The threshold δ needs to be calibrated on a validation set, along with the recall loss introduced by early stopping.

    4.3 The Boundaries of Cache Reuse

    When the same prefix is requested multiple times, the candidate sets of adjacent queries are often similar. The previous result can be cached, and a new query can reuse it directly with only minor corrections.

    Cache design should include:

    • Cache key: composed of the tenant ID, model version, context prefix hash, and region ID. Missing any of these can produce incorrect results.
    • Reuse condition: the new query vector and