Sparse Attention in Latent Space: 22x Speedup for 10M-Token Contexts
As context windows scale from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from computing attention to finding which tokens to attend to. This article breaks down that cost structure and explains latent sparse attention, which moves key scoring into a low-dimensional latent space through a three-stage funnel of compression, coarse filtering, and exact attention. It covers why the approach preserves accuracy, practical engineering considerations, and how to evaluate it.
Sparse Attention in Latent Space: 22x Speedup for 10M-Token Contexts
1. Background: Contexts Are Moving from Millions to Tens of Millions, and the Cost Structure Has Shifted
Over the past year, long-context capability has quickly become a default configuration for large model serving. Million-token context has gone from a selling point to a baseline, and the research frontier has already started pushing toward tens of millions of tokens. The technical routes supporting this trend are highly consistent: instead of having every query traverse all historical tokens, each query selects only a few of the most relevant keys to participate in the computation.
This route has been repeatedly validated at the million-token scale. But when the context grows another tenfold, an overhead that was once unremarkable starts to spiral out of control. Take a model with global attention layers as an example: when doing prefill at the ten-million-token scale, the majority 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 long proven that “looking at only a small portion” is enough to preserve accuracy; what truly blocks ultra-long context is that the cost of “finding that small portion” still grows quadratically with context length.
2. Cost Breakdown: Attention Is Not Expensive, Indexing Is
The cost of standard full attention is clear: with a context length of T, the prefill stage costs roughly O(T²) in computation, and decoding each token costs roughly O(T). When the context grows from one million to ten million tokens, the prefill attention computation grows by about 100x.
The division of labor in sparse attention is: a lightweight indexer first selects the k most relevant keys for each query, then the actual attention computes only over those k keys, compressing the attention body itself to O(Tk).
This idea holds under one premise: the model really does need only a few keys. Validation across the industry at medium lengths is fairly consistent — at the hundred-thousand-token level, letting each query look at only the small number of keys it is genuinely most relevant to (usually a few thousandths of all keys) still preserves over 90% of full-attention accuracy on long-context benchmarks.
But a paradox hides here: for the indexer to know which keys are most relevant, it must first score every key. So while the attention body is pushed down, the indexer pushes the cost back up to quadratic. Common mitigations include lowering scoring precision, compressing dimensions, and aggregating multiple keys into a block for scoring. These reduce the constant factor but do not change the order of magnitude.
3. Core Idea: Move “Scoring” into Latent Space
The key judgment behind latent sparse attention is that indexing does not have to happen on the original token representations; it can be done in a low-dimensional latent space.
You can think of it as a three-stage funnel:
- Stage one, compression. Project the key representations of the long sequence into a low-dimensional latent space to form a compact “index view.” This stage performs only one linear transformation, with cost growing linearly with T.
- Stage two, coarse filtering. The query is likewise projected into the latent space and dot-producted with the compact index to obtain a candidate set. Because the dimensionality drops significantly, the constant for this step is minimal.
- Stage three, exact computation. True attention is executed only over the candidate set, restoring a high-precision numerical path.
The illustrative pseudocode below emphasizes structure over specific interfaces:
# 1) Precompute: compress keys into latent space (one-time, linear cost)z_k = proj_k(key_states) # [T, d_latent], d_latent << d_head# 2) Each query does coarse filtering in latent spacez_q = proj_q(query_states) # [B, T, d_latent]scores = z_q @ z_k.transpose(-1, -2) # low-dimensional dot product, minimal constanttopk_idx = scores.topk(k, dim=-1).indices# 3) Exact attention only over candidatesk_sel = gather(key_states, topk_idx)v_sel = gather(value_states, topk_idx)out = softmax(q @ k_sel.T / sqrt(d)) @ v_sel
What truly brings the change in order of magnitude is this: the complexity of the indexing stage shifts from “T² × high-dimensional dot product” to “T × low-dimensional dot product + T × candidate selection,” bringing the whole into the sub-quadratic range.
4. Why You Can Get Both Speed and Accuracy
The speed comes from two effects stacking: first, the constant compression from the reduced indexing dimensionality; second, the small candidate set means exact attention runs only at a small scale. On a single-layer prefill at ten million tokens, this class of methods can already compress the time by more than an order of magnitude. The gains differ across model sizes, and larger models benefit more noticeably, because their indexers originally account for a larger share.
The accuracy comes from the fact that “coarse filtering does not pursue absolutely correct ordering, only recall.” As long as the true top-k very likely falls within the candidate set, the third-stage exact computation can restore the numerical behavior of the original model. In engineering, the candidate set is usually set to a multiple of k, trading recall for stability.
5. Engineering Implementation Notes
- Latent dimension choice: Too small hurts recall; too large loses the point of compression. A common starting point is to scan from around half the head dimension.
- Candidate multiplier: A candidate set of 2–4x k is a common starting point; long-tail tasks need it raised.
- Compatibility with positional encoding: The low-dimensional projection must retain positional information, otherwise coarse filtering degrades into “looking only at content, not distance.”
- Working with block sparsity: Block-level aggregation can serve as a complement to the first-stage compression, further reducing the indexing constant.
- Memory and bandwidth: The index view must stay resident, so pay attention to coordinated layout with the KV cache to avoid repeated data movement.
- Decoding stage: During single-token decoding, the indexing cost share is low, so it can degrade to an ordinary sparse path, reducing branch complexity.
6. Evaluation Method: Do Not Look Only at Total Time
It is recommended to measure at three levels: end-to-end prefill throughput, the indexer’s standalone time share, and recall (the coverage ratio of the candidate set over the true top-k). On the accuracy side, cover at least three task types — long-context retrieval, multi-hop reasoning, and long-document summarization — and compare against a full-attention baseline at the same length.
7. Conclusion
When context enters the tens-of-millions range, the focus of optimization has shifted from “compute less” to “search less.” Moving indexing into latent space, replacing full scoring with low-dimensional coarse filtering plus exact computation over a small set, is currently a viable path for pulling long-context cost back into the sub-quadratic range. For engineering teams working on long-document understanding, code-base-level analysis, and multi-turn long-term memory, this route is worth validating first.
