Sparse Attention at 10M Context: A 22x Prefill Speedup with Latent Space Retrieval

    As language models push context windows from 1M to 10M tokens, sparse attention alone is no longer enough because the indexer that selects relevant tokens becomes the dominant cost. This article explains Latent Sparse Attention (LSA), which compresses keys into a low-dimensional latent space for coarse filtering before high-dimensional fine ranking, delivering a 22.7x to 72.4x per-layer prefill speedup at 10M tokens with comparable accuracy. It covers the quadratic bottleneck of conventional sparse attention, the two-stage retrieval mechanism, memory-bandwidth optimizations, and practical engineering considerations for deployment.

    Introduction: The New Bottleneck in the Ultra-Long Context Era

    Since 2025, the race to extend context windows in large language models has entered a new phase. What began with 1M tokens becoming a default specification for some cloud services has expanded into exploration at the 10M token level, continually pushing how long a sequence a model can process. Across the industry, a strong consensus has emerged: instead of having the query vector scan every historical token indiscriminately, a model should interact only with the few most relevant tokens. This is the sparse attention mechanism.

    This design performs remarkably well at the 1M context level, with minimal accuracy loss. However, as the context length scales further to the 10M level, an initially negligible overhead begins to spiral out of control. In the global attention layers of certain hybrid-architecture models, during the prefill stage at 10M tokens, the compute core spends more than 98% of its time not on the attention matrix multiplications, but on the indexer that “decides which tokens to look at.”

    The nature of the problem has changed: sparse attention proved that “looking at only a small subset” is viable, but “how to quickly find that small subset” remains constrained by a retrieval cost that grows quadratically with context length.

    To address this pain point, industry research organizations have proposed a new approach called Latent Sparse Attention (LSA). At the 10M token level, this approach speeds up the prefill of a single attention layer by 22.7x to 72.4x (depending on model size), while maintaining accuracy comparable to the original model. This article breaks down the underlying logic of this technique and explores its practical engineering path.

    1. The Quadratic Curse of Traditional Sparse Attention

    To understand the breakthrough of LSA, we must first analyze why traditional sparse attention fails at the 10M token scale.

    The computational complexity of standard full attention is $O(N^2)$, where $N$ is the sequence length. The original intent of sparse attention was to reduce this complexity to $O(N \cdot k)$, where $k$ is the number of tokens each query actually attends to ($k \ll N$).

    However, in practical implementations, to select these $k$ most relevant tokens, the system must first compute a similarity score (such as a dot product) between each query and all $N$ historical token keys, and then take the Top-K. This process itself still requires $O(N^2)$ operations.

    This creates a paradox: we choose sparsification to save compute, but the process of filtering for sparse targets cancels out most of the gains. In a 10M token scenario, the dimensions of keys and queries are typically 128 or higher. The massive volume of floating-point operations and the contention for memory bandwidth turn the indexer into an absolute performance black hole.

    2. The Core Mechanism of Latent Sparse Attention (LSA)

    The core idea of LSA is both direct and elegant: do not perform retrieval in the original high-dimensional token space. Instead, compress all tokens into a low-dimensional “latent space” for coarse filtering.

    1. Dimensionality Reduction and Compression

    LSA introduces a lightweight projection matrix that compresses a key vector of dimension $d$ into a latent vector of dimension $d’$ ($d’ \ll d$, for example $d=128, d’=16$). Because the dimensionality is greatly reduced, computing the similarity between queries and keys in the latent space reduces both computation and memory access by orders of magnitude.

    2. Two-Stage Retrieval Architecture

    LSA decouples the retrieval process into two stages:

    • Coarse-grained stage: Quickly compute similarity in the latent space, using minimal compute to lock onto a candidate set of Top-M tokens from 10M tokens (for example, $M=2048$).
    • Fine-grained stage: Perform precise attention computation using the original high-dimensional keys only within the candidate set.

    This “coarse first, fine later” strategy transforms the original $O(N^2)$ high-dimensional computation into an $O(N \cdot d’)$ low-dimensional retrieval plus an $O(M \cdot N)$ sparse computation, completely breaking the quadratic curse.

    3. Illustrative Pseudocode

    The following logic shows the workflow difference between LSA and traditional sparse attention:

    1. # Traditional approach: full high-dimensional dot product
    2. scores = torch.matmul(Q_high, K_high.transpose(-1, -2)) # The 10M x 10M disaster
    3. top_k_indices = torch.topk(scores, k=2048)
    4. # LSA approach: latent space retrieval
    5. Q_low = project_to_latent(Q_high) # Dimensionality reduction projection
    6. K_low = project_to_latent(K_high)
    7. latent_scores = torch.matmul(Q_low, K_low.transpose(-1, -2)) # Fast low-dimensional computation
    8. candidate_indices = torch.topk(latent_scores, m=4096) # Coarse filtering
    9. # Restore high-dimensional computation only on the candidate set
    10. K_candidates = gather(K_high, candidate_indices)
    11. final_scores = torch.matmul(Q_high, K_candidates.transpose(-1, -2))

    3. The Art of Balancing Performance and Accuracy

    The reason LSA can achieve tens of times speedup lies in its extreme optimization of memory bandwidth. During the prefill stage, the bottleneck is often not compute (FLOPS) but data movement. Compressing high-dimensional vectors into low dimensions reduces data reads significantly, so the GPU’s compute units are no longer “starved.”

    At the same time, to avoid information loss from dimensionality reduction, LSA typically combines the following engineering techniques:

    • Multi-subspace parallelism: Instead of compressing into a single latent space, split into multiple subspaces for parallel retrieval, then merge the candidate sets, trading recall for a small amount of extra computation.
    • Dynamic candidate set size: Reduce candidate tokens in smooth text regions and increase the candidate count in information-dense regions, ensuring long-tail information is not truncated.

    4. Engineering Implementation and Cloud Platform Practices

    For developers who want to leverage ultra-long context capabilities in their own applications, adopting LSA or a similar mechanism requires attention to the following engineering aspects:

    1. Heterogeneous compute scheduling: Latent space retrieval involves a large amount of unstructured memory access. It is recommended to use a container platform to deploy a mixed compute resource pool, separating the scheduling of low-precision retrieval tasks from high-precision computation tasks.
    2. Full-pipeline monitoring: Performance fluctuations with ultra-long contexts can be significant. Integrate logging services and monitoring/alerting systems, focusing on the proportion of indexer time during prefill. Once indexer overhead exceeds a threshold, an optimization strategy should be triggered immediately.
    3. Data preprocessing pipeline: During the data ingestion stage, you can pre-build a latent space index cache for tokens using message queues and object storage, avoiding repeated projection computation during online inference.

    Baidu AI Cloud offers full-stack capabilities in this area, from underlying compute to upper-layer data processing. Developers can use its general-purpose container platform and monitoring systems to quickly validate the real-world benefits of such cutting-edge attention optimization algorithms in long-text business scenarios such as codebase analysis and long-document question answering.

    Conclusion

    The leap from 1M to 10M tokens is not merely an expansion of parameter scale, but a paradigm shift in the underlying attention algorithm. Through the decoupled design of “low-dimensional coarse filtering + high-dimensional fine ranking,” Latent Sparse Attention frees indexer overhead from quadratic complexity. For teams pursuing extreme long-text processing efficiency, mastering and applying this class of techniques will be the key to breaking through the physical limits of context length.