Running 26B to 30B LLMs on a Single 24 GB GPU: A Practical Guide

    This guide explains how to fit 26B to 30B parameter GGUF models on one 24 GB GPU using llama.cpp, Ollama, or other GGUF-compatible tools. It covers why total parameters determine memory use while active parameters determine speed in MoE models, how to estimate VRAM for weights, KV cache, and vision encoders, and how to tune context length, KV cache quantization, and parallel requests. It also gives a step-by-step order for resolving out-of-memory errors.

    This guide is for developers running models locally with llama.cpp, Ollama, or other tools that support the GGUF format. It does not cover model training, multi-GPU parallelism, or high-concurrency production serving.

    1. Total parameters determine VRAM, and active parameters determine speed

    Mixture of Experts (MoE) models replace some network layers with multiple expert networks. During inference, a gating mechanism selects a few experts for each token. As a result, a model with 26B total parameters and about 4B active parameters performs only about 4B parameters of computation for each generated token.

    This design reduces computation but not storage. All expert weights must stay in VRAM. The gate can select any expert, and that expert must run immediately once selected. Whether a model fits therefore depends on its total parameter count after quantization, not its active parameter count. The active parameter count mainly affects how many tokens the model generates per second.

    2. Calculating the VRAM budget

    A 24 GB GPU must divide its VRAM among four parts: model weights, the vision encoder (only in multimodal models), the KV cache, and runtime overhead. The total of these four must not exceed the usable VRAM. Otherwise, loading or inference fails with an OOM (out of memory) error.

    Start with the weights. At FP16 precision, each parameter takes 2 bytes. The weights of a 14B dense model take about 28 GB, which does not fit on a 24 GB GPU. With Q4_K_M quantization, each weight takes about 4.85 bits on average. Based on this, the weights of a 26B model take about 16 GB, and those of a 30B model take about 18 GB. These figures come from parameter counts. For actual file sizes, check the size listed on the model’s release page or in the downloaded file, because some tensors in a GGUF file keep higher precision.

    The KV cache stores the keys and values for every past token in every layer. Its size can be estimated as follows:

    1. KV cache VRAM ≈ 2 × layers × KV heads × head dimension × context length × bytes per element

    The factor of 2 accounts for keys and values, which are stored separately. The number of KV heads is not the same as the number of attention heads. Models that use grouped-query attention (GQA) or multi-query attention (MQA) have far fewer KV heads than attention heads. If you use the attention head count in the formula, you overestimate VRAM. Use the value of num_key_value_heads in the model’s configuration file.

    Consider a hypothetical example with 48 layers, 8 KV heads, a head dimension of 128, a context length of 8,192 tokens, and an FP16 KV cache (2 bytes per element). Substituting these values gives 2 × 48 × 8 × 128 × 8192 × 2 bytes, or about 1.5 GiB. If you quantize the KV cache to q8_0, its size roughly halves.

    The formula gives only a rough range. After loading the model, use the VRAM figures in the framework log or the output of nvidia-smi.

    Take a 30B model as an example. Its weights take about 18 GB, which leaves about 6 GB for the CUDA context, runtime buffers, the vision encoder, and the KV cache. If the vision encoder takes about 1 GB, only 4 to 5 GB remains for the KV cache. This is why you must control both context length and parallel slots.

    3. Multimodal models: vision encoder and context length

    A multimodal model has a vision encoder in addition to its language component. The encoder converts images into tokens. In llama.cpp, the vision encoder is usually a separate mmproj file, which you load with an additional parameter. It uses VRAM, and higher image resolution produces more image tokens, which increases the KV cache.

    The context budget is therefore tighter for multimodal use than for text-only use. Load the model with a short context first, such as 4,096 tokens. Confirm that it runs stably, then increase the context gradually. The upper limit depends on the model’s training context length, the number of tokens each image produces, whether KV cache quantization is enabled, and the framework’s implementation. Do not apply a fixed number from another setup.

    For high-resolution images, reduce the image size before sending it to the model. This cuts both the vision encoder’s computation and the number of image tokens.

    4. Code models: long context and KV cache quantization

    Code completion and codebase Q&A require placing large amounts of file content in the context. The KV cache then becomes the main VRAM cost. A 30B code model has weights of about 18 GB, which leaves limited room for context. Two approaches help.

    First, lower the KV cache precision. In llama.cpp, set the key and value cache types separately with --cache-type-k and --cache-type-v. q8_0 is a common choice. Quantizing the V cache usually also requires enabling Flash Attention. Ollama also provides environment variables for configuring the KV cache. Lower precision causes some quality loss, so compare outputs on your own code tasks instead of relying only on VRAM numbers.

    Second, limit the number of parallel requests. When a server handles several requests at once, each parallel slot holds its own KV cache. Increasing the number of slots from 1 to 2 roughly doubles KV cache usage. For single-user local use, set the parallel count to 1. This is the safest option.

    5. Choosing Q4_K_M

    Q4_K_M is a 4-bit quantization format common in the llama.cpp ecosystem. It belongs to the K-quant family. It uses different quantization precision for weights of different importance, which balances file size and quality.

    Q4_K_M suits most single 24 GB GPU setups, but it is not the best choice for every model. Judge it by three criteria. First, after you add the vision encoder and KV cache budgets to the weights, is at least 2 GB of VRAM still free? Second, is the output quality acceptable for your tasks, such as code completion or image-and-text Q&A? Third, does the generation speed meet your needs?

    If VRAM is insufficient, switch to a smaller quantization, such as a lower-bit variant. Quality will drop more noticeably. If you have VRAM to spare, try Q5_K_M. It produces a larger file but usually gives better quality. Test again after either change. Do not decide based on file names alone.

    6. Inference settings and OOM troubleshooting

    GPU layers. A GGUF model can run some or all of its Transformer layers on the GPU. Layers that are not on the GPU run on the CPU. In llama.cpp, the -ngl parameter sets how many layers to offload. A very large value offloads as many layers as possible. A higher value is not always better. First confirm that the sum of weights, vision encoder, KV cache, and runtime overhead fits in the usable VRAM. Then raise the layer count as far as possible. If loading fails with an out-of-memory error, reduce the count by about 5 layers at a time until the model loads reliably. Partial offloading slows generation, but it is more useful than a failed load.

    Context length. In llama.cpp, the -c parameter sets the context window. It directly limits the KV cache and is the most effective tuning setting.

    VRAM monitoring. After you start the server, run nvidia-smi in a terminal and check memory.used. When generating long outputs, run watch -n 1 nvidia-smi to monitor usage in real time. nvidia-smi also works on Windows. If VRAM usage keeps rising as the output grows, the KV cache is growing. Reduce the context length further.

    OOM adjustment order. When you hit an OOM error, try these steps in order. Change one setting at a time, and record the result of each change.

    1. Reduce the context length.
    2. Enable KV cache quantization.
    3. Set the parallel request count to 1.
    4. Reduce the number of GPU layers.
    5. Lower the image resolution for multimodal inputs, or temporarily skip loading the vision encoder.
    6. Switch to a smaller quantized model file.

    This order adjusts the settings with the least effect on quality first. It changes the model itself last.

    7. Summary

    Whether a 26B or 30B model runs on a 24 GB GPU depends on the combined size of the quantized weights, vision encoder, KV cache, and runtime overhead. Total parameter count and active parameter count alone do not decide it. MoE architectures reduce the computation per token well below the total parameter count, but all weights must still fit in VRAM. Q4_K_M is a common compromise, and you should verify it against the file size of your specific model and your own tasks. When deploying, start with a short context and a single parallel slot. Then relax the settings based on measured VRAM usage. Keep the OOM adjustment order above for troubleshooting.