Breaking the Memory Wall: How Latent Cache Offloading and Prefetching Boost Long-Context Inference

    This article explains why long-context LLM inference hits a GPU memory capacity wall rather than a compute wall, and how an offloading and prefetching design called Expanded Sparse Server (ESS) addresses it. It covers the causal chain from growing context length to stalled decode throughput, the three prerequisites for offloading latent cache to CPU memory, ESS design principles including a tiered cache pool and an asynchronous prefetch pipeline, and the engineering details around page granularity, prediction accuracy, and bandwidth arbitration. It also describes a high-fidelity simulator used to evaluate trade-offs before implementation, simulated results showing throughput gains from combining multi-token prediction with offloading, and a practical tuning checklist for deployment.

    1. Where the Problem Starts: Why Long-Context Inference Hits a Memory Wall

    In large model inference serving, Prefill and Decode are usually deployed in separate resource pools, an architecture known as PD disaggregation. Splitting the two stages lets each provision compute according to its own bottleneck, but it also exposes the inherent weakness of the Decode stage more clearly. Decode generates tokens autoregressively, one at a time, and every step must read the cache corresponding to all historical context. Compute utilization is therefore naturally low, and performance depends heavily on memory access and capacity.

    Take a very large model that uses a sparse attention mechanism as an example. Its attention computation path has already been significantly compressed in terms of computation and latency under long context through sparsification, but the size of the cache (Latent Cache) still grows roughly linearly with sequence length. When the context extends from 32K to 128K, the cache required by a single request inflates several times, and GPU memory is quickly filled by only a few concurrent requests.

    This leads to a counterintuitive phenomenon: if Batch Size cannot go up, throughput cannot go up. In theory, increasing Batch Size raises the Arithmetic Intensity of GEMM operators, pushing originally memory-bound operators into the compute-bound region, thereby amortizing the cost of weight reads and increasing the effective output per unit of memory bandwidth. But memory capacity is a hard constraint. Batch Size is capped, and the room to improve Arithmetic Intensity is sealed off along with it.

    The chain can be summarized as a clear causal sequence:

    • Context gets longer → per-request cache inflates linearly
    • Cache stays resident in GPU memory → available memory is divided among concurrent requests
    • Concurrency is limited → Batch Size cannot be enlarged
    • Insufficient Arithmetic Intensity → inefficient bandwidth utilization
    • The end result is stalled Decode throughput and persistently high cost per token

    2. Optimization Motivation: Decoupling the Cache from the Compute Path

    Since the bottleneck is capacity rather than compute, the most direct idea is to offload the Latent Cache from GPU memory down to CPU memory, trading capacity for concurrency. CPU memory capacity is typically an order of magnitude larger than that of a single GPU card, and in theory can support several times to tens of times more concurrent residency.

    But the feasibility of this strategy depends on three prerequisites, none of which can be missing:

    1. Whether sparse attention really needs the full cache. Sparsification means each step only accesses a subset of historical positions. If the access set is predictable, most of the cache is in a “temporarily unused” state, which provides the physical basis for offloading.
    2. Whether the transfer overhead can be hidden behind computation. The bandwidth of host-device links such as PCIe is far lower than memory bandwidth. If on-demand fetching cannot be hidden within the computation time window, offloading degrades into pure loss.
    3. Whether there is a clear upper bound on the benefit. The upper limit of the benefit is determined jointly by “how much Batch increment the freed memory can support” and “whether the transfer latency can be hidden.” This must be quantified rather than assumed in a general way.

    The third point deserves emphasis: offloading is not a cost-free silver bullet. In short-context, low-concurrency scenarios, the cache itself is small, the memory savings from offloading are limited, and the fixed transfer and synchronization overhead still has to be paid, so the net benefit may be negative. Engineering practice must therefore first delineate the applicable range: the longer the context, the higher the cache proportion, and the more memory-bound Decode is, the greater the marginal benefit of offloading.

    3. ESS Design: Decoupling Storage from the Compute Path

    For industrial deployment, we turned the above idea into a systematic solution (Expanded Sparse Server, ESS), whose core is “decoupling cache storage from the compute path,” following three design principles.

    Principle one: a tiered cache pool. GPU memory only retains the working set actually needed by the current step, while CPU memory holds the full historical cache, forming a two-level structure. The memory layer pursues low-latency hits; the CPU memory layer pursues large-capacity residency.

    Principle two: a prefetch pipeline. Organize cache transfer into a producer-consumer pipeline that asynchronously prefetches the pages needed for step t+1 while step t is being computed, so that transfer latency is hidden behind computation latency. The schematic logic is as follows:

    1. for step in decode_steps:
    2. pages_next = predict_pages(step + 1) # predict based on the sparse access pattern
    3. async_prefetch(host -> device, pages_next) # overlap with the current step's computation
    4. kv = wait_or_hit(pages_current) # zero wait on a hit
    5. out = sparse_attention(q, kv) # transfer completes within the compute window
    6. swap_out(pages_cold) # reclaim cold pages to free GPU memory

    Principle three: orthogonal and compatible with mainstream optimizations. The solution does not intrude on the model structure and can be stacked with optimizations such as multi-token prediction (MTP) and Two-Batch Overlap, serving as an enhancement component for existing inference systems rather than a replacement. This is especially critical for industrial adoption: it means the modification cost is controllable, and the change can be rolled out gradually and rolled back.

    Several details also need to be handled in engineering: page granularity selection (too small causes a surge in scheduling and transfer counts; too large reduces hit flexibility), access-set prediction accuracy (prediction deviation translates directly into waiting bubbles), bandwidth arbitration across multiple requests (avoiding a few long sequences starving other requests), and failure and backpressure handling (queuing and degradation strategies when the memory-layer capacity is full).

    4. A High-Fidelity Simulator: Getting Trustworthy Conclusions Before Implementation

    The tuning space for an offloading solution is large and the combinations are many. Relying entirely on real-machine experiments would make validation extremely costly. To address this, we built a high-fidelity simulator that models three types of overhead simultaneously:

    • Model computation: operator-level latency and how Arithmetic Intensity varies with Batch and sequence length;
    • Communication latency: collective communication overhead under tensor parallelism and pipeline parallelism;
    • Offload-prefetch overhead: link bandwidth, page granularity, prediction hit rate, and overlap efficiency.

    The value of the simulator is turning “guessing” into “table-lookup decisions”: developers can scan the throughput and latency Pareto frontier under different context lengths, MTP multipliers, and concurrency configurations before coding, quickly locating the benefit inflection point and the failure region, thereby significantly reducing engineering trial-and-error costs and accelerating solution iteration.

    5. Simulation Results and Analysis

    Under a 32K context configuration, when MTP is increased from 2 to 4, overall throughput improves by about 123.4%. A further breakdown shows that about 53.1 percentage points come from MTP itself, while the remaining roughly 70.2 percentage points come from the concurrency space released by the offload-prefetch mechanism. This indicates that the two are not simply additive, but positively coupled: MTP increases per-step output, while offloading increases the concurrency that can be accommodated.

    In the ultra-long context test, with MTP = 2 and a 128K context, the offload-prefetch mechanism alone brings about a 123% throughput improvement. This result confirms the judgment in Section 2: the longer the context and the higher the cache proportion, the more efficiently capacity release translates into throughput.

    It should be stated objectively that the above data comes from simulator modeling results and is used to evaluate trends and benefit boundaries. Actual values in real deployment will be affected by hardware link bandwidth, model sparsity patterns, request length distribution, and scheduling policies, and should be based on real measurements.

    6. Deployment Recommendations and Tuning Checklist

    • Assess the benefit boundary first: gather statistics on the business context length distribution and the proportion of cache in GPU memory, and confirm you are in the high-benefit range before investing.
    • Prioritize getting the prefetch pipeline working: hit rate and overlap rate determine success or failure. It is recommended to start with a larger page granularity to guarantee bandwidth efficiency, then refine it gradually.
    • Validate stacked with existing optimizations: evaluate incremental benefit with MTP and Two-Batch Overlap enabled, to avoid double-counting benefits already achieved.
    • Build observability: use general monitoring, alerting, and logging services to continuously track hit rate, waiting bubbles, link utilization, and queuing latency.
    • Keep a degradation path: automatically fall back to a pure GPU-memory mode when memory-layer pressure is too high, to ensure service stability.

    7. Conclusion

    The throughput bottleneck of long-context inference is essentially a capacity bottleneck rather than a compute bottleneck. Decoupling the Latent Cache from the critical compute path, combined with asynchronous prefetching and a tiered cache pool, can significantly increase Decode concurrency and throughput under latency constraints while amortizing the cost per token. With a high-fidelity simulator completing the upfront evaluation, teams can complete solution selection and iteration at lower cost and faster pace. This is also a direction that Baidu AI Cloud continues to focus on in the engineering practice of inference serving.