The Era of 10 Million Tokens: Breaking the Quadratic Bottleneck in Sparse Attention

    As context windows scale from millions to tens of millions of tokens, sparse attention still carries a hidden quadratic cost in the indexing step that decides which tokens to attend to. This article breaks down that cost structure and explains latent-space sparse attention, which moves relevance scoring into a compressed low-dimensional space and uses two-stage retrieval to push overall complexity toward sub-quadratic. It covers the pseudocode, why accuracy is preserved, engineering considerations, and how to evaluate both accuracy and performance.

    1. The First-Principles Conflict of Ultra-Long Context: From “Too Much to Compute” to “Too Hard to Find”

    Over the past two years, mainstream large models have gradually made million-token context a default capability, and hybrid architectures combining sparse attention with linear attention have been widely adopted. The shared logic is simple: instead of having every query vector scan the entire token history, each query selects only a small set of the most relevant keys to participate in computation.

    This approach has been validated repeatedly at the million-token scale. But as context keeps growing toward 10 million tokens, an overhead that once seemed negligible starts to spiral out of control. Take a global attention layer as an example: at the 10-million-token scale, during the prefill stage, roughly 98% of the 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 therefore changed. Sparse attention has already proven that “looking at 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 context length.

    2. Cost Structure Review: Why Sparse Attention Still Has a Hidden Bill

    In standard full attention, every query computes relevance against all preceding keys. With a context length of T, prefill computation is O(T²), and decoding each token costs O(T). Going from a million to 10 million tokens increases the attention computation by roughly 100x.

    Sparse attention divides the work as follows: 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 cost to O(Tk).

    This idea holds under one premise: the model really does need only a few keys. Existing validation shows that at a scale of a few hundred thousand tokens, letting each query attend to only the 512 truly most relevant keys (about 0.8% of all keys) still preserves roughly 98% of full-attention accuracy on long-context benchmarks.

    But here is the paradox: for the indexer to know which 512 keys are most relevant, it must first score every key. Common industry approaches include lowering scoring precision, compressing dimensions, and aggregating multiple keys into a single block, but these techniques only shrink the constant factor. They cannot change the structural cost that “the number of scoring operations grows with T, and every query must scan the entire history.” As a result, the indexer itself becomes the new quadratic bottleneck.

    3. Latent-Space Sparse Attention: Moving Retrieval into a Low-Dimensional Space

    A research team proposed latent-space sparse attention, whose core idea is to move the “relevance judgment” from the original key space into a compressed latent space. The abstract approach can be summarized in three steps:

    1. Build a low-dimensional latent representation: map historical keys into a latent space with significantly lower dimensionality, preserving the information relevant to relevance judgment and discarding redundant detail.
    2. Do coarse filtering in the latent space: the query only needs to match against historical latent representations in the latent space, reducing the candidate pool from the full history to a scale that is only weakly correlated with context length.
    3. Return to the original space for fine computation: run the actual attention in the original dimensions on only the small set of candidates selected by coarse filtering.

    Illustrative pseudocode:

    1. # z: latent-space history representation, shape [T, d_latent], d_latent << d_head
    2. # q_lat: query projected into the latent space
    3. # step1 coarse filtering: complexity ~ O(T * d_latent), can combine with chunking/quantization
    4. scores = einsum('td, d -> t', z, q_lat)
    5. topk_idx = top_k(scores, k=k) # k is constant-scale
    6. # step2 fine computation: full-dimension attention only on candidates
    7. k_sel = keys[topk_idx] # [k, d_head]
    8. v_sel = values[topk_idx]
    9. out = softmax(q @ k_sel.T) @ v_sel

    The key gain is that the per-step cost of the coarse filtering stage drops from a “full-dimension dot product” to a “low-dimensional dot product,” and the candidate set size no longer expands linearly with context. This moves overall complexity from quadratic toward sub-quadratic. In a single-layer prefill test at the 10-million-token scale, this method achieved a speedup on the order of tens of times relative to existing sparse approaches, while maintaining accuracy close to the original model across a range of long-context tasks.

    4. Why Accuracy Is Preserved: Information Bottleneck and Two-Stage Retrieval

    The reason latent-space retrieval does not sacrifice accuracy comes down to the division of labor in two-stage retrieval:

    • The first stage pursues high recall: the low-dimensional representation only needs to ensure that “truly relevant keys are not missed,” with little demand for ranking precision.
    • The second stage pursues high precision: the candidate set is small, so an “expensive but accurate” scoring pass can be done in the original dimensions.

    This is consistent with information bottleneck theory: as long as the latent representation retains the sufficient statistics relevant to query-key relevance, compression will not cause irreversible information loss. In engineering practice, techniques such as distillation alignment, shared projection matrices, and error compensation on the latent representation can further reduce the ranking discrepancy between coarse filtering and fine computation.

    5. Engineering Implementation Notes

    1. Chunking and streaming: split history into fixed-size chunks and update latent representations incrementally per chunk to avoid repeated construction.
    2. Low-precision and quantization coordination: the coarse filtering stage can use lower precision or even binarized similarity, while the fine computation stage keeps training precision.
    3. Memory and bandwidth optimization: keep latent representations resident in GPU memory and swap original keys and values in on demand to reduce peak memory usage.
    4. Hybrid with linear attention: use linear attention for local windows to ensure smoothness, and latent-space sparse retrieval for global layers to cover long-range dependencies.
    5. Reuse during decoding: for incremental decoding, only append the latent representation of the new token and update the candidate set, avoiding full recomputation.

    6. Evaluation Recommendations

    • Accuracy side: use comprehensive long-context benchmarks covering retrieval, aggregation, and multi-hop reasoning tasks, and report the accuracy retention rate relative to full attention.
    • Performance side: measure prefill throughput, per-token decoding latency, and peak memory separately, and provide curves as context length varies to verify whether complexity is truly sub-quadratic.
    • Ablation studies: disable latent-space coarse filtering alone, vary the candidate count k, and vary the latent dimension to observe the accuracy-speed tradeoff inflection point.

    7. Conclusion

    In the second half of the 10-million-token competition, the contest is no longer about “whether you can be sparse,” but about “whether you can cheaply find the part you should look at.” Moving relevance judgment into a low-dimensional latent space and replacing single-stage full scoring with two-stage retrieval is a viable path for pushing attention toward sub-quadratic complexity today. For teams that need to balance cost, latency, and accuracy under ultra-long context, this approach is worth validating first in their own inference stack.