Layer-Wise KV Cache Partitioning: Reducing Inference Memory Redundancy
KV Cache grows linearly with sequence length during LLM inference, and naive Context Parallel implementations can replicate the full cache across every GPU. This article explains where that redundancy comes from, how LayerSplit reduces per-GPU persistent KV Cache to roughly 1/N by partitioning layers instead of sequences, and what cross-GPU broadcast, communication-computation overlap, and measurement practices are required to make it work.
Why Long-Context Inference Hits the Memory Wall
During autoregressive generation, every new token requires the Key and Value tensors produced by all previous tokens at every layer. The system stores these results as the KV Cache. It eliminates redundant computation, at the cost of memory that grows linearly with sequence length. As context windows expand from a few thousand tokens to hundreds of thousands or even millions, a single GPU often cannot hold the full inference state.
The industry commonly splits workloads with Tensor Parallelism (TP), Pipeline Parallelism (PP), and Sequence Parallelism (SP). Context Parallelism (CP) is a form of sequence parallelism that partitions very long sequences into segments distributed across GPUs. CP reduces per-GPU compute pressure, but whether it introduces KV Cache redundancy depends on the specific implementation.
Where the Redundancy Comes From
Not every CP implementation creates redundancy. In a replicated implementation, each GPU keeps the full-sequence KV Cache locally—or temporarily pulls it via operations such as All-Gather—so that attention can run over the complete sequence. In this case, a global KV Cache that logically needs only one copy exists as N copies across N GPUs. The longer the context, the larger each copy, and the sharper the conflict between memory pressure and concurrency.
Sharded CP works differently. In Ring Attention, for example, each GPU holds only the KV of its local shard and passes shards around a ring to neighboring GPUs, accumulating partial results at each step. Persistent storage in this design is not duplicated N-fold, but it requires careful scheduling and synchronization of ring communication. The first step in assessing redundancy is therefore to confirm how your CP framework organizes KV.
The Core Idea of LayerSplit: Partition by Layer, Not by Sequence
LayerSplit changes how the KV Cache maps to GPUs. A replicated scheme has every GPU store the cache for all tokens across all layers. LayerSplit partitions along network depth instead: each GPU persistently holds the KV Cache of only a subset of layers.
Take an 80-layer model with 8 GPUs: each GPU persistently stores the KV Cache of about 10 layers. Globally there is a single copy, apportioned by layer. Compared with the replicated scheme, per-GPU persistent KV Cache drops to roughly 1/N—linear in the number of GPUs. Two caveats apply. First, what is reduced is the persistent cache; computing a given layer still requires temporary buffers. Second, the concurrency gain depends on how much memory remains after weights, activations, and temporary buffers—it cannot simply be scaled by 1/N.
The table below summarizes the key differences:
| Comparison | Replicated CP | LayerSplit (layer-wise partitioning) |
|---|---|---|
| Per-GPU persistent KV Cache | Approximately the full global cache | Roughly 1/N of the global cache |
| Temporary buffer when computing a layer | Usually no extra per-layer buffer needed | Needs a temporary buffer receiving the full KV of the current layer |
| Cross-GPU communication | Implementation-dependent; often pulls the full sequence | Per-layer broadcast; volume grows with layer count and sequence length |
| Main risks | Memory redundancy; limited batch size | Communication latency; higher scheduling complexity |
Cross-Node Broadcast: Whoever Holds It, Sends It
Computing attention at a given layer requires the full KV of that layer. The GPU holding that layer’s cache acts as the data source and sends the KV to the other participating GPUs over high-speed interconnects such as NVLink or InfiniBand. Data is transferred on demand rather than pre-staged as a copy on every GPU.
Four boundary conditions must be clarified in practice:
- Whether receivers cache the data: each receiving GPU keeps a temporary buffer for the current layer and releases it after use. If prefetching is enabled, the buffer count grows; double buffering doubles the peak.
- How many layers to buffer: typically only the current layer and the next layer are kept simultaneously. Prefetching too deep consumes the memory LayerSplit saved.
- When to release: release the buffer only after the layer’s attention completes, to avoid overwriting data while communication is still in flight.
- Peak memory estimation: the KV size of one layer is roughly 2 × sequence length × number of KV heads × head dimension × bytes per element. For a 128K sequence, 8 KV heads, head dimension 128, and BF16 (2 bytes per element), K and V together per layer are about 2 × 131072 × 8 × 128 × 2 bytes, i.e., roughly 512MiB. Temporary-buffer peaks must be counted separately in the memory budget.
On communication volume: every receiving GPU receives that layer’s KV once per step at every layer. Total traffic grows with layer count, sequence length, and the number of receivers, and must be evaluated against the bandwidth of the actual topology.
Overlapping Communication with Computation
Broadcast introduces latency, so the third part of LayerSplit is overlapping network transfer with computation. The approach described in the source material: while KV data is in transit, the receiving GPU runs Indexer-related preprocessing in parallel—including position index computation, attention mask generation, and sequence routing. Once the data arrives, the GPU immediately enters the attention computation.
A workable pipeline looks like this:
- Asynchronously initiate the KV broadcast for layer i.
- In parallel, run the Indexer preprocessing for layer i.
- At the synchronization point, wait until the KV of layer i is ready.
- Run the attention computation for layer i.
- Release the temporary buffer for layer i, and repeat for layer i+1.
Overlap fully hides latency only when the single-layer transfer time is shorter than the single-layer computation time (including preprocessing). If interconnect bandwidth is insufficient, or sequences are short, communication becomes the bottleneck and LayerSplit adds latency instead. When a synchronization point times out, record that layer’s communication time and fall back to synchronous fetching or reduce prefetch depth—never skip synchronization and compute anyway.
How to Measure Performance Gains
The source material mentions that “system throughput can improve by 10% to 132%,” but provides no baseline configuration, sequence length, batch size, GPU count, network topology, measurement methodology, or source. Such a figure cannot serve as a basis for technology selection, so this article does not cite it.
To validate the effect in your own environment, record at least the following:
- Baseline configuration: replicated CP or another parallelism strategy, with the framework version noted.
- Workload: input and output length distributions, batch size, and concurrent request count.
- Hardware and topology: GPU model, GPU count, NVLink or InfiniBand bandwidth, and cross-node hop count.
- Metrics: per-GPU peak memory (distinguishing persistent cache from temporary buffers), time to first token, output tokens per second, and P99 latency.
- Measurement methodology: warmup steps, measurement duration, and whether tokenization and network overhead are included.
Results are meaningful only when compared under identical conditions.
Common Issues and Troubleshooting Directions
- Memory did not decrease: check whether the framework still keeps the global cache locally, or whether prefetch buffers are configured too deep—overly deep prefetching consumes the memory savings LayerSplit provides.
