KV Cache Layer Split Storage for Long-Context Inference: Practice and Evaluation

    This article explains how layer-split KV Cache storage relieves the GPU memory bottleneck in long-context LLM inference. It covers why prefill dominates latency under long-context workloads, how assigning model layers to different GPUs reduces per-card cache usage, how on-demand broadcasting and compute overlap hide communication costs, and the engineering trade-offs around layer-to-GPU mapping, cache lifecycle, sequence parallelism, numerical consistency, and observability. It also outlines when layer splitting pays off, when to avoid it, and a practical evaluation methodology.

    1. The Starting Problem: Long-Context Workloads Push the Bottleneck to Memory Capacity

    In applications such as AI coding tools and code assistants, request contexts are often very long. Repository-level retrieval results, multi-turn conversation history, and tool-call payloads keep accumulating. At the same time, many requests share the same prefix (system prompts, project descriptions, common dependency code), which keeps prefix cache hit rates high. Together, these two factors produce a direct consequence: the prefill phase becomes the main contributor to end-to-end latency, rather than the decode phase.

    To compress prefill latency, the industry commonly adopts Context Parallelism (CP): splitting a request’s sequence dimension across multiple accelerators so the cards jointly complete attention computation. The problem is that in mainstream open-source implementations, every card keeps a full copy of the Key-Value Cache (KV Cache). The sequence is split, but the cache is not, which creates redundancy: a single card’s memory is occupied by cache portions that do not belong to it, and both the number of concurrent requests and the supported context length are pushed down. The end result is that compute is not yet saturated while memory is already exhausted, and GPU utilization is capped by capacity.

    Layer Split storage targets exactly this issue: each card holds the KV Cache for only some of the model’s layers instead of all of them. Memory usage drops roughly in proportion to the number of layers, so capacity stops being the ceiling on compute.

    2. How It Works: Three Actions Form a Complete Loop

    2.1 Layer-wise Ownership: Split the Cache by Layer Across Cards

    Every layer’s attention has its own key and value tensors, and there are no cache-level dependencies between layers, which gives natural freedom to split. Distributing L layers across N cards reduces per-card cache from roughly L × S × H × D to about (L/N) × S × H × D (where S is sequence length, H is the number of heads, D is head dimension, multiplied by the actual coefficients for data type and layer count). This is a split dimension orthogonal to sequence splitting, and the two can be combined.

    2.2 On-Demand Broadcast: Whoever Holds the Data Serves It

    Computing a layer’s attention requires that layer’s complete keys and values. The card holding that layer’s cache broadcasts the data to the other cards in the same group, which then complete attention for the sequence slice they own. The key design point is aligning communication scope strictly with the parallel group: broadcast only within the context parallel group to avoid amplifying cross-group traffic, and keep broadcast granularity aligned with layer boundaries so it lines up with the compute pipeline.

    2.3 Compute Overlap: Hide Communication Inside the Indexer

    Broadcasting has a cost, but long-context scenarios happen to provide room to hide it. Before attention computation, there is usually an indexing/filtering stage (the Indexer, used to locate the context blocks that need to participate in computation). That work depends on the subsequent attention, but has no dependency on the next layer’s cache broadcast. Issuing the broadcast early and running it in parallel with indexing hides most of the communication time behind compute time. The implementation relies on dual concurrent streams and inter-stream synchronization:

    1. # Illustration: overlapping broadcast with indexer computation
    2. comm_stream.wait_stream(compute_stream)
    3. with stream(comm_stream):
    4. # The card holding this layer's cache initiates the broadcast
    5. kv_layer = broadcast_kv(layer_id, src=rank_of(layer_id))
    6. with stream(compute_stream):
    7. idx = indexer.score(query, coarse_kv) # index computation running in parallel with the broadcast
    8. compute_stream.wait_stream(comm_stream)
    9. attn = attention(query, kv_layer, idx) # depends on both being ready

    3. Engineering Implementation Notes

    • Layer-to-card mapping strategy: contiguous splitting is simple to implement and offers good communication locality; interleaved splitting (card i holds layers i, i+N, i+2N, …) balances load across cards better but requires weighing indexing and scheduling complexity.
    • Cache lifecycle: on a prefix cache hit, the reused prefix stays resident only on its owning card, which further amplifies the capacity gain; eviction policies must be decided per layer to avoid one card becoming the weak link.
    • Coordination with sequence parallelism: sequence splitting decides “how many tokens each card computes,” layer splitting decides “how many layers each card stores,” and only their product reflects real per-card pressure, so capacity planning must estimate both jointly.
    • Numerical consistency: the broadcast path may change reduction order, so fix the accumulation order or use deterministic operators to prevent the same request from drifting in output across different parallel configurations.
    • Observability: collect cache usage, broadcast bytes, overlap ratio, and wait time per layer and per card, so you can tell whether the gain comes from capacity or from communication hiding.

    4. Where the Gains Come From and Where It Applies

    Throughput improvement comes from two paths: first, freed capacity raises concurrency, batch size grows, and compute utilization improves; second, communication hidden behind compute lowers end-to-end latency. The longer the context, the more layers, and the higher the parallel degree, the more pronounced the benefit. Conversely, with short contexts and low concurrency, broadcast overhead may consume the entire gain and even cause a regression.

    Typical cases where layer split storage is not recommended: very short sequences, low prefix cache hit rates, clearly insufficient bandwidth within the parallel group, or a model with fewer layers than parallel cards, which leaves insufficient split granularity.

    5. How to Evaluate a Rollout

    1. Baseline measurement: record the maximum supported context, peak concurrency, per-card memory watermark, and throughput with layer split disabled.
    2. Single-variable comparison: fix parallel degree and batch strategy, toggle only the layer split switch, and check whether the memory reduction approaches 1/N.
    3. Communication checkup: measure the share of single-step latency spent on broadcast; if it stays above compute time, adjust layer mapping or increase intra-group bandwidth first.
    4. Long-context stress test: validate at 32K/128K and longer tiers, and confirm the gain changes monotonically with length as expected.
    5. Stability regression: verify output consistency, time to first token, and tail latency to rule out long-tail jitter.

    6. Conclusion

    Layer split storage essentially breaks the implicit assumption that every card must hold the full cache, trading controllable communication for meaningful capacity. Under workload profiles with long contexts and high prefix hit rates, this trade is usually well worth it. For inference systems whose compute utilization is limited by memory capacity, it can be adopted as a standard companion capability to context parallelism, and combined with capacity estimation, overlap tuning, and an observability system, it reliably delivers gains in both capacity and throughput.