New Tool Radar

AI Concepts

How to Quantize a Model to Q4_K_M With llama.cpp

Convert a Hugging Face model to GGUF, quantize it to Q4_K_M, and test it with llama-cli.

Schematic with three steps: Convert the model to a full-precision GGUF; Quantize the f16 file to Q4_K_M; Test the quantized file.

GGUF is the file format used by llama.cpp, the inference engine that runs models on CPUs, Apple Silicon and consumer GPUs, and Q4_K_M is its most widely used quantization level: 4-bit, using the K-quant method, medium size. The published workflow converts a Hugging Face model to a full-precision GGUF first, then quantizes that file down to Q4_K_M. The conversion is only a format change with no quality loss; the quantize step is where the bit reduction happens.

What you need

  • A Hugging Face model downloaded locally
  • llama.cpp built from source
  • Enough disk space for both the f16 file and the quantized file

Step 1: Convert the model to a full-precision GGUF

Run the conversion script, for example python convert_hf_to_gguf.py with the model path, an outfile and outtype f16. This step only changes the format.

Step 2: Quantize the f16 file to Q4_K_M

Run the quantize tool on the f16 file, for example ./llama-quantize model-f16.gguf model-Q4_K_M.gguf Q4_K_M. The step takes a few minutes for a 7B model.

Step 3: Test the quantized file

Run the quantized file directly with llama-cli and a short prompt, for example asking it to explain quantization in one sentence, with a small output limit. Check the reply is coherent before using the file.

Worked example: synthetic llama.cpp quantization run

The table below is a synthetic example. It shows the expected result of a quantization run; no model was converted.

Step Command Result
Convert convert_hf_to_gguf.py, outtype f16 model-f16.gguf created
Quantize llama-quantize, Q4_K_M model-Q4_K_M.gguf created
Test llama-cli, one sentence Coherent reply
Size About a quarter of f16 Pass

Expected result (UNTESTED). the f16 file converts cleanly, the Q4_K_M file is about a quarter of its size, and a one-sentence test reply is coherent. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • GGUF is a file format, not a quantization method; GPTQ is a method, GGUF is the answer to what file format.
  • The K-quants use a mix of bit widths across different parts of the model to balance quality and size.
  • Q4_K_M is the practical recommendation for most users; if you have memory to spare, Q5_K_M gives a bit more quality.
  • If you have memory to spare and want more quality, go to Q5_K_M; if you are desperate for memory, Q3_K_M still works reasonably for larger models.

Sources

See all