Latent Sparse Attention Speeds Up 10-Million Token Contexts by 22x

    As long-context models scale to 10 million tokens, the indexer used in sparse attention becomes a critical bottleneck. Latent sparse attention addresses this by compressing the token selection process into a low-dimensional latent space, reducing indexing overhead and accelerating the prefill phase by over 22 times. This article explores the limitations of full and standard sparse attention, the mechanics of latent sparse attention, accuracy preservation, and key engineering trade-offs for production deployment.

    In the evolution of long-context models, sparse attention has become the default choice. However, as context length scales from millions to tens of millions of tokens, a “supporting role”—the indexer—becomes the real bottleneck. A recently proposed latent sparse attention scheme offers a new solution: compressing the process of “deciding which tokens to look at” into a low-dimensional space, thereby accelerating the core layers of the prefill phase by more than 22 times.

    1. The Cost Curve of Full Attention

    In standard full attention, each query computes relevance with all previous keys. When the context length is T, the prefill phase requires O(T²) computation, and generating each token requires O(T). As the context grows from 1 million to 10 million tokens, prefill attention computation increases by 100 times. If we factor in the hidden dimension d, this quadratic growth becomes extremely severe.

    For example, when T=10 million and d=4096, computing the dot product of all queries with all keys requires about 4×10^17 multiply-accumulate operations. Regardless of how much compute is stacked, this scale is difficult to handle directly. Furthermore, in ultra-long context scenarios, users often demand low first-token latency, yet the prefill phase is where compute is most concentrated.

    Therefore, the industry naturally shifted toward sparse attention: instead of having each query look at all historical tokens, only a few most relevant keys are selected for computation. This reduces the attention compute from O(T²) to O(Tk), where k is the number of keys selected per query.

    The logic is clear, and the cost seems minimal. But the real problem is often hidden in the step of “selecting the few most relevant keys.”

    2. The Overlooked Cost of the Indexer

    A typical sparse attention structure works as follows:

    1. A lightweight indexer selects Top-k keys from all keys for each query.
    2. The actual attention is computed only on these k keys.

    Step 2 is indeed significantly compressed, but Step 1 does not disappear. To provide a reliable Top-k, the indexer must score the candidate keys to some extent. If the indexer needs to scan all keys and assign a score to each, its complexity remains at the O(T²) level, just with a smaller constant than full attention.

    At the 10M token level, this “small constant” spirals out of control. Performance profiling shows that in some global attention layers during long-context prefill, over 98% of the time is spent not on actual attention computation, but on the indexer “deciding which tokens to look at.” This creates a paradox:

    We already know that each query needs only a few keys to maintain accuracy, but finding these keys still costs nearly as much as a full scan.

    This contradiction is not theoretical. In 128K token long-context evaluations, if each query only reads 512 keys (about 0.8% of all keys) ranked by true relevance, the model retains about 98% of full attention accuracy. This shows that ultra-long contexts contain massive redundancy, and that the “selection” process itself is the cost bottleneck.

    3. Latent Sparse Attention: Making the Indexer “Lightweight”

    The core idea of latent sparse attention is not limited to optimizing attention computation; it moves the optimization target forward to the indexer itself.

    It does something straightforward: projects the query and key from the original high-dimensional space into a lower-dimensional latent space, where relevance scoring and Top-k selection occur. Because the latent dimension is much smaller than the original, the computation required by the indexer decreases proportionally. The pseudo-code roughly looks like this:

    1. def latent_sparse_attention(query, key, value, top_k):
    2. # 1. Project query/key to a low-dimensional latent space
    3. q_latent = latent_project(query) # (T, d_latent)
    4. k_latent = latent_project(key) # (T, d_latent)
    5. # 2. Compute relevance in the latent space and select Top-k
    6. scores = q_latent @ k_latent.T / sqrt(d_latent)
    7. top_indices = scores.topk(top_k, dim=-1).indices
    8. # 3. Compute actual attention using the original high-dimensional K/V
    9. k_selected = key.gather(top_indices) # (T, top_k, d)
    10. v_selected = value.gather(top_indices) # (T, top_k, d)
    11. attn = softmax(query @ k_selected.T / sqrt(d)) @ v_selected
    12. return attn

    In actual engineering, the original K/V are not fully expanded before gathering. Instead, optimizations like chunking, fused operators, and custom kernels are used to optimize memory access. But this simple flow clarifies the key changes:

    • The scoring dimension of the indexer drops from d to d_latent.
    • True high-dimensional attention only occurs on the selected Top-k keys.
    • The indexer and attention use the same latent representation, so relevance judgment does not completely detach from model semantics.

    If d_latent is 128 and the original dimension d is 4096, the theoretical computation of the indexer scoring phase drops by about 32 times. In experiments under the 10M token scenario, prefill speedups ranging from over 20x to over 70x were observed across different models, aligning with this ratio.

    More importantly, this approach does not simply reduce dimensions; it decouples the “selection criteria” from the “content evidence.” The latent space determines “which keys might be relevant,” while the original space provides “the precise content of the selected keys.” As long as Top-k recall is sufficiently accurate, accuracy loss can be kept minimal.

    4. Why is Accuracy Maintained?

    Many worry: will dimensionality reduction projection lose information, causing the indexer to select the wrong keys?

    The answer lies in the characteristics of attention distribution. In ultra-long contexts, the keys each query truly depends on are often highly sparse. In document Q&A, a query only relates to a few paragraphs; in code understanding, a symbol only relates to a few definitions and call sites. The relevance distribution naturally features “a few critical items determining most of the output.”

    Latent projection is not arbitrary compression; it is learned through training. It focuses on “what is most important for judging relevance,” rather than “how to completely reconstruct key content.” Therefore, the latent dimension can be very small while still preserving the relative relationship between query and key. Meanwhile, the final attention still uses the original high-dimensional vectors for computation without latent reconstruction, preventing low-dimensional projection errors from propagating to the output.

    Additionally, such methods can typically be combined with existing block-sparse mechanisms. For example, consecutive keys are divided into fixed-size blocks, and a coarse screening is done at the block level; for the few selected blocks, fine selection is done at the token level. This two-tier structure of “coarse screening + fine selection” reduces indexer computation while maintaining high recall, which is key to engineering viability in ultra-long context scenarios.

    5. Key Trade-offs in Engineering Implementation

    Turning latent sparse attention from a paper into production-ready capability requires solving several practical issues.

    1. Non-Differentiable Top-K

    The Top-K operation itself is non-differentiable, causing gradient breakage during direct end-to-end training. Common practices include:

    • Using differentiable sparse gating or relaxed selection during training.
    • Using full attention output as a distillation target to teach the model how to select.
    • Switching to hard Top-K during inference.

    This ensures the model learns not just “how to compute attention,” but also “how to select keys.”

    2. Block Sparsity and Memory Access Efficiency

    Token-level Top-K leads to non-contiguous memory access, which is unfriendly to GPU kernels. In practice, keys are usually chunked into blocks of 64 or 128 tokens: select blocks first, then select within blocks. This leverages the matrix multiplication capability of Tensor Cores and reduces indexing overhead. Block size is a critical parameter balancing accuracy and efficiency: larger blocks mean cheaper indexing but may include irrelevant tokens; smaller blocks mean more precise selection but scattered memory access.

    3. Reuse in the Decoding Phase

    The prefill phase can select Top-K for all queries at once, but during decoding, each newly generated token must re-decide which historical KVs to look at. Latent sparse attention reduces scoring dimensions, lowering indexing overhead during decoding; however, if the context reaches tens of millions of tokens, linearly scanning all keys at each step is still not cheap.

    Therefore, production systems often maintain hierarchical or incremental indexes: clustering the latent representations of historical keys into summaries in advance. A new query first matches against summaries, then drills down to specific tokens. This reduces the selection cost during decoding from O(T) to near O(log T) or O(√T) levels.

    4. Integration with Position Encoding

    Sparse selection only determines “which tokens to look at,” but positional information remains crucial. Latent projection should ideally explicitly incorporate relative position encoding, or the final attention computation should preserve complete position encoding. Otherwise, the model might select content-relevant tokens but fail to correctly organize the context due to lost positional relationships.

    6. When is it Appropriate to Use?

    Latent sparse attention is not a silver bullet for all scenarios. It excels in tasks with extremely long contexts and naturally sparse relevance distributions, such as:

    • Multi-document Q&A and long document summarization.
    • Large-scale codebase understanding.
    • Conversation history and long-term memory retrieval.
    • Enterprise knowledge base Q&A.

    If the context is short (e.g., only a few thousand tokens), the overhead of full attention is minimal, and introducing an indexer adds unnecessary latency. If a task requires every query to deeply interact with all keys (e.g., specific reasoning tasks requiring global aggregation), overly aggressive sparse selection may hurt accuracy. Therefore, a more practical approach is a hybrid architecture: local layers continue using sliding window or full attention, while global layers use latent sparse attention.

    7. Conclusion

    Ten-million token contexts have shifted the focus of attention optimization from “computing attention” to “computing selection.” The reason sparse attention falls a bit short is not because “looking at only a few tokens” isn’t good enough, but because “finding those tokens” is still too expensive.

    Latent sparse attention provides a clear path: let the indexer run in a low-dimensional latent space, and let attention execute in the high-dimensional original space. Use low dimensions to judge “who to look at,” and use high dimensions to compute “what is seen.” This way, the indexer is no longer an invisible burden in long contexts, but an engineering-friendly, parallelizable preprocessing step.

    For developers, the focus should not just be on the computation of an attention layer, but on the cost structure of the complete selection-computation pipeline. Whoever can make the “finding keys” step lightweight enough will truly turn 10-million token contexts into a usable service capability.