Cross-Layer KV Reuse: An Architecture Approach to Memory Compression for Long Contexts

    As the demand for long-context large language models grows, the KV Cache often becomes the primary bottleneck for GPU memory consumption. This article explores Cross-Layer KV Reuse, an architectural approach that reduces memory usage by sharing Key and Value tensors across multiple Transformer decoder layers. We cover the core concepts, two main implementation paths (compact vs. sparse), the evolution from MQA and GQA, critical engineering challenges for production deployment, and future research directions in memory compression.

    1. Why is the KV Cache Consuming GPU Memory?

    In autoregressive generation, every Transformer decoder layer must store Keys and Values for each generated token. These cached tensors are known as the KV Cache. Its memory footprint can be estimated using the following formula:

    Memory = 2 × Number of Layers × KV Heads × Head Dimension × Sequence Length × Bytes per Element

    The coefficient of 2 accounts for storing both K and V. The longer the sequence length, the larger the cache, growing linearly with the context.

    Let’s calculate an example using a hypothetical configuration: 128 layers, 96 attention heads, a head dimension of 128, a context length of 128K tokens, and FP16 precision (2 bytes per element).

    Scheme Layers Storing KV KV Heads Estimated KV Cache
    Standard MHA 128 96 ~768 GiB
    GQA (4 KV Heads) 128 4 ~32 GiB
    GQA + Cross-Layer Reuse (Shared KV every 2 layers) 64 4 ~16 GiB

    These numbers are based on the above assumptions and only illustrate the order of magnitude. Real-world models must also account for weights, activations, and framework overhead, so actual memory usage should be profiled on the target model.

    2. The Basic Concept of Cross-Layer KV Reuse

    In a standard Transformer, each layer independently computes and stores its own KV, with no sharing between layers. Cross-Layer KV Reuse breaks this isolation. The model divides layers into several groups. Within each group, one layer is responsible for computing the KV, while the remaining layers directly read this shared KV and only use their own Queries to perform attention computation.

    This reduces the number of KV copies that need to be saved from the total number of layers (L) down to the number of layers that actually generate KV. Since the hidden representations of adjacent layers are typically similar, reuse is feasible to some extent. However, the degree of similarity varies by model and task. How many layers to reuse and which specific layers to reuse must be determined through experimentation.

    3. Two Implementation Paths: Compact and Sparse

    In practice, cross-layer reuse is generally categorized into two approaches. These labels are common in engineering practice rather than standardized terminology.

    The Compact approach establishes the reuse relationship during the architecture design phase. For instance, Layer 1 computes the KV, Layer 2 reuses Layer 1’s KV, Layer 3 recomputes, and Layer 4 reuses Layer 3’s KV. Its structure is fixed, incurring no additional routing overhead during inference, which makes implementation and deployment relatively simple. The trade-off is that the KV source for each layer is fixed, limiting the flexibility of cross-layer information flow.

    The compact approach does not necessarily require extra parameters. If the differences between inter-layer representations are small and the reuse span is short, KV can be directly reused. When the reuse span increases and inter-layer differences become prominent, a lightweight projection layer (e.g., a small linear layer) can be added along the reuse path to map the source layer’s KV into the current layer’s representation space. Projection layers add parameters and computational overhead, requiring a simultaneous evaluation of both quality and memory changes.

    The Sparse approach does not force every layer to reuse. It uses learnable routing or attention masks to let the model decide which layers share KV and which compute independently. This method is more flexible and can theoretically retain more quality under similar memory constraints. However, training is more complex and usually requires auxiliary losses to guide routing decisions. Dynamic routing during inference can also introduce latency and complicate batching and operator implementation.

    Choosing which path to take depends on training resources, serving latency requirements, and the tolerance for model quality degradation. When resources are limited and stable deployment is required, the compact approach is easier to operationalize.

    4. Evolution from MQA to Cross-Layer Reuse

    Multi-Query Attention (MQA) was proposed by Noam Shazeer in 2019. Its core idea is that all query heads share the same set of Keys and Values. It predates GPT-3 (released in 2020), which used standard Multi-Head Attention (MHA). MQA drastically reduces the KV Cache but can lead to quality degradation on certain tasks.

    Grouped-Query Attention (GQA) was proposed by Ainslie et al. in 2023. It divides query heads into several groups; heads within a group share KV, while heads across groups remain independent. GQA strikes a good balance between quality and efficiency and has been adopted by several mainstream open-source models.

    Cross-layer reuse operates on a different dimension. MQA and GQA reduce the number of KV copies along the head dimension, while cross-layer reuse reduces it along the layer dimension. Their compression targets are different and can theoretically be stacked. Existing papers have proposed the concept of Cross-Layer Attention (CLA) and reported results of combining it with MQA and GQA. Such conclusions depend on specific models and training setups, so validation on your own model is necessary before application.

    Cross-layer reuse also introduces a problem not present in MQA and GQA: inter-layer representation differences. Shallow layers tend to capture local syntactic patterns, while deeper layers handle global semantic relationships. Forcing deep layers to use shallow layers’ KV directly may introduce mismatched information, leading to an increase in perplexity. The projection layers mentioned earlier are designed specifically to mitigate this issue.

    5. Key Engineering Challenges for Production

    1. Selecting Reuse Layers. Shallow layer representations change rapidly, making reuse riskier. Middle and deep layer representations are relatively stable and more suitable for reuse. Which specific layers can be reused should be tested layer-by-layer for quality changes on a validation set, rather than applying a fixed scheme.

    2. Training and Fine-tuning. Cross-layer reuse typically needs to be established during the training phase. Applying it directly to an already pre-trained standard model often leads to noticeable quality degradation. In such cases, continued training or distillation is required to recover quality. During fine-tuning, the training and inference must use the same structure to avoid behavioral inconsistencies.

    3. KV Quantization. Shared KV is read by multiple layers, meaning quantization errors will accumulate across these layers. There is no universally optimal quantization scheme (symmetric vs. asymmetric, i.e., scale or zero-point). It should be recalibrated on a model incorporating cross-layer reuse, comparing schemes based on perplexity and target task metrics. One should not assume in advance which method is better.

    4. Inference-time Cache Management. The lifecycle of a shared KV depends on when all layers referencing it have finished their computations. The corresponding memory can only be released after all references are resolved. Therefore, the cache manager needs to track reference counts. If using paged caching, the page table must allow multiple layers to point to the same physical page and check references before release. Otherwise, premature or duplicate releases may occur, leading to data corruption. This usually requires custom implementation or modification of existing inference frameworks.

    5. When Not to Use Cross-Layer Reuse. Careful evaluation or postponement is advised under the following circumstances:

    • The model is already in production and cannot be retrained or distilled.
    • The task is highly sensitive to precision (e.g., complex reasoning or precise information extraction) and validation results fail to meet standards.
    • Inter-layer representation differences are large, and no suitable projection scheme is available.
    • Latency introduced by dynamic routing exceeds serving requirements.
    • Current memory is sufficient, and the compression gains do not offset the engineering complexity.

    6. Research Directions and Maturity

    Research into cross-layer reuse is still expanding. One direction involves extending the shared objects from KV to attention weights or intermediate hidden states. Most of this work is currently in the exploratory research phase. Attention weights have different semantic and computational constraints compared to KV, so conclusions from KV sharing cannot be directly applied; quality and stability must be verified independently.

    Another concept is dynamically adjusting the reuse strategy based on input complexity—for example, reusing more for simple inputs while retaining more independent KV for complex, long documents. This direction is attractive but lacks large-scale, reproducible validation, so it should still be considered a conceptual idea for now.

    Cross-layer KV reuse is an architectural-level compression technique. It is complementary to methods like KV quantization and cache eviction and can be used in combination. However, every combination must be re-evaluated on the target model and tasks.