Cross-Layer KV Sharing: Reducing GPU Memory Use for Long-Context LLMs
Sharing key and value tensors across transformer layers can reduce long-context KV cache memory and complement Grouped Query Attention. This article explains how cross-layer sharing works, its training requirements and performance trade-offs, and what to validate before deploying it on local devices or cloud infrastructure.
Why Long Contexts Exhaust GPU Memory
During autoregressive generation, a model caches the key and value tensors for each processed token. When generating a new token, it reads these cached tensors instead of recomputing them. This speeds up generation, but the memory required grows linearly with context length.
KV cache size can be estimated as follows:
KV cache bytes per token = 2 × number of layers × number of KV heads × head dimension × bytes per value
Consider a model with 32 layers, 8 KV heads, a head dimension of 128, and FP16 values at 2 bytes each. Its KV cache requires approximately 128 KiB per token. At a context length of 128K tokens, the cache alone occupies roughly 16 GiB, excluding model weights and intermediate activations.
These figures are illustrative. Actual memory use depends on the model architecture and inference framework.
What GQA Already Optimizes—and Why It Is Not Enough
In standard Multi-Head Attention (MHA), each query head has its own key and value heads. Grouped Query Attention (GQA) groups multiple query heads so that each group shares one pair of KV heads. Reducing the number of KV heads reduces KV cache size proportionally.
Multi-Query Attention (MQA) takes this approach to its limit: all query heads share a single KV head.
GQA is common in modern large language models, but it reduces the number of KV heads only within individual layers. As contexts grow longer, there is limited room for further reductions within a layer. Reducing the KV head count further can also introduce more noticeable quality degradation. This has motivated research into sharing KV tensors across layers.
| Approach | KV heads per layer | KV cache size relative to MHA | Quality risk | Training requirements | Typical use cases |
|---|---|---|---|---|---|
| MHA | Same as the query head count | 1× baseline | No additional sharing-related risk | No additional requirements | Small and medium models with demanding quality requirements |
| GQA | One KV head pair per query-head group | Approximately KV head count ÷ query head count | Usually modest | Can be incorporated during pretraining | Mainstream large language models |
| MQA | One | Approximately 1 ÷ query head count | Often more noticeable | Generally incorporated during training | Throughput-sensitive serving |
| Cross-layer KV sharing | One KV set per group of sharing layers | Approximately 1 ÷ layers per sharing group, with GQA savings applied separately | Depends on the sharing design and training results | Requires training or fine-tuning for the sharing architecture | Long-context inference on memory-constrained edge or local devices |
The ratios are theoretical approximations. Actual savings also depend on cache implementation, memory alignment, and other GPU memory allocations.
How Cross-Layer KV Tensor Reuse Works
The basic approach divides adjacent layers into groups. The first layer in each group computes and stores the key and value tensors. Later layers in the group reuse those tensors rather than computing their own K and V. Each layer still computes its own queries and performs attention.
There is an important prerequisite: in a conventional pretrained model, each layer has learned its own KV projections. Forcing later layers to reuse an earlier layer’s KV tensors at inference time changes the inputs those layers receive and will typically cause substantial quality degradation.
Cross-layer sharing therefore requires either training with the sharing architecture from the outset or dedicated fine-tuning of an existing model. It is not an inference-time optimization that can simply be applied to ordinary model weights.
Public research has explored several approaches. Cross-Layer Attention (CLA), introduced in 2024, investigated sharing KV projections across adjacent layers. YOCO (You Only Cache Once), also introduced in 2024, explored a decoder architecture that caches KV once for reuse by later layers.
These designs differ. When evaluating a specific model, consult its paper, configuration files, and model card to establish which layers share KV and the granularity of that sharing.
Effects on Prefill and Decode
Cross-layer sharing affects the two inference stages differently:
- Prefill—processing the input context: Layers that reuse KV skip the K and V projection matrix multiplications and the corresponding cache writes. However, they still compute queries and attention scores. The reduction covers the KV projection work, not half of the model’s total computation.
- Decode—generating tokens one at a time: Each new token must still attend to all historical tokens. The main savings are KV cache storage and, depending on the implementation, the amount of data read from GPU memory at each step. When decoding is memory-bandwidth-bound, reduced memory traffic can improve speed. The relationship between attention computation and context length remains unchanged.
For a 32-layer model, sharing one KV set between every two layers reduces the number of cached layer-specific KV sets from 32 to 16, approximately halving KV cache memory. If the model also uses GQA, the two optimizations can be combined.
Considerations for Different Model Architectures
Different model architectures have different constraints, so cross-layer sharing should be evaluated separately for each.
Lightweight Models for Mobile and Embedded Devices
These models have particularly tight memory budgets, and KV cache size is often a major constraint on long-context support. Cross-layer sharing can reduce cache memory, but smaller models also have limited representational capacity. Any loss in capability needs to be assessed through evaluation.
Mixture-of-Experts Models
In Mixture-of-Experts (MoE) models, expert parameters can account for much of the memory footprint. Their size is independent of the KV cache.
Cross-layer sharing reduces only the attention-related KV cache. Its effect on total memory therefore depends on the cache’s share of overall memory use. When expert weights dominate, the total savings may be limited.
Dense Models
Dense models generally have established training and post-training workflows. Changing their attention architecture may affect compatibility with existing fine-tuning data, alignment methods, and inference interfaces, requiring renewed validation.
Teams that prioritize model quality and straightforward post-training should assess whether the benefits justify the cost of retraining.
When Cross-Layer KV Sharing Is—and Is Not—a Good Fit
Cross-layer sharing is a good candidate when:
- The target context is long, and the KV cache accounts for a large share of GPU memory use.
- A model supporting the architecture is available, or the team can train or fine-tune one.
- Deployment memory is constrained, such as on edge devices or single-GPU local servers.
It is generally a poor fit when:
- Only a conventionally trained model is available, with no plan to retrain or fine-tune it.
- Contexts are short and the KV cache is too small for the savings to change deployment requirements.
- The task requires highly precise recall, such as verbatim quotations from long documents, and quality regression testing has not been completed.
Cloud Deployment Considerations
The inference engine must support the target model’s KV cache layout. Cross-layer references require suitable memory-access handling; an inefficient implementation can offset the benefits of a smaller cache.
GPU scheduling and inference-serving infrastructure can host these models, but support for a particular architecture must be checked against the platform’s documentation and release notes.
If using a platform such as Baidu AI Cloud, first verify that its inference service supports the target architecture. Then select GPU configurations for load testing. Tests should cover the actual maximum context length and expected concurrency, rather than relying only on short-text examples.
Pre-Deployment Validation Checklist
- Model architecture: Confirm which layers share KV, how many layers are in each sharing group, and which layers retain their own KV cache.
- Memory measurement: Record memory use for model weights, KV cache, and activations separately. Include batch size, context length, and data type.
- Quality regression: Compare against the original model or a baseline on long-context retrieval, reasoning problems, and tool-calling examples.
- Throughput: Measure prefill and decode tokens per second separately, recording batch size and concurrency.
- Long-context limits: Test at the maximum target context length to verify that both peak memory use and output quality meet requirements.
- Rollback: Keep the original model available so that the deployment can be switched back quickly if problems arise.
Conclusion
Cross-layer KV tensor reuse is an effective way to reduce KV cache memory, and it can complement GQA. However, it depends on a purpose-built architecture and an appropriate training process; it cannot simply be applied to a conventional model at inference time.
Adoption decisions should be based on measured memory use, quality regression results, and throughput testing—not theoretical cache reduction ratios alone.
