Breaking the Memory Bottleneck: How Cache Offloading and Prefetching Work

    This article explains why long-context LLM inference throughput is often constrained by GPU memory capacity rather than compute, and how offloading compressed KV cache to host memory with a prefetch engine can raise batch size without hurting latency. It covers a paper-level feasibility model, a four-layer system design with scheduling pseudocode, high-fidelity simulation methodology, simulated throughput results at 32K and 128K contexts, and a practical engineering checklist for capacity planning, bandwidth budgeting, observability, and fallback.

    1. Where the Problem Starts: Why Throughput Gets Locked by GPU Memory

    In long-context inference, an easily overlooked fact is that decode-phase throughput is usually limited not by compute but by GPU memory capacity. Take a 32K context as an example: with a fixed system configuration, plotting batch size against decode throughput shows the curve is not monotonically increasing. It rises quickly and then flattens near a certain batch size. Pushing the batch larger buys almost no additional throughput, and can even cause a slight decline due to scheduling and memory-access pressure.

    The reason is that generating each token in the decode phase requires reading the entire accumulated cache for that request. Cache size grows linearly with sequence length, while per-GPU memory is a hard ceiling. Once the cache fills memory, the number of concurrent requests that fit is fixed, matrix multiplication operators cannot reach sufficient arithmetic intensity, and hardware utilization stays low for long stretches. In other words, if the batch cannot grow, spare compute cannot be converted into throughput.

    Two distinct kinds of pressure need to be separated here. The first is capacity pressure, which determines how many requests can be held simultaneously. The second is bandwidth pressure, which determines how many bytes must be moved per decode step. Capacity pressure limits batch size; an insufficient batch size keeps arithmetic intensity low, which ultimately shows up as idle compute. Understanding this causal chain is the prerequisite for designing an offloading scheme.

    2. A New Variable: Sparse Attention

    Sparse attention reduces compute and memory-access overhead in long contexts significantly by narrowing the range of historical information that participates. Decode latency improves as a result. But it does not eliminate linear cache growth: the retained compressed representation (which can be understood as a low-rank implicit cache) still accumulates token by token and stays resident in GPU memory.

    This leads to a key judgment. Since the per-request cache is already compressed, it is smaller and more regular in structure, so its transfer cost is relatively controllable. Placing it in host memory, which offers larger capacity at lower cost, and using prefetching to hide the latency, therefore becomes feasible. Feasibility is not the same as benefit, though. The benefit boundary depends on three ratios: transfer bandwidth to compute time, cache growth rate to prefetch lead distance, and the marginal throughput gain from a larger batch.

    3. Feasibility Assessment: Establish the Upper Bound on Paper First

    Before writing any implementation, it is worth running a paper analysis with an analytical model so engineering effort does not go to waste. The core inequality can be abstracted as:

    1. Condition for a net gain (illustrative):
    2. T_prefetch + T_compute_overlap <= T_decode_step
    3. where T_prefetch ≈ CacheBytesPerStep / EffectiveBandwidth
    4. CacheBytesPerStep ≈ BatchSize * AvgSeqLen * BytesPerTokenPerLayer * LayerFactor

    Three conclusions follow. First, the longer the context, the more cache must be moved per step, so the benefit of offloading rises and then falls, leaving an optimal range. Too short, and the gain is eaten by fixed overhead; too long, and bandwidth becomes the new bottleneck. Second, increasing batch size also scales up the transfer volume, so offloading must be coordinated with batch-size scheduling rather than being an independent on/off switch. Third, prefetch depth needs to be designed along two dimensions, per layer and per request. Too little lead degrades into synchronous waiting; too much wastes bandwidth and competes with the compute pipeline.

    What the assessment should produce is a benefit boundary table: context length on the horizontal axis, batch size on the vertical axis, and relative throughput change in each cell. This table determines the default parameter values and the fallback strategy that follow.

    4. System Design: Decoupling the Storage and Compute Paths

    The core idea is to separate where the cache is stored from where it is used. GPU memory holds only the working set that the current decode step actually needs, while the historical portion is pushed down to a memory pool and prefetched on demand through an independent path. The design has four layers.

    1. Cache management layer. The cache is organized in blocks, with block granularity balancing prefetch efficiency against fragmentation. A request-to-block mapping table is maintained to support dynamic growth and reclamation. Blocks that are too large inflate the per-transfer volume; blocks that are too small increase metadata overhead and random access. In practice, a common choice is a granularity where one decode step can consume whole blocks.

    2. Prefetch engine. It generates an access plan based on the attention sparsity pattern, queues the blocks that will be needed several steps ahead, and dispatches them by priority. Scheduling follows nearest-before-farthest and critical-layers-before-secondary-layers, with a bandwidth quota in place so prefetch traffic does not interfere with the compute stream.

    3. Compute path changes. Decode operators are changed to trigger on block readiness. Blocks that are not ready do not stall the whole batch; instead the affected request briefly yields for that step and is caught up as soon as its blocks arrive. This lowers the wait from batch level to request level, greatly reducing the impact on overall throughput.

    4. Compatibility and fallback layer. The scheme composes orthogonally with common optimizations such as multi-step prediction and dual-batch overlap. When bandwidth or memory watermark exceeds a threshold, it automatically falls back to full residency so latency does not degrade.

    Illustrative scheduling pseudocode:

    1. def decode_step(requests, cache_pool, prefetcher, budget):
    2. ready, waiting = [], []
    3. for r in requests:
    4. blocks = cache_pool.blocks_of(r, step=r.step + 1)
    5. (ready if blocks.all_resident() else waiting).append((r, blocks))
    6. prefetcher.submit(waiting, budget=budget) # move data ahead within the bandwidth quota
    7. for r, blocks in ready:
    8. blocks.warm_up() # trigger the prefetched, already-resident portion
    9. run_decode(r, blocks)
    10. for r, blocks in waiting: # catch up once ready, without stalling the batch
    11. if blocks.all_resident():
    12. run_decode(r, blocks)
    13. return ready, waiting

    5. High-Fidelity Simulation: Get the Numbers Before Implementing

    Engineering validation is expensive and slow, so a numerical sandbox is needed. The simulator should model at least four kinds of overhead: operator compute time (distinguishing compute-bound from bandwidth-bound regions), cross-node communication latency (including queuing and topology differences), cache transfer overhead (effective bandwidth varies with block size and concurrency), and scheduling and synchronization cost.

    Three modeling points matter. First, effective bandwidth is not peak bandwidth, so a derating factor that decays with access pattern must be introduced. Second, the long tail must be represented: a small number of large sequences can significantly raise overall latency, and averages hide the problem. Third, parameters must be injectable so context length, batch size, and prefetch depth can be swept, producing a benefit boundary table rather than single data points.

    The recommended calibration flow is to first fit the derating factors and fixed overhead with small-scale real load tests, then use the simulator for large-scale extrapolation, and finally regress against a few key points. Only when the error is within an acceptable range should the results be used for decisions.

    6. Simulation Results and Interpretation

    At a 32K context, raising the multi-step prediction stride from 2 to 4 yields roughly a 123% improvement in overall throughput. About 53 percentage points come from multi-step prediction itself, and the remaining roughly 70 percentage points come from the offloading and prefetching mechanism. This split shows that increasing the prediction stride alone quickly hits the memory and bandwidth ceilings, while the offloading mechanism raises those ceilings so the benefit of a longer stride can be realized. The two combine multiplicatively rather than simply adding up.

    With a 128K extra-long context and a prediction stride of 2, the offloading and prefetching mechanism alone delivers roughly a 123% throughput improvement while latency stays within the constraint. This confirms the earlier judgment: the longer the context, the more acute the cache capacity pressure, and the higher the marginal value of offloading.

    It should be stressed that these numbers come from simulation. They are meant to set direction and parameter ranges; real gains must be confirmed by end-to-end load tests on the target hardware.

    7. Engineering Implementation Checklist

    • Capacity planning: estimate the memory pool size from context length × concurrency × cache bytes per token, and leave watermark headroom.
    • Bandwidth accounting: derive the required effective bandwidth from per-step transfer volume and decode step duration, then check it against memory channels and interconnect capability.
    • Default parameters: tune block granularity, prefetch depth, and bandwidth quota together, since individually optimal settings can be globally suboptimal.
    • Observability: monitor and alert on cache hit rate, prefetch wait time, memory watermark, and batch size distribution. A persistently low hit rate means the access plan is off target.
    • Fallback plan: define multiple fallback levels, from partial offloading to full residency, with latency as the priority.
    • Compatibility regression: after combining with multi-step prediction and dual-batch overlap, re-scan the benefit boundary table to prevent bandwidth contention introduced by the combination.

    8. Summary

    The throughput bottleneck in long-context inference is fundamentally a batch-size limit under a capacity constraint. Moving the linearly growing implicit cache down from GPU memory to host memory, and using an access-plan-driven prefetch engine to hide transfer latency beneath compute, can substantially raise batch size and throughput without sacrificing latency. Practice at Baidu AI Cloud in inference acceleration indicates that a viable path for controlling cost and speeding up iteration is to assess the benefit boundary first, then decouple storage from compute, and finally use high-fidelity simulation to replace part of the real-hardware validation.