Millisecond-Scale Context: The Key Technique Behind 22x Faster Sparse Attention

    As context windows scale from millions to tens of millions of tokens, the main bottleneck in sparse attention shifts from computing attention to finding which tokens to attend to. This article breaks down the cost structure of sparse attention, explains why traditional block-based indexing still scales quadratically, and introduces latent-space sparse attention, which moves retrieval into a low-dimensional latent space to achieve near-linear indexing. It covers the core design, supporting engineering patterns, accuracy considerations, and an evaluation checklist for production deployment.

    1. The Problem: When Contexts Get Longer, the Bill Moves

    Over the past two years, long context has shifted from an experimental feature to a default specification. Million-token context is becoming standard on some flagship services, and hybrid attention architectures—alternating global attention layers with linear or local layers—are widely adopted. These designs share the same assumption: each query does not need to scan the entire history, only to pick out the small, most relevant segment to “look at.”

    At the million-token scale, this assumption has been repeatedly validated. But as context pushes toward ten million tokens, an initially negligible cost starts to spiral: in a global attention layer doing prefill at ten-million-token scale, roughly 98% of the time is spent not on attention itself, but on the indexing step that decides which tokens to look at.

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

    2. Cost Breakdown: The Hidden Bill in Sparse Attention

    In standard full attention, every query computes relevance against all previous keys. With context length T, prefill compute is roughly O(T²), and decoding each token costs about O(T). Going from one million to ten million tokens increases attention compute by roughly 100x.

    Sparse attention splits the work: a lightweight indexer first selects the k most relevant keys for each query, then the actual attention computes only over those k keys, reducing the attention body to O(T·k).

    This holds only if the model truly needs very few keys. Validation in the industry at 128k length shows that if each query attends to only the 512 truly most relevant keys (about 0.8% of all keys), it still retains roughly 98% of full-attention accuracy on long-context benchmarks.

    The paradox is this: for the indexer to know which 512 keys are most relevant, it must first score every key. Existing approaches reduce the indexer’s burden by lowering scoring precision, compressing dimensions, or merging multiple keys into a block. But as long as scoring remains “query × all keys,” indexing cost stays O(T²)—only the constant gets smaller. At ten million tokens, the quadratic term dominates again.

    Illustration (pseudocode, expressing complexity relationships only):

    1. # Traditional block-sparse indexing: still T x T scoring
    2. for q_block in query_blocks: # T / Bq
    3. for k_block in key_blocks: # T / Bk
    4. score = low_dim_dot(q_block, k_block) # scoring
    5. topk = top_k(score, k) # block selection

    3. The Core Idea: Move “Who to Compute With” into Latent Space

    A class of methods called latent-space sparse attention moves retrieval from “scoring every key in the original token space” to “similarity retrieval in a low-dimensional latent space.”

    Three changes are key:

    1. Keys are compressed into low-dimensional latent representations, significantly reducing scoring dimensions while preserving discriminative power.
    2. The retrieval structure supports approximate nearest-neighbor-style access, avoiding a full scan of all keys for every query.
    3. Indexing is decoupled from attention; the index can be built offline or incrementally, and attention only consumes the candidate set.

    The result: the indexing phase shifts from quadratic growth to sub-quadratic growth, and overall prefill complexity changes from “quadratic attention + quadratic indexing” to “near-linear indexing + sparse attention.” At ten million tokens in a single attention layer prefill scenario, public results show roughly 22x to 70x speedup over existing sparse approaches (depending on model size and layer structure), with accuracy close to the original model.

    Three supporting designs are typically needed in engineering:

    • Hierarchical indexing: a coarse-grained block index first narrows the candidate range, then fine-grained ranking is done within candidates.
    • Query-side caching: the decoding phase reuses the already-built latent-space structure to avoid repeated indexing.
    • Coordination with linear/local layers: global layers handle “precise long-range recall,” while linear layers handle “cheap short-range coverage.”

    4. Why Accuracy Holds Up

    Sparse attention works because attention distributions are highly concentrated: for the vast majority of queries, attention mass concentrates on very few keys. The value of latent-space retrieval is that it approximates the “true top-k” at a lower scoring cost, rather than sacrificing recall with a coarser approximation.

    Two metrics are worth watching in practice: recall (the fraction of the true top-k covered by the candidate set) and attention mass distribution shift. If recall is too low, accuracy collapses quickly; if k is set too large to boost recall, the speedup is eaten away. Typically k in the hundreds is sufficient, jointly tuned with block size and latent dimension.

    5. Deployment and Evaluation Checklist

    • Complexity accounting: measure the time share of attention versus indexing separately, and confirm whether the bottleneck has shifted.
    • Length extrapolation: test throughput and memory across a 4k/32k/128k/1M/10M gradient.
    • Accuracy regression: validate with both a comprehensive long-context benchmark and your own business evaluation set.
    • Productionization: combine container platforms, object storage, logging services, and monitoring/alerting to establish length-based tiers and degradation policies.

    6. Conclusion

    The core tension in ten-million-token context has shifted from “can we compute it” to “can we find it fast.” Moving indexing into a low-dimensional latent space and replacing full scoring with approximate retrieval is the current key path to pushing sparse attention toward sub-quadratic behavior while preserving accuracy under ultra-long context. For platform providers, this means the unit cost of long-context services can grow roughly linearly with length, rather than being dragged down by the quadratic term.