Ten-Million-Token Context: Breaking the Quadratic Bottleneck in Sparse Attention
As context windows scale from millions to tens of millions of tokens, sparse attention shifts the bottleneck from attention computation to token retrieval. This article breaks down the cost structure of full and sparse attention, explains why the indexing step becomes the dominant expense at extreme context lengths, and introduces latent-space sparse attention—a method that performs relevance retrieval in a low-dimensional latent space to reduce indexing cost while preserving accuracy. It also covers accuracy safeguards and key engineering considerations for deployment.
Introduction: The Cost Structure of Ultra-Long Context Has Changed
Over the past two years, long-context capability has shifted from an optional feature to a baseline specification. Million-token context is becoming a default configuration for many model services, and exploration of ten-million-token context has already begun. One key technology enabling this trend is sparse attention, which allows each query to skip scanning the entire history of tokens and instead select only a few of the most relevant ones for computation.
This approach has been repeatedly validated at the million-token scale. But as context pushes toward ten million tokens, an originally negligible overhead starts to spiral out of control: the indexing step that decides which tokens to look at has become the primary bottleneck. Starting from the cost structure, this article analyzes the nature of the problem and introduces a solution that performs retrieval in latent space.
1. Two Decompositions of Attention Cost
1.1 Quadratic Growth of Full Attention
In standard full attention, every query computes relevance against all preceding keys. With a context length of T, the prefill stage costs roughly O(T²) in computation, and the decode stage costs roughly O(T) per generated token. Growing the context from one million to ten million tokens increases prefill attention computation by roughly 100x in theory.
1.2 The Division of Labor in Sparse Attention
The core idea of sparse attention is to split the work into two steps:
- Step 1: A lightweight indexer selects the k most relevant keys for each query.
- Step 2: The actual attention is computed only over those k keys.
This compresses the main attention cost to O(Tk), which is highly efficient when k is much smaller than T. A common industry practice also includes block sparsity, where several adjacent keys are compressed into a block before scoring, reducing indexing cost.
1.3 The Overlooked Hidden Bill
The problem is that to decide which k keys are most relevant, the indexer must first score all keys. If the scoring process itself grows linearly with context length, then running it for every query still results in quadratic total overhead. At the ten-million-token scale, this overhead can account for more than 90% of the prefill time of a single global attention layer, while the actual attention computation takes only a small fraction.
In other words, sparse attention has proven that “looking at only a small part” is enough to preserve accuracy, but “finding that small part” remains expensive. The nature of the problem has therefore changed: the bottleneck has shifted from computation to retrieval.
2. Latent-Space Sparse Attention: Moving Retrieval to a Low-Dimensional Space
A recent study on latent-space sparse attention targets exactly this retrieval bottleneck. Its core idea is to avoid full scoring in the high-dimensional, full-precision key space. Instead, keys are first projected into a low-dimensional latent space, relevance retrieval is performed in that latent space, and attention computation is then carried out back in the original space.
2.1 Three Key Steps
- Dimensionality-reduction projection: A lightweight projection maps each key to a low-dimensional latent representation, with a dimension much smaller than the original head dimension.
- Latent-space scoring: The query performs dot products against all key latent representations in latent space to obtain relevance scores and select the top-k.
- Original-space computation: Attention is computed at full precision only over the selected k keys using their original high-precision representations.
2.2 Why It Speeds Things Up
The speedup comes from two sources: first, the low dimensionality of the latent space substantially reduces the arithmetic cost of scoring itself; second, latent representations can be precomputed and cached, and reused in both the prefill and decode stages. Illustrative pseudocode:
# Precompute: latent representations of keys, cacheable within the layerK_latent = project_down(K) # [T, d_latent], d_latent << d_head# Each query retrieves top-k in latent spacescores = Q_latent @ K_latent.T # [T, T], low-dimensional dot productsidx = topk(scores, k) # [T, k]# High-precision attention only on selected keysout = attention(Q, K[idx], V[idx])
2.3 Complexity Change
Let the latent-space dimension be d_latent and the original head dimension be d_head, with d_latent much smaller than d_head. The indexing stage drops from O(T²·d_head) to O(T²·d_latent), while the attention stage remains O(Tk·d_head). When d_latent is small enough, indexing is no longer the dominant term, and the overall cost trends toward sub-quadratic.
3. Why Accuracy Holds Up
The sparse approach rests on the premise that the model truly needs only a few keys. Existing validation shows that at the scale of over a hundred thousand tokens, letting each query attend to only the few hundred most relevant keys still preserves over 90% of full-attention accuracy on long-context benchmarks.
The main accuracy risk of latent-space retrieval lies in the projection step: dimensionality reduction may lose discriminative power. Common mitigation techniques include:
- Concatenating a small number of original dimensions with the latent dimensions to balance discriminative power and speed;
- Normalizing latent representations to stabilize the dot-product distribution;
- Using two-stage retrieval with block-level coarse filtering followed by fine ranking within blocks to reduce the impact of projection error;
- Jointly optimizing the projection with the main model during training or fine-tuning so that the latent space aligns with true relevance.
4. Key Engineering Considerations
- Caching strategy: Latent representations and index results should be cached in layers to avoid recomputation during decoding.
- Batching and chunking: Prefill of ultra-long context requires chunked pipelining to control peak GPU memory usage.
- Compatibility with decoding: Each decode step adds only one new query, so the index can be updated incrementally.
- Accuracy regression: Run systematic evaluation on long-context benchmarks before launch, focusing on how retrieval recall affects downstream tasks.
- Resource elasticity: Ultra-long-context inference is sensitive to GPU memory and bandwidth, so containerized deployment with monitoring and alerting can help ensure stability.
5. Conclusion
At the ten-million-token context scale, the real adversary of sparse attention is not attention computation but retrieval itself. Migrating relevance judgment to a low-dimensional latent space is an effective path to reducing indexing cost while preserving accuracy. For engineering teams, successful deployment requires focused investment in three areas—projection design, cache reuse, and accuracy regression—to achieve both speed and quality in ultra-long-context scenarios.
