How to Quantize an LLM to GGUF: Q4_K_M Guide

What quantization actually does

A language model is mostly a very large collection of numbers: its weights. In the FP16 format most models ship in, every weight takes 16 bits. Quantization stores those weights at lower precision — 8, 6, 5, or 4 bits — so the file gets much smaller and inference gets faster, at the cost of a small amount of accuracy.

GGUF is the file format used by llama.cpp and the ecosystem around it, including Ollama and LM Studio. When you download a file named something like model-Q4_K_M.gguf, you are looking at a GGUF file quantized to the Q4_K_M level. The naming is worth decoding: the number is roughly the bits per weight, K marks the newer K-quant family (which groups weights into blocks and scales each block separately), and the suffix — S, M, or L — is small, medium, or large within that level.

llama.cpp's own benchmark for Llama 3.1 8B shows the trade-off clearly: FP16 is 14.96 GiB and generates about 29 tokens per second, Q8_0 is 7.95 GiB at about 51 tokens per second, Q5_K_M is 5.33 GiB at about 67 tokens per second, and Q4_K_M is 4.58 GiB at about 72 tokens per second. Q4_K_M is the default recommendation for a reason: it keeps most of the quality while cutting the model to under a third of its FP16 size.



 
Source: Lilian Weng, Large Transformer Model Inference Optimization


Prerequisites

  • At least 16 GB of RAM for an 8B model — quantization loads the whole model into memory, and you need disk space for the original plus each quantized output.
  • Python 3, for the Hugging Face converter.
  • Access to the model you want (some gated models require accepting a license on Hugging Face).
  • llama.cpp, installed via its installer, pre-built binaries, or built from source following the project's build guide.

Step 1: Convert the Hugging Face model to GGUF

Quantization in llama.cpp is a two-phase process: first convert the original model to GGUF, then quantize the converted file. Keep the full-precision GGUF as your master copy — every quantized variant should be produced from it, never from another quantized file.

python convert_hf_to_gguf.py --outfile model-f16.gguf --outtype f16 ./model-dir

The --outtype f16 flag writes a full-precision GGUF. If the model is distributed in 16-bit format, you can also omit --outtype and let the converter decide. If you have not downloaded the model locally, the converter accepts a --remote flag with the repository name instead of a local directory.

Step 2: Pick your quantization level

  • Q4_K_M — the default. Best balance of size, speed, and quality for everyday chat and general use. Start here unless you have a specific reason not to.
  • Q5_K_M — one step up. Slightly larger and slower, slightly better quality. A good choice for code generation or reasoning-heavy work, where errors are more costly.
  • Q6_K or Q8_0 — near-lossless. Pick these when you have RAM to spare and want the closest thing to the original model, for example for evaluation.
  • Q3_K_M and below — last resort. These fit big models onto small machines, but the quality drop becomes visible.

Step 3: Quantize

The command itself is short. Run it from your llama.cpp directory, with the binary path adjusted to match how you installed it:

./build/bin/llama-quantize model-f16.gguf model-Q4_K_M.gguf Q4_K_M

To produce several levels from the same master, just repeat the command with different targets:

./build/bin/llama-quantize model-f16.gguf model-Q5_K_M.gguf Q5_K_M 
./build/bin/llama-quantize model-f16.gguf model-Q8_0.gguf Q8_0

Two flags are worth knowing: --leave-output-tensor keeps the output layer unquantized for a quality gain at some size cost, and --allow-requantize permits quantizing an already-quantized file — the official docs warn this can severely reduce quality, so treat it as an escape hatch, not a workflow.

Step 4 (optional): Use an importance matrix for better quality

An importance matrix, or imatrix, measures which weights matter most by running calibration text through the model. The quantizer then protects the important weights and spends fewer bits on the rest. The improvement is real but modest, and it costs an extra pass over calibration data — worth it for a model you plan to use heavily.

./build/bin/llama-imatrix -m model-f16.gguf -f calibration-data.txt -o imatrix.gguf -ngl 99 
./build/bin/llama-quantize --imatrix imatrix.gguf model-f16.gguf model-Q4_K_M.gguf Q4_K_M

For calibration data, a few hundred megabytes of plain text in the model's dominant language works — public-domain books or a Wikipedia dump are typical choices. The -ngl 99 flag offloads layers to the GPU if you have one.

Step 5: Test the result before you trust it

Run a quick generation to confirm the file loads and produces coherent text:

./build/bin/llama-cli -m model-Q4_K_M.gguf -p "Explain quantization in one sentence." -n 100

Then compare quant levels on a prompt you care about — a coding task, a few math questions. If Q4_K_M handles your real prompts, you are done.

Troubleshooting

  • Out of memory. You need roughly the full-precision model's size in free RAM, plus headroom. Close other programs, or quantize on a machine with more memory.
  • "Unknown chat template" warning from the converter. Fix it before distributing the file — a wrong chat template makes the model behave badly no matter how good the weights are.
  • The quantized model rambles or repeats. First check that you quantized from the FP16 master, not from another quantized file. Then try one level up (Q5_K_M) on the same prompts.

When to quantize yourself — and when not to

Do it yourself when you need a quant level nobody publishes, when you want an imatrix tuned to your domain, or when you want to verify exactly what went into the file. Skip it when a reputable publisher already offers the level you need. If your goal is simply to run a model locally, our Ollama walkthrough gets you running in 20 minutes with pre-quantized models. If you want to serve a model to multiple users with vLLM, that pipeline uses different quantization formats (AWQ, GPTQ, FP8) rather than GGUF.

Comments

Popular posts from this blog

Open-Source AI Weekly: Bonsai, GigaChat 4 Lead

Claude Claims Proof of Riemann Hypothesis After 167 Years

How to Build an AI Agent with LangGraph in 30 Minutes