Cross-Layer Index Reuse for Ultra-Long-Context Sparse Attention

    Cross-layer index reuse reduces the overhead of dynamic sparse attention by running a full top-k indexer at fixed layer intervals and sharing the selected token indices across subsequent layers. This article explains the architecture, execution flow, implementation requirements, and quality trade-offs, including masking, incremental decoding, training consistency, and tuning for million-token contexts.

    Cross-Layer Index Reuse for Ultra-Long-Context Sparse Attention

    1. The Problem: Indexer Overhead in Ultra-Long Contexts

    In inference with million-token contexts, attention bottlenecks extend beyond computing the attention weight matrix. Selecting candidate tokens for sparse attention can also impose substantial overhead.

    Dynamic sparse attention typically scores queries against keys, selects the top-k most relevant tokens, and computes attention only over those tokens. This substantially reduces attention computation, but the indexer still has to score and rank a large candidate set. If every model layer runs a full indexer independently, the number of indexer calls grows linearly with model depth. For long sequences, batched requests, and incremental decoding, the resulting compute and memory-access costs can become significant.

    Adjacent Transformer layers often show strong correlations in their assessments of token importance. Many layers do not need to rediscover important tokens from scratch; they can work with candidates identified by preceding layers.

    Cross-layer index reuse builds on this observation. Rather than changing the basic sparse attention operation, it replaces independent per-layer index selection with selection shared within layer groups, reducing repeated indexer overhead.

    2. Architecture: Combining Compressed Representations with Dynamic Sparse Selection

    A multilayer sparse attention architecture can combine two complementary capabilities:

    • KV cache compression: A multi-head latent attention variant uses low-rank latent representations to reduce KV cache size and memory-access pressure in long-context processing.
    • Dynamic sparse attention: A top-k indexer selects a small subset of important tokens from a long sequence for attention computation.

    The first addresses oversized caches; the second addresses overly dense attention.

    Cross-layer index reuse operates within the index-selection stage of dynamic sparse attention. It reuses neither attention weights nor the KV cache. Instead, it reuses the indices identifying which tokens have been selected.

    Subsequent layers still compute attention weights using their own queries and keys, but their candidate token sets come from a full indexer run in an earlier layer. This reduces indexer calls while retaining layer-specific attention computation.

    3. How Cross-Layer Index Reuse Works

    A typical design divides consecutive layers into groups. The first layer in each group runs the full indexer: it scores queries against candidate keys and selects the top-k token indices. The remaining layers reuse that index set instead of running their own full indexers.

    With a reuse interval of four layers, for example, the full indexer runs once per group, and the next three layers reuse its selected indices. For a model with L layers and a reuse interval of N, the number of full indexer calls drops from L to approximately ceil(L / N).

    Several boundaries must be explicit:

    • Indices are reused, not attention scores. Each layer still computes its own attention weights.
    • Reuse is primarily across layers. Indices may be shared across heads or maintained independently, depending on the implementation.
    • Indices must remain compatible with the current layer. This includes its KV layout, positional encoding, and mask.
    • Incremental decoding requires updates. When new tokens arrive, the index set must be refreshed according to an update policy rather than reused indefinitely without adjustment.

    4. Execution Flow and Pseudocode

    A simplified forward pass checks whether the current layer is scheduled to run the full indexer. If so, it computes scores, selects top-k indices, and stores them in the layer-group cache. Otherwise, it reads the cached indices. Each layer then performs its own sparse attention computation.

    1. def forward_layers(layers, hidden_states, interval=4):
    2. cached_indices = None
    3. outputs = []
    4. for layer_idx, layer in enumerate(layers):
    5. q, k, v = layer.project_qkv(hidden_states)
    6. need_full_index = (layer_idx % interval == 0) or (cached_indices is None)
    7. if need_full_index:
    8. scores = layer.index_score(q, k)
    9. cached_indices = top_k(scores, k=layer.topk, mask=layer.valid_mask)
    10. else:
    11. # Reuse token indices within the layer group,
    12. # but still compute this layer's attention weights.
    13. cached_indices = align_indices(cached_indices, layer.valid_mask)
    14. selected_k = gather(k, cached_indices)
    15. selected_v = gather(v, cached_indices)
    16. attn_out = layer.sparse_attention(q, selected_k, selected_v)
    17. outputs.append(attn_out)
    18. hidden_states = layer.ffn(attn_out)
    19. return outputs

    A production implementation must also handle variable-length sequence padding, causal masks, chunked prefill, incremental decoding caches, and index invalidation policies.

    For example, chunked inference can retain global indices for historical chunks while refreshing local indices for new chunks. If a mask changes, invalid positions must be removed and the candidate set replenished to maintain the required top-k coverage. Otherwise, reused indices may leave too few valid candidates.

    5. Correctness and Quality Trade-Offs

    The benefit of cross-layer index reuse comes from reducing indexer calls. Its main risk comes from differences in token selection across layers.

    If token importance rankings change sharply in some layers, indices selected earlier may omit tokens that are critical to the current layer. This can affect long-document retrieval, multi-hop reasoning, and fine-grained localization tasks.

    Protective measures include:

    • Retaining tokens from a local attention window.
    • Retaining global summary tokens or tokens at designated special positions.
    • Monitoring overlap between reused indices and layer-specific selections.
    • Restoring full indexer execution at critical layers.

    Evaluation should compare index reuse against a baseline that runs the indexer at every layer. Relevant workloads include long-document question answering, understanding long code files, multilingual document processing, positional extrapolation, and incremental decoding.

    Task accuracy alone is not sufficient. Useful diagnostics also include top-k index overlap, candidate recall, attention entropy, and whether long-range dependencies are captured.

    A larger reuse interval N is not necessarily better. Ablation studies are needed to balance reduced indexer overhead against token-selection accuracy.

    6. Training and Inference Consistency

    With a fixed indexing interval during training, reuse layers do not update their indexer parameters through a local indexer execution, although their attention weights can still be learned.

    To reduce mismatches between training and inference, training can randomize the reuse interval or introduce index-sharing regularization. These approaches help the model tolerate small deviations in candidate sets.

    Inference should follow the layer-group structure, masking rules, and top-k policy used during training. For incremental decoding, the index-cache update logic must remain consistent with prefill behavior as new tokens are added. This helps avoid position misalignment and stale cached indices.

    7. Engineering and Tuning

    Cross-layer index reuse can be integrated with KV cache management, chunked attention, and batch scheduling.

    Several implementation choices can reduce overhead:

    • Organize index caches by layer group, sequence chunk, and head group to avoid repeated memory allocation.
    • Batch indexer calls across requests to improve accelerator utilization.
    • Fuse top-k selection with mask handling to reduce additional memory accesses.

    During tuning, monitor the proportion of runtime spent in the indexer, top-k overlap, cache hit rate, and quality on long-context tasks. Adjust the reuse interval incrementally based on these measurements.

    For million-token contexts, cross-layer index reuse does not replace chunking, retrieval augmentation, or positional extrapolation. It complements these techniques by reducing the recurring overhead of index selection.

    8. Applicability and Conclusion

    Cross-layer index reuse is best suited to ultra-long-context models where token importance remains relatively stable across layers and indexer execution accounts for a substantial share of runtime.

    If a task depends heavily on fine-grained per-layer routing, or token selections differ significantly between layers, use a shorter reuse interval or enable reuse only in selected layer groups.

    The central design is straightforward: run a full indexer at fixed layer intervals and reuse its top-k token indices within each group. Each layer continues to compute its own sparse attention weights, while index-selection compute and memory-access costs are reduced. This provides an interpretable, tunable engineering approach for improving the efficiency of million-token-context inference, subject to careful evaluation of selection quality.