Decode Memory Wall in Long-Context LLM Inference: When to Offload and Prefetch Latent Cache
This article explains how to tell whether GPU memory limits decode throughput for sparse attention models in prefill-decode (PD) disaggregated deployments with long contexts. It covers the four checks to run before offloading latent cache, how to compare prefetch transfer time with compute time, how to set Top-K, hit rate, and batch size limits, and the common deployment mistakes and canary rollout criteria.
1. Confirm That GPU Memory Is the Decode Bottleneck
In models with sparse attention, each query selects only Top-K keys and values from the historical tokens for computation. This lowers the compute cost of attention. However, in prefill-decode (PD) disaggregated deployments, decode throughput can still hit its ceiling first as context grows. In that case, memory capacity is usually the root cause.
This conclusion has a precondition. Offloading cache improves throughput only when memory capacity runs out before other resources. If bandwidth is the bottleneck, offloading only shifts the pressure onto the PCIe link. Before launch, check the following four items:
- The P95 of GPU memory usage reaches 90% or higher, and new requests queue or get rejected due to insufficient memory;
- A batch sweep in a test environment (for example, 16, 32, and 64) shows throughput rising by 50% or more each time the batch size doubles;
- Compute utilization (MFU) for operators such as GEMM is below 40%;
- Without offloading, PCIe throughput is below 50% of peak.
If any one of the four items is not met, investigate other bottlenecks first. Three counterexamples illustrate this:
- Memory usage is only 60%, yet requests still queue. The queuing may come from the scheduling policy or from prefill resources, not from memory.
- The batch size goes from 32 to 64, and throughput barely changes. The bottleneck may be in communication or CPU scheduling, so offloading will not help.
- Compute utilization is already above 60%. Decode is close to the compute limit, so freeing memory cannot raise throughput further.
2. What Latent Cache Is and Why It Grows Linearly with Sequence Length
Latent cache is the cache stored by models that compress KV with latent variables, such as MLA-style architectures. Instead of storing a full key and value for each attention head, these models store one low-dimensional latent vector per token at each layer. Top-K selection depends on these latent vectors, so the cache must keep all historical tokens.
Each new token adds one record at every layer, so the cache size is proportional to sequence length. The cache size for a single token is L × d × b, where:
- L is the number of model layers;
- d is the dimension of the latent vector;
- b is the number of bytes per element, which is 2 for BF16.
With L = 60, d = 512, and b = 2, the cache for one token is about 60 KB. A 32K context takes about 2 GB, and a 128K context takes about 8 GB. At a batch size of 32, the cache for 128K contexts alone is close to 250 GB. When memory cannot hold these caches, the batch size is effectively locked.
3. How to Judge Whether Computation Can Hide the Prefetch
After offloading, the tokens selected for each step that are not in GPU memory must be prefetched back to the GPU before that step’s computation. Whether computation can hide the prefetch depends on the ratio of the transfer volume to the effective bandwidth. The incremental transfer volume per request per step is estimated as:
V = (1 − r) × K × L × d × b
The symbols mean the following:
- K is the number of Top-K tokens selected per layer per step;
- r is the hit rate, meaning the share of tokens selected in this step that are already in GPU memory. Measure r online for each request. Do not assume it from experience;
- L, d, and b are the same as above.
Let B be the batch size, so the total transfer per step is B × V. BW is the measured effective bandwidth, not the nominal link rate. The transfer time is:
T_copy = B × V / BW
Let D be the prefetch depth, meaning data can start moving D steps ahead of use. Let T_step be the decode compute time for one step. The condition is:
T_copy ≤ D × T_step
When this condition holds, computation can hide the transfer. When it does not, decoding slows while it waits for data. The benefit of offloading then disappears, and it can even turn negative.
Worked Example
Assume K = 2048, L = 60, d = 512, b = 2, r = 0.8, B = 32, BW = 25 GB/s, D = 1, and T_step = 40 ms. The calculation runs as follows:
- For one request in one step, the full selected data is K × L × d × b, which is 2048 × 60 × 1024 bytes, or about 125.8 MB.
- The missed portion is (1 − 0.8) × 125.8 MB, or about 25.2 MB.
- At a batch size of 32, the total transfer is about 806 MB.
- The transfer time is about 806 MB ÷ 25 GB/s, or about 32 ms.
- Comparing 32 ms with 40 ms, computation can hide the transfer, with a margin of about 20%.
If the hit rate drops to 0.6 and the other parameters stay the same, the missed portion becomes 50.3 MB. The total transfer is then about 1.6 GB, and the transfer time is about 64 ms, which exceeds 40 ms. Decoding then slows while it waits for data. The hit rate has a large effect on the conclusion, so it must be measured online.
4. How to Choose K, r, and the Batch Size Limit
The table below gives three sets of starting parameters for canary rollout. All values must be re-measured on the target cluster. The “Minimum T_step required” column comes from the inequality in the previous section.
| Scenario | K | Target r | Batch size limit | Prefetch depth D | Minimum T_step required | Expected benefit | Main risk |
|---|---|---|---|---|---|---|---|
| Conservative | 1024 | ≥ 0.9 | 16 | 1 | About 4 ms | Less memory is freed, so the gain is small | The gain may not be noticeable |
| Balanced | 2048 | ≥ 0.8 | 32 | 1 | About 32 ms | Higher batch size limit; throughput gain must be measured | Decoding slows if the transfer margin is too small |
| Aggressive | 2048 | ≥ 0.6 | 64 | 2 | About 64 ms | Most memory freed; throughput gain must be measured | Prefetch buffers use GPU memory, and PCIe is prone to saturation |
Choose the parameters in this order. First set the target hit rate r. Then use the formula from the previous section to calculate T_copy. Finally, set the batch size limit. The prefetch buffer also uses GPU memory, so its size must be subtracted from the memory freed.
This article does not promise specific throughput gains. Simulated benefits apply in only two situations: bandwidth can be measured stably, and Top-K changes little between adjacent steps. If Top-K changes sharply, the hit rate r drops, and simulated results will not reflect online performance.
5. Common Deployment Mistakes
- Looking only at memory usage and skipping the batch sweep. High memory usage does not mean the batch size is limited.
- Using the nominal link bandwidth instead of the measured effective bandwidth. Effective PCIe bandwidth is usually below the nominal value, and it depends on the copy method.
- Estimating the hit rate from averages. Calculate it per request, and pay close attention to long-tail requests.
- Ignoring the GPU memory used by prefetch buffers. Oversized buffers can cause O
