The Key to 22x Faster Sparse Attention at 10M Token Context

    As context windows scale from 1M to 10M tokens, the cost structure of long-context inference shifts fundamentally. Sparse attention has already shown that letting each query attend to only a few of the most relevant historical tokens is enough to preserve accuracy, yet the real bottleneck is not attention itself but the indexing step that decides which tokens to look at. Using a global attention layer from a model as an example, this article shows that at the 10M token scale, roughly 98% of prefill time is spent in the indexer rather than in attention computation. This article explains why selection cost grows quadratically with context length, how latent sparse attention moves retrieval into a low-dimensional latent space, why this removes an order of magnitude of overhead, and what to consider when putting it into production, including block sizing, positional encoding, accuracy fallbacks, observability, and evaluation.

    When context length grows from 1M to 10M tokens, the cost structure of long-context inference changes fundamentally. The sparse attention approach widely used in the industry has already proven that having each query look at only a small number of the most relevant historical tokens is sufficient to maintain accuracy. What has truly spiraled out of control, however, is not attention itself but the indexing stage that decides which tokens to look at. This article takes one global attention layer of a model as an example: at the 10M token scale, roughly 98% of prefill time is consumed by the indexer rather than by attention computation. In other words, selection cost grows quadratically with context length and has become the first bottleneck for ultra-long context.

    1. Reframing the Problem: From Computing Attention to Finding Tokens

    The compute and memory cost of traditional full attention grows quadratically with sequence length. Sparse attention starts from the idea of compressing the visible range of each query to a fixed, small set of positions, bringing computation back to near-linear. In engineering practice, however, sparsification merely shifts the cost: to pick out “the most relevant small subset,” you must first score and rank all historical tokens, and in a naive implementation this step also traverses the entire history and is often more expensive than a single efficient attention kernel.

    The nature of the problem has therefore changed. Sparse attention solved “looking at too much” but did not solve “searching too expensively.” At the 1M scale, indexing overhead can still be hidden by a highly optimized attention kernel; at the 10M scale, the indexer becomes the dominant term by far. Any approach that keeps optimizing along the “score precisely first, then truncate” line will hit the same wall.

    2. Core Idea: Move Retrieval into the Latent Space

    One validated direction is latent sparse attention: instead of doing exact relevance matching in the original token dimension, first compress the long history into a low-dimensional latent representation, do coarse filtering in the latent space, and then run exact attention on only a small number of candidates.

    The logic chain breaks down into three steps:

    1. Compression: map the keys and values of historical tokens into low-dimensional latent vectors, forming an “index view” much smaller than the original sequence.
    2. Coarse filtering: the query performs similarity retrieval in the latent space, with cost tied to the latent dimension and the number of candidate blocks rather than to the square of the total length.
    3. Exact computation: run full attention only on the few selected tokens, so representational accuracy is not eroded by compression loss.

    Illustrative pseudocode:

    1. # History side: compress into a latent index in blocks (built offline/incrementally)
    2. latent_index = compress_to_latent(kv_history, block=256, dim=64)
    3. # Query side: coarse filtering in latent space + exact computation in original space
    4. cand = latent_index.topk(q_latent, k=64) # cost is roughly linear
    5. out = sparse_attention(q, kv_history[cand]) # exact computation on candidates only

    The key point is the division of labor between “cheap coarse filtering” and “accurate exact computation”: the information loss from compression is absorbed by the redundancy of the candidate set, while the final output is still determined by attention at original precision.

    3. Why This Delivers an Order-of-Magnitude Speedup

    Writing out the complexity makes the benefit obvious. Let the sequence length be L, the latent dimension d, and the number of candidates k:

    • Naive sparse indexing: roughly O(L²) for scoring and ranking;
    • Latent-space coarse filtering: roughly O(L·d) for retrieval, where d is a constant far smaller than L;
    • Exact attention: roughly O(k·L) or O(k²), with k fixed.

    When L grows from 1M to 10M, the first term grows 100x while the latter two barely change. This is exactly where the twenty-plus and even seventy-plus speedups for single-layer prefill in public results come from: speedup ratios vary noticeably across models because of differences in the original indexer implementation and attention kernel efficiency, but the trend is consistent — what gets eliminated is the overhead bound to the square of the length.

    4. Engineering Implementation Notes

    • Decouple indexing from computation: the latent index can be built incrementally and cached per block, avoiding a rebuild on every prefill.
    • Block granularity trade-off: larger blocks mean higher compression and coarser recall; smaller blocks mean more accurate recall and a larger index. A grid search constrained by recall rate is recommended.
    • Coordinate with positional encoding: latent-space compression weakens positional resolution, so inject positional information before compression or keep a small number of “anchor tokens.”
    • Accuracy fallback: keep full-precision attention on a local window, running in parallel with the global sparse path, to cover both nearby detail and long-range recall.
    • Observability: treat “indexer time / exact-computation time / candidate hit rate” as first-class metrics wired into logging and monitoring alerts; otherwise regressions cannot be located.

    5. How to Evaluate the Benefit

    Do not look only at end-to-end throughput. Measure at three levels: the split between indexer and exact-computation time for single-layer prefill; end-to-end time to first token and throughput; and recall-oriented metrics on long-context tasks (needle in a haystack, multi-hop retrieval, long-document question answering). In engineering practice, a container platform can elastically spin up a load-testing cluster, and object storage can archive input and output samples to guarantee reproducibility.

    6. Conclusion

    In the era of 10M tokens, the competitive point of long context has shifted from “how fast attention computes” to “how cheaply candidates are found.” Latent sparse attention trades one low-dimensional coarse filtering pass for quadratic exact retrieval, and is currently one of the most cost-effective paths. For platform providers, turning this kind of capability into a configurable long-context inference option, together with a complete load-testing and observability system, is what reliably converts the speedup ratios in papers into latency and cost benefits on the business side.