Running MoE Models on Consumer GPUs: Balancing VRAM and System RAM

    Learn how to deploy mixture-of-experts models on consumer GPUs by distributing weights across VRAM and system RAM. This guide covers memory estimates, expert offloading, context-length trade-offs, deployment steps, and performance measurement. It also explains why a model’s active parameter count does not determine its total memory requirements.

    1. Why Mixture-of-Experts Models Matter

    A mixture-of-experts (MoE) architecture divides feed-forward networks into multiple expert subnetworks. For each input token, a router scores the experts and selects only a small subset to perform computation. As a result, a model’s total parameter count differs from the number of parameters activated during inference.

    Consider a model with approximately 35 billion total parameters and 3 billion active parameters. Its forward-pass computation can be closer to that of a 3-billion-parameter dense model, while its capacity draws on the full set of experts. This partially separates model capacity from computational cost, making MoE models attractive for personal hardware.

    That separation applies to computation, not storage. All weights still need to reside somewhere in the storage hierarchy: VRAM, system RAM, or disk. The central deployment challenge is therefore deciding where to place weights so that limited VRAM supports the most frequent and latency-sensitive computation.

    2. Estimate Memory Requirements First

    Before deployment, estimate three categories of memory use: model weights, the KV cache—the attention states that grow with context length—and runtime overhead.

    A rough estimate for weight storage is:

    Weight size ≈ parameter count × average bits per parameter ÷ 8

    For a 35-billion-parameter model quantized to approximately 4 bits per parameter, the weights occupy about 17.5 GB. That exceeds an 8 GB GPU’s capacity, so the entire model cannot reside in VRAM.

    The following functions estimate weight and KV cache sizes:

    1. def estimate_weight_gb(params_billion, bits_per_param):
    2. """Estimate quantized weight size in GB."""
    3. return params_billion * 1e9 * bits_per_param / 8 / 1e9
    4. def estimate_kv_cache_gb(layers, kv_heads, head_dim, context_len, bytes_per_elem=2):
    5. """Estimate KV cache size in GB, storing both K and V."""
    6. return 2 * layers * kv_heads * head_dim * context_len * bytes_per_elem / 1e9
    7. print(estimate_weight_gb(35, 4)) # Approximately 17.5

    These formulas provide only rough estimates. Actual usage also includes compute buffers, system-reserved memory, and driver overhead. Plan for 10–20% headroom.

    3. Expert Offloading: Separate Resident and On-Demand Weights

    Effective offloading starts by dividing weights into two categories.

    The first includes components used for every token: token embeddings, attention layers, the router, and shared experts, if the architecture includes them. These components are accessed frequently and are sensitive to latency, so they should receive priority for GPU placement.

    The second category consists of routed expert weights. They account for most of the model’s size, but only a few experts are activated for any individual token. These weights can reside in system RAM, with disk considered only when RAM is insufficient.

    The placement strategy follows three principles:

    1. Keep frequently used, latency-sensitive computation in VRAM.
    2. Place large, sparsely accessed expert weights in system RAM.
    3. Use disk only when RAM is also insufficient, because disk access latency can substantially slow generation.

    A placement plan might look like this. This is an illustrative structure, not a valid configuration for a specific tool:

    1. placement:
    2. gpu:
    3. - token_embedding
    4. - attention_layers
    5. - router
    6. - shared_experts
    7. cpu_memory:
    8. - routed_experts # Accessed on demand
    9. kv_cache: gpu # Fall back to system RAM if VRAM is insufficient
    10. context_length: 65536

    The claim that “inactive experts consume no memory” depends on how the model is loaded. With memory-mapped loading, only pages that have been accessed need to become resident in RAM. Over a long conversation, however, different tokens may access more experts, expanding the resident working set.

    A low memory reading at one point therefore describes only that moment. It does not establish the session’s steady-state memory requirement.

    4. Context Length Versus Speed

    KV cache size grows linearly with context length. Increasing the context from 4K to 64K makes the KV cache approximately 16 times larger. When VRAM is limited, the cache may become a bottleneck before the weights do.

    In practice:

    • Set the context limit according to the task rather than automatically choosing the maximum.
    • Summarize or truncate long conversations periodically, since conversation history continues to accumulate.
    • Measure the impact of moving the KV cache to system RAM before deciding whether the resulting slowdown is acceptable.

    5. Deployment Workflow

    1. Prepare the inference environment. Choose an inference runtime that supports MoE models and allows control over weight placement. Following its official installation and build documentation is usually the most straightforward approach.

    2. Obtain the model files. Choose a community-tested quantized version. Lower-bit quantization reduces file size but generally introduces greater quality loss. Compare two or three versions using the same set of test prompts.

    3. Validate with a small configuration. Start with a short context window and confirm that output is correct before gradually increasing the context limit.

    4. Tune systematically. Change one variable at a time. Record VRAM usage, system RAM usage, time to first token, and generation speed in a comparison table.

    5. Wrap the model as a service if needed. If you expose an API, add authentication, rate limiting, and logging. Bind only to the necessary network addresses to avoid accidentally exposing the service.

    6. What Affects Performance?

    Generation speed depends primarily on:

    • The amount of data transferred between system RAM and VRAM: more transfers mean higher latency.
    • System memory bandwidth and disk read speed.
    • The distribution of activated experts, since different topics may select different expert combinations.
    • Context length and KV cache placement.
    • Inference thread count and resource consumption by background processes.

    Even two tests on the same machine can produce noticeably different results. For a meaningful comparison, keep the prompts, sampling temperature, and output length fixed. Run multiple measurements and average the results rather than treating a single fluctuation as a conclusion.

    7. Common Troubleshooting

    • Out of VRAM at startup: Reduce the number of GPU-resident layers, lower the context limit, or use a lower-bit quantized version.