LLM Inference Memory Residency and Cache Hit Strategies Explained

    This article breaks down how memory residency and prefetching strategies improve LLM inference throughput by keeping high-frequency data in HBM and ensuring cache hits. It covers capacity accounting for weights, KV cache, and runtime overhead, explains how prefetch scheduling turns hit rate into a timing problem, and discusses the trade-offs among throughput, latency, and stability. It also lists key metrics for validation and common pitfalls to avoid when tuning for real workloads.

    1. Background: Why Keeping Data Resident Is Key to Inference Acceleration

    In large language model (LLM) inference serving, the cost of moving weights and KV cache often hurts more than the computation itself. HBM bandwidth is limited, and if every decoding step has to shuttle large tensors back and forth across the memory hierarchy, even abundant compute will be stalled by data supply. The idea of keeping data resident in HBM is essentially about letting high-frequency data stay in high-bandwidth memory over the long term, avoiding repeated swaps in and out, so that effective throughput can be pushed close to the hardware limit. The core reason some high-performance inference solutions achieve hundreds of tokens/s is not a single optimized operator, but a system-level design that combines data residency with hit assurance.

    2. Two Mainstream Strategies: Residency-Based and Prefetch-Based

    The original observation groups the approaches into two categories, which can be abstracted and stated more rigorously as follows:

    1. Residency-based (no eviction until memory is full): As long as the working set does not exceed the available HBM capacity, cache entries are not evicted, which is equivalent to trading capacity for hit rate. This relies on accurate capacity accounting and a stable access set.
    2. Prefetch-based (moved in ahead of time, present exactly when needed): Before the workload actually reads the data, the next batch of required data is moved into the high-bandwidth tier in advance, so that the access hits at the right moment. It trades time alignment for hit rate and places higher demands on scheduling and bandwidth budgeting.

    The two are not mutually exclusive: production systems are usually residency-first with prefetch as a supplement, meaning hot data stays resident while cold data is moved in ahead of time based on prediction.

    3. Capacity Accounting: The First-Order Constraint of Residency

    Whether the residency strategy holds depends on one inequality: resident data volume + runtime overhead ≤ available HBM. The accounting must distinguish three types of usage:

    • Model weights: directly related to parameter count and precision;
    • KV cache: grows linearly with concurrency and context length, and is the part most likely to get out of control;
    • Temporary buffers and communication regions: operator workspaces, parallel communication buffers, and so on, which are often underestimated.

    An illustrative estimate (only expressing order-of-magnitude relationships, not actual product data):

    1. # Illustrative only: shows the accounting approach, not real implementation parameters
    2. bytes_per_param = 2 # half precision
    3. weight_bytes = params * bytes_per_param
    4. kv_bytes = layers * 2 * kv_heads * head_dim * ctx_len * batch * bytes_per_param
    5. overhead = weight_bytes * 0.1 # empirical reserve for workspace and communication buffers
    6. fits = (weight_bytes + kv_bytes + overhead) <= available_hbm

    Once fits is false, eviction or paging must be introduced, and “no data on disk” degrades into “partial residency,” with the hit rate dropping accordingly.

    4. Prefetch Strategy: Turning Hits into a Timing Problem

    The key to prefetching is not moving a lot of data, but moving it accurately, early, and just in time. In engineering practice, this usually relies on three layers of information:

    • Predictability of the access sequence: Read patterns during decoding are relatively regular, so data movement can be issued ahead of time;
    • Bandwidth budget: Prefetching competes with computation for bandwidth and must be rate-limited to avoid starving the computation;
    • Failure handling: If the prediction is wrong, there must be a fallback path that does not block the main flow.

    A simplified prefetch scheduling example:

    1. def schedule_prefetch(step, working_set, hbm_free):
    2. nxt = predict_next(working_set) # predict what the next step needs
    3. if nxt.size <= hbm_free and bandwidth_ok():
    4. issue_prefetch(nxt) # move data in ahead of time
    5. else:
    6. fallback_on_demand(nxt) # load on demand if prediction fails

    5. Engineering Trade-offs: The Triangle of Throughput, Latency, and Stability

    • Residency-based offers the most stable latency but is hard-constrained by capacity and degrades under high concurrency;
    • Prefetch-based offers a higher throughput ceiling but is sensitive to prediction accuracy and bandwidth scheduling, with greater jitter risk;
    • A hybrid strategy that combines a resident hot set, a prefetched cold set, and paging beyond the limit strikes a balance and is the practical choice for most high-throughput services.

    6. Metrics and Validation: Do Not Tune by Feel

    It is recommended to observe at least four types of metrics: cache hit rate, swap-in/swap-out volume, the distribution of per-step decoding latency (with attention to tail latency), and peak HBM usage. For validation, use fixed inputs and fixed concurrency as a baseline, then increase the load step by step and observe in which concurrency range the hit-rate inflection point appears—that point is the capacity boundary of the residency strategy.

    7. Common Pitfalls

    • Treating “always looks like a hit” as a real hit: if the metric does not include prefetch failures and fallbacks, the numbers will be inflated;
    • Counting only weights and not KV: in long-context scenarios, KV is the main variable;
    • Prefetching without rate limiting: bandwidth is fully consumed by prefetching, and computation actually slows down;
    • Ignoring tail latency: a good average with P99 jitter still means a poor user experience.

    8. Conclusion

    Keeping data resident in HBM is not a slogan, but systems engineering that coordinates capacity accounting, prefetch scheduling, and metric validation. First confirm whether the working set can stay resident, then use prefetching to fill the timing gap, and finally use observable metrics to hold the boundary—only then can you stably approach the hardware throughput ceiling under real workloads.