Sparse Attention for Million-Token Contexts: Cutting Prefill Cost with Latent-Space Retrieval

    Long-context models are hitting a new bottleneck: at ten-million-token scale, the module that decides which tokens to attend to can consume roughly 98% of prefill time, while the attention math itself is negligible. This article breaks down where the quadratic cost actually hides, explains how Latent Sparse Attention moves relevance scoring into a low-dimensional latent space to restore sub-quadratic total cost, and outlines why accuracy holds up, plus a practical path for integrating sub-quadratic attention into existing systems.

    1. The Real Cost of Ultra-Long Context: From Computing to Finding

    Over the past two years, long-context capability has shifted from a differentiating feature to a baseline specification. Million-token input windows are now a default configuration in mainstream model services, and the industry conversation has moved on to the usability and economics of ten-million-token scenarios.

    In these scenarios, one fact is easy to miss: what actually slows the system down is usually not attention itself, but the step that decides which tokens to look at.

    Take a model with global attention layers as an example. During prefill at ten-million-token scale, roughly 98% of the time is spent in the indexer, the module responsible for filtering relevant tokens, while the actual attention computation accounts for only a tiny fraction. This means that continuing down the path of “retrieve precisely first, then compute sparsely” will see its gains quickly eaten by retrieval overhead.

    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 blocks ultra-long context is that the cost of “finding that small subset” still grows quadratically with context length.

    2. Breaking Down the Cost Structure: Where the Quadratic Term Hides

    The cost structure of standard full attention is clear: with context length T, the prefill stage costs roughly O(T²) in computation, and decoding each token costs roughly O(T). Going from one million to ten million tokens inflates prefill attention computation by about a hundred times.

    Sparse attention divides the work like this: a lightweight indexer first picks the k most relevant keys for each query, and the real attention then computes over only those k keys, pushing the attention itself down to O(Tk).

    This approach rests on one premise: the model really does need only a few keys. Relevant validation shows that at 128k tokens, 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.

    The problem is that the indexer itself typically has to score or retrieve across all historical tokens, so its complexity remains on the order of O(T²). The result is an awkward situation: after attention is sparsified, the bill simply moves to a different line item, and the total does not go down.

    Schematically, the cost of a single layer can be written as:

    1. total cost ≈ index cost + attention cost
    2. ≈ c1 * T^2 + c2 * T * k

    At ten million tokens, the first term dominates completely. The focus of optimization must shift to reducing the order of magnitude of the index cost.

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

    The core claim of Latent Sparse Attention is this: do not perform exact retrieval in the original token space; instead, move the relevance decision into a low-dimensional latent space, pushing overall complexity down to sub-quadratic levels.

    The logic breaks into three steps:

    1. Compressed representation: map historical keys into low-dimensional latent representations whose dimension is far smaller than the original head dimension, so that the absolute cost of “scanning all history once” drops substantially.
    2. Scoring in latent space: the query is projected into the same latent space and interacts with the historical latent representations via dot products or lightweight operations to produce a candidate set. Because the dimension is low and memory access is contiguous, this step is friendlier to hardware.
    3. Second-stage refinement: only the small number of tokens in the candidate set go back to the original space for exact attention, ensuring that representational accuracy is not lost to the compression step.

    The key point is that although the first and second steps still involve a linear scan over T, the constant factors are significantly reduced and the memory access pattern is closer to contiguous reads; the cost of the third step is strictly bounded by k. The overall effect is that at ten million tokens, single-layer prefill shifts from “retrieval-dominated” back to “compute-dominated.”

    From an engineering perspective, the reason this class of methods achieves more than an order-of-magnitude speedup is not mysterious:

    • Low-dimensional dot products require far less compute than high-dimensional attention scoring;
    • Latent representations can be generated in one batch, avoiding repeated reads of the original keys;
    • The candidate set is small and fixed, so downstream kernels have stable shapes and are easy to optimize and parallelize.

    4. Why Accuracy Holds: Sparsity Is a Validated Prior

    The confidence behind compressing in latent space comes from a repeatedly validated conclusion about sparse attention: attention distributions are highly concentrated, with the vast majority of probability mass falling on very few keys.

    The engineering trade-off is therefore explicit: accept the risk of “possibly missing a very small number of marginally relevant tokens” in exchange for an order-of-magnitude cost reduction. As long as candidate recall is high enough, the second-stage refinement can pull accuracy back close to full attention.

    It is worth stressing that the compression dimension and the candidate count k are two key knobs: too low a dimension loses discriminative power, and too small a k lowers recall. In practice, teams usually run a grid search on a small-scale benchmark first, then stress-test at the target length.

    5. Implementation Guidance: Plugging Sub-Quadratic Attention into Existing Systems

    For teams preparing to deploy in ultra-long-context scenarios, the following path is worth considering:

    1. Do cost attribution first. Use monitoring and logging services to measure the time share of each prefill stage and confirm whether the bottleneck really is the indexing step, so you do not optimize in the wrong direction.
    2. Validate in layers. First match full-attention accuracy at the 128k level, then scale up gradually to the million- and ten-million-token levels while watching the accuracy and latency curves.
    3. Capacity and elasticity. Ultra-long contexts put extreme pressure on GPU memory and bandwidth; combining elastic scaling from a container platform with object storage for long-text corpora can lower always-on costs.
    4. Observability. Set up alerts on candidate hit rate, KL divergence before and after compression, and per-layer latency to catch distribution drift early.
    5. Gradual rollout. Run the new attention implementation in offline evaluation and shadow traffic first, then ramp up to a small percentage of live traffic.

    6. Conclusion

    In the ten-million-token era, the competitive question has moved from “can we be sparse” to “can we cheaply find the part worth looking at.” The answer Latent Sparse Attention offers is to put retrieval into a low-dimensional space so that total cost genuinely returns to the sub-quadratic range. For engineering teams, this is both an algorithmic choice and a system-level cost restructuring.