Reducing Sparse Attention Indexing Overhead by an Order of Magnitude for Ten-Million-Token Contexts

    Long-context inference is shifting from full attention to sparse attention, but at ten-million-token lengths the token selection step can consume most of the runtime. This article explains how Latent Sparse Attention (LSA) moves coarse token filtering into a low-dimensional latent space, what speedups the reported experiments show, and what engineering teams can take from this approach for large-scale long-context deployment.

    1. From Full Attention to Selective Attention

    Large language models have traditionally processed long inputs by letting each query attend to every historical token. As context grows from tens of thousands to millions of tokens, this full attention becomes too expensive in both memory and compute time. The common response is sparse attention or linear attention. Instead of scanning all historical tokens, each query attends only to a small set of the most relevant tokens.

    This approach has been repeatedly validated at the million-token scale. Accuracy loss stays under control, and compute drops substantially. However, as context lengths continue to grow toward ten million tokens, a cost that was previously minor starts to become a serious bottleneck.

    2. The Overlooked Bottleneck: Indexing

    A sparse attention pipeline has two steps:

    1. Indexing: For each query, decide which tokens to attend to.
    2. Computation: Run the actual attention calculation over the small set of selected tokens.

    Most optimization work focuses on the second step, reducing the number of tokens that take part in the actual computation. Few people ask how much the first step costs.

    A common way to build the index is to keep a low-rank statistic for each token, such as a sliding mean of its key vectors, and then query these statistics to estimate token importance quickly. The problem is that the storage and update cost of these statistics grows linearly, or even superlinearly, with context length. In a naive implementation, the number of comparisons at query time also grows with context length.

    A research team profiled a typical sparse attention layer and found a counterintuitive result. In the ten-million-token prefill setting, about 98% of the time was not spent on attention computation. It went to the indexer that decides which tokens to attend to. In other words, attending to a small subset of tokens has already been shown to preserve accuracy. What actually blocks very long contexts is that the process of finding that small subset still grows quadratically with context length.

    3. The Core Idea: Moving Coarse Filtering into Latent Space

    To address this bottleneck, a joint research effort proposed Latent Sparse Attention (LSA). Its core insight is that token selection does not need to happen in the original high-dimensional space.

    Specifically, the method introduces a low-dimensional latent space and projects the key of every token into it. Because the dimension is greatly reduced, coarse filtering in the latent space, such as computing approximate similarity between a query’s latent vector and each token’s latent vector, costs far less than the same operation in the original space. After coarse filtering, only the small set of selected tokens returns to the original space for exact attention computation.

    This design provides three benefits:

    • Lower indexing compute: The latent dimension is typically a fraction of the original key dimension or even smaller, so the cost of inner products and sorting drops significantly.
    • More memory-friendly access: Low-dimensional vectors use contiguous, compact storage, which improves cache hit rates and reduces the memory bandwidth bottleneck common in large-scale indexing.
    • Manageable summary maintenance: Token summaries in the latent space are small, so the cost of updating and traversing them no longer becomes a burden.

    Algorithmically, this is a two-stage filtering structure. The latent space performs cheap coarse recall, and the original space performs exact top-k confirmation and computation.

    The following pseudocode illustrates the computation flow:

    1. # Pseudocode: two-stage filtering sparse attention
    2. q_latent = project_q(q) # Project the query into the latent space
    3. k_latent = latent_cache[kv] # Latent summaries of historical tokens
    4. candidates = topk(q_latent @ k_latent.T, k1) # Stage 1: coarse filtering in latent space (cheap)
    5. scores = q @ keys[candidates].T # Stage 2: exact scoring in the original space
    6. top = topk(scores, k2) # Second-stage top-k selection
    7. out = softmax(top) @ values[top[best]]

    4. Results: Large Speedups with Accuracy Largely Maintained

    The paper reports experimental results that support this approach:

    • In the ten-million-token prefill setting, a single global attention layer in a model with a sparse attention architecture achieves a speedup of up to roughly 22x.
    • For a mid-sized model from another mainstream open-source architecture, the same layer achieves a speedup of more than roughly 70x.
    • On a range of long-text understanding and retrieval tasks, accuracy remains largely on par with the original model.

    The most important finding is the profiling result after optimization. The indexing stage dropped from 98% of total runtime to a share comparable to the computation stage, which means the bottleneck was effectively removed. This suggests a systematic solution to the “last mile” problem of sparse attention at ten-million-token contexts and beyond.

    5. Implications for Engineering Practice

    For teams deploying long-context services at scale in the cloud, whether through cloud providers’ inference services or self-hosted inference clusters, this work offers several transferable lessons:

    1. Profile first, then optimize: In sparse attention workloads, the real hotspots are often not in the attention kernel but in indexing and data movement. Without detailed performance profiling, optimization efforts can easily go in the wrong direction.
    2. Dimensionality reduction is a general technique: Running expensive computation in a low-dimensional space for coarse filtering is a common pattern across retrieval, recall, attention, and other domains. It is worth reusing in custom inference optimization.
    3. Pay attention to memory access patterns: At the ten-million-token scale, many cases of “slow algorithms” are really “slow memory.” Compact, contiguous cache design may yield more benefit than fine-tuning the algorithm.
    4. Two-stage structures fit tiered storage: Latent summaries are small enough to stay resident in fast GPU memory, while the selected original KV entries are loaded on demand. This pairs well with the tiered KV cache strategies common in cloud inference platforms.

    6. Conclusion

    Competition in long-context processing is shifting from “how many tokens can be fit” to “how quickly that many tokens can be processed.” Latent Sparse Attention shows that the accuracy assumption behind sparse attention holds, and that the real bottleneck was the search process itself. With coarse filtering in a low-dimensional latent space, the search cost can be brought back in line with the actual computation cost. As techniques like this move into mainstream inference frameworks and cloud inference services, ten-million-token context processing could move from a research capability to a standard engineering practice.