Skip to content
mentoor

Topic 6 of 6 · 6 min read

Quantizing a model yourself with llama.cpp

Goal: Write the command sequence that turns a Hugging Face model into a Q4_K_M GGUF file and test it.

Quantizing yourself is three commands: convert to a 16-bit GGUF, optionally build an importance matrix, then run llama-quantize, and then test the result.

Check yourself · 1 of 3

What is the correct order to produce a Q4_K_M file from a Hugging Face model?

Choose one answer

Why it matters

You need this when no good quant exists: a fine-tune of your own, a new release, or a level the uploaders skipped. It also teaches what the files contain. Do it only when needed; for a popular model a quant from the maker or a known quantizer is cheaper and often tested.

Before you start

  • Disk space: the original weights, a 16-bit GGUF of the same size, and the quant. For a 8B model, about 16 GB for each of the first two.
  • A built llama.cpp (from source or a release), and its Python requirements for the converter.
  • A local copy of the model with its tokenizer files.

The steps

# 1. get the original weights
huggingface-cli download Qwen/Qwen2.5-7B-Instruct --local-dir ./qwen-src

# 2. convert to a 16-bit GGUF
python convert_hf_to_gguf.py ./qwen-src --outtype f16 --outfile qwen-f16.gguf

# 3. (optional) importance matrix from sample text
llama-imatrix -m qwen-f16.gguf -f calibration.txt -o qwen.imatrix

# 4. quantize
llama-quantize --imatrix qwen.imatrix qwen-f16.gguf qwen-Q4_K_M.gguf Q4_K_M

Use calibration text that looks like your real use: code for a coding model, your language for a multilingual one. A few hundred KB is usually enough. Without --imatrix, Q4_K_M still works; the matrix helps most below Q4.

Check the result

# loads and answers?
llama-cli -m qwen-Q4_K_M.gguf -p "List three prime numbers." -n 64

# quality against the 16-bit baseline
llama-perplexity -m qwen-Q4_K_M.gguf -f wiki.test.raw

Compare the size to what you expected from the arithmetic, run the same prompt set you used on the baseline, and keep the pass rate with the file.

Before and after

Before: llama-quantize on the original .safetensors folder, which fails because it expects a GGUF input. After: convert first, then quantize.

What goes wrong

  • Converting from an already quantized model. Always start from 16-bit or 32-bit weights.
  • Calibrating on text unlike the real workload, which tunes the matrix to the wrong tokens.
  • Skipping the test and publishing a file that loads but fails the task set.
  • Mismatching the converter and the llama.cpp build, so the file's metadata is wrong or unreadable.

Examples

Command order

Convert with convert_hf_to_gguf.py to an f16 file, build the matrix with llama-imatrix, then call llama-quantize with the matrix and the type name. The type name is the last argument.

A missing step

If llama-quantize prints an error about the file magic, the input was not a GGUF. Run the converter first.

Key points

  • The sequence is download, convert to f16 GGUF, optional imatrix, llama-quantize, then test.
  • llama-quantize takes a GGUF input, not a Hugging Face folder.
  • Calibrate the importance matrix on text that looks like your real workload.
  • Always start from 16-bit or 32-bit weights, never from another quant.
  • Test the output with a quick generation, perplexity and your own task set, and record the size and result.

Common mistakes

  • Passing a safetensors folder straight to llama-quantize.
  • Re-quantizing a Q8 file to Q4 and expecting full quality.
  • Using the same calibration text for every model and language.

Check yourself · 2 of 3

Why should an importance matrix use text similar to your real workload?

Choose one answer

Check yourself · 3 of 3

llama-quantize fails on a folder of .safetensors files. What is the fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here