Quantizing to 4 bits throws away precision, and an importance matrix tells the quantizer which weights deserve the precision that remains. It is built by running the full-precision model over real text and recording which weights see large activations. The result is a small file that llama-quantize uses to protect the important weights — most useful at 4 bits and below, where the plain quantizer has the least room to work with.
Related guide: How to Quantize a Model Smaller with llama.cpp
Build the matrix, then quantize
Start from a full-precision GGUF — convert a Hugging Face checkpoint with convert_hf_to_gguf.py at --outtype bf16 and keep that file as your master copy, because the quantizer refuses to requantize an already-quantized file by default. Then build the matrix from calibration text (a few megabytes of text in the domain the model will serve is better than generic text):
llama-imatrix -m model-bf16.gguf -f calib.txt -o imatrix.gguf -c 512 --chunks 200 -ngl 99
llama-quantize --imatrix imatrix.gguf model-bf16.gguf model-q4-k-m.gguf Q4_K_M
The -ngl 99 on the imatrix step pushes the layers to the GPU, which is the difference between minutes and an hour on a large model. The quantizer writes the new file; the original is untouched.
Measure, do not guess
Score the quantized model against the full-precision one with llama-perplexity on held-out text, and compare against a no-imatrix quantization of the same model. One published comparison found the imatrix version ahead by roughly 14% less degradation on the same recipe. If the imatrix version is not better on your text, your calibration text does not match your workload — rebuild it from your own documents.
Expected result. We checked the llama.cpp repository for the quantize/imatrix/perplexity tools and the converter, and the linked guides for the exact llama-imatrix and llama-quantize commands and the rule about keeping the BF16 master copy. We did not run this software ourselves for this guide, so no output here is a recorded result; steps marked UNTESTED were checked against the sources below, not run.
Limits
An importance matrix helps most at low bit rates; at Q8 it buys little. Calibrating on text unlike your workload can make the result worse, not better, and building the matrix takes real time on a large model. We did not run llama-imatrix or llama-quantize, so no number here is a measured result on this machine.
Sources
- llama.cpp repository — llama-quantize, llama-imatrix and llama-perplexity are part of the project; convert_hf_to_gguf.py converts a Hugging Face checkpoint to GGUF
- Quantize an LLM to GGUF with llama.cpp — llama-imatrix needs a model and a calibration file; llama-quantize refuses to requantize an already-quantized file by default; keep the BF16 GGUF as the master copy
- Quantize your own GGUF with imatrix — llama-imatrix -m model -f calib.txt -o imatrix.gguf; llama-quantize –imatrix imatrix.gguf model-f16.gguf model-q4.gguf Q4_K_M; -ngl 99 pushes layers to the GPU
Checked 2026-11-28.
