Sparse Attention at 10M Context: How Latent-Space Retrieval Delivers a 22x Speedup

    As context windows move from 1M to 10M tokens, the real bottleneck in sparse attention shifts from computing attention to finding which tokens to attend to. This article breaks down the three stages of a sparse attention call, explains why the indexer's scoring and selection costs grow quadratically, and shows how moving retrieval into a compressed latent space decouples candidate size from context length. It also covers how accuracy is preserved, which four dimensions to measure in evaluation, and practical guidance for deployment.

    1. Background: Once 1M Context Became Standard, the Cost Curve Bent

    Over the past year, long context has moved from a differentiating feature to a baseline specification. Mainstream model services now treat context in the 1M token range as a default capability, and architecturally they combine sparse attention with linear attention. The reasoning behind this is consistent: instead of having every query scan the entire history of tokens, each query selects a small number of the most relevant segments to participate in the computation.

    At the 1M scale, this approach has been validated repeatedly. But as context keeps expanding toward 10M tokens, an overhead that used to be unremarkable starts to spiral. Take a single global attention layer: at the tens-of-millions token scale, most of the prefill time is not spent on the attention matrix computation at all, but on the indexer that decides which tokens to look at.

    In other words, the nature of the problem has changed. Sparse attention has long since proven that attending to only a small fraction is enough to preserve accuracy; what actually blocks ultra-long context is that the process of finding that small fraction still grows quadratically with context length.

    2. Breaking Down the Bottleneck: Why the Indexer Becomes the Main Cost

    To understand this, split a single sparse attention call into three stages:

    1. Score: compute relevance between each query and candidate keys, typically a dot product after a low-dimensional projection;
    2. Select: take the top-k by score, which involves sorting or partial sorting;
    3. Aggregate (gather + attention): compute an attention-weighted sum over only the selected k positions.

    In the 4K to 128K range, the third stage dominates, so the gains from sparsification are obvious. But the candidate pool grows linearly with context, and the number of queries also grows linearly with context, so the combined cost of the first and second stages scales quadratically. By the time context reaches the tens of millions, even with k held constant, the total work of scoring and selection drowns out the actual attention computation.

    Schematically, the logic looks like this:

    1. # Pseudocode: cost structure of explicit top-k sparse attention
    2. scores = q @ k_proj(x).T # O(L^2 * d_low), L is the context length
    3. idx = topk(scores, k) # selection cost on the order of O(L^2 log k)
    4. out = weighted_sum(v[idx], scores[idx]) # O(L * k * d)

    What actually needs optimizing is the first two lines, not the third.

    3. Core Idea: Select in Latent Space Instead of Selecting Tokens

    The key abstraction behind this class of methods is: do not retrieve in the original token space. Instead, first compress the long context into a set of low-dimensional, fixed-budget latent representations (latent slots); the query matches only in latent space, and the latent slots then map back to the original segments.

    This yields three structural benefits:

    • Candidate size is decoupled from context length. The number of latent slots is determined by a budget rather than by L, reducing scoring and selection from quadratic to near-linear.
    • The selection granularity is more sensible. A latent slot typically corresponds to a semantically coherent segment, avoiding the noise and redundancy of per-token scoring.
    • Memory access is more friendly. The latent representations are small and can stay resident, reducing random reads over an extremely long KV.

    In engineering terms, this usually involves four steps:

    1. chunk encoding → latent slot compression → latent-space scoring and top-k → segment-level attention aggregation

    Latent slot compression can reuse pooling, low-rank projection, or learnable aggregation weights; the aggregation stage concatenates the KV of the selected original segments into a short sequence and runs standard attention once more.

    4. Why Accuracy Holds Up

    The premise that makes sparse attention work is that the attention distribution is itself highly concentrated: a few segments carry the vast majority of the weight. Latent-space retrieval does not change this fact; it only moves the matching process onto a smoother, more noise-resistant representation. Three points need attention in practice:

    • Training/adaptation consistency: the way latent slots are constructed must align with the model’s existing positional encoding and normalization scheme, otherwise long-context extrapolation degrades;
    • Recall fallback: keep a small fixed window (local attention) outside the top-k to prevent key neighboring information from being missed;
    • Layered budgets: shallow layers can use a more aggressive compression ratio while deeper layers retain higher recall, which keeps overall accuracy more stable.

    5. Performance Evaluation: Do Not Look at a Single Multiplier

    To evaluate this kind of approach, cover at least four dimensions:

    Dimension Metric of interest Notes
    Single operator Single-layer prefill latency Reflects whether the indexer overhead is truly eliminated
    End to end Time to first token, throughput Avoid optimizing one stage locally while being constrained by others
    Accuracy Long-document retrieval, multi-hop QA, summarization Watch the degradation on recall-oriented tasks
    Resources Peak GPU memory, bandwidth usage Whether resident latent slots squeeze out KV space

    Public results show that at the tens-of-millions token scale, a single attention layer’s prefill can achieve more than a 20x speedup, and up to tens of times faster under some model configurations, with accuracy close to the original model. The significance of these numbers is not the specific multiplier but that they validate the diagnosis of the bottleneck: optimize the index, not the multiplication.

    6. Deployment Recommendations

    1. Profile before optimizing. Use operator-level timing to confirm the indexer’s share, and avoid investing engineering effort in the wrong place.
    2. Prefer a hybrid architecture. Global latent-space retrieval + a local window + a linear attention channel is a more stable combination for long context.
    3. Coordinate with inference infrastructure. Chunked prefill, KV reuse, and prefix caching all stack with the latent slot approach, and the gains multiply.
    4. Back it with general-purpose cloud capabilities. Using a container platform to host inference services, object storage to hold long-document corpora, and logging plus monitoring and alerting to track time to first token and recall metrics is a fairly general engineering path. In long-context inference scenarios like this, Baidu AI Cloud is typically designed around a combination of three general capability categories: compute, storage, and observability.

    7. Conclusion

    At 10M token context, the core tension has shifted from “can we compute it” to “can we find it.” Moving retrieval from the original token space into latent space, and replacing a candidate set that expands with length with a fixed budget, is currently the most cost-effective direction. For developers, the real takeaway is not a particular multiplier but a reusable framework for reasoning: first locate the quadratic term, then use representation compression to pull it back to linear, and finally use a recall fallback to protect accuracy.