Cross-Layer Index Sharing for Sparse Attention in Long-Context Inference
Cross-layer index sharing reduces the cost of sparse attention by computing top-k token indices at anchor layers and reusing them across subsequent layers. This article explains how the architecture works with multi-head latent attention, where it can save compute and memory bandwidth, and how to choose a sharing period without overlooking retrieval and generation quality.
1. The Bottleneck Is More Than Attention Compute
As large language model context windows grow from tens of thousands of tokens to a million or more, inference costs come from more than computing attention matrices. Compute, GPU memory capacity, and memory bandwidth all come under pressure. Common approaches include multi-head latent attention (MLA), sparse attention, blockwise computation, KV cache compression, and hierarchical caching. MLA uses a low-rank latent representation to reduce the cost of storing and reading keys and values. Sparse attention uses an indexer to select a small set of relevant tokens for each query, avoiding attention over the entire available history.
Sparse attention still has a cost: the indexer scores candidate keys against each query and performs top-k selection. When that work is repeated at every layer, scoring intermediates, top-k selection, and index cache traffic can consume substantial compute and bandwidth, especially at million-token context lengths.
Cross-layer index sharing addresses this overhead without removing sparse attention. Instead of deciding independently at every layer which tokens should participate, the model refreshes the selection periodically and reuses it across layers.
2. Architecture: Reusing Token Indices Across Layers
In an architecture that combines MLA with a sparse attention indexer, MLA compresses the key-value representations, while the indexer determines which token positions each query attends to. Cross-layer sharing changes where those positions come from, not how attention is computed over them. Each layer still uses its own queries, keys, and values; only the selected token positions are shared.
The layers are divided into groups. At the start of each group, an anchor layer runs the full indexer, scores candidate keys, selects the top-k token positions for each query, and caches them. The remaining layers in the group read those positions from the cache instead of running their own indexers. The next anchor layer computes a fresh selection.
Running the full indexer once every four layers is one example, not a fixed requirement. The sharing period is configurable. The trade-off is whether layers within a group select similar enough tokens for index reuse to be worthwhile.
3. How the Inference Workflow Works
- Split the input into blocks and prepare layer representations. Divide the long input into token blocks and prepare each layer’s queries, keys, and values. If latent compression is used, cache the key-value information in low-rank latent form to reduce GPU memory use.
- Run the full indexer at the anchor layer. Score candidate keys against each query and select the top-k token positions.
- Cache the selected positions. Store the indices for the current layer group. Depending on the cache layout, they can be represented as token ID lists, compressed bitmaps, or segmented offsets.
- Reuse the indices at subsequent layers. Non-anchor layers skip the full indexer and compute sparse attention using their own representations at the cached positions.
- Refresh the cache at the next anchor layer. Recompute the indices and replace or append cache entries for the next group.
- Combine the shared selection with other sparse patterns where needed. A sliding window, global tokens, or special separator tokens can supplement the shared top-k set to reduce the chance of missing important context.
for layer in model_layers:if layer % SHARE_PERIOD == 0:scores = indexer(query[layer], key[layer])selected = topk(scores, K)index_cache[layer // SHARE_PERIOD] = selectedelse:selected = index_cache[layer // SHARE_PERIOD]output[layer] = sparse_attention(query[layer],key[layer],value[layer],selected)
The indexer runs only at anchor layers. The attention path remains sparse throughout: layers that reuse indices still attend only to selected tokens rather than falling back to dense attention.
4. Why Index Sharing Can Lower Million-Token Inference Costs
With a sharing period of N, the model runs the full indexer roughly one-Nth as often as it would with per-layer indexing. That reduction applies to indexer calls, not to total inference cost. In deep models with long contexts, fewer calls mean less score computation, fewer intermediate scores to store or move, fewer top-k operations, and fewer cache updates. At million-token context lengths, memory bandwidth is often more constrained than floating-point throughput, so reducing this traffic can help improve end-to-end throughput.
Index reuse can also improve cache locality. Multiple layers read the same selection instead of generating a new one at each layer, reducing cache churn. In batches with widely varying sequence lengths, reuse can simplify scheduling and make blockwise sparse attention kernel launches more consistent.
The architecture still uses sparse attention and leaves the KV compression path intact. For systems that already combine MLA with a sparse indexer, cross-layer sharing is a configurable way to reduce indexing overhead rather than a replacement for the underlying attention architecture.
5. Trade-Offs and Risks
Index sharing assumes that layers within a group tend to select similar tokens. When their attention patterns differ, the anchor layer’s selection may omit tokens that a later layer would have retrieved on its own. This can weaken long-range dependency modeling and affect generation quality.
- Attention patterns can drift across layers. Earlier layers may emphasize local syntactic features, while deeper layers may focus on semantic or task-relevant tokens. A longer sharing period can make these differences more consequential.
- Queries with narrow retrieval needs can lose recall. A query that must retrieve a specific token may be better served by per-layer indexing.
- Training and inference can diverge. Enabling index sharing only at inference time may introduce a distribution shift. Validate perplexity, retrieval recall, and downstream task metrics before deploying it.
- Refresh frequency affects both cost and quality. Short periods offer limited savings, while long periods increase the risk of missed tokens. Compare settings across model depths, task types, and context lengths.
- Cache entries must remain isolated by request. In batched inference, one request must never read another request’s indices. Cache lifecycle management is especially important when batch membership changes dynamically.
6. Implementation Recommendations
Treat the sharing period and anchor positions as configuration rather than hard-coded constants.
- Configure the period by layer range. For example, use shorter periods in earlier layers and longer ones in deeper layers if evaluation supports that choice.
- Choose anchors based on cross-layer similarity. Favor layers that produce useful selections and whose selected positions are representative of what subsequent layers need.
- Keep the index cache compact. Segmented offsets, bitmaps, and compressed lists are options for reducing the bandwidth required to read top-k selections.
- Measure quality alongside latency. Average latency alone can hide regressions. Evaluate recall on long-tail requests, generation consistency, and task accuracy.
- Include index sharing in capacity planning and monitoring. For cloud deployments, account for the sharing period, anchor placement, and cache strategy. Monitor index cache hit rates, the fraction of tokens used by sparse attention, and GPU memory bandwidth utilization.
7. When to Use Cross-Layer Index Sharing
Cross-layer index sharing is best suited to workloads such as long-document question answering, repository-scale code understanding, long-log analysis, and multi-turn conversations, especially when indexing accounts for a substantial share of inference cost. For tasks that depend on precise token-level retrieval or are highly sensitive to recall, use it cautiously. Shorter sharing periods, local windows, and global tokens can help reduce the risk of missed context.
The core trade-off is straightforward: use similarity across layers to avoid repeating token selection. Periodic refreshes preserve the sparse attention path while spreading indexing costs across several layers. Whether that trade-off pays off at million-token context lengths depends on the sharing period, anchor placement, cache design, and quality validation.
