LSA Achieves Up to 22.7x Speedup for Single-Layer Prefill at 10M Tokens
As large language model context windows expand to 10 million tokens, the prefill phase becomes a major computational bottleneck. In sparse attention mechanisms, the indexing process used to find relevant tokens can overshadow the attention computation itself. Latent Sparse Attention (LSA) addresses this by projecting queries and keys into a lower-dimensional space for scoring before executing precise attention. This article breaks down the LSA workflow, analyzes its theoretical complexity, and reviews empirical data showing up to a 22.7x speedup for single-layer prefill at the 10M token scale.
Millions to Tens of Millions: The Rising Cost of “Searching” in Sparse Attention
The context length of large language models is expanding rapidly. Documents with tens of thousands of tokens are now common, million-token codebases and multi-turn dialogue records are entering practical applications, and tests with tens of millions of tokens have already emerged. The prefill phase for ultra-long contexts demands the highest level of compute and memory.
Sparse attention is a standard approach to reducing computational load during this phase. Instead of calculating attention between every query and all historical tokens, a lightweight indexer first selects a small number of relevant tokens. The formal attention computation is then performed only on these selected tokens. This preserves long-range dependencies while reducing the complexity of attention computation from quadratic to quasi-linear.
At the million-token scale, this approach has been validated by several mainstream models. However, when the context reaches 10 million tokens, the problem shifts. According to the original material, during a 10M token prefill on a global attention layer of a certain model series, approximately 98% of the time was spent on the indexer rather than the attention matrix multiplication itself. In other words, the cost of “looking” has decreased, but the cost of “searching” has become the new bottleneck.
The root cause is that the indexer still needs to perform some form of comparison or index construction between each query and all keys. When the sequence length reaches the tens-of-millions level, the overhead of this process remains nearly quadratic, keeping latency high.
The Basic Workflow of Latent Sparse Attention
Latent Sparse Attention (LSA) specifically targets the cost of “searching.” Its approach is to first project queries and keys into a low-dimensional latent space, perform scoring and filtering in that latent space, and finally return to the original space for exact computation.
The process consists of four steps:
- Low-dimensional projection: Use a learnable projection matrix to map each query and key from their original dimensions to a smaller latent dimension.
- Latent space scoring: Compute the inner product or similarity between queries and keys in the low-dimensional space.
- Top-k selection: Retain the indices of the top k keys with the highest scores for each query.
- Exact attention: Retrieve the selected keys and values, and execute standard scaled dot-product attention in the original space.
The projection matrix is trained end-to-end along with the attention weights, allowing the model to gradually learn to map semantically related tokens to nearby locations.
Below is a tutorial-style code snippet illustrating the data flow:
def latent_sparse_attention(query, key, value, proj, k):# query, key: [batch, seq_len, dim]# proj: [dim, latent_dim], where latent_dim << dimq_lat = query @ projk_lat = key @ proj# Compute relevance scores in latent space: inner product of each query with all keysscores = q_lat @ k_lat.transpose(-1, -2)# Select the top-k key indices with the highest scores for each querytop_idx = scores.topk(k, dim=-1).indices# Gather only the selected key/value pairs and execute exact attentionselected_key = gather(key, top_idx)selected_value = gather(value, top_idx)return scaled_dot_product_attention(query, selected_key, selected_value)
Note that the scores in the code is a full seq_len × seq_len matrix. Even if the latent dimension is very small, its computation is still O(N² · d_latent). Therefore, this code alone does not demonstrate that the complexity has become linear.
Complexity: Low-Dimensional Scoring Remains Quadratic
This point needs clarification, as many discussions mistakenly equate “dimension reduction” with “complexity reduction.”
Reducing the latent dimension only decreases the constant overhead of scoring each pair; it does not change the scale of “comparing every query with all keys.” For N=10M, even if d_latent is very small, the computation for full scoring remains massive. Therefore, the scoring phase can only approach linear complexity if one of the following conditions is met:
- Chunking keys so that each query is only compared to a fixed number of candidate chunks.
- Using approximate nearest neighbor (ANN) search or clustering to restrict the candidate set to a fixed size.
- Reusing candidate indices saved across decoding steps to reduce redundant filtering.
The original material does not specify which method is actually used. Therefore, the claim of being “quasi-linear” should be understood as “under the condition that the candidate size is constrained,” rather than holding true for any arbitrary implementation.
The table below compares the scoring complexity of three approaches (N is sequence length, d is original dimension, d_latent is latent dimension, and m is the number of candidates per query):
| Approach | Per-Layer Scoring Complexity (Approx.) | Description |
|---|---|---|
| Full scoring in original space | O(N² · d) | High-dimensional inner product, highest cost |
| LSA full low-dimensional scoring | O(N² · d_latent) | Smaller constant, remains quadratic |
| LSA candidate-constrained version | O(N · m · d_latent) | Grows near-linearly with N when candidate count m is fixed, but requires additional retrieval or chunking overhead |
This table provides theoretical estimates and does not account for memory access, index construction, or parallel efficiency. Actual latency requires empirical testing.
How to Interpret the Empirical Data
The original material provides two sets of results. In a 10M token prefill, the execution time for a single global attention layer was reduced, achieving a maximum speedup of 22.7x. The text also mentions another set of results involving longer or different contexts, though the specific details were not fully provided in the source material.
