Running MoE LLMs Locally: A Practical Guide to Layered Weight Placement in VRAM and System Memory

    This article explains why memory bandwidth, not raw compute, is the real bottleneck for running large language models on consumer GPUs, and how Mixture-of-Experts (MoE) architectures enable a practical middle ground: keeping hot-path weights in VRAM while offloading large expert weights to system memory. It covers configuration, tuning methodology for offloaded layer counts, VRAM budgeting for long contexts, troubleshooting common failure modes, and when to move inference to the cloud.

    1. The Real Bottleneck of Local Deployment: Memory, Not Compute

    When running large models on consumer GPUs, most failures are not caused by insufficient compute power, but by weights that simply won’t fit. Inference—especially single-request decoding—is fundamentally a memory-bandwidth-bound task: every generated token requires a full pass over the weights involved in the computation. The metrics that determine speed are therefore VRAM bandwidth and “how much data must reside on high-speed media,” not theoretical FLOPS.

    This leads to a common misconception: users see “30 billion total parameters” and assume the model can’t run. In reality, what determines speed is the number of parameters actually involved in each step of computation. MoE architectures happen to decouple these two things.

    2. MoE: Decoupling Total Parameters from Activated Parameters

    Mixture of Experts (MoE) uses a gating network (router) to select a few experts out of many at each layer. Think of it as a team of specialists: the roster has dozens of people, but only two or three are called in to handle each question.

    This gives rise to two naming conventions: total parameters / activated parameters. For example, a model with “35 billion total parameters, 3 billion activated” has capacity close to a 35B model, but the per-step computation is only at the 3B level—so its decoding speed approaches that of a much smaller model. This is exactly what makes local deployment viable:

    • Dense models: every parameter must be read every step. If the weights don’t fit in VRAM, your only options are lowering precision across the board or switching to a smaller model—there is no intermediate state.
    • MoE models: there is a natural “hot/cold split.” You can keep most expert layers in CPU memory while keeping only a few layers resident in VRAM, creating an adjustable trade-off point.

    3. Core Idea: Hot Path Stays in VRAM, Cold Path Sinks to System Memory

    Mainstream local inference runtimes generally provide an experimental switch whose semantics are roughly “force N MoE layers’ weights to be computed in CPU memory.” Its purpose is not to compress the model, but to change which medium the weights reside on:

    1. # Illustrative configuration (field names may vary by runtime — check the actual documentation)
    2. {
    3. "gpu_layers": 999, # offload as much as possible to the GPU
    4. "cpu_moe_layers": 15, # force 15 MoE layers' weights to stay in system memory
    5. "context_length": 32768,
    6. "quantization": "3bit-class"
    7. }

    The effect can be understood as follows: attention layers, shared layers, and gating layers—smaller components used every step—stay in VRAM; the bulky expert weights sink to system memory layer by layer. Since MoE activates only a few experts per step, the offloaded layers are not “excluded from computation” but rather participate at lower bandwidth. The overall speed loss is far smaller than offloading dense model weights to system memory.

    4. Who This Is For—and Who It Isn’t

    Well suited for:

    1. GPUs with 8–16 GB of VRAM that want to run MoE models with ~30B total parameters;
    2. 24 GB GPUs that need very long contexts—KV cache grows linearly with context, so VRAM must be freed up;
    3. Systems with ample RAM (recommended at least 1.5× the model file size) in a dual-channel or multi-channel high-bandwidth configuration.

    Not suited for:

    • Dense models: this option is largely ineffective for them;
    • Systems with tight memory already using swap: offloading just moves the bottleneck from VRAM to disk, and speed falls off a cliff;
    • Interactive scenarios demanding minimal time-to-first-token: CPU participation increases latency jitter.

    5. Starting Points by VRAM Tier

    VRAM Suggested starting point Notes
    8 GB 3-bit-class quantization + a higher number of offloaded layers First make it fit, then optimize for speed
    12 GB / 16 GB Try offloaded layers at 10 / 15 / 20 / 25 Change only one variable at a time
    24 GB + long context Start at 5 The main goal is freeing room for KV cache

    These numbers are search starting points, not optimal values. The optimum depends on the specific model’s layer structure, quantization format, and memory bandwidth.

    6. Tuning Methodology: Use Benchmarks, Not Guesswork

    A recommended approach is “single-variable grid search + dual-metric evaluation”:

    1. Fix the quantization format, context length, and concurrency;
    2. Vary only the number of offloaded layers, scanning in steps of 5;
    3. Record two metrics per setting: decoding throughput (tokens/s) and time to first token (TTFT);
    4. Also record peak VRAM and system memory usage to confirm no swapping occurs;
    5. Pick the setting with “the highest speed and no dangerously small OOM headroom,” then fine-tune by ±2 layers.
    1. # Illustrative tuning script
    2. for n in [0, 5, 10, 15, 20, 25]:
    3. cfg = base_config(cpu_moe_layers=n)
    4. m = run_benchmark(cfg, prompt_len=2048, gen_len=256)
    5. print(n, m.tokens_per_sec, m.ttft_ms, m.vram_peak, m.ram_peak)

    An empirical pattern: throughput usually declines as offloaded layers increase, but the decline curve often flattens out after a certain range. If “more layers makes it slower,” it usually means memory bandwidth is saturated or swapping has kicked in.

    7. Special Considerations for Long Contexts

    Going from 4K to 32K context can consume several GB of VRAM for the KV cache. In this case, explicitly split your VRAM budget:

    1. Total VRAM ≈ resident weights + KV cache + activation buffers + safety margin (0.5–1 GB recommended)

    First estimate the KV cache for your target context, then decide the number of offloaded layers based on the remaining space. This is far more reliable than “stuffing weights in first and blowing up VRAM later.” If space is still insufficient, consider KV cache quantization (trading slight precision for space) or reducing batch concurrency.

    8. Troubleshooting Common Failure Modes

    • OOM on load: too few offloaded layers, or quantization precision too high;
    • Extremely slow, CPU at 100%: insufficient memory has triggered swapping—check available RAM and swap;
    • Speed drops instead of improving: offloaded layers exceed what your memory bandwidth can sustain; step back 5 layers and retry;
    • Noticeably degraded output quality: quantization precision is too low—this is unrelated to the offloading strategy; raise the quantization level;
    • Slows down on the later part of long texts: KV cache is crowding out VRAM, forcing some layers to be offloaded passively; re-allocate the budget.

    9. When to Move to the Cloud

    The marginal benefit of local inference declines quickly when you need stable concurrency, million-token contexts, strict SLAs, or heavyweight capabilities such as multimodality and tool calling. In these cases, a more sensible path is to use general-purpose cloud capabilities—container platforms to host inference services, object storage for models and data, and logging services with monitoring and alerting for observability—with elastic scaling on the platform side replacing local hardware stacking. Baidu AI Cloud offers capabilities in these general categories that can serve as a reference point for architecture selection.

    10. Summary

    MoE’s decoupling of “total parameters vs. activated parameters” provides the only viable intermediate state for VRAM-constrained local deployment: sink the largest expert weights layer by layer into system memory, and keep the hot path in VRAM. Methodologically, three principles are enough to converge reliably—quantize first, then layer; run single-variable grid searches; and decide based on the dual metrics of throughput and time to first token, with an explicit VRAM budget reserved for the KV cache.