Sparse Attention at 10M Tokens: Why the Indexing Step Becomes the Bottleneck

    At very long context lengths, sparse attention shifts the bottleneck from attention computation itself to the indexing step that decides which tokens to examine. This article explains why indexing cost grows with sequence length, compares the complexity of different scoring approaches, examines what latent-space pre-filtering can and cannot do, and provides a practical engineering checklist for identifying and addressing the indexing bottleneck in long-context inference.

    1. Key Takeaway First: The Bottleneck Has Shifted from Attention Computation to Token Selection

    At the 10-million-token scale, the core computation of sparse attention has already been compressed significantly. However, the indexing step—deciding “which tokens to look at”—still grows with context length. In some implementations, profiling shows that this step can account for the majority of prefill latency. This conclusion depends on specific hardware, batch size, sequence length, and sparsity ratio, so readers should verify it against their own baselines.

    2. Why Long Contexts Need Sparse Attention

    As context expands from one million to ten million tokens, the cost of standard full attention rises rapidly. Every query must compute relevance against all preceding keys. For a context of length T:

    • Prefill computation is O(T²);
    • Attention cost per decoded token is O(T).

    Sparse attention splits the computation into two steps. First, a lightweight indexer selects k candidate keys for each query; then attention is computed only over those k keys, at a cost of roughly O(Tk). This approach assumes the model can maintain accuracy with only a small subset of keys. Prior research has reported that, in specific 128k-context settings, keeping only about 1% of keys allows the model to remain close to full-attention performance on long-context benchmarks. That figure depends on the specific model and benchmark and should not be applied directly to other scenarios.

    3. The Hidden Cost of the Indexer

    To determine which k keys are most relevant, the indexer must first score every key. Mainstream approaches typically use a low-dimensional scoring network for this step. Low-dimensional interactions reduce the constant factor per scoring operation, but if every query interacts with all keys, the total prefill complexity remains O(T²).

    With shorter contexts, the constant-factor advantage makes indexing overhead negligible. Once the context grows by an order of magnitude, the growth of the T² term outpaces the linear attention computation, and the indexing step’s share of total time rises accordingly.

    The table below compares three approaches in terms of prefill and single-step decoding complexity, where d is the scoring dimension and m is the number of candidates each query accesses:

    Approach Prefill scoring complexity Single-step decoding scoring complexity Notes
    Full attention O(T²·d) O(T·d) Every query accesses all keys
    Full low-dimensional scoring + sparse attention O(T²·d_latent) O(T·d_latent) Scoring still grows quadratically with T
    Each query accesses only m candidates O(T·m·d_latent) O(m·d_latent) Sub-quadratic when m does not grow with T, but recall must be evaluated separately

    4. Latent-Space Pre-Filtering: What It Can and Cannot Do

    One class of approaches projects keys into a low-dimensional latent space, performs coarse filtering there, and then computes exact attention in the original dimensions. The process has three steps:

    1. Latent projection: learnable projections compress keys to dimension d_latent, maintaining a compact index;
    2. Low-dimensional coarse filtering: projected queries compute scores against the compressed keys to select candidates;
    3. Exact computation: attention is computed over candidate keys in the original dimensions.

    Projection only lowers the constant factor of scoring. If each query still scans all keys, the coarse-filtering complexity remains O(T²·d_latent). To achieve sub-quadratic complexity, each query must access only a subset of keys—for example, via block partitioning, clustering, routing, or approximate nearest neighbor search. These mechanisms introduce recall loss, which must be measured separately in experiments.

    The following pseudocode illustrates the core prefill logic, omitting causal masking, block partitioning, and memory optimizations:

    1. # Inputs: Q [T, d], K [T, d], V [T, d_v], with d_latent << d
    2. # Full scoring: complexity O(T^2 * d_latent), memory usage O(T^2)
    3. Q_lat = proj_q(Q) # [T, d_latent]
    4. K_lat = proj_k(K) # [T, d_latent]
    5. scores = Q_lat @ K_lat.T # [T, T]
    6. # Causal mask: future positions in scores must be set to -inf (omitted)
    7. topk_idx = scores.topk(k, dim=-1).indices # [T, k]
    8. # Exact attention: each query computes only over its k candidates
    9. K_sel = K[topk_idx] # [T, k, d]
    10. V_sel = V[topk_idx] # [T, k, d_v]
    11. logits = (Q[:, None, :] * K_sel).sum(-1) / d**0.5 # [T, k]
    12. attn = logits.softmax(dim=-1) # [T, k]
    13. out = (attn[..., None] * V_sel).sum(1) # [T, d_v]

    During single-step decoding, q_lat has shape [1, d_latent], and the scoring cost drops to O(T·d_latent). To change from full scoring to having each query access only m candidates, you would need an additional candidate structure, and the code would need to be rewritten accordingly.

    5. Engineering Checklist

    1. Profile first. Record the time share of indexing, candidate selection, attention computation, and other operators separately, and note the hardware, batch size, sequence length, and baseline configuration.
    2. Sweep k and d_latent. Plot accuracy-versus-latency curves to find the point where accuracy starts to degrade noticeably.
    3. Measure recall. Using full-attention results as a reference, measure the overlap between top-k candidates and the truly important keys.
    4. Evaluate across lengths. Repeat experiments at different values of T to observe how the indexing share changes with context length.
    5. Monitor continuously after deployment. When the input distribution shifts, recall and accuracy may drift and require periodic checks.

    6. FAQ

    Why is the indexer still O(T²)? If every query must score all keys, the computation scales with T² regardless of how low the dimension is. Dimensionality reduction only shrinks the constant factor.

    Does low-dimensional projection mean sub-quadratic? No. Scoring only becomes sub-quadratic when the number of candidates each query accesses does not grow with T.

    How to choose k at 10M tokens? There is no universal value. Start with a small k, measure accuracy on your target benchmarks, then finalize it based on your latency budget.

    Does the indexer need end-to-end training? Incorporating the indexer into training may be more stable than post-hoc heuristic rules on some long-range dependency tasks, but it costs more to train. Decide based on your task.

    7. Closing Thoughts

    Sparse attention solves the computation problem, while the indexing step determines whether ultra-long contexts can actually run faster. Start by profiling to confirm where your bottleneck lies, then decide whether to introduce latent-space pre-filtering or candidate routing.