Reducing Long-Context Inference Costs: How Cross-Layer Index Reuse Works

    This article explains how cross-layer index reuse (IndexShare) reduces the cost of million-token context inference. It covers why standard attention becomes expensive as context grows, how Multi-head Latent Attention (MLA) compresses the KV cache, how sparse attention and indexers select only the most relevant tokens, and how running the indexer once every four layers cuts indexing overhead to roughly a quarter. It also outlines engineering considerations, validation methods, and the main takeaway for teams deploying long-document and code-repository understanding workloads.

    Reducing Long-Context Inference Costs: How Cross-Layer Index Reuse Works

    1. Why Million-Token Contexts Are Expensive

    Demand is growing for long-document understanding, repository-level code question answering, and multi-turn long-term memory. In response, context windows have expanded from a few thousand tokens to the million-token range, and this has become an industry-wide trend. The problem is that cost does not scale linearly with context length. Attention computation and memory bandwidth pressure grow close to quadratically. In standard self-attention, every query token must interact with all historical key/value tokens, so doubling the sequence length makes the attention matrix four times larger.

    The industry generally relies on two approaches to ease this pressure: compressing the KV representation and sparsifying attention computation. The first focuses on how much is stored per token; the second focuses on how much each token actually attends to. A combination that has been repeatedly validated over the past two years is Multi-head Latent Attention (MLA), which compresses the KV cache into a low-rank latent space, together with sparse attention approaches such as DeepSeek Sparse Attention (DSA), which let each query interact only with the top-k tokens selected by an indexer. A newer architecture builds on this foundation with a further engineering optimization: it makes index computation itself a resource that can be reused across layers. This is the problem that cross-layer index reuse (IndexShare) addresses.

    2. Mechanism Breakdown: From MLA to DSA to Cross-Layer Index Reuse

    2.1 Multi-head Latent Attention: Compressing the KV Cache

    The core idea of MLA is not to cache the full key and value directly. Instead, it caches a low-dimensional latent vector and reconstructs the per-head key/value through an up-projection matrix. The benefit is a substantial reduction in memory usage, while the low-rank constraint also provides a degree of information-bottleneck regularization. For long-context inference, the KV cache is often the main consumer of GPU memory, so compressing the KV cache directly increases the supported concurrency and sequence length.

    2.2 Sparse Attention and the Indexer: Computing Only What Is Necessary

    The key to sparse attention is deciding what to attend to. A typical implementation includes a lightweight indexer that scores each query and selects the top-k most relevant historical tokens. Attention then performs a weighted sum over only those k positions. The indexer itself is far cheaper than full attention, but it still needs one lightweight interaction with all historical tokens. When it runs at every layer, the accumulated overhead becomes non-negligible at the million-token scale.

    2.3 Cross-Layer Index Reuse: From Once per Layer to Once per Four Layers

    The logic behind cross-layer index reuse is straightforward: the attention targets of adjacent layers are highly similar, so there is no need to redo top-k selection at every layer. The indexer is therefore run less frequently — for example, once every four layers — and the next three layers directly reuse the already selected token indices. This preserves the original premise of sparse attention, where each query attends to only k tokens, while reducing the total cost of the indexing stage to roughly one quarter of the original (estimated per group of four layers).

    It is worth stressing that this is not crude pruning that trades accuracy for fewer computations. It rests on an empirical assumption about the similarity of attention distributions across layers. In practice, it is usually combined with a few corrective measures, such as retaining local window tokens, forcibly including sink tokens, and slightly expanding the index on reuse layers, in order to prevent error from accumulating across layers.

    Illustrative pseudocode (expressing control flow only, not any specific product interface):

    1. # Compute the index once every four layers; reuse it on subsequent layers
    2. if layer_idx % 4 == 0:
    3. topk_idx = indexer(hidden_states, q) # Lightweight scoring against the full history
    4. cached_idx[layer_group] = topk_idx
    5. else:
    6. topk_idx = cached_idx[layer_group] # Reuse directly
    7. attn_out = sparse_attention(q, kv_latent, topk_idx)

    3. Why This Technique Pays Off Most at 1M Tokens

    The cost structure determines where the gains appear. With short contexts, the indexer accounts for a very small share of the cost, so optimizing it is barely noticeable. At the million-token scale, the candidate set the indexer must score is extremely long, and with many layers and long sequences per batch, the indexing stage shifts from a rounding error to one of the major costs. Reducing indexing frequency by four times then removes a meaningful share of the total inference budget.

    Another gain comes from memory access. Index selection determines which KV blocks the subsequent attention must read at random, so reducing index computation lowers the number of scoring-related reads. This is especially friendly to inference clusters that are tight on memory bandwidth. In engineering practice, the selected token blocks are often rearranged into contiguous form to reduce fragmented random access.

    4. Engineering Implementation Notes

    1. Choosing the group size: Four is an empirical compromise. A larger group raises the risk to accuracy, while a smaller group yields insufficient gains. It is advisable to run small-scale comparison experiments with 1/4 and 1/2 as candidates.
    2. Mandatory retention set: Always retain the most recent window, the sink tokens at the start of the sequence, and the segments relevant to the current instruction, so that critical long-range information is not dropped by sparsification.
    3. Separate KV cache and index cache: Keep the latent KV cache resident in GPU memory, and store the index cache per layer group. Coordinate with paged cache management to avoid fragmentation.
    4. Training and inference consistency: If index reuse is enabled only during inference, the train-inference distribution gap must be evaluated. A safer approach is to introduce the same reuse pattern during training.
    5. Observability: Monitor the index hit rate, the positional distribution of selected tokens, and the output difference between reuse layers and compute layers (such as cosine similarity), as early signals of accuracy regression.

    5. Suggested Validation Methods

    • Baseline comparison: Compare three setups — full attention, per-layer sparsity, and cross-layer reuse — with fixed random seeds and data.
    • Long-range dependency evaluation: Use tasks that require reasoning across long distances, and observe how much recall drops due to reuse.
    • Cost quantification: Measure indexing time, attention time, and KV read volume separately, so that looking only at total latency does not hide structural changes.
    • Load testing shapes: Cover both a single request with an extremely long context and multiple concurrent requests with medium-length contexts. The former reveals memory behavior; the latter reveals throughput.

    6. Conclusion

    The value of cross-layer index reuse does not lie in proposing a new attention paradigm. It lies in a cost-effective engineering refactor of the existing sparse attention pipeline: amortizing highly repetitive index computation across multiple layers, so that million-token context inference costs are brought into an acceptable range while model behavior remains stable. For teams deploying long-document and code-repository understanding scenarios, this kind of “same structure, optimized scheduling” approach is often easier to turn into deterministic gains than switching models, and it is also easier to roll out incrementally and validate with canary releases on existing inference frameworks.