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

    As context windows push past the million-token mark toward 10 million tokens, the bottleneck in long-context inference shifts from computing attention to finding which tokens matter. This article explains why the indexing overhead of mainstream sparse attention grows quadratically with context length, how Latent Sparse Attention moves relevance retrieval into a compressed latent space to bring that cost back to linear scaling, and why reported speedups range from roughly 22x to 72x. It also covers the accuracy trade-offs of compression and practical guidance on layer selection, prefill and decode execution, caching, and evaluation.

    1. Background: The Bottleneck Shifts Once Contexts Reach 10 Million Tokens

    Over the past two years, long-context capability has moved quickly from an experimental feature to a default specification. Million-token contexts are becoming a standard configuration for many model services, and hybrid architectures that combine sparse attention with linear attention are widely adopted. The shared premise is that a query vector does not need to scan every historical token; it only needs to involve a small set of the most relevant ones in the computation.

    That premise has been validated repeatedly at the million-token scale. But as contexts extend to 10 million tokens, an overhead that once seemed negligible starts to spiral. Take a global attention layer as an example: at the 10-million-token scale during the prefill stage, roughly 98% of the time is spent not on the 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 already shown that “looking at only a small fraction” is enough to preserve accuracy. What actually blocks ultra-long contexts is that the cost of “finding that small fraction” still grows quadratically with context length.

    2. Root Cause: Why Indexing Overhead Grows Quadratically

    Mainstream sparse attention implementations typically follow two steps:

    1. Scoring: compute similarity between the query vector and the key vectors of all historical tokens to produce relevance scores.
    2. Selection: take the Top-K by score and run attention only over the selected tokens.

    The first step has a complexity of O(L²·d), where L is the context length and d is the head dimension. When L grows from 1M to 10M, the compute for this step increases by roughly 100x, while the K value that actually participates in attention is often only a few thousand and barely grows with L. As a result, a large share of compute goes into “filtering” rather than “computing.”

    Illustrative pseudocode:

    1. # Naive sparse attention: the bottleneck is scoring
    2. scores = q @ K.T # [B, H, L_q, L], grows quadratically with L
    3. idx = topk(scores, k=2048) # selection overhead is relatively negligible
    4. out = sparse_attention(q, K, V, idx)

    Two engineering factors amplify the issue. First, scoring requires keeping the full key cache in GPU memory or high-speed storage, which puts heavy pressure on bandwidth. Second, Top-K selection and sorting are hard to parallelize over ultra-long sequences and tend to form a serial bottleneck.

    3. Core Idea: Move Retrieval into the Latent Space

    The key abstraction behind Latent Sparse Attention is that relevance retrieval does not have to happen in the original token dimension; it can be done in a compressed, lower-dimensional latent space.

    The logic chain is as follows:

    • Compress the historical keys and values into a sequence of latent states with a fixed length that is far smaller than the original token count.
    • Project the query vector into the same latent space and match it against the latent states for similarity.
    • After completing Top-K selection in the latent space, map back to the original token range to run attention.

    Because the retrieval length drops from L to M, which is tied to the compression ratio and satisfies M ≪ L, the complexity of the scoring step falls from O(L²·d) to roughly O(L·M·d). At the 10-million-token scale, this gain is significantly amplified.

    1. # Latent retrieval: the scoring length is compressed
    2. Z = compress(K) # [B, H, M], M << L
    3. scores = q_latent @ Z.T # substantially lower complexity
    4. idx = topk(scores, k=2048)
    5. out = sparse_attention(q, K, V, idx)

    It is worth stressing that latent-space compression is not lossless. Its feasibility depends on two preconditions: first, historical information must retain enough distinguishability after compression; second, the compression and retrieval modules need to be trained jointly with the backbone model or lightly fine-tuned to maintain accuracy.

    4. Gains and Costs: Understanding the 22x vs. 72x Difference

    At the 10-million-token scale, the method achieves roughly 22.7x and 72.4x speedups for the prefill of a single attention layer, while maintaining accuracy close to the original model across a range of tasks. The gap between the two numbers is not accidental and mainly depends on:

    • The share of indexing overhead in the original implementation: the higher the share, the larger the gain after optimization.
    • Whether the model already uses a highly optimized sparse kernel: the stronger the baseline, the more conservative the multiple.
    • How well the compression ratio matches the head dimension.

    The costs are equally clear: the extra compression and projection add a small amount of parameters and GPU memory overhead; too high a compression ratio loses fine-grained information and degrades accuracy on tasks that require precise long-distance retrieval. In practice, the compression ratio is therefore usually treated as a tunable hyperparameter and jointly tuned with the K value.

    5. Engineering Recommendations

    1. Layered design: enable latent-space retrieval only for global attention layers and leave local window layers unchanged, which offers the best cost-performance trade-off.
    2. Staged execution: use latent-space retrieval in the prefill stage to reduce filtering cost, and keep the incremental cache in the decode stage to avoid repeated compression.
    3. Caching strategy: latent states can be updated incrementally as chunked prefill proceeds; combined with chunked pipelining and operator fusion, this reduces GPU memory round trips.
    4. Accuracy guardrails: set up three evaluations—“needle in a haystack,” multi-hop reasoning, and long-document summarization—to monitor degradation caused by changes in the compression ratio.
    5. Cloud deployment: inference services can run on a container platform, evaluation corpora can be stored in object storage, and first-token latency and throughput changes can be observed through log services and monitoring alerts.