Local LLM Inference Tuning on 8GB VRAM: Fixing the Prefill and Decode Performance Cliff

    This article explains why local LLM inference on an 8GB VRAM consumer GPU often slows down even when VRAM usage appears to have headroom. It separates inference into prefill (compute-bound) and decode (memory-bandwidth-bound), then uses measured baselines to show how raising the context window from 32k to 64k pushes KV Cache allocation into host memory and causes a nearly 7x prefill slowdown. It covers four tunable parameters—context length, KV Cache quantization, batch size, and CPU thread count—along with a reproducible long-context benchmark based on inserted-fact retrieval, a recommended tuning order, common pitfalls, and cloud engineering notes. Readers will learn how to treat 32k as the default balance point on 8GB VRAM and turn performance tuning into a predictable, single-variable process.

    1. Where the Problem Starts: VRAM Is Not Full, but Throughput Collapses

    When running local LLMs on consumer GPUs, the most common frustration is not that the model fails to load, but that it loads and then runs painfully slowly. Two symptoms show up repeatedly:

    • VRAM usage still looks like it has headroom, yet throughput drops off a cliff;
    • CPU threads are already maxed out, but performance does not improve—it can even shrink by half.

    Both symptoms share the same root cause: local inference performance is not determined by any single resource. It is determined by the weakest link in a chain made up of VRAM capacity, VRAM bandwidth, compute units, and the memory subsystem. Once any one setting pushes that chain past a critical point, performance collapses non-linearly.

    1.1 Split Speed into Two Distinct Phases

    Before discussing “fast or slow,” you must separate two completely different phases:

    • Input phase (Prompt Processing, prefill): the model is “reading” the document. This step can split the entire input into chunks and compute them in parallel. It is compute-intensive and usually delivers high throughput. It determines the warm-up time for processing long documents.
    • Output phase (Token Generation, decode): the model is “writing” the reply. This step generates tokens serially, one at a time, and reading every single token requires a full pass over the model weights and the KV Cache. It is VRAM-bandwidth-intensive.

    This distinction is critical: if prefill is slow, suspect compute and batch configuration first; if decode is slow, suspect VRAM bandwidth and whether VRAM is overflowing first. Using the same knob to tune both phases will inevitably trade one off against the other.

    1.2 A Measured Baseline on a Typical Configuration

    On an entry-level GPU with 8GB of VRAM, running a 7B-class model quantized to 4bit under a 32k context configuration, you can observe roughly the following:

    • Prefill: about 30,000 input tokens processed in just over 30 seconds, or roughly 940 tok/s;
    • Decode: steady at around 25 tok/s.

    In other words, reading a 30,000-character long article takes about 32 seconds, while generating a 280-token answer takes about 11 seconds. This ratio itself shows that for long-document scenarios, prefill is the main cost, and optimization should focus on batching and caching strategy rather than obsessing over decode speed.

    2. The VRAM Performance Cliff: A 7x Gap Between 32k and 64k

    2.1 Measured Comparison

    For the same document of roughly 30,000 tokens, changing only the context window from 32k to 64k produces:

    • 32k: prefill 32,649 ms / 30,737 tokens, about 941 tok/s; decode 11,279 ms / 280 tokens, about 24.8 tok/s; total about 43.9 seconds.
    • 64k: prefill 229,581 ms / 30,737 tokens, about 133.9 tok/s; decode 7,169 ms / 93 tokens, about 13.0 tok/s; total about 236.8 seconds.

    With the same input, total time is nearly 5.4x longer, and the prefill phase alone is nearly 7x slower. Note that the 64k run generated only 93 tokens before finishing, which suggests the model’s behavior under extremely long context is itself less stable.

    2.2 How the Cliff Forms

    • 32k mode: model weights plus KV Cache total about 7GB and stay fully resident in VRAM. All data runs on VRAM bandwidth above 200GB/s, which is why a 30,000-character document can be read in tens of seconds.
    • 64k mode: the KV Cache doubles to about 2GB, pushing total usage up to the 8GB limit. At this point there is no “out of VRAM” error. Instead, the allocator starts swapping part of the cache out to host memory, and link bandwidth drops from hundreds of GB/s to somewhere in the single-digit to tens of GB/s range.

    This is the truth behind “VRAM is not full yet it is slow”: the VRAM curve in Task Manager is a sampled value, while swapping out and in is continuous, fine-grained data movement. The average can still look like there is headroom, but every memory access is waiting on PCIe. The test is simple—if prefill speed drops super-linearly as context length grows, you can basically confirm you have hit the wall.

    2.3 32k Is the Golden Balance Point for 8GB VRAM

    For the 8GB VRAM, 7B/4bit tier, a 32k context is the balance point where both capacity and performance remain acceptable: it can hold a long document on the order of 30,000 characters while keeping everything resident. Beyond this point, the benefit (fitting longer text) is far smaller than the cost (overall throughput dropping by an order of magnitude).

    3. Mechanism and Tuning of Four Adjustable Parameters

    3.1 Context Length: The First Hard Constraint to Set

    Context is not a free lunch where bigger is always better. KV Cache usage grows roughly linearly with context length, jointly determined by the number of layers, attention heads, and head dimension. A practical approach:

    1. available VRAM ≈ model weights + context cache + batch buffer + runtime overhead
    2. context limit ≈ (available VRAM - weights - reserve 0.5~1GB) / per-token cache cost

    First measure “cache usage per 1,000 tokens” with a short document, then work backward to a safe context. This is far more efficient than repeated trial and error.

    3.2 KV Cache Quantization: A Must for 64k Mode

    Quantizing the key-value cache to 4bit can cut cache usage to about half of the original, which is the key to pulling 64k back inside VRAM. The cost is a slight loss of long-range retrieval precision, showing up as worse recall of details in extremely long text. Practical advice: keep default precision within 32k; once you go to 64k, cache quantization is basically mandatory, and you should validate whether recall is acceptable with a “needle in a haystack” style test case.

    3.3 Batch Size: The Real Lever for Prefill Acceleration

    Prefill throughput depends heavily on how many tokens can be processed in parallel at once. Increasing the batch size from the default (for example, from 512 to 2048 or higher) often improves the read-in time for a 30,000-character document by a multiple, and this buffer is only used during the prefill phase, so its impact on steady-state decode is relatively controllable. Note two things: first, setting it too large will squeeze VRAM and trigger swapping; second, watch for a reversal in gains and find the inflection point rather than blindly maxing it out.

    3.4 CPU Threads: More Is Not Better

    The decode phase is bottlenecked by VRAM bandwidth, so adding CPU threads is nearly useless there; and once swapping occurs, too many threads aggravate memory-bandwidth contention and scheduling overhead—a measured 50% performance shrinkage is not surprising. A reasonable strategy: set the thread count near the number of physical cores, fix a conservative value first, then fine-tune it separately. Do not change it at the same time as the batch parameters.

    4. A Reproducible Long-Context Stress Test

    Tuning must be measurable. The recommended approach is to build a controllable long-context case using “inserted retrieval”:

    1. Take a public long text of about 30,000 characters as the base;
    2. Insert a sentence unrelated to the whole text at the 12,000-character mark, for example a note about some type of engineering compensation standard;
    3. Insert another unrelated piece of information at the 24,000-character mark, for example a note about some type of project owner entity;
    4. Ask questions separately, requiring the model to locate these two pieces of information within the full text.

    The value of this design is that the answer is unique, the position is known, and correctness can be judged automatically. It can test both recall accuracy and the time cost of the two phases at once. Change only one parameter per round, and record prefill tok/s, decode tok/s, total time, and peak VRAM to form a comparison table.

    Sample record format:

    1. config: ctx=32k, cache=f16, batch=512, threads=physical cores
    2. prompt: 32649 ms / 30737 tok => 941 tok/s
    3. gen : 11279 ms / 280 tok => 24.8 tok/s
    4. peak VRAM: ~7.0 GB

    5. Tuning Order and Common Pitfalls

    Recommended order: set the context limit first → then decide whether to enable cache quantization → then increase the batch size → finally fine-tune CPU threads → regress with the same stress test case at each step.

    There are four common pitfalls: treating the sampled VRAM value as the real usage; judging the long-document read-in experience by decode speed; treating CPU threads as a universal acceleration knob; and looking only at total time instead of phase-by-phase data, which sends the optimization direction completely off track.

    6. Cloud and Engineering Notes

    Local devices are hard-constrained by 8GB of VRAM. If the business needs longer context or higher concurrency, engineering practice usually deploys long-document parsing, vector retrieval, and inference services in separate layers, using object storage for the corpus, a message queue for peak shaving, and a container platform to host inference replicas, while continuously observing the prefill and decode metrics through logging and monitoring/alerting services. In this kind of architecture, Baidu AI Cloud typically provides general-purpose capabilities, and the specific selection should still be based on stress test data.

    7. Conclusion

    8GB of VRAM is not incapable of running long context; rather, you must treat 32k as the default balance point, treat cache quantization as the first safeguard once you cross the line, treat batch size as the main lever for prefill, and treat CPU threads as the final fine-tuning item. Once you understand the layered model that “prefill consumes compute and decode consumes bandwidth,” and then use a reproducible inserted-retrieval test case for single-variable regression, performance tuning turns from luck into a predictable engineering process.