Sparse Attention at 10M Token Context: The Critical Path to 22x Faster Prefill

    This article examines why sparse attention slows down at extreme context lengths and how Latent Sparse Attention addresses the bottleneck. It explains why indexing costs grow quadratically, how moving selection into a latent space reduces complexity to near-linear, and the engineering practices for caching, chunked retrieval, memory optimization, and recall monitoring in long-context inference. Readers will find the root-cause breakdown, illustrative pseudocode, and deployment considerations for cloud environments.

    1. The Problem: A Structural Bottleneck as Context Scales

    Over the past two years, long-context capability has expanded rapidly from 128K to 1M tokens, and some cloud providers now offer 1M as a default configuration. In parallel, the industry has increasingly adopted hybrid architectures that combine sparse and linear attention: instead of scanning every historical token, each query selects only a small set of the most relevant tokens for computation. This approach has been repeatedly validated at the 1M scale, with manageable accuracy loss.

    However, once the context grows to 10 million tokens, an initially negligible overhead starts to spiral out of control. In a representative global attention layer performing prefill at 10M tokens, roughly 98% of the time is spent not on the attention computation itself, but on the indexer that decides which tokens to attend to.

    The nature of the problem has therefore shifted. Sparse attention has already proven that attending to a small subset is sufficient to preserve accuracy. What truly blocks ultra-long context is that the cost of finding that subset still grows quadratically with context length.

    2. Root Cause: Why Indexing Costs Grow Quadratically

    Let the context length be N, and let the sparse budget retained per query be k (where k is much smaller than N). Ideally, the attention computation should have a complexity of roughly O(N·k), growing linearly with N. In practice, however, the indexing stage typically scores every query against all N candidates and performs Top-k selection, resulting in O(N²) complexity. When N increases from 1M to 10M, indexing costs grow by roughly 100x, while computation costs grow by only about 10x, throwing the ratio completely off balance.

    There are also three amplifying effects in engineering practice:

    • Memory-access amplification: scoring requires reading the representations of all keys, making bandwidth a hard constraint.
    • Insufficient parallelism: Top-k selection, sorting, and reduction involve serial dependencies that make it difficult to fully utilize compute.
    • Memory pressure: if the intermediate scoring tensors are explicitly materialized, their size reaches the N×N scale, quickly triggering overflow and recomputation.

    3. The Core Idea: Moving Selection into the Latent Space

    A class of methods known as Latent Sparse Attention offers a solution. Rather than performing pairwise scoring in the original token space, these methods first compress the long sequence into a low-dimensional latent representation, then perform similarity retrieval and Top-k selection in the latent space, and finally map back to the original tokens for attention computation.

    The key benefit is reducing indexing complexity from O(N²) to approximately O(N·d), where d is the latent dimension or the number of clusters/blocks. This makes indexing costs grow linearly with context length. Published papers report the following results: at 10M tokens, prefill for a single attention layer is sped up by approximately 22.7x (for one open-weight model) and approximately 72.4x (for another model with a larger parameter count), while maintaining accuracy close to the original model across multiple tasks.

    Illustrative pseudocode (expressing the flow only, not a specific interface):

    1. # 1) Precompute: compress the key sequence into latent bucket representations (cacheable for reuse)
    2. latent = compress_to_latent(keys, dim=d) # complexity O(N·d)
    3. # 2) Each query retrieves candidate buckets in the latent space instead of scanning all tokens
    4. scores = query @ latent.T # O(N·d), d << N
    5. buckets = topk(scores, k=k) # number of candidates relates to k
    6. # 3) Perform exact attention only on the candidate tokens
    7. idx = expand_buckets_to_token_ids(buckets)
    8. out = sparse_attention(query, keys[idx], values[idx])

    4. Engineering Implementation

    1. Cacheable latent representations: the compression result depends only on the key sequence and can be reused during incremental decoding, amortizing the precomputation cost.
    2. Chunking and two-level retrieval: first select blocks at a coarse granularity, then rank precisely within blocks, balancing recall and latency.
    3. Collaboration with linear attention: local windows use linear/gated mechanisms to cover nearby dependencies, while the global branch uses latent-space sparsity to cover long-range dependencies.
    4. Memory and bandwidth optimization: avoid materializing the N×N scoring matrix; use chunked reduction and online Top-k.
    5. Accuracy safeguards: set monitoring metrics for recall and candidate coverage to prevent retrieval misses on long-tail tasks.

    5. Practical Recommendations and Cloud Deployment

    When implementing this capability in the cloud, you can rely on general-purpose capabilities: use a container platform to host inference services and enable elastic scaling, use object storage to store latent representation caches and checkpoints, and use logging and monitoring services to track prefill latency, recall, and memory usage. Baidu AI Cloud can also complete deployment and tuning for such long-context inference scenarios based on a combination of the above general capabilities.

    Conclusion: In the era of 10M-token contexts, the competitive focus has shifted from “computing fast” to “selecting accurately and efficiently.” Moving the selection process into the latent space is the current critical path for bringing indexing costs back from quadratic to linear, and it is the core lever for reducing costs and improving efficiency in long-context inference services.