Cross-Layer Index Sharing for Sparse Attention in Large Language Models
This article explains the cross-layer index sharing mechanism for sparse attention in large language models. It covers the inference cost challenges of million-token contexts, the roles of Multi-head Latent Attention (MLA) and sparse attention, the redundant cost of per-layer indexing, and how reusing top-k token indexes across layer groups reduces inference overhead with controlled accuracy impact. Engineering considerations such as group size trade-offs, accuracy regression testing, and synergy with other optimizations are also discussed.
1. Background: The Inference Cost Challenge of Ultra-Long Contexts
As the context windows of large language models scale from tens of thousands of tokens to millions, the core inference bottleneck has shifted from raw compute to the attention mechanism itself. In standard autoregressive decoding, generating each token requires computing attention scores against every token in the historical context, so complexity grows linearly or even quadratically with sequence length. At a 1M-token context length, even with efficient data loading and kernel optimizations, attention computation and KV cache reads remain the dominant sources of latency and memory overhead.
The industry has proposed several optimization paths for this problem, among which sparse attention is one of the most representative: not all historical tokens are equally important for generating the current token. If a low-cost method can identify the small set of tokens most relevant to the current query, full attention only needs to be executed over those tokens—dramatically reducing computation with almost no loss in accuracy.
2. Foundational Components: Multi-head Latent Attention and Sparse Attention
Modern long-context models typically modify the attention module along two dimensions.
2.1 Multi-head Latent Attention (MLA)
MLA applies low-rank compression to key-value vectors, compressing the KV data cached per layer into a low-dimensional latent space. This significantly reduces KV cache memory footprint and memory access. Compared with traditional multi-head attention, MLA preserves representational power while making cache management at long contexts feasible. Note, however, that MLA solves the question of “how large the cache is”—it does not change how many tokens attention must traverse.
2.2 Sparse Attention (DSA-style Mechanisms)
Sparse attention addresses the question of “how many tokens to look at.” The typical approach introduces a lightweight indexer (selector): for each query, a low-cost scoring network first selects the top-k most relevant tokens from the full context, and precise attention is then computed only over those k tokens. As a result, the main attention cost drops from O(sequence length) to a constant scale determined by k, while the indexer’s own overhead is far smaller than full attention.
3. A New Problem: Redundant Indexer Computation
Although effective, the scheme above has an easily overlooked overhead in real deployments: the indexer runs independently at every layer. For the same query, each layer of the model rescans the entire context and recomputes the top-k index. With dozens of layers and million-token contexts, these repeated index computations accumulate into a substantial fixed cost.
A natural observation is that attention distributions between adjacent layers are often highly correlated. If layer i deems certain tokens important for the current query, layer i+1 will very likely reach a similar judgment. Running a complete index selection independently at every layer is therefore clearly redundant.
4. How Cross-Layer Index Sharing Works
The core idea of cross-layer index sharing can be summarized in one sentence: index computation does not need to happen at every layer—key results can be reused across layers.
Specifically, the mechanism divides the model’s layers into groups, for example four layers per group:
- First layer of each group: runs the sparse attention indexer in full, computing scores over the entire context and selecting the top-k token indexes.
- Subsequent layers in the group: do not rerun the indexer; they directly reuse the token index set obtained by the first layer and only perform attention over the selected tokens.
The logic can be expressed with the following pseudocode:
selected_idx = Nonefor layer_id, layer in enumerate(model.layers):if layer_id % GROUP_SIZE == 0:# First layer of the group: compute the full top-k indexselected_idx = indexer(layer.hidden_states, context_kv)# Later layers reuse the index and only run sparse attentionattn_out = sparse_attention(layer.hidden_states,context_kv[selected_idx])layer.hidden_states = layer.hidden_states + attn_out
It is worth emphasizing that this approach does not change the fundamental idea of sparse attention—it is still “select tokens first, then compute attention.” It simply reduces the frequency of the selection step. Therefore its impact on model accuracy is controllable, while the benefit is direct: the number of indexer invocations drops to a fraction of the original, and the corresponding compute and memory-access costs decline accordingly. At million-token contexts, where the indexer must scan an enormous candidate set, the end-to-end inference cost improvement from eliminating repeated scans is especially significant.
5. Why This Design Delivers the Greatest Benefit at Ultra-Long Contexts
The benefit of cross-layer index sharing is closely tied to context length, which can be understood from three perspectives:
Candidate set size: The longer the context, the higher the cost of each indexer scan, and the greater the absolute gain from reducing scan count. For short contexts the indexer’s overhead is limited, so sharing offers little value; at 1M tokens, every scan is a full traversal of the entire context.
Index stability: The stronger the correlation of attention patterns between adjacent layers, the smaller the accuracy loss from sharing indexes. In practice, attention distributions of adjacent layers in deep models are highly similar, providing a theoretical basis for cross-layer sharing.
Memory-access friendliness: Reusing indexes means multiple layers read the same batch of KV entries, which improves cache hit rates and data locality, further reducing memory-access pressure.
6. Engineering Considerations in Practice
When implementing cross-layer index sharing in production, the following points typically require attention:
- Group size trade-off: Larger groups save more index computation but carry a higher risk of accumulated cross-layer deviation; smaller groups behave oppositely. Accuracy evaluations should be used to find the balance between overhead and quality.
- Accuracy regression validation: Index sharing is a lossy optimization. Systematic regression should be conducted on representative tasks such as long-document understanding, retrieval, and code to confirm that key metrics show no significant degradation.
- Synergy with other optimizations: This mechanism is independent of KV cache compression, kernel fusion, and parallel decoding, and can be combined with them to build a low-cost long-context inference solution. When deploying on cloud GPU resources and container platforms, monitoring and alerting services can also be used to observe changes in the share of index computation and verify the optimization effect.
7. Conclusion
Cross-layer index sharing provides a simple yet effective complement to the engineering of sparse attention: while keeping the “filter-then-compute” paradigm of sparse attention unchanged, it removes a considerable chunk of redundant overhead in ultra-long-context inference by reducing the indexer’s invocation frequency. It exemplifies a general principle of long-context optimization—identifying repeated computation across layers or steps and eliminating it with well-reasoned approximation. For teams that need to control inference costs in million-token scenarios, understanding the principles and trade-offs of such mechanisms is an important step toward building efficient inference services.
