Running a 4B Multimodal Model Locally: VRAM, Quantization, and llama.cpp Setup

    Learn how to choose between 4B and 9B multimodal models, estimate memory requirements, quantize language model weights, and configure an F16 vision projector in llama.cpp. This guide also covers performance measurement, chat template troubleshooting, and practical steps for reducing peak VRAM usage.

    Running a multimodal large language model on a personal device means balancing limited GPU memory and compute resources. A practical deployment starts with choosing a model size, then configuring quantization and the vision projector, checking the chat template, and resolving memory constraints.

    The commands below use llama.cpp. Command-line options can change between versions, so check the --help output for your installed version before deploying.

    Prerequisites

    Before starting, confirm that you have the following information and files:

    • Your operating system, driver version, GPU model, and VRAM capacity.
    • Your llama.cpp version, available through --version.
    • A language model GGUF file and a matching vision projector file, commonly called an mmproj file. Both should belong to the same model family; otherwise, image features may not align with the language model, producing unreliable answers.
    • A fixed test set containing both text-only questions and questions with images.

    1. Choosing Between 4B and 9B Models

    The practical differences between a 4B model, with four billion parameters, and a 9B model, with nine billion parameters, depend on quantization, context length, hardware, and concurrency. Parameter count alone does not determine which model is the better fit.

    Compare the two under the same conditions. Otherwise, the measurements will not be meaningful.

    Estimate weight storage and memory requirements

    A rough estimate of weight storage is:

    Weight size in bytes ≈ parameter count × bits per parameter ÷ 8

    At a theoretical four bits per parameter, a 4B model requires about 2 GB for weights, while a 9B model requires about 4.5 GB.

    Actual K-quant mixed quantization retains higher precision for some tensors. GGUF files also include embedding and output layers, along with metadata. As a result, Q4_K_M files are typically larger: approximately 2–2.5 GB for a 4B model and 5–5.5 GB for a 9B model.

    These ranges vary with model architecture, embedding size, and quantization version. Use the actual GGUF file size as the final reference.

    Weights are only part of the GPU memory requirement. Inference also consumes memory for the KV cache, vision encoder activations, and runtime overhead. Allow an additional 10–20% margin beyond the weights and KV cache to help prevent out-of-memory errors during usage spikes.

    Measure four key metrics

    Metric What it measures How to measure it
    Peak VRAM usage Maximum GPU memory usage during inference Sample with nvidia-smi or a system monitor during load testing
    Time to first token (TTFT) Time from sending a request to receiving the first output token Record the request start time and the arrival of the first data chunk on the client
    Generation speed (tokens/s) Output tokens generated per second after the first token Divide the generated token count by the generation time after the first token
    End-to-end latency Total time from sending a request to receiving the final token Measure on the client; note in the report whether the output includes reasoning content

    Use the following command to sample GPU memory usage once per second and save the results to a log:

    1. nvidia-smi --query-gpu=timestamp,memory.used --format=csv -l 1 > mem.log

    After testing, find the maximum recorded memory usage.

    For client-side timing, Python’s time.perf_counter() can record when the request is sent and when the first data chunk arrives. The difference provides a TTFT measurement based on that first chunk.

    Use the same test questions for every metric. The median across multiple requests is more reliable than a single result.

    Models with extended reasoning capabilities may produce lengthy intermediate reasoning. A 4B model will often generate individual tokens faster, but if it produces more reasoning tokens for the same question, its end-to-end latency may not be lower. Record both the output token count and total latency when comparing models.

    Make the decision in three steps

    1. Check whether the model fits. VRAM must accommodate the weights, vision projector, and KV cache for the target context length, with room to spare.
    2. Adjust model size or quantization if necessary. If a 9B model does not fit, try lower-bit quantization or choose a 4B model.
    3. Compare accuracy on your own data. For tasks involving complex reasoning or long-document understanding, speed measurements cannot replace accuracy testing.

    2. Quantization and the F16 Vision Projector

    The language model is typically converted to an F16 GGUF file first, then quantized with llama-quantize:

    1. llama-quantize model-F16.gguf model-Q4_K_M.gguf Q4_K_M

    Q4_K_M is a common balance between memory use and accuracy. Q5_K_M generally stays closer to the original model’s accuracy but requires more storage and VRAM. Q3 variants use less memory, but the accuracy loss may be more noticeable and should be checked against your test set.

    The vision projector converts image information into features the language model can use. It is relatively small compared with the language model and directly affects image-understanding quality. Keeping it in F16 is therefore recommended rather than reducing its precision solely to save memory.

    Some model conversion scripts can export the vision projector separately. Check the script’s --help output for the exact options supported by your version.

    Start the server

    The following is an example configuration. Confirm the options against your installed version:

    1. llama-server \
    2. -m ./models/model-Q4_K_M.gguf \
    3. --mmproj ./models/mmproj-F16.gguf \
    4. -ngl 99 \
    5. -c 8192 \
    6. --port 8080

    The main options are:

    • -m: Path to the language model file.
    • --mmproj: Path to the vision projector file.
    • -ngl: Number of layers to offload to the GPU. Higher values place more computation on the GPU. A sufficiently large value attempts to offload all layers.
    • -c: Context length. Larger values increase KV cache memory usage.

    Verify text and image requests separately

    Start with a text-only request to confirm that the server is working:

    1. curl http://127.0.0.1:8080/v1/chat/completions \
    2. -H "Content-Type: application/json" \
    3. -d '{"messages":[{"role":"user","content":"Introduce yourself in one sentence."}]}'

    The returned JSON should contain a choices field, and the response should be relevant to the question.

    Next, test a request containing an image. If text-only requests work but image requests fail, first check whether the language model and mmproj file belong to the same model family.

    3. Enabling and Troubleshooting Chat Templates

    A chat template defines how user, system, and assistant messages are assembled into input the model understands. An incorrect template can cause the model to emit unexpected role markers or continue generating without stopping.

    Recent versions of llama.cpp can use --jinja to process the template embedded in the GGUF file. You can also select a built-in template with --chat-template, such as chatml. Check --help for the template names available in your version.

    When troubleshooting, change only one setting at a time and retest. Follow this order:

    Order Symptom What to check Action
    1 Output contains markers such as `< im_start >`, or the response style is clearly abnormal Whether the template name or version matches the model Verify the template in the model documentation, or use the embedded template with --jinja
    2 Output does not stop, or repeated beginning-of-sequence (BOS) tokens appear at the start Whether special tokens are being added more than once Check whether the client already formats messages manually; apply the template in only one place
    3 An error reports unsupported roles, or system messages are ignored Whether message roles follow the template’s requirements Merge the system message into the first user message, or adjust the roles to match the template

    4. What to Do When VRAM Is Insufficient

    Apply the following changes in order, retesting after each step:

    1. Reduce -c. Check the KV cache size in the startup logs, then use nvidia-smi to confirm that peak memory usage has decreased.

    2. Lower -ngl. Keep some layers on the CPU instead of offloading them to the GPU. This reduces speed but can allow the model to run when GPU memory is insufficient.

    3. Reduce batch sizes with -b and -ub. Also limit individual image resolution if your version provides an option for it. These changes are intended to reduce peak VRAM usage.