Running Large Language Models Locally with Limited VRAM: A Practical Guide to MoE Offloading and Context Quantization

    This guide explains how to self-host a Mixture-of-Experts (MoE) model with roughly 35 billion total parameters on a consumer desktop with 8GB of VRAM and 16GB of system memory, while supporting multi-turn conversations with a context length of around 60,000 tokens. It covers why sparse activation makes local deployment feasible, the distinct roles of VRAM, system memory, and storage, how to build a MoE-capable inference runtime from source, which GGUF quantization level to pick, and a step-by-step tuning process for layered CPU/GPU offloading and KV cache quantization, along with realistic performance expectations and optimization directions.

    1. Background: Why Consumer Hardware Can Run “Large” Models

    Over the past year, open-source Mixture-of-Experts (MoE) models have become a mainstream choice for local deployment. These models have a very large total parameter count, but each forward pass activates only a small subset of their expert networks, so the compute per inference is far lower than that of a dense model of comparable size. This characteristic opens a window for personal devices with limited VRAM: a model with roughly 35 billion total parameters and about 3 billion activated parameters can have a compute profile similar to a small dense model, while often delivering quality closer to a much larger dense model.

    The scenario discussed here is very specific: a mid-range consumer desktop with 16GB of system memory and a GPU with 8GB of VRAM, intended to self-host an MoE model with about 35 billion total parameters and serve multi-turn conversations with a context length of around 60,000 tokens. The core tension is that the model weights still take up about 18GB after 4-bit quantization, more than twice the VRAM capacity, and long contexts consume a considerable amount of additional VRAM on top of that.

    2. Core Principle: Why Weights Do Not All Need to Fit in VRAM

    To understand the whole approach, start by breaking down the role each resource plays during a single inference.

    • VRAM: Provides the compute for matrix multiplications and high-speed data supply, and holds the KV cache for the attention mechanism. It determines “how fast you compute” and “how much you can remember.”
    • System memory: In a weight-streaming setup, it mainly acts as a relay and buffer, moving weights from storage to VRAM; it does not itself participate in large-scale parallel computation.
    • Storage: Inactive expert weights can stay resident on disk and be read on demand, the so-called “disk on standby.”

    The key property of MoE is sparse activation: in each conversation turn, only about 3 billion parameters actually take part in computation, while the other 30-plus billion sit idle. So rather than insisting that the full set of weights stay resident in VRAM, the engineering goal should be to place the parts that most affect speed and stability in VRAM, the routing layers that select which experts to activate, the shared experts, the attention modules, and some feed-forward layers, and to keep the rarely activated expert layers in system memory or even on disk, scheduling them on demand through layered offloading.

    A common misconception is to blame the bottleneck on memory capacity. In practice, under weight streaming, the main constraint on generation speed is memory bandwidth. Capacity only needs to be enough to hold the resident portion plus scheduling buffers, while bandwidth directly determines how many weights can be moved per second, and therefore how many tokens can be generated per second. This also explains why upgrading dual-channel memory to a higher-frequency spec often yields a noticeable speed improvement.

    3. Preparation: Building the Inference Runtime from Source

    The first step in local deployment is to obtain an inference runtime that supports MoE layered offloading and context quantization. It is recommended to clone the official source repository and compile locally to get the fastest support for new architectures and features. The build usually depends only on a basic compilation toolchain; just follow the repository instructions to configure and compile.

    If you are not comfortable with the command line, you can hand a request like “help me compile a local inference runtime from source” to an AI coding tool and let it handle environment checks, dependency installation, and build verification. Note that build artifacts are typically executables under the build directory, and you should reference that path directly when starting the service later.

    For model weights, it is recommended to obtain a quantized GGUF version from a public model hosting platform. Choosing a 4-bit quantization level (such as a non-uniform scheme like IQ4_NL) is a common balance point between VRAM usage and quality: compared with an 8-bit version, VRAM usage is nearly halved, while the quality loss is not obvious in most conversation and coding scenarios.

    4. Key Configuration: Layered Offloading and Parameter Tuning

    When starting the local inference service, the parameters are designed around one goal: fitting both the resident weights and the KV cache within 8GB of VRAM while keeping VRAM utilization in a healthy range. An illustrative startup looks like this (see comments for parameter meanings):

    1. ./build/bin/llama-server \
    2. --host 0.0.0.0 \ # Serve externally, so other devices on the LAN can access it
    3. --port 1234 \ # Service port
    4. --timeout 600 \ # Request timeout
    5. --threads -1 \ # Auto-detect thread count
    6. --batch-size 2048 \ # Batch size; affects throughput and peak VRAM
    7. --ctx-size 64000 \ # Context length; set according to actual needs
    8. --n-gpu-layers 99 \ # Max out GPU offloaded layers to put as much as possible in VRAM
    9. --flash-attn auto \ # Enable fused attention implementation
    10. --cache-type-k q4_0 \ # KV cache quantization; significantly reduces context VRAM
    11. --cache-type-v q4_0

    The parameter that really needs repeated experimentation is the number of CPU offloaded layers. The recommended tuning process is: first set the GPU offloaded layers to a sufficiently large value, then gradually increase the CPU offloaded layers starting from 0, sending a real request after each adjustment and observing VRAM usage and generation speed, stopping when VRAM utilization reaches about 80%. The reserved 20% is for the KV cache and temporary buffers; the exact amount to reserve depends on the target context length and concurrency. With 8GB of VRAM and a 60,000-token context, offloading about 33 layers to the CPU is a workable balance point, with system memory usage around 3GB and VRAM usage around 7.3GB.

    Context quantization is another switch that is easy to overlook. Setting the key and value types of the KV cache to 4-bit quantization can reduce the VRAM cost of long contexts by several times, and is a necessary condition for “running long contexts on limited VRAM.” The cost is a slight drop in retrieval accuracy under extremely long contexts, which is acceptable for most applications.

    5. Performance Expectations and Tuning Directions

    With the configuration above, when multi-turn conversation accumulates to about 23,000 tokens, generation speed can hold at a level of a dozen or so tokens per second, which is in the “barely usable but feels smooth” range and on the same order as the response speed of commercial cloud models. To improve further, try the following directions in order:

    1. Increase memory bandwidth: Switching to higher-frequency dual-channel memory usually gives the most direct benefit.
    2. Adjust the quantization level: Under the premise of acceptable quality, switch to a lower-bit quantization to free VRAM for more resident layers.
    3. Optimize batch size and threads: Batch size affects throughput and peak VRAM, and needs to be tuned jointly with context length.
    4. Shorten the context: Set the context length according to actual business needs, and avoid paying for redundant history.

    6. Deployment Advice and Cloud Coordination

    The value of local deployment lies in keeping data inside the intranet, zero marginal cost, and the ability to run offline, making it suitable for personal experiments, intranet knowledge bases, and privacy-sensitive scenarios. When the business needs higher concurrency, longer contexts, or a more stable service level, a device-cloud collaboration approach can be adopted: the local side handles interaction and preprocessing, while the cloud hosts the inference service through a container platform, with object storage managing model versions and logging and monitoring services providing observability. In such hybrid architectures, Baidu AI Cloud typically plays the role of elastic compute and operations, and specific capabilities are subject to the official documentation.

    Overall, the MoE architecture combined with layered offloading and context quantization has brought the “35-billion-parameter model” within reach of an ordinary personal computer. Understanding how VRAM, memory, and bandwidth divide the work matters more than memorizing parameters.