Running 35B MoE Models on 16GB VRAM: A Guide to Q4 Quantization
Running a 35B Mixture of Experts (MoE) model on a 16GB VRAM GPU is possible through Q4 quantization and strategic memory offloading. This article explores how to calculate memory requirements, establish standardized testing metrics, optimize deployment parameters, and troubleshoot common issues like out-of-memory errors and throughput drops. It also outlines an evaluation method for prompt generation tasks to ensure consistent model performance.
1. Calculating Memory Requirements First
Many local deployment failures stem from miscalculating the model’s memory footprint. Loading a 35B parameter model in FP16 requires about 70GB for weights alone (2 bytes per parameter), far exceeding 16GB of VRAM.
Q4 quantization compresses each parameter to an average of 4 to 5 bits. Taking the common GGUF Q4_K_M as an example, the weight file for a 35B model is typically around 20GB, depending on the exact quantization file and model structure. Even with a weight file under 20GB, the GPU must also accommodate the KV cache, computation buffers, and runtime overhead. 16GB of VRAM is rarely enough to keep all weights resident.
The MoE (Mixture of Experts) architecture offers a viable workaround. These models contain numerous expert networks but only activate a few for each token—around 3B active parameters. Therefore, you can offload the majority of expert weights to system memory, keeping only the attention layers, shared layers, and frequently used layers on the GPU. The trade-off is that when a required expert is not in VRAM, data must traverse the PCIe bus from system RAM, subjecting performance to PCIe and memory bandwidth limits.
Running such models on 16GB VRAM depends on three factors: acceptable weight size, a reasonable offload ratio, and a context length that doesn’t over-consume the cache. If any of these exceed limits, performance will degrade noticeably.
2. Defining Testing Standards
Tokens-per-second benchmarks found online often lack necessary context, making them hard to reproduce. A comparable test record should include the following fields:
- Hardware: GPU model, VRAM capacity, driver version, CPU model, system RAM capacity and frequency.
- Software: Inference engine and version, CUDA or other backend versions, quantized file name and size, and the file’s SHA256 checksum.
- Parameters: Context length, parallel slots, number of GPU layers, whether MoE expert tensors are offloaded to CPU, and KV cache data type.
- Metrics: Prefill speed (tokens/s), generation speed (decode, tokens/s), time to first token, and peak VRAM usage during runtime (sampled via
nvidia-smi). - Workload: Number of input tokens, number of output tokens, number of repeated tests, and the method used to calculate the median.
Prefill and generation speeds should be recorded separately. Long prompts slow down prefilling, while everyday chat is primarily affected by generation speed. Mixing the two prevents readers from identifying the actual bottleneck.
3. Key Deployment Tips
- Start with a smaller context length, such as 4096, and incrementally increase it once stability is confirmed. The KV cache grows linearly with context length, making long contexts a common cause of out-of-memory errors.
- Gradually adjust the number of GPU layers. Start with fewer layers, monitor the VRAM peak, and then increase until you approach the limit, leaving a 1GB to 2GB buffer for other applications and computation buffers.
- If using newer versions of
llama.cpp, you can use specific parameters to keep MoE expert tensors in CPU memory. Parameter names may vary between versions, so consult the help documentation for your specific version. - Enable Flash Attention if your inference engine and GPU support it, as this reduces the VRAM overhead for attention calculations.
- Close other applications that consume VRAM, such as hardware acceleration in web browsers or local image generation software, to prevent resource contention.
4. Common Failures and Fallback Strategies
- Failure 1: Out of memory on startup. This usually happens when GPU layers are set too high or the context is too long. First, reduce the context length, then decrease the GPU layers to keep more experts and layers in system memory.
- Failure 2: Short conversations work, but long ones crash mid-way. As the conversation grows, the KV cache expands until it exceeds remaining VRAM. The solution is to limit the context window and implement truncation or summarization for conversation history at the application level.
- Failure 3: Speed suddenly drops to single-digit tokens/s. This indicates an excessive offload ratio, causing massive data transfers back and forth over the PCIe bus, or system RAM exhaustion leading to disk swapping. Check system monitoring for memory usage; if heavy disk read/write occurs, reduce the offload amount or switch to a lower-bit quantized version.
Fallback strategies, in order of priority: shrink the context length, reduce GPU layers, switch to a lower-bit quantized file (e.g., from Q4 to Q3, though output quality must be re-evaluated), or offload complex tasks to cloud-based models. Change only one parameter at a time to isolate the effect.
5. Evaluating Prompt Generation Tasks
Text-to-image workflows are a typical application for these models. A user provides a short description, and the model outputs a prompt containing natural language paragraphs and modular tags. Evaluation should rely on fixed criteria rather than subjective impressions.
It is recommended to prepare at least 50 input descriptions covering characters, scenes, styles, and multi-subject scenarios. For each output, verify the following:
- Is the format complete? For instance, are all required paragraphs or tags present?
- Did the model introduce new subjects not present in the original input?
