Millisecond-Scale Context: Solving the Quadratic Bottleneck in Sparse Attention

    Long context has shifted from a premium feature to a baseline expectation, but pushing it to 10 million tokens exposes a hidden cost: the indexer, not the attention itself. This article breaks down why sparse attention still scales quadratically, how moving relevance retrieval into a low-dimensional latent space enables sub-quadratic search, and what engineering teams should consider when adopting it, including layered caching, hybrid linear attention, staged validation, and recall monitoring.

    1. Ultra-Long Context Is Now a Default Spec, Not a Selling Point

    Over the past year, long context has quickly moved from a differentiator to a baseline capability. Several model services now list million-token context as their official default, and mainstream open-source model families have recently adopted hybrid architectures that combine sparse attention with linear attention. The industry consensus is straightforward: attention no longer needs every query to scan the entire token history. It only needs to select the few most relevant tokens for computation.

    This approach has been validated repeatedly at the million-token scale. But as context extends to 10 million tokens, an initially negligible cost starts to spiral.

    2. The Hidden Bill: The Indexer Consumes 98% of the Time

    Start with the cost structure of attention.

    In standard full attention, every query computes relevance against all previous keys. With a context length of T, the prefill stage costs roughly O(T²), and decoding costs about O(T) per generated token. Scaling context from 1M to 10M increases prefill attention computation by roughly 100x in theory.

    Sparse attention divides the work: a lightweight indexer first selects the k most relevant keys for each query, and the attention itself runs only over those k keys, reducing the core attention cost to O(Tk).

    This only works if the model genuinely needs very few keys. Validation at 128k tokens shows that letting each query attend to only its 512 most relevant keys (about 0.8% of all keys) still preserves roughly 98% of full-attention accuracy on long-context benchmarks.

    Here is the paradox: the indexer must score every key before it can identify the top 512. Mainstream sparse approaches, whether two-stage indexing with coarse filtering followed by fine ranking or shared low-rank projection scoring, cannot avoid this step. Scoring traverses the entire key history and remains O(T²), only with a smaller constant.

    So the nature of the problem changes. Sparse attention has already proven that attending to a small subset is enough to preserve accuracy. What blocks ultra-long context is that finding that subset still grows quadratically with length. For a single global attention layer at 10M tokens, roughly 98% of prefill time is spent on indexing rather than attention computation.

    3. The Fix: Move Retrieval into Latent Space

    A recent study on latent-space sparse attention points to a direction: instead of scoring all keys in the original token space, compress keys into a low-dimensional latent space, complete candidate selection there, and then return to the original space for exact attention. The core idea is sub-quadratic retrieval complexity.

    Think of it as a three-stage funnel:

    1. Coarse filtering: a shared low-rank projection compresses all keys into short vectors, and the query performs approximate nearest-neighbor search in latent space at a cost close to O(T·d), where d is the latent dimension and far smaller than the head dimension;
    2. Fine ranking: only the small set of candidates from coarse filtering (for example, on the order of O(√T)) are rescored in the original space;
    3. Exact attention: computed only over the final k keys.

    Illustrative pseudocode (structure only, not a specific implementation):

    1. # z: latent-space key cache [T, d], d << head_dim
    2. # q: current query [head_dim]
    3. cand = ann_search(z, project(q), topk=approx_k) # sub-quadratic coarse filtering
    4. scores = [dot(q, K[i]) for i in cand] # limited fine ranking
    5. topk = argsort(scores)[-k:]
    6. out = softmax_attention(q, K[topk], V[topk]) # O(Tk)

    Public results show that at 10M tokens, this approach delivers tens of times speedup for prefill on a single attention layer (speedup varies significantly by model scale, ranging from roughly 20x to over 70x), while maintaining accuracy close to the original model on long-context benchmarks.

    4. Engineering Recommendations

    • Layered caching: keep latent-space vectors resident in GPU memory or host memory, with paged management to reduce fragmentation;
    • Hybrid with linear attention: use linear attention in local layers to retain recent information, and latent-space sparse retrieval in global layers to retain distant information;
    • Staged validation: first verify accuracy regression at 128k, then progressively scale to 1M and 10M to stress-test throughput;
    • Monitor index hit rate: treat coarse-filter recall as a core metric and automatically increase the candidate count when it falls below threshold.

    5. Conclusion

    The key tension in 10M-token context has shifted from “can we compute it” to “can we find it.” Moving relevance retrieval from the original space to a low-dimensional latent space, trading approximate search for sub-quadratic complexity, is currently one of the most engineering-feasible paths. For teams building ultra-long context capabilities, the recommendation is to introduce a pluggable index-layer abstraction into the inference pipeline early, leaving room to swap retrieval strategies later.