Cross-Layer KV Cache Reuse: A Guide to Long-Context Memory Optimization for Small Models
Autoregressive decoding is memory-bandwidth bound, and the KV cache is often the single largest consumer of memory on mobile and embedded (IoT) devices. This article examines cross-layer KV tensor sharing—an approach where only a subset of layers actually compute and store key/value tensors while the remaining layers reuse them—as a way to shrink KV cache memory along the layer dimension. It walks through the memory formula, the common sharing granularities, realistic memory and latency gains, the trade-offs in model expressiveness, the training-side prerequisites, the inference-framework changes involved, and a practical rollout checklist, and it closes with guidance on when the technique is actually worth adopting.
1. Where the Problem Starts: The Cost Structure of Long-Context Inference Has Changed
Autoregressive inference has two phases. Prefill is compute-intensive and bound mainly by compute. Decode generates one token at a time, and every step reads back the entire history, so it is memory-bound and limited primarily by memory bandwidth. What actually overwhelms mobile and embedded (IoT) devices is the KV cache.
Its footprint can be approximated as:
KV_bytes ≈ 2 × L × H_kv × d_head × S × B × bytes_per_element
Here 2 accounts for the two tensors, Key and Value; L is the number of layers; H_kv is the number of KV heads; d_head is the per-head dimension; S is the accumulated sequence length; and B is the batch size. This expression tells us three things: the footprint scales linearly with context length, with the number of layers, and with the number of KV heads.
For on-device hardware, available memory is often only a few hundred MB to a few GB, and it must be shared with the OS, the graphics stack, and other apps. As the context grows from 2K to 32K, KV cache usage can increase more than tenfold, easily exceeding the size of the model weights themselves.
The industry generally compresses the KV cache along three directions:
- Compressing the head dimension: MQA (all query heads share one set of KV) and GQA (grouped sharing);
- Compressing precision: quantizing KV to int8 / fp8 or even lower bit-widths;
- Compressing the layer dimension: cross-layer KV tensor sharing.
This article focuses on the third direction. It is not new—academia proposed similar ideas years ago—and it has recently been picked up again by several teams and incorporated into the design of on-device small-model families.
2. Core Mechanism: Cross-Layer KV Tensor Reuse
In a standard decode layer, every layer independently computes its own K and V for the current token and maintains its own history cache. But from a representational standpoint, the K/V tensors of adjacent layers are highly correlated statistically: research has repeatedly observed that even bypassing the KV computation of some layers affects the final output far less than intuition suggests, and the attention of certain layers even resembles an identity transform that mainly “moves information around.”
The cross-layer reuse approach works like this: only a subset of layers (usually the first few) actually compute and write to the cache, while the remaining layers read the K/V of the shared upstream layers directly during attention.
There are two common granularity choices:
- Global sharing: all layers use the KV of the first layer (or the first k layers). This gives the largest benefit but also carries the highest quality risk.
- Grouped sharing: layers are divided into several groups, sharing within a group and staying independent across groups, using an adjustable sharing granularity to balance quality against memory.
Illustrative pseudocode:
# Prefill phase: only kv_owner layers write to the cachefor layer_id, layer in enumerate(layers):x = layer.input_norm(x)if layer_id in kv_owner_layers: # e.g. {0} or {0, 1, 2}k, v = layer.compute_kv(x) # [B, H_kv, S, d_head]kv_cache.store(layer_id, k, v)else:k, v = kv_cache.load(layer.share_from) # reuse the KV of its groupx = x + layer.attention(x, k, v)x = x + layer.mlp(x)
The change at decode time is where the real benefit comes from: because only a few “KV owner layers” need to append writes, the write bandwidth and write compute of a single decode step drop accordingly.
One common misreading needs clarifying: what is shared is only the cache tensors. The Query is still computed independently per layer, and each layer’s projection matrices, output projections, and feed-forward networks remain independent. So model capacity does not shrink proportionally with the number of layers—which is exactly why this approach is preferable to simply deleting layers.
3. The Magnitude of the Benefit: Doing the Math
Replace L with the actual number of layers that write to the cache, L_shared, and memory usage drops proportionally. In theory, single-layer sharing can reduce it to roughly 1/L (about 3% at the theoretical limit with 32 layers), but in practice a few owner layers are usually kept for grouping, so the realized benefit falls back into the 1/2 to 1/4 range—with noticeably smaller quality loss.
Beyond memory, there are three benefits that are easy to overlook:
- Decode memory bandwidth: each step reads only one shared copy of KV, which directly improves TPOT (time per output token);
- Cache reusability: shared blocks naturally fit prefix caching and can hit both within a batch and across requests;
- Long-context feasibility: once memory pressure eases, the same device can support longer windows or larger batch sizes.
4. The Costs and Engineering Challenges
- Constrained expressiveness. The number of layers is the model’s “depth capacity,” and forcing K/V to converge weakens the division of labor between layers. On tasks that require fine-grained information routing—multi-step reasoning, code generation—the degradation is usually more pronounced than on short text Q&A.
- Training-side coordination is mandatory. Cross-layer sharing is not a switch you can simply flip at inference time; the model must be adapted to this topology during pretraining or at least post-training, or accuracy collapses. This is also why it tends to appear in the design of new model families rather than being bolted onto older models.
- Attention sinking and position extrapolation. When fewer KV tensors must carry the information of all layers, the importance of early tokens rises; combined with sliding-window attention, the window length and the retention policy for global tokens must be designed carefully.
- Inference framework changes. The cache index shifts from (layer, head) to (group, head), which touches reference counting, the layer mapping of paged blocks, memory-pool management, and batch scheduling logic.
- Ecosystem compatibility. The quantization pipeline, distillation and fine-tuning scripts, model export formats, and inference-accuracy alignment tests all need to be adapted to the new topology.
5. A Practical Rollout Checklist
- Establish a baseline first. Record peak memory, TPOT, and throughput at different context lengths, along with accuracy baselines on long-text tasks, so that later changes can be attributed.
- Change things incrementally. Start with grouped sharing (for example, sharing once every 4 layers), and use ablation experiments to determine the placement and number of owner layers. Do not jump straight to single-layer sharing.
- Align training. Mix in longer-context samples during continued pretraining or instruction fine-tuning so the model learns to work under limited KV; if necessary, apply loss weighting to the owner layers.
- Stack orthogonal techniques. KV quantization, sliding-window attention with a few global tokens, sparse attention, and speculative decoding can all be combined, but errors accumulate, so each must be ablated individually rather than enabled all at once.
- Coordinate on the serving side. Enable prefix caching to reuse the shared blocks; dynamically adjust batch size and maximum context length based on remaining memory so that long requests do not drag down overall latency.
- Build a regression dashboard. Put memory, latency, and quality metrics side by side in a single table rather than looking only at single-point conclusions.
6. How to Decide Whether It Is Worth Doing
Three questions can point the way:
- Is the bottleneck capacity or compute? If it is mainly a compute bottleneck, the gains are limited.
- Is the scenario “single request, long context, small batch”? On-device long conversations, document Q&A, and long-trajectory tasks benefit the most; high-concurrency short-request scenarios dilute the benefit.
- Do you have training or fine-tuning capability? If not, you can only choose models that natively support this structure, leaving little room for modification.
At its core, cross-layer KV tensor reuse is about “trading model capacity for memory and bandwidth.” It compresses the layer-related dimension of the KV cache, complementing rather than replacing MQA/GQA (which compress the head dimension) and KV quantization (which compresses the precision dimension). For on-device and edge inference, this kind of structural design is often more valuable than pure quantization tuning—because it changes the cost formula itself, not just a coefficient within it.
