Sparse Attention at 10 Million Tokens: The Hidden Indexing Cost and How to Fix It
As context windows push toward 10 million tokens, hybrid architectures that combine sparse and linear attention still leave one cost unchecked: in a single global attention layer during prefill, roughly 98% of the time goes to indexing rather than attention math. This article explains where that hidden bill comes from, presents a general latent-space sparse attention method that moves relevance scoring into a low-dimensional latent space to bring indexing below quadratic complexity, and provides a complexity comparison, illustrative code, an accuracy validation approach, and an engineering checklist for staged adoption.
In long-context settings, mainstream large models commonly adopt a hybrid architecture that combines sparse attention with linear attention, so each query attends to only a small set of the most relevant historical tokens. This route has been validated repeatedly at the million-token scale. But as the context grows to the ten-million-token level, an originally unremarkable overhead starts to spiral out of control: during prefill in a single global attention layer, about 98% of the time is not spent on attention computation at all, but consumed by the indexing step that decides “which tokens to look at.”
The nature of the problem changes here. Sparse attention has long proven that “looking at only a small part” is enough to preserve accuracy. What actually blocks ultra-long context is that “finding that small part” still grows quadratically with context length.
This article takes a neutral view of the causes behind this hidden bill and abstracts a general method for latent-space sparse attention: move relevance scoring into a low-dimensional latent space, reducing indexing complexity to sub-quadratic. The article provides a complexity comparison, illustrative code, ideas for accuracy validation, and an engineering practice checklist, so readers can judge whether their own inference pipeline is worth reworking and how to roll out changes in stages.
Where the Hidden Bill Comes From
The cost structure of a global attention layer is easy to misread. Attention itself is already sparse, so the arithmetic looks cheap. The indexing step that produces the sparse pattern is not sparse, however: scoring a query against the full history means touching every candidate token, and that work repeats for every query. Two quadratic terms stack up, and the one that dominates at ten million tokens is the one people rarely profile.
A General Method: Latent-Space Sparse Attention
The proposed abstraction is to perform relevance scoring in a low-dimensional latent space rather than in the original token space. Candidates are represented by compact latent vectors, and the query is scored against those vectors, which brings indexing complexity down to sub-quadratic while leaving the downstream sparse attention computation unchanged in form.
Complexity Comparison
The comparison below contrasts the indexing cost of scoring in token space with scoring in latent space.
| Stage | Token-space indexing | Latent-space indexing |
|---|---|---|
| Relevance scoring | Quadratic in context length | Sub-quadratic in context length |
| Attention computation | Sparse, unchanged | Sparse, unchanged |
Illustrative Code
# Score queries against compact latent representations instead of full token stateslatent = project_to_latent(token_states) # low-dimensional latent vectorsscores = query_latent @ latent.T # sub-quadratic relevance scoringindices = topk(scores, k) # select the tokens to attend to
Accuracy Validation Approach
Validation should confirm that moving scoring into the latent space does not degrade the selected token set. The practical idea is to compare the tokens chosen by latent-space scoring against those chosen by token-space scoring on the same inputs, and to check that the resulting outputs stay consistent with the token-space baseline.
Engineering Practice Checklist
- Profile a single global attention layer during prefill and separate indexing time from attention time before optimizing anything.
- Confirm that indexing, not attention, is the dominant term at your target context length.
- Introduce the latent-space scoring path behind a switch so it can be compared against the existing path.
- Validate the selected token set and output quality against the token-space baseline.
- Roll out in stages, starting from the layers and context lengths where indexing dominates.
