Sparse Attention at 10M Token Context: Key Practices Behind a 22x Speedup
As context windows cross the 10-million-token mark, the bottleneck in sparse attention shifts from computing attention to finding which tokens to attend to. This article breaks down where the quadratic cost actually comes from, explains how latent-space sparse attention moves selection into a compressed representation through a three-stage funnel, and outlines the engineering details required to preserve accuracy, along with rollout advice and common pitfalls for teams building long-context systems.
1. Background: Cost Structure Inverts Past 10M Tokens
Over the past two years, long context has moved from an experimental feature to a default specification. Million-token context is already a baseline configuration in several model services, and hybrid architectures combining sparse and linear attention are widely adopted. The shared premise is simple: each query vector no longer scans every historical token, but selects a few of the most relevant segments to participate in the computation.
This approach has been validated repeatedly at the million-token scale. But as context pushes toward 10 million tokens, an overhead that once seemed negligible starts to spiral. Take a global attention layer as an example: at 10M tokens, roughly 98% of prefill time is not spent on the attention matrix multiplications themselves, but on the indexer that “decides which tokens to look at.”
The nature of the problem has changed. Sparse attention has long proven that “attending to a small subset” is enough to preserve accuracy. What actually blocks ultra-long context is that the cost of “finding that small subset” still grows quadratically with context length.
2. Complexity Breakdown: Where the Quadratic Term Comes From
It helps to split attention into three parts:
- Scoring and selection (indexing stage): estimating each query’s relevance to all historical positions, at a cost of roughly O(N²·d);
- Sparse aggregation (compute stage): interacting only with the k selected positions, at a cost of roughly O(N·k·d);
- Index structure maintenance (block statistics, bucket sorting, prefix sums, etc.): typically O(N log N) or O(N).
At the million-token scale with k in the thousands, the second stage still dominates. At 10M tokens, the N² term in the first stage rapidly consumes the entire budget. In other words, sparsification compressed the “compute” but not the “retrieval.” That is the part that needs to be redesigned.
3. Core Idea: Move “Selection” into Latent Space
One class of methods, known as latent-space sparse attention, offers an answer: instead of doing exhaustive comparison in the original token space, first compress the long sequence into low-dimensional latent representations, then perform relevance estimation and candidate filtering in latent space, and finally pull only a small number of original tokens back into the main compute path.
You can think of it as a three-stage funnel:
- Stage 1 (coarse filtering): split the sequence into blocks, compress each block into one or a few latent vectors, and let the query do a small-scale similarity computation against block-level latent vectors only, reducing complexity to O(N·b·d), where b is the number of blocks and is far smaller than N;
- Stage 2 (fine selection): within the few shortlisted blocks, use lightweight scoring to pick k tokens;
- Stage 3 (exact compute): run full attention on those k tokens only.
Illustrative pseudocode follows; the point is the structure, not the specific interfaces:
# 1) Block-level compression: compress the long sequence into a few latent vectorslatent = block_compress(kv, block_size=256) # [b, d], b << N# 2) Coarse filtering: query compares only against block latent vectorsscores = q @ latent.T # [1, b]top_blocks = topk(scores, k_blocks=8)# 3) Fine selection: pick tokens within the shortlisted blockscand = gather(kv, top_blocks)idx = lightweight_score(q, cand, k=2048)# 4) Exact compute: attention over the selected tokens onlyout = attention(q, kv[idx])
4. Why You Get Both Speedup and Accuracy
The speedup comes from a change in the order of magnitude of complexity: the dominant term drops from N² to N·b and N·k. At 10M tokens, a speedup of more than 20x for single-layer prefill is not counterintuitive, and the gain grows further when block granularity is finer and indexing is cheaper.
Accuracy depends on three engineering details:
- Train-inference consistency: the compressor and selector need to be jointly trained with the main model or aligned through distillation, to avoid distribution drift at inference time;
- Recall fallback: keep a small fixed window and cross-block representative positions for each query, so critical long-range information is not missed;
- Controllable compression error: block representations must retain enough separability; too high a compression ratio makes coarse filtering lose discriminative power.
5. Deployment Advice and Common Pitfalls
- Roll out in stages: enable it in the prefill stage first, then gradually extend to the decode stage, using monitoring and alerting to watch time-to-first-token and throughput changes;
- Tune parameters per task: block size, candidate count, and recall window must be tuned jointly; the optimal combination differs between long-document retrieval and code completion;
- Do not expect zero cost: index structure maintenance, extra GPU memory usage, and implementation complexity are all real costs;
- Evaluate in layers: look at both overall metrics like perplexity and targeted tasks such as long-range retrieval and multi-hop reasoning, so averages do not hide local regressions.
6. Conclusion
In the 10M-token regime, the bottleneck has shifted from “can we compute it” to “can we find it.” Moving the selection process into a low-dimensional latent space and replacing exhaustive comparison with a tiered funnel is currently the most engineering-feasible path. For teams building ultra-long context capabilities, refactoring the index layer first often pays off more than continuing to optimize the attention kernel. Building the evaluation pipeline on a cloud container platform with log monitoring services can help reproduce this kind of tiered gain data more reliably.
