Local LLM Deployment: Tuning VRAM Usage and Inference Performance
This article explains how to tune local large language model inference when a service runs but performs poorly. It covers MoE expert-layer offloading, KV cache precision, context length probing, parallel slots, batch size, and non-generative conversation compression, with reproducible settings for 8GB GPUs.
1. Background: Running Is Not the Same as Running Well
When deploying large language models locally, a common situation is that the inference service starts normally and the API returns results, yet GPU utilization stays low and generation speed remains in the single digits per second. The cause is usually not the hardware, but runtime parameters left at generic defaults. Context length, KV cache precision, the placement of mixture-of-experts layers, and batch size all directly change the balance between VRAM usage and compute scheduling. This article focuses on these easily overlooked variables and provides a reproducible tuning approach.
Note that local inference engines typically offer both a graphical interface wrapper and a command-line service. Their underlying capabilities are the same; the difference is how many parameters are exposed. For deep tuning, use the command-line service, because critical items such as VRAM allocation, cache precision, and parallel slots can only be controlled precisely there.
2. Mixture-of-Experts Architecture: Offloading Strategy Matters More Than Parameter Count
Mixture-of-experts (MoE) models have a very large total parameter count, but each token activates only a few expert networks. This creates a key characteristic: expert layers are computationally sparse, while attention layers are computed at every step.
Testing an MoE model with roughly 30 billion total parameters and about 3 billion activated parameters on an 8GB VRAM device shows a clear difference between two strategies:
- All layers loaded into VRAM: about 7549MB used (about 93%), generation speed about 3 tok/s;
- Only attention layers resident in VRAM, with expert layers computed in CPU memory: about 2603MB used (about 32%), generation speed about 21 tok/s.
The latter saves about 65% of VRAM and improves speed by about 7x. The reason is that VRAM is used for the frequently accessed attention computation, while the sparsely activated expert portion is placed in memory that has larger capacity but lower bandwidth, so the overall pipeline is not slowed down.
The practical point is that most inference engines do not automatically identify which tensors belong to expert layers. You need to manually determine the naming pattern (expert weights usually carry a feature like ffn_.*_exps) and then specify placement through tensor-level offloading parameters. With a graphical interface, adjustment is generally limited to a coarse “number of layers” granularity, which lacks precision.
3. KV Cache Precision: An Undervalued Performance Variable
The KV cache grows linearly with context length, and its storage precision affects speed far more than generally expected. Running an 8-billion-parameter model on the same 8GB GPU, the measured results for different configurations are:
| Cache precision combination | Parallel slots | Context | Speed | |
|---|---|---|---|---|
| Quantized precision combination A | 4 | 8K | 19.4 tok/s | |
| Low-bit combination | 1 | 8K | 38.2 tok/s | |
| Half-precision combination | 1 | 8K | 51.7 tok/s | |
| Half-precision combination | 1 | Auto | 64K | 26.2 tok/s |
Half precision is nearly 3x faster than a certain quantized precision, but it has higher VRAM overhead. Therefore, the correct strategy is not to pursue low-bit precision blindly, but to first calculate the VRAM requirement for a half-precision cache, use half precision if it fits, and step down level by level only if it does not.
The estimation formula is:
KV_MB = 2 × layers × kv_heads × head_dim × ctx × bytes / 1024²
The factor of 2 corresponds to the two caches for keys and values, and bytes is determined by precision (2 for half precision, about 1 for 8-bit quantization, and about 0.5 for 4-bit).
4. Context Length: Skip Formula-Based Prediction and Use Binary Probing
The VRAM estimation formulas circulating in the community are mostly fitted for 8-bit weights and half-precision caches. Once more aggressive quantization is enabled, they severely overestimate VRAM requirements and compress the usable context to 4K or even lower. In testing, the actual usable limit of the same model reached 64K.
A more reliable approach is to let the inference engine provide the answer through binary probing:
upper = min(native_context, 65536)lower = 4096repeat up to 5 times:mid = (upper + lower) / 2if service starts with ctx = mid: lower = midelse: upper = midadopt lower
It converges in at most 5 attempts, at the cost of tens of seconds of startup overhead, in exchange for a configuration close to the hardware’s real boundary.
5. Parallel Slots and Batch Size: Two Traps in Single-User Scenarios
The default value for parallel slots is usually 4, designed to serve multiple concurrent users, with VRAM split evenly into four parts. In a single-user scenario, set it to 1; measured speed improved from 18.5 tok/s to 38.2 tok/s, nearly doubling.
There is no universal optimal value for batch size (ubatch); it depends on model structure and GPU bandwidth. The measured trend is that the longer the context, the more noticeable the benefit of a larger batch size—about 7.6% improvement at 8K context and about 21.6% at 64K context. In engineering practice, there is no need to guess from documentation; simply run a benchmark for each candidate value and take the best.
6. Long Conversation Compression: Do Not Use Generative Summarization
When the context approaches its limit, history needs to be compressed. If a local model is called to generate a summary, it competes for the same slot as the main inference, causing blocking or even timeouts. A more robust approach is to use non-generative strategies: keep recent dialogue with a sliding window by turn, apply rule-based extraction to earlier content (keeping user intent sentences and key conclusion sentences), or write history to local storage and retrieve and backfill it on demand. These approaches do not consume inference resources and have controllable latency.
7. Recommended Implementation Workflow
- Determine whether the scenario is single-user or concurrent, and set parallel slots accordingly;
- Identify the model architecture, and prioritize tensor-level offloading for MoE models;
- Calculate the half-precision cache requirement using the formula to decide cache precision;
- Use binary probing to determine the context limit;
- Run two to three benchmark tests for batch size;
- Use non-generative compression for long conversations, and observe VRAM and throughput changes with logs and monitoring metrics.
Tuning in the order above typically yields several times the throughput on 8GB-class GPUs while keeping VRAM usage at a safe level. For multi-machine or elastic scenarios, you can further use a container platform to host the inference service, object storage to hold model weights, and monitoring and alerting to track VRAM and latency metrics, forming a reusable setup consistent across local and cloud environments.
