Sparse Attention at 10 Million Token Contexts: The Key to 22x Faster Prefill

    As context windows scale from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from compute to search. This article explains why the indexing stage grows quadratically even when attention itself is sparse, and how Latent Sparse Attention addresses this by moving retrieval into a low-dimensional latent space. It covers the two-stage coarse-and-exact filtering design, its combination with linear attention, the engineering work needed to turn theoretical speedups into real throughput, and practical guidance on tuning sparsity budgets and avoiding distribution drift.

    Over the past two years, long context has moved from an experimental feature to a default specification. Million-token windows are now standard in many model services, and a wave of models has adopted hybrid architectures that combine sparse and linear attention. These designs share the same premise: a query vector does not need to scan every historical token, only a small set of the most relevant segments.

    That premise has been validated repeatedly at the million-token scale. But as contexts push toward ten million tokens, an overhead that once looked negligible starts to spiral. Take a single global attention layer: at ten million tokens, most of the prefill time is not spent on the attention matrix multiplications, but on the indexing step that decides which tokens to look at.

    The nature of the problem has changed. Sparse attention has already shown that attending to a small subset is enough to preserve accuracy. What actually blocks extreme-length contexts is that the cost of finding that subset still grows quadratically with context length. In other words, we compressed the compute but not the search.

    2. A Complexity View: Where the Overlooked Quadratic Term Lives

    Standard attention costs O(n²·d) because it computes full similarity between queries and keys. Sparse attention reduces this to O(n·k·d), where k is the number of tokens each query actually attends to, typically far smaller than n. The problem looks solved, but there is another layer in real implementations:

    • If the indexer performs a lightweight score between each query and every key, even with a heavily compressed dimension, that step is still O(n²·d’).
    • If block-based approximate retrieval is used, extra block summaries, sorting, and deduplication logic push up both constant factors and memory access counts.
    • Index results often involve irregular memory access, which keeps GPU compute utilization low and makes actual latency much higher than theoretical FLOP estimates suggest.

    This explains a counterintuitive observation: at ten million tokens, attention compute is no longer the bottleneck. The indexer is. The optimization focus has to shift from computing less to finding cheaply.

    3. The Core Idea of Latent Sparse Attention

    A recent paper from a startup research team in collaboration with universities introduces Latent Sparse Attention. Its core contribution is not a smarter retrieval algorithm, but a change in the space where retrieval happens:

    1. Compressed representation: project keys and values into a low-dimensional latent space to form compact summary vectors, making the scoring dimension d’ much smaller than the original d.
    2. Two-stage filtering: first run coarse-grained scoring in the latent space to lock in candidate blocks, then apply exact attention in the original space on a small number of candidates so accuracy does not regress.
    3. Collaboration with linear attention: local and linear channels handle dense short-range information, while the sparse channel handles long-range exact recall. They complement rather than replace each other.

    The result: at ten million tokens, a single attention layer can achieve more than 20x faster prefill, with speedups reaching several tens of times on some models, while maintaining accuracy close to the original model on long-document retrieval, multi-hop question answering, and similar tasks. The gain does not come from sacrificing quality; it comes from reducing both the constant factor and the dimension of the quadratic search.

    A schematic flow looks like this:

    1. # Pseudocode: two-stage sparse attention
    2. latent = project_to_latent(keys) # n x d', d' << d
    3. block_scores = coarse_score(q_latent, latent) # coarse filtering at much lower cost
    4. cand = topk_blocks(block_scores, budget) # keep only a few candidates
    5. out = exact_attention(q, keys[cand], values[cand]) # exact computation to preserve accuracy

    4. Engineering: Turning Theoretical Speedups into Real Throughput

    Algorithmic gains only show up in service metrics after a few engineering details are handled:

    • Blocking strategy: block size must be tuned jointly with the latent dimension. Blocks that are too large waste the exact-computation budget; blocks that are too small bring the coarse-filtering cost back up.
    • Memory access optimization: collecting candidate tokens should be made as contiguous as possible to reduce bandwidth waste from irregular reads.
    • Coordination with the KV cache: caches are enormous in long-context inference. Latent summaries can stay resident as a secondary index, while the original KV is paged in and out in tiers.
    • Batching and scheduling: candidate counts vary widely across requests, so dynamic batching is needed to keep long and short requests from slowing each other down.
    • Accuracy regression: before launch, build regression sets for long-document retrieval, code base question answering, and multi-turn dialogue, and monitor recall rather than loss alone.

    If you are building your own long-context service, a general-purpose container platform can host the inference cluster, object storage can hold the tiered KV and summary indexes, and logging plus monitoring and alerting can track prefill duration, candidate hit rate, and GPU memory levels, giving you a solid basis for capacity planning.

    5. Practical Guidance and Boundaries

    First, measure before optimizing. Use layered profiling on real workloads to confirm how much of the time the indexer takes, and avoid spending engineering budget on something that is no longer the bottleneck. Second, the sparsity budget should be configurable. Tasks differ enormously in their need for long-range dependencies: retrieval-heavy tasks can raise the candidate count, while generation-heavy tasks can tighten it. Third, watch for distribution drift. Latent summaries depend on the training distribution, so recall should be spot-checked on cross-domain corpora. Fourth, hybrid architectures are a trend, not a silver bullet. The ratio of local windows, linear channels, and sparse channels needs tuning based on model size and hardware characteristics.

    6. Conclusion

    The real barrier at ten million token contexts has shifted from being able to compute to being able to find cheaply. Moving retrieval into a low-dimensional latent space and replacing full scoring with a two-stage coarse-then-exact structure is currently one of the most cost-effective directions. For developers, the key insight is that long-context optimization is moving from operator-level acceleration toward a systematic redesign of index structures and memory access layouts. Whoever drives down the cost of finding first will turn extreme-length context from a demo feature into a scalable service capability.