Sparse Attention at 10 Million Token Contexts: Key Practices Behind a 22x Prefill Speedup

    As context windows scale from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from computing attention to selecting which tokens to attend to. This article explains why the indexing stage grows quadratically and comes to dominate prefill time, then walks through Latent Sparse Attention (LSA), which moves selection into a low-dimensional latent space to replace the quadratic term with a near-linear one. You will see reported speedups of roughly 22.7x and 72.4x on a single global attention layer, why approximate indexing can preserve accuracy, and a practical engineering checklist covering layered attention budgets, incremental latent updates, recall calibration, memory accounting, observability, and elastic capacity.

    1. The Problem: Cost Structure Inverts as Context Scales

    Over the past two years, long context has shifted from an optional feature to a default specification. Million-token windows are now standard across several model services, and the research frontier is pushing toward tens of millions of tokens. The shared technical choice behind this shift is to stop having every query scan the entire history and instead let it attend to only a few of the most relevant segments. At the million-token scale, this approach has been validated repeatedly with controllable accuracy loss.

    But as the window grows to tens of millions of tokens, an overhead that once seemed negligible starts to spiral. Take one global attention layer in an open-source model that uses a hybrid attention architecture. At the ten-million-token scale, during the prefill stage, roughly 98% of the time is not spent on the attention computation itself, but on the indexing step that decides which tokens to look at.

    This changes the nature of the problem. Sparse attention has already shown that attending to a small subset is enough to preserve accuracy. What actually blocks ultra-long context is that the process of finding that small subset still costs quadratic growth as context length increases.

    2. A Complexity View: Why Selection Costs More Than Computation

    Attention is easier to reason about when split into two stages:

    1. Scoring/indexing stage: evaluate the relevance of candidate keys for each query and select the top-k.
    2. Aggregation stage: perform a weighted sum only over the selected k positions.

    In a naive implementation, the first stage requires comparing the query against every key, giving O(N²·d) complexity. The second stage is only O(N·k·d), because k is a constant. When N grows from 10⁶ to 10⁷, the quadratic term grows 100x while the linear term grows only 10x, so indexing completely dominates runtime. This also explains why “just make k smaller” barely helps: the bottleneck is not aggregation, it is selection.

    Three common engineering mitigations each have clear limits:

    • Block-level sparsity: split tokens into blocks, score block representative vectors first, then rank precisely within a block. This reduces the candidate count, but the quality of the block representative vectors directly determines recall.
    • Compressed/summary-style indexing: summarize long history with a small number of “summary tokens” and score the query only against those summaries. Building and updating the summaries carries its own cost, and the information bottleneck is significant.
    • Two-stage retrieval: coarse ranking followed by fine ranking. This still compares in the original token space, so the benefit diminishes as N grows.

    3. The Core Idea: Move Selection into Latent Space

    A recent paper from a startup research team in London and a technical university in Zurich introduces Latent Sparse Attention (LSA). Its key insight is that indexing is expensive because it performs pairwise comparisons in the original token dimension; moving indexing into a latent space whose dimension is far smaller than the sequence length changes the cost structure.

    The design has three layers:

    • Latent compression: project long history into a number of latent vectors (latent slots) far smaller than the token count, where each latent vector carries aggregated information about a segment of history.
    • Latent scoring: the query evaluates similarity only over the set of latent vectors, reducing the candidate pool from N to M (M ≪ N) and the indexing complexity from roughly O(N²) to O(N·M).
    • Back-mapping and exact computation: after selecting the relevant latent vectors, map them back to the original token ranges they cover and perform exact attention aggregation only within those ranges.

    Illustrative pseudocode (structure only, not a specific API):

    1. # N: sequence length, M: number of latent vectors, d: latent dimension
    2. latent = compress(history, slots=M) # O(N·d) one-time build
    3. scores = query @ latent.T # O(N·M·d) main indexing cost
    4. slots = topk(scores, k=K) # select a few relevant latent vectors
    5. span = expand_to_tokens(slots) # latent vectors -> original token ranges
    6. out = softmax(qk(span)) @ v(span) # exact computation only within small ranges

    The most important point here is not the implementation of any single step, but the substitution of complexity terms: the term that grows quadratically with N is replaced by a term that grows linearly with N and has a very small coefficient.

    4. Results and Accuracy: Why Approximate Indexing Does Not Necessarily Lose Quality

    Public results show that at the ten-million-token scale, this method delivers two orders of speedup for the prefill stage of a single global attention layer, roughly 22.7x and 72.4x (corresponding to two open-source models of different scales), while maintaining accuracy close to the original model across multiple tasks.

    Accuracy holds for three reasons:

    • Recall-first coarse filtering: the latent space is responsible only for “not missing anything,” not for “computing precisely,” so a looser K can be used to buy high recall.
    • Exact aggregation as a safety net: the final values still come from attention over the original tokens; approximation happens only in candidate generation.
    • Observable error: both the number of latent vectors M and K are tunable knobs, forming a clear speed-versus-accuracy tradeoff curve.

    5. Engineering Implementation Checklist

    To borrow this idea in your own ultra-long-context pipeline, focus on the following:

    1. Layered attention budget: global layers are limited in number, so concentrate the optimization budget on those few global layers and keep local layers cheap.
    2. Incremental updates for latent construction: in streaming scenarios, avoid rebuilding the compressed representation in full every time; use a sliding window plus periodic re-compaction.
    3. Recall, not latency, as the primary metric: first use offline evaluation to determine safe ranges for M and K, then tune performance.
    4. Memory and bandwidth accounting: the compressed representation itself occupies memory and must be included in capacity planning alongside the KV cache.
    5. Observability: track index hit rate and the distribution of candidate range lengths through monitoring, alerting, and logging services to avoid long-tail requests dragging down P99.
    6. Elastic capacity: prefill is a compute-intensive, bursty workload, so elastic scaling and queue-based scheduling on a container platform are more economical than fixed sizing.

    When deploying on the cloud, these capabilities generally map to common categories: object storage for long-document corpora, message queues for peak shaving, a container platform for inference services, and monitoring, alerting, and logging services for quality regression. If you use the relevant general capabilities from Baidu AI Cloud, you can compose them along the same lines without relying on any proprietary interface to build a comparable architecture.

    6. Conclusion

    In the era of tens of millions of tokens, the real bottleneck is not “can we compute it” but “can we find it.” Moving indexing from the original token space to a low-dimensional latent space is the key step that replaces quadratic complexity with near-linear complexity; combined with exact back-mapped aggregation and tunable recall knobs, it can preserve accuracy while delivering order-of-magnitude speedups. For developers, the focus of implementation has shifted from model structure to three engineering capabilities: incremental maintenance of the compressed representation, offline calibration of recall, and observable governance of long-tail latency.