Sparse Attention at 10 Million Tokens: The Key to a 22x Prefill Speedup

    As context windows scale from millions to tens of millions of tokens, the cost structure of long-context inference shifts fundamentally. This article explains Latent Sparse Attention: why the real bottleneck in ultra-long prefill is the indexing step that decides which tokens to attend to, how moving selection into a low-dimensional latent space reduces complexity from quadratic to near-linear, how it relates to linear and global attention, and what engineering teams must handle around memory, operator fusion, and accuracy regression to realize the gains in production.

    When context length moves from the million-token range to tens of millions of tokens, the cost structure of long-context inference changes fundamentally. The common assumption is that the bottleneck is the attention computation itself. In ultra-long prefill, however, most of the time is often consumed by the indexing step—the process of deciding which tokens to look at. Starting from the nature of the problem, this article walks through the design motivation, working principle, complexity changes, and relationship to linear and global attention behind Latent Sparse Attention. It also covers the memory, operator fusion, and precision-preservation issues that matter in production, giving readers a reusable framework for optimizing ultra-long context.

    1. Background: Sparsity Is Solved; Finding the Sparse Set Is Not

    Over the past two years, mainstream large model services have generally adopted million-token context as a default specification, and hybrid architectures combining sparse and linear attention are widely used. The core consensus is that iterating over every historical token for each query is wasteful. Selecting only the few most relevant tokens for computation leaves accuracy largely unaffected.

    This approach has been repeatedly validated at the million-token scale. But as context pushes toward tens of millions of tokens, an overhead that once seemed negligible starts to spiral. Take a global attention layer as an example: when running prefill at ten million tokens, most of the time is not spent on the attention matrix multiplications but on the “indexer”—the process of scoring, sorting, and selecting the top-k candidates for each query.

    The nature of the problem has therefore changed:

    • Sparse attention addresses “how much to look at,” and it has been proven that attending to a small subset is sufficient.
    • What actually blocks ultra-long context is “how to find it.” The naive approach still scores every historical token, and the cost grows quadratically with context length.

    In other words, sparsification lowered the computation cost but pushed up the selection cost. Once selection becomes the dominant term, increasing k or adding more layers only makes things worse.

    2. Core Idea: Move Selection into Latent Space

    The key insight behind Latent Sparse Attention is that selecting candidate tokens does not need to happen in the original high-resolution key space.

    A traditional indexer computes similarity between the query and every historical key directly. With high dimensionality and a large number of entries, the cost is naturally high. The latent-space approach can be summarized in three steps:

    1. Dimensionality reduction: project historical keys into a low-dimensional latent space to form a compact “index representation.”
    2. Latent-space retrieval: project the query into the same latent space and perform similarity scoring and top-k selection in that low-dimensional space.
    3. Compute in the original space: run the actual attention computation in the original dimensions for only the small set of selected tokens.

    Because scoring and sorting happen in the low-dimensional space, the per-step selection cost drops substantially. Since the real attention computation applies to only k candidates, overall complexity shifts from “growing quadratically with length” to “growing approximately linearly with length, plus a term proportional to k.”

    The structure can be expressed with illustrative pseudocode:

    1. # Conceptual illustration, not a specific API
    2. latent_keys = project_to_latent(keys) # Low-dimensional index representation, cacheable
    3. scores = query_latent @ latent_keys.T # Low-dimensional scoring, much lower cost
    4. topk_idx = top_k(scores, k) # Select a small set of candidates
    5. out = attention(query, keys[topk_idx], values[topk_idx])

    Two engineering points here are easy to overlook. First, the latent-space projection is low-rank, which means the index representation can stay resident in memory or even be offloaded to cheaper storage tiers. Second, top-k selection is a discrete operation and is naturally suited to fusion with surrounding operators, reducing reads and writes of intermediate tensors.

    3. Why It Preserves Both Speed and Accuracy

    Latent-space retrieval does not significantly degrade accuracy because it is essentially a two-stage “coarse filter plus fine computation” structure:

    • The coarse-filtering stage is responsible only for recall, not precise ranking. The low-dimensional space is sufficient to keep the vast majority of truly relevant tokens in the candidate set.
    • The fine-computation stage runs full attention over the candidates in the original space, preserving numerical precision and representational capacity identical to the original model.

    This is highly analogous to the retrieve-then-rerank paradigm familiar in engineering: use a cheap model or representation to achieve high recall first, then use expensive but precise computation for the final decision. As long as the recall of the coarse filter is high enough, the final output closely matches full attention.

    It is worth emphasizing that accuracy hinges on recall rather than ranking precision. In practice, recall stability can be improved by moderately increasing k, normalizing the latent space, and jointly optimizing the indexer with the backbone during training or fine-tuning.

    4. Complexity: From Quadratic to Near-Linear

    Let the context length be n, the number of candidates be k, and the latent dimension be d (with d much smaller than the original head dimension). A rough comparison:

    • Full attention: both computation and selection are on the order of O(n²).
    • Naive sparse: computation drops to O(n·k), but selection remains O(n²), and at ten million tokens selection becomes the dominant term.
    • Latent sparse: selection drops to O(n·d) and computation is O(n·k), giving an overall complexity of approximately O(n·(d+k)), i.e., linear growth with n.

    This explains the source of order-of-magnitude differences such as the “22x faster” figure in the original text: when n reaches the tens of millions, the quadratic term is replaced by a linear term, so the gain is not a constant-factor optimization but a leap in order of magnitude. The benefit varies widely across models and layer configurations, and is more pronounced for models with higher sparsity and more global layers.

    5. Relationship to Linear Attention and Hybrid Architectures

    Linear attention modifies the kernel function to achieve linear complexity, but it applies an equivalent compression to the entire history, limiting long-range precise retrieval. Global attention offers good accuracy but the highest cost. Sparse attention strikes a balance between the two but introduces selection cost.

    Latent Sparse Attention can be seen as applying the same linearization to the “selection” component of the sparse route, so it is naturally compatible with hybrid architectures:

    • Local window layers handle fine-grained modeling over short distances.
    • Global layers use latent sparse attention for long-range retrieval.
    • Linear layers handle cheap global information aggregation.

    Combined, these three can push prefill cost into a usable range at ten-million-token context.

    6. Production Implementation Notes

    1. Index caching and tiered storage: the latent index representation is small and suitable for staying resident in memory, while cold data can be offloaded to object storage or a caching service, with monitoring and alerts to track hit rates.
    2. Operator fusion: fuse projection, scoring, top-k, and gather into a few kernels to reduce memory-bandwidth pressure. This is often the dividing line between realizing the theoretical gains and not.
    3. Memory and batching: the prefill stage is the source of peak memory usage. It is advisable to combine chunked prefill with containerized elastic scaling, scheduling long and short tasks in separate pools.
    4. Accuracy regression: before launch, run recall and end-to-end metric regression on tasks such as long-document question answering, cross-section retrieval, and code-base understanding to avoid “speed target met, accuracy regressed.”
    5. Observability: build logs and distributed tracing for candidate hit rate, average candidate length, and per-layer time breakdown to identify whether the indexer has become the bottleneck again.

    7. Conclusion

    The competitive focus of ultra-long context has shifted from “can it be computed” to “can the right part to compute be found cheaply.” The value of Latent Sparse Attention lies in accurately identifying this change in cost structure and reducing the selection step from quadratic to near-linear, delivering an order-of-magnitude prefill speedup at ten million tokens while maintaining accuracy close to the original model. For platform and engineering teams, the real implementation challenge lies not in the algorithm itself but in the supporting engineering capabilities: operator fusion, index caching, and accuracy regression.