Sparse Attention at 10M Token Context: How Latent Space Indexing Delivers Up to 22x Prefill Speedup

    As context windows scale from 1 million to 10 million tokens, the cost structure of long-context inference changes fundamentally. This article examines Latent Sparse Attention (LSA), a method that moves the retrieval step into a compressed latent space to remove the indexing bottleneck. It covers the problem redefinition, the two-stage retrieval design, the mechanisms that preserve accuracy, engineering implementation details, and an evaluation methodology, with reported prefill speedups of roughly 22x and 72x depending on model configuration.

    When context length grows from 1 million tokens toward 10 million, the cost structure of long-context inference changes fundamentally. The industry has repeatedly confirmed that letting each query attend to only a small set of the most relevant historical tokens is enough to preserve accuracy on most tasks. But the work of finding that small set still grows quadratically with context length.

    Take one global attention layer of an ultra-long-context model as an example. During prefill at the 10-million-token scale, roughly 98% of the time is not spent on attention computation at all, but on the indexer that decides which tokens to look at. This means further optimizing the attention matrix multiplications yields limited returns; the real leverage lies in redesigning the indexing mechanism.

    This article focuses on a method called Latent Sparse Attention (LSA). At the 10-million-token scale, it can speed up the prefill of a single attention layer by more than an order of magnitude (public descriptions cite roughly 22x relative to a certain class of sparse attention baseline, and roughly 72x under another model configuration), while maintaining accuracy close to the original model. The breakdown below covers five layers: problem definition, method principles, engineering implementation, accuracy guarantees, and evaluation methodology.

    1. Reframing the Problem: From “Slow Compute” to “Slow Search”

    The complexity of traditional dense attention is O(N²), where N is the context length. Sparse attention compresses each query’s visible set to k tokens, reducing computation to O(N·k). But when N reaches the tens of millions, the indexer must score, sort, and take the top-k over all N candidates in the original token space. This step remains on the order of O(N²·d) (where d is the scoring dimension), and it repeatedly reads and writes a massive key cache.

    In other words, sparsification only solved the “multiply-accumulate” problem, not the “retrieval” problem. The indexer becomes the new hotspot: its share of total time rises monotonically as N grows, eventually consuming nearly all of the gains. This is the core contradiction in engineering context lengths at the ten-million scale.

    2. The Core Idea of LSA: Moving Retrieval into a Low-Dimensional Latent Space

    The key observation behind LSA is that judging “relevance” does not require full-dimensional keys, only a low-dimensional projection with enough discriminative power. So LSA migrates the indexing process from the original token space into a compressed latent space:

    1. Dimensionality reduction projection: compute a low-dimensional latent vector for each token (for example, compressing head_dim from 128 down to 16–32) to serve as the index key.
    2. Coarse-grained bucketing: cluster or segment the latents in the latent space (an inverted structure for approximate nearest neighbor), organizing N tokens into on the order of √N buckets.
    3. Two-stage retrieval: first score buckets against the query and select a small number of candidate buckets; then perform exact top-k within those candidate buckets. Overall complexity drops from O(N²) to close to O(N·√N) or lower.
    4. Exact read-back: only for the final k selected tokens, fetch the full-dimensional key/value from the original key cache and perform the actual attention computation.

    Illustrative pseudocode:

    1. # Prefill stage: build the latent space index
    2. latent = proj_lowdim(keys) # [N, d_latent], d_latent << head_dim
    3. buckets = cluster(latent) # approximate nearest neighbor inverted index, ~sqrt(N) buckets
    4. # Retrieval per query
    5. def lsa_select(q, k, top_b=8):
    6. q_lat = proj_lowdim_q(q)
    7. b_scores = score_buckets(q_lat, buckets) # score buckets only
    8. cand = topk(b_scores, top_b) # union of candidate tokens
    9. idx = exact_topk(q, keys[cand], k) # exact top-k within buckets
    10. return idx
    11. # Attention computation only touches the selected tokens
    12. out = attention(q, keys[idx], values[idx])

    3. Why Accuracy Holds Up

    The accuracy foundation of sparse attention lies in the high concentration of the attention distribution: for most queries, the attention mass concentrates on very few keys. LSA adds two safeguards on top of this:

    • Recall compensation for coarse filtering: the number of candidate buckets, top_b, is tunable, trading a small amount of extra retrieval for high recall and avoiding “missing the truly relevant tokens.”
    • Local window fallback: forcibly retain the most recent w tokens plus a few global anchor tokens, covering local dependencies and task instructions, and preventing retrieval from failing in long-tail scenarios.

    These two points give LSA a controllable trade-off between “retrieval” and “computation”: k controls the computation volume, top_b controls recall, and w controls locality.

    4. Engineering Implementation Notes

    1. Tiered caching: keep the latent index resident in GPU memory or system memory, and page the original key/value in by bucket to reduce peak GPU memory usage.
    2. Operator fusion: fuse the dimensionality-reduction projection, bucket scoring, and top-k into a single kernel to reduce the round-trip bandwidth for ten-million-scale tensors.
    3. Intra-batch sharing: multiple heads within the same request can share the bucket structure, amortizing the cost of building the index.
    4. Decoupling from the decode stage: use LSA to build the index during prefill, and reuse the same index for incremental retrieval during decode, avoiding repeated construction.
    5. Cloud deployment: inference services can run on a container platform, with object storage caching long-context KV, and monitoring and alerting observing index hit rate and latency percentiles.

    5. How to Evaluate the Gains

    It is recommended to use “single attention layer prefill time” as the minimum comparable unit: fix k, top_b, and w, sweep N ∈ {1M, 4M, 10M}, and record the index time share, end-to-end latency, and peak GPU memory. On the accuracy side, compare against the original model on tasks such as long-document question answering, cross-segment retrieval, and repository-level code completion. Focus on two inflection points: when the index share exceeds 90%, and how large top_b must grow before recall saturates.

    Conclusion

    The next stage of ten-million-token context is not about computing attention faster, but about making the task of “finding tokens” cheaper. Through dimensionality-reduced retrieval plus two-stage filtering, Latent Sparse Attention pushes indexing complexity below quadratic, moving ultra-long context from “able to run” to “economical to run.” For engineering teams, the implementation path is clear: first implement the latent dimensionality reduction and bucketing, then tune the k/top_b/w triple, and finally squeeze out bandwidth with tiered caching and operator fusion.