Cross-Layer KV Cache Reuse for Long-Context LLM Inference: Memory and Bandwidth Optimization

    This article explains why the KV cache becomes a primary bottleneck for long-context LLM inference and introduces cross-layer KV cache reuse as a layer-dimension optimization. It covers the core idea of sharing KV states across layers, grouped sharing implementations, combination with grouped-query attention, training requirements, trade-offs between quality and compression, and practical deployment recommendations for edge, local, and long-context serving scenarios.

    1. Why KV Cache Becomes a Bottleneck in LLM Inference

    In the inference process of autoregressive generative large models, inference frameworks typically cache the Key and Value tensors of each layer to avoid recomputing the attention states of historical tokens. This is the KV cache. Its size can be estimated with a simple formula:

    1. KV cache memory ≈ 2 × layers × sequence length × heads × head dimension × precision bytes × batch size

    As the formula shows, KV cache usage scales linearly with the number of layers and the context length. When the context length grows from 4K to 128K or even longer, the memory overhead of the KV cache can exceed the model weights themselves, becoming the first bottleneck limiting long-context inference. The symptoms are concrete:

    • Memory wall: a single GPU may not be able to hold the cache required for long contexts, forcing multi-GPU parallelism or CPU offloading, which increases both latency and cost;
    • Throughput drop: the larger the cache, the fewer requests that can be processed concurrently, limiting overall serving throughput;
    • Memory bandwidth pressure: attention computation is a typical memory-bound operation. The larger the cache, the more data must be moved per decoding step, slowing generation.

    The industry has developed several optimization routes around this problem: KV quantization and compression, sliding-window attention, grouped-query attention (GQA), multi-head latent attention (MLA-style) compression, and others. This article focuses on cross-layer KV reuse, an approach that targets the layer dimension.

    2. Core Idea of Cross-Layer Reuse

    A traditional Transformer maintains an independent set of KV states for every layer. However, many observations show that attention patterns and Key/Value representations are highly similar between adjacent layers. Given this, later layers do not need to recompute and cache a nearly identical set of tensors from scratch.

    The basic approach of cross-layer reuse is to let later layers directly reuse the Key-Value states already computed by earlier layers. Layers are divided into groups. Within each group, only the first layer fully computes and writes the KV cache, while the remaining layers share that cache. In the extreme case, this reduces the number of effective cached layers to a fraction of the original, and both memory usage and decode-time memory traffic drop proportionally.

    It is worth noting that this idea is not the exclusive invention of any single team. Academic research has systematically discussed cross-layer attention sharing, for example, papers that reduce KV cache size through cross-layer attention mechanisms. Its value lies in achieving substantial resource savings with very small structural changes. More recently, the open-source model ecosystem has begun to bring such mechanisms into mainstream open-weight architectures at scale, moving them from papers into engineering practice.

    3. Typical Implementations

    3.1 Grouped Sharing

    The most common form is to divide the model’s layers into groups and share one KV cache within each group. If a model has L layers and every K layers form a group, the number of cached layers drops from L to L/K, and the memory benefit is approximately linear.

    Illustrative pseudocode:

    1. # Pseudocode: inference loop with cross-layer KV sharing
    2. shared_kv = {} # written only by the first layer in each group
    3. for i, layer in enumerate(layers):
    4. if i % K == 0:
    5. kv = layer.compute_kv(hidden_states) # compute fully and cache
    6. shared_kv[i // K] = kv
    7. else:
    8. kv = shared_kv[i // K] # later layers reuse directly
    9. hidden_states = layer.attention(hidden_states, kv)

    3.2 Combination with GQA

    Cross-layer reuse can usually be combined with grouped-query attention. GQA reduces the cache along the head dimension, where multiple query heads share one set of KV heads, while cross-layer reuse reduces the cache along the layer dimension. The two are orthogonal, and their combination can provide considerable compression. This is also a common configuration in several current small and medium-sized models targeting edge and local inference.

    3.3 Training-Side Requirements

    It is important to note that if a model is trained from scratch with a shared-KV structure, each layer can adaptively learn to use the shared representations, and quality loss can be kept under control. However, if sharing is introduced after the fact into an already trained dense model, additional structural modification and continued training are usually required; otherwise, quality degradation may be noticeable.

    4. Benefit-Cost Trade-offs

    No structural optimization is free. The trade-offs of cross-layer reuse are mainly reflected in the following aspects:

    1. Balance between quality and compression ratio: the higher the sharing factor (the smaller the group), the greater the memory benefit, but the higher the quality risk from differences in inter-layer representations. In engineering practice, parameter sweeps are needed between the target context length and quality metrics.
    2. Targeted benefit for long contexts: in short-context scenarios, the KV cache itself accounts for a limited share, so the benefit is limited. In long-document understanding, multi-turn long conversations, and similar scenarios, the benefit scales linearly with sequence length.
    3. Clear improvement in decode throughput: because the entire KV cache must be read at each decoding step, a smaller cache means lower memory traffic. This is especially friendly to bandwidth-constrained edge and local inference devices.
    4. Relatively small benefit in the prefill stage: prefill is one-time batched computation, and its workload is mainly determined by floating-point operations. Cache compression has a less direct impact on prefill than on decoding.
    5. Post-training friendliness: if a model with such structures also adopts complex architectures such as sparse experts, post-training and tuning become more difficult. Therefore, many model families provide both sparse variants with shared KV and dense variants without sharing, allowing users to choose as needed—the former for extreme inference efficiency, and the latter for quality and ease of tuning.

    5. Deployment Recommendations

    For developers planning to apply such models in their own business, the following points are worth attention:

    • Measure before optimizing: use the memory profiling tools included in the inference framework to confirm the real proportion of KV cache in total usage and determine the room for optimization;
    • Choose an appropriate context configuration: set the cache limit and paging policy based on real session lengths in the business, avoiding full cache reservation for rarely occurring ultra-long requests;
    • Combine with quantization and paged management: cross-layer reuse can be stacked with KV quantization and paged cache management to further reduce usage. Commonly used open-source inference engines already provide good compatibility with such structures;
    • Edge devices benefit first: on edge devices with limited memory and bandwidth, such structures offer the best cost-performance and are suitable for lightweight long-context scenarios such as voice assistants and local document question answering.

    6. Conclusion

    The essence of cross-layer KV cache reuse is to exploit redundancy in inter-layer representations and change the cache from independent per layer to shared within groups. This reduces the memory and bandwidth overhead of long-context inference by a multiple, with almost no increase in inference complexity. As mainstream open-source architectures include it as a standard configuration, this technology is moving from an academic idea to an engineering default. For teams that need to support long-context services under limited resources, understanding and making good use of such structures is one of the most cost-effective optimization paths at the current stage.