Reducing Long-Context Inference Cost with Cross-Layer Index Reuse
This article explains how cross-layer index reuse lowers the cost of long-context inference. It covers the cost structure of sparse attention, why the indexer itself becomes a bottleneck at million-token scale, how reusing top-k token indices across a fixed layer interval reduces repeated full scans, how the mechanism works alongside Multi-head Latent Attention, and the trade-offs and practical tuning recommendations for reuse intervals and index caching.
1. The Cost Structure of Long-Context Inference
When context length grows from a few thousand tokens to the million-token range, inference cost does not expand only because of weight reads. A large share comes from two parts of attention: scoring and filtering the relevance between tokens, and the actual weighted aggregation over the selected token set.
The common industry approach is sparse attention. A lightweight “indexer” first selects the top-k most relevant historical tokens for each query position, and exact computation is then performed only on those k positions. This reduces attention complexity from being quadratic in sequence length to being linear in k.
However, many engineering efforts overlook one point: the indexer itself must still scan all historical tokens to choose the top-k. In other words, even when exact computation is sparsified, the “which tokens to select” step remains a full scan. In million-token scenarios, the memory access and computation of this step quickly become a new bottleneck, offsetting a significant portion of the gains from sparsification.
2. Core Mechanism: From Indexing Every Layer to Indexing Every Few Layers
The idea behind cross-layer index reuse is straightforward: adjacent layers make highly similar relevance judgments about historical tokens, so there is no need to rerun the full indexer at every layer. The approach sets a reuse interval N (for example, every four layers). The full top-k index computation runs only at the first layer of each interval, and the following N-1 layers directly reuse the same token index, performing attention aggregation only at those indexed positions.
The rationale rests on two observations. First, although representations in deep networks become progressively more abstract layer by layer, the coarse-grained judgment of “which historical positions are worth attending to” changes very little between adjacent layers. Second, the indexer output is a discrete top-k set and is inherently somewhat fault-tolerant; small deviations usually do not change the final selection.
Illustrative pseudocode:
# layer_idx is the current layer index, N is the reuse intervalif layer_idx % N == 0:idx = indexer_topk(q, k_cache, k) # full scan, produces top-k indicescache_idx[layer_idx] = idxelse:idx = cache_idx[layer_idx - layer_idx % N] # reuse the most recent indexout = sparse_attention(q, k_cache, v_cache, idx) # aggregate only at indexed positions
3. Synergy with Multi-head Latent Attention
In long-context scenarios, the memory footprint of the key-value cache often becomes a bottleneck before compute does. Multi-head Latent Attention applies low-rank compression to keys and values, shrinking the cache representation into a smaller latent vector space and significantly reducing cache size, while sparse attention reduces the number of tokens involved in aggregation. When the two are combined, the indexer scans the compressed latent representations, making each scan cheaper. This makes periodic index recomputation more cost-effective: even recomputing once every N layers, the amortized cost is far lower than recomputing at every layer.
One engineering caveat is that the indexer input must stay in the same coordinate system as the cache representation, and the timing of compression and decompression must not be misaligned; otherwise the top-k selection will be distorted.
4. Benefits and Costs
The benefits appear in three areas: memory access during the indexing stage is amortized to roughly 1/N; scheduling can pipeline index computation with the previous layer’s attention aggregation; and in batch serving of very long contexts, the compute per request drops, increasing the number of concurrent requests.
The costs are equally clear. The index is “stale” within the reuse interval, so accuracy may degrade slightly if certain layers genuinely need to attend to different positions. Implementation complexity rises, requiring additional index caching and cross-layer dependencies. In addition, the reuse interval N becomes a new critical hyperparameter: too small yields insufficient gains, while too large raises accuracy risk.
5. Practical Recommendations
- Start with a small reuse interval (such as 2 to 4), compare against a baseline that recomputes at every layer using an offline evaluation set, and watch for the inflection point in the accuracy curve.
- Keep full indexing for the first and last layers rather than reusing; these two points have a larger impact on the final representation.
- Isolate the index cache per request to avoid cross-contamination between different sequences in a batch.
- Monitoring should include the share of time spent on indexing, cache hit rate, and end-to-end latency, rather than optimizing a single point.
- If the inference service is hosted on a container platform with monitoring and alerting capabilities, such as those offered by Baidu AI Cloud, these metrics can be brought into unified observability to support capacity planning and elastic scaling.
