Indexing Bottlenecks at 10M Tokens and Latent Sparse Attention

    As LLM context windows push toward 10M+ tokens, the bottleneck of ultra-long context has shifted. Sparse and linear attention have made compute nearly linear, but the indexing step that decides which tokens to attend to now dominates. This article explains why, and how Latent Sparse Attention (LSA) moves relevance matching into a low-dimensional latent space, with a reference implementation.

    As LLM context windows scale toward the 10M+ token range, the bottleneck of ultra-long context is changing. Many mainstream models now treat million-token context as standard, typically by combining sparse attention with linear attention so that each query attends to only a small set of the most relevant tokens. This approach has been well validated at the million-token scale. As context keeps growing, the cost of sparse computation itself remains manageable, but the indexing process that decides which tokens to look at starts to dominate the runtime.

    1. Why the Sparse Attention Bottleneck Shifts to Indexing

    Sparse attention generally works in two steps. The first step is retrieval: computing the relevance between the current query and historical keys, and selecting the tokens to keep. The second step is computation: running standard attention only over the selected subset. Sparse computation reduces the cost of the second step to roughly linear in context length, which makes the first step the new dominant cost.

    If retrieval uses exact brute-force dot products, the prefill stage must compare every query against every key, so the number of pairwise operations grows as N² with sequence length N. Going from 1M to 10M tokens amplifies this cost by roughly a factor of 100.

    Claims circulating online such as “indexing accounts for over 98% of runtime” do not specify hardware, model layers, or sequence length, so this article does not adopt them. The actual ratio depends on the specific model and configuration, and should be measured layer by layer with a profiler, separating retrieval time from computation time.

    2. How Latent Sparse Attention Works

    The core idea of Latent Sparse Attention (LSA) is to move relevance matching from the original dimension D into a low-dimensional space r, where r is much smaller than D. The process has three steps:

    1. Latent projection: Map queries and keys into the low-dimensional space using pre-trained linear matrices. These projection matrices must be learned during training; otherwise, distances in the low-dimensional space cannot reflect semantic relevance in the original space.
    2. Approximate retrieval: In the low-dimensional space, use Locality-Sensitive Hashing (LSH) or Approximate Nearest Neighbor (ANN) search to find the top-k candidate keys for each query. Ideally, the retrieval cost per query grows nearly logarithmically or sublinearly.
    3. Exact computation: Return to the original space and run attention only over the selected k keys and values. The computation over the selected portion is exact.

    The pseudocode is as follows.

    Variable definitions: B is the batch size, H is the number of attention heads, Nq and Nk are the query and key lengths (equal during prefill), D is the per-head dimension, r is the latent dimension, and k is the number of keys kept per query.
    Input: q has shape [B, H, Nq, D]; k and v have shape [B, H, Nk, D].
    Output: out has shape [B, H, Nq, D].

    1. import math
    2. def latent_sparse_attention(q, k, v, proj_q, proj_k, top_k, causal=True):
    3. # q: [B, H, Nq, D] k, v: [B, H, Nk, D]
    4. # proj_q, proj_k: [D, r], learned during training, r << D
    5. q_lat = q @ proj_q # [B, H, Nq, r]
    6. k_lat = k @ proj_k # [B, H, Nk, r]
    7. # Each query retrieves its own top_k keys; the causal constraint applies during retrieval
    8. idx = ann_search(q_lat, k_lat, k=top_k, causal=causal) # [B, H, Nq, top_k]
    9. # Gather keys and values per query; indices differ across queries
    10. k_sel = gather_per_query(k, idx) # [B, H, Nq, top_k, D]
    11. v_sel = gather_per_query(v, idx) # [B, H, Nq, top_k, D]
    12. # Fallback mask: block padding positions (idx == -1) and future positions
    13. q_pos = arange(Nq)[:, None] # [Nq, 1]
    14. valid = idx >= 0 # [B, H, Nq, top_k]
    15. if causal:
    16. valid = valid & (idx <= q_pos)
    17. scores = einsum("bhqd,bhqkd->bhqk", q, k_sel) / math.sqrt(D)
    18. scores = where(valid, scores, -inf)
    19. attn = softmax(scores, dim=-1)
    20. # Zero out rows where all entries are invalid, to avoid NaN from softmax
    21. attn = where(valid.any(-1, keepdim=True), attn, 0)
    22. out = einsum("bhqk,bhqkd->bhqd", attn, v_sel)
    23. return out

    The code above is a conceptual implementation, with ann_search and gather_per_query as placeholder functions. A production system needs fused kernels to implement these operations; otherwise, the intermediate tensors would negate the performance gains.