Sparse Attention at 10M Context: A 22x Prefill Speedup by Fixing the Index Cost

    As context windows push from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from the attention computation itself to the indexing step that decides which tokens to attend to. This article breaks down where the quadratic cost comes from, explains how latent-space sparse attention moves scoring into a low-dimensional space to achieve near-linear indexing, and reports prefill speedups of roughly 20x to 70x at the 10M-token scale while preserving accuracy. It also covers why this matters for time-to-first-token, throughput, and cost, plus four practical considerations for adoption: recall-based evaluation, adaptive k, train-inference consistency, and KV cache coordination.

    1. The First-Principles Conflict of Ultra-Long Context: Compute Got Cheaper, Selection Got Expensive

    Over the past two years, the long-context race has centered on window size. Million-token context has become a default spec for some flagship services, and hybrid attention architectures are now common in both open-source and closed models. These designs share one assumption: instead of letting every query vector scan the entire key history, each query selects only a few of the most relevant keys to participate in the computation.

    That assumption has been validated repeatedly at the million-token scale. But push context to 10 million tokens and an originally negligible overhead spins out of control. Take a single global attention layer: at 10M tokens, roughly 98% of prefill time is not spent on the attention computation itself, but on the indexing step that decides which tokens to look at.

    The nature of the problem has changed. Sparse attention has already proven that attending to a small subset is enough to preserve accuracy. What actually blocks ultra-long context is that the process of finding that subset still grows quadratically with context length.

    2. Cost Breakdown: Where the Quadratic Term Comes From

    In standard full attention, every query computes relevance against all previous keys. With context length T, prefill costs O(T²) and each decoded token costs O(T). Going from 1M to 10M tokens increases the attention computation 100x.

    Sparse attention divides the work: a lightweight indexer first picks the k most relevant keys for each query, then the real attention runs only over those k keys, reducing the attention body to O(Tk).

    This works under one condition: the model genuinely needs very few keys. The industry validated this at the 128k level — letting each query attend only to its 512 most relevant keys (about 0.8% of all keys) still retained roughly 98% of full-attention accuracy on long-context benchmarks.

    But there is a paradox here. To know which 512 keys are most relevant, the indexer must first score every key. The complexity merely moves from the “attention matrix” to the “scoring matrix”; the order of magnitude does not actually drop.

    1. # Complexity intuition for a naive indexer (illustrative)
    2. # scores: [T_q, T_k] each query scored against all keys
    3. scores = query @ key.T # O(T^2) scoring
    4. topk_idx = scores.topk(k, dim=-1).indices
    5. attn_out = sparse_attention(query, key, value, topk_idx) # O(T*k)

    Existing approaches lighten the indexer’s load by reducing precision, shrinking dimensions, or compressing multiple keys into a block. These are all constant-factor optimizations; none of them change the quadratic skeleton of the scoring matrix.

    3. Latent-Space Sparse Attention: Turning Retrieval into a Low-Dimensional Problem Too

    A recent paper on sub-quadratic attention for ultra-long context proposes a different path: rather than retrieving in the original token space, map keys and queries into a low-dimensional latent space, perform similarity scoring and top-k selection there, then use the resulting indices to run sparse attention back in the original space.

    The core insight comes down to three points:

    1. The hard part of retrieval is not slow arithmetic, but high dimensionality and a large candidate set. Moving scoring into a low-dimensional latent space substantially lowers the per-unit cost of scoring.
    2. Indexing and attention are decoupled. The indexer only ranks; it does not carry information. Information aggregation is still done by full-dimensional keys and values, so accuracy loss is confined to occasionally selecting the wrong candidates.
    3. The selection process can be amortized. During prefill, many queries share the same history, so latent representations and index structures can be reused instead of rescanning the full history for every query.

    The result: at the 10M-token scale, a single attention layer’s prefill can be sped up by tens of times (reported figures vary widely by model size, from roughly 20x to over 70x), while accuracy stays close to the original model across a range of long-context tasks.

    The complexity comparison can be sketched as:

    Stage Full Attention Block Sparse (Existing) Latent-Space Sparse
    Indexing/Scoring None O(T²/d_block) Near-linear (low-dim latent space)
    Attention Body O(T²) O(Tk) O(Tk)
    Dominant Prefill Term Compute Indexing Compute

    4. Why This Matters in Engineering

    Prefill is the main cost driver of long-context serving. When the indexing term dominates latency, any effort that only optimizes the attention kernel hits a ceiling — no matter how fast the kernel gets, it cannot recover that 98%. Reducing indexing from quadratic to near-linear is equivalent to rewriting the cost structure of long-context serving:

    • Time to first token: Prefill time drops sharply, directly improving the experience for long-document QA, repository-level code understanding, and similar scenarios.
    • Throughput and concurrency: The same compute can carry longer contexts, amplifying the gains from caching and batching strategies.
    • Controllable accuracy: Because information aggregation still happens in the original space, accuracy risk comes mainly from candidate recall and can be tuned by increasing k or improving the latent mapping.

    5. Four Key Considerations for Adoption

    1. Recall over average error: Evaluate the indexer by top-k recall, not mean similarity error. A practical gate metric is the cosine similarity between full-attention output and sparse output.
    2. Adaptive k: Sensitivity to k varies greatly across layers and attention heads, so a fixed k is often not optimal. Set k per layer, or run lightweight calibration after training.
    3. Train-inference consistency: If the indexer and latent mapping are untrained, distribution drift can cause recall to collapse. The ideal approach is to let the model get used to sparse indexing during pretraining or post-training.
    4. Coordination with caching: The latent index structure must be managed alongside the KV cache, involving paging, eviction, and reuse policies. Pair it with monitoring, alerting, and logging to observe hit rates and latency percentiles.

    6. Conclusion

    The next barrier for ultra-long context is not whether we can compute, but whether we can cheaply find the part worth computing. Once indexing becomes the dominant cost, the optimization focus inevitably shifts from kernels to representations and retrieval structures. Latent-space sparse attention offers one answer: put selection in a low-dimensional space too, and make sub-quadratic the default complexity. For teams building long-context applications, the main thread worth watching early is not any specific implementation but the indexing cost curve — it determines whether 10M-token context can move from demo to an affordable, everyday capability.