Sparse Attention for Million-Token Contexts: How Latent Space Speeds Up Selection

    As context windows grow from millions to tens of millions of tokens, the bottleneck in long-context inference shifts from compute to selection. This article explains why standard sparse attention still scales quadratically due to the indexer, and how Latent Sparse Attention moves candidate scoring into a learned low-dimensional space to achieve near-linear attention. It covers the two-stage retrieval pseudocode, why recall in latent space preserves accuracy, and practical guidance on evaluation across long-context benchmarks, context tiers, prefill versus decode profiling, and memory bandwidth trade-offs.

    1. Longer Contexts Change the Cost Structure First

    Over the past year, million-token context has moved from a differentiating feature to a baseline specification. As long-context capability becomes standard, the main question is no longer whether a model can fit the context, but how much it costs to do so.

    In standard full attention, every query computes relevance against all keys in the history. With a context length of T, the prefill stage costs roughly O(T²), and decoding costs about O(T) per generated token. Scaling the context from millions to tens of millions of tokens increases the attention portion alone by roughly two orders of magnitude.

    The basic division of labor in sparse attention is this: a lightweight indexer first selects the k most relevant keys for each query, and the actual attention is computed only over those k keys, reducing the main attention cost to O(Tk). This approach has been validated repeatedly: in long-context benchmarks, letting each query attend to about 0.8% of the keys still preserves roughly 98% of full-attention quality.

    The problem is that the indexer must score every key to decide which k are most relevant. This creates a paradox: the main attention becomes cheaper, but the process of choosing what to attend to still grows quadratically with context length. In measured tens-of-millions-token prefill runs on a certain class of global attention layers, about 98% of the time was spent in the indexer rather than in the attention computation itself. What truly blocks ultra-long context is not “attending to only a small part,” but “finding that small part.”

    2. Latent Sparse Attention: Moving the Search into a Low-Dimensional Space

    The core idea of Latent Sparse Attention is to avoid exhaustive scoring in the original high-dimensional key space. Instead, queries and keys are first compressed into a low-dimensional latent space, candidate selection happens there, and precise attention is then performed back in the original space.

    It can be understood as two-stage retrieval:

    1. # Pseudocode: two-stage sparse attention
    2. q_low = W_dq @ q # query dimensionality reduction
    3. k_low = W_dk @ k # key dimensionality reduction (cacheable)
    4. scores = q_low @ k_low.T # low-dimensional scoring, much cheaper
    5. idx = topk(scores, k) # select candidates
    6. out = softmax(q @ k[idx].T) @ v[idx] # exact computation in original space

    There are three key points:

    1. The dimensionality-reduction matrices are learned rather than random projections, so low-dimensional scores remain consistent with the original relevance;
    2. The latent representations of keys can be precomputed and cached, so decoding does not repeat the projection;
    3. Exact attention runs only on the candidate set, ensuring the output distribution is not over-approximated.

    The complexity shifts from “exhaustive high-dimensional scoring” to “low-dimensional scoring plus small-scale exact attention,” bringing the overall cost close to linear and enabling sub-quadratic attention for ultra-long contexts.

    3. Why Accuracy Holds Up

    Sparse attention relies on the premise that the model genuinely needs only a small number of keys. The latent approach adds another layer of protection: the low-dimensional space is responsible only for recall, not for final scoring. The recall stage tolerates some error, and as long as the truly relevant keys fall within the candidate set, the final output is still determined by the original representations.

    In engineering practice, three types of measures are typically used to control risk:

    • Candidate set union: merge the local window, fixed important keys, and retrieval candidates to reduce the chance of missing relevant keys;
    • Hierarchical filtering: coarse filtering followed by fine ranking, using a small number of high-precision scores to correct the ordering;
    • Training alignment: during pretraining or post-training, adapt the model to the sparse pattern to avoid distribution drift at inference time.

    4. Implementation and Evaluation

    Evaluating this kind of solution should not rely on a single speedup number. At a minimum, cover four dimensions:

    1. Long-context benchmarks: test retrieval, aggregation, and multi-hop reasoning separately, to avoid gains that hold on only one task;
    2. Different context tiers: from 128k to 1M and then 10M, observe how the speedup and accuracy curves change;
    3. Separate prefill and decode statistics: make clear whether time goes to indexing, attention, or communication and memory access;
    4. Memory and bandwidth: latent representation caching increases memory usage, which must be weighed together with batch size and concurrency.

    For implementation, enable latent filtering first in global attention layers while keeping window attention in local layers; fuse the indexer and the main attention into the same operator where possible to reduce round trips of intermediate tensors; and cache key latent representations in blocks, reusing the existing key-value cache management mechanism.

    5. Conclusion

    The bottleneck of ultra-long context has shifted from “being able to compute” to “selecting accurately and quickly.” Latent Sparse Attention offers a clear path: replace high-dimensional exhaustive scoring with low-dimensional recall, compress quadratic cost to near-linear, and constrain accuracy risk to the candidate recall stage. For systems that need prefill and long-document understanding at the tens-of-millions-token scale, this is worth adding to the technology selection and load-testing checklist.