Sparse Attention Index Optimization for 10M-Token Contexts

    As context windows move from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from attention computation itself to the indexer that decides which tokens each query should attend to. This article explains why conventional sparse-attention indexing becomes dominant at 10M-token scale, introduces Latent Sparse Attention (LSA) with low-dimensional projection and two-stage filtering, summarizes reported prefill speedups and accuracy trade-offs, and offers practical guidance on block size, latent dimension, profiling, decoupled retrieval/computation design, and deployment resource constraints.

    Sparse Attention Index Optimization for 10M-Token Contexts

    1. The New Bottleneck in the Ultra-Long-Context Era

    Over the past year, large-model context windows have moved rapidly from the 100K range to the million-token range, and many vendors have made million-token contexts a default specification for mainstream services. The key technical route behind this leap is sparse attention: instead of having every query scan all historical tokens, it selects only a small number of the most relevant key-value pairs for computation. This approach has been repeatedly validated at million-token scale, with controllable accuracy loss and a substantial reduction in computation.

    However, as contexts move toward the ten-million-token scale, a previously minor cost begins to spiral out of control. Sparse attention usually has two stages: first, an “indexer” selects candidate historical tokens for each query; then the actual attention computation is performed on the selected tokens. At million-token scale, the indexing stage is acceptable as a share of total latency. At ten-million-token scale, however, measurements show that for some models, when global attention layers perform prefill, more than 90% of the time is spent not on attention computation itself but on the indexing step that decides which tokens to look at.

    The nature of the problem therefore changes. Sparse attention has long demonstrated that “looking at only a small subset” is enough to preserve accuracy. What actually blocks ultra-long contexts is that “finding that small subset” still grows quadratically with context length. Unless the indexer is fundamentally restructured, the gains from sparsification will be consumed by retrieval cost.

    2. Root Cause: Why Indexers Become More Expensive

    Mainstream sparse-attention indexing methods fall roughly into three categories:

    1. Full scoring followed by Top-k selection: Compute a relevance score between each query and all historical keys, then select the k keys with the highest scores. This type achieves high selection quality, but the scoring process itself is O(N²) or scales between linear and quadratic with context length, so it becomes a bottleneck at large scale.
    2. Coarse filtering based on blocks or clustering: Organize historical tokens by blocks or cluster centers, and compare queries only with block representatives. This reduces the number of comparisons, but critical tokens may be missed within a block, and as the number of blocks grows, the cost of comparing representatives also expands linearly.
    3. Hashing or approximate nearest-neighbor retrieval: Use approximation methods such as locality-sensitive hashing to accelerate retrieval. However, approximation errors in the high-dimensional attention space directly translate into wrong token selections, and accuracy can fluctuate significantly.

    In all three cases, the core conflict is the same: indexing operates in the same high-dimensional space as the original hidden states, and the number of candidates to scan grows linearly or even quadratically with context length. The only way out is to make indexing itself “cheaper”—reducing both the cost of a single comparison and the number of candidates that must be compared.

    3. Latent Sparse Attention: Moving Retrieval into a Low-Dimensional Space

    A recent method jointly proposed by academia and industry offers an instructive answer. Its core idea can be summarized as Latent Sparse Attention (LSA): instead of performing relevance filtering in the original high-dimensional key space, first project keys into a “latent space” whose dimension is much lower than the original attention dimension, complete coarse filtering in this low-dimensional space, and then map back to the original space for exact attention.

    This design brings two benefits:

    • The cost of a single comparison drops sharply. Suppose the original key dimension is d and the latent-space dimension is r, where r is much smaller than d. A single scoring operation then drops from O(d) to O(r). When r is one-tenth or less of d, this alone delivers nearly an order-of-magnitude saving.
    • The index structure can be precomputed and reused. Low-dimensional projected key representations are small and structurally regular, so a compact index structure can be built in advance, such as low-dimensional centroids aggregated by block. During prefill, queries only need to compare with centroids; after a block is selected, fine filtering is performed within the block, compressing the candidate set from N to N/B, where B is the block size.

    The overall workflow can be abstracted as follows:

    1. # Pseudocode: two-stage low-dimensional indexing
    2. keys_low = W_down @ keys # Low-dimensional projection, shape [N, r]
    3. block_centers = mean_pool(keys_low, block_size=B) # Block centroids [N/B, r]
    4. for q in queries:
    5. q_low = W_down_q @ q
    6. scores = q_low @ block_centers.T # Coarse filtering: compare only with N/B centroids
    7. top_blocks = topk(scores, k_blocks)
    8. candidates = gather(keys, top_blocks) # Retrieve original keys in selected blocks
    9. final_sel = topk(q @ candidates.T, k) # Fine filtering within blocks
    10. output = attention(q, final_sel_keys, final_sel_vals)

    It is worth noting that the low-dimensional projection matrix is not arbitrary. It needs to be learned from a small amount of calibration data or distilled from the original attention weights so that relevance ranking in the low-dimensional space stays as consistent as possible with that in the high-dimensional space. This is the key to preserving accuracy.

    4. Measured Results and Accuracy Trade-offs

    In prefill tests at the ten-million-token scale, the method showed significant speedups across models of different sizes. For an efficient model using a hybrid sparse and linear attention architecture, a single attention layer was accelerated by more than 22×. For a medium-sized dense model, the speedup exceeded 70×. More importantly, on a range of long-context benchmarks such as long-text retrieval, multi-hop reasoning, and repository-level code understanding, accuracy was essentially on par with the original model. Some tasks even improved slightly because indexing noise was reduced.

    This result confirms a judgment: index quality matters more than index density. Although low-dimensional projection loses information in theory, because the projection directions are learned for the task, its ability to distinguish relevant from irrelevant tokens can be better than naive full-dimensional approximation. At the same time, the two-stage filtering structure ensures that the final token set used in attention computation overlaps strongly with the ideal Top-k set.

    Of course, engineering deployment still requires balancing several factors. If block size B is too large, fine-filtering cost within blocks increases; if it is too small, the coarse-filtering stage degrades. If latent dimension r is too low, selection accuracy suffers; if it is too high, the speedup benefit disappears. Practical experience suggests that r set to 1/8 to 1/16 of the original dimension and block size set to 64 to 128 are relatively robust starting points.

    5. Lessons for Engineering Practice

    For teams building ultra-long-context inference capabilities, this work offers three transferable lessons.

    First, profile before optimizing. In long-context systems, attention computation is not necessarily the bottleneck. Indexing, KV movement, and memory bandwidth can all become dominant costs. At the ten-million-token scale, teams should perform fine-grained latency breakdowns of each prefill stage before deciding where to optimize.

    Second, design retrieval and computation as decoupled modules. Split “which tokens to select” and “how to compute attention” into two independently optimizable modules. The indexing side can aggressively adopt low-dimensional, quantized, and blocked approximations, while the computation side remains high-precision. The two are connected through a two-stage structure.

    Third, pay attention to deployment-side resource constraints. Ultra-long-context inference places extremely high demands on compute, memory, and scheduling. In production, teams can rely on general cloud compute and inference acceleration services and combine them with the indexing optimizations above to turn ten-million-token contexts from paper metrics into affordable online capability.

    6. Conclusion

    Sparse attention solved the question of “how much to compute,” while Latent Sparse Attention further addresses “how to find it.” As context windows move toward ten million tokens, indexing efficiency will replace attention computation itself as the primary variable determining system throughput. The paradigm of low-dimensional projection plus two-stage filtering provides a feasible path that balances speed and accuracy, and it draws a new starting line for the architecture design of next-generation ultra-long-context models.