How to Quantize a Local Model Yourself

llama-quantize turns a full-precision GGUF into a Q4_K_M or an i-quant. The two-phase pipeline, the importance matrix, and how to check the compression cost.

Choosing between published quantizations is a download. The 9B model used as an example below arrives as ready-made GGUF model files running from 3.19 GB to 17.92 GB, the largest being the unquantized BF16 build. Building those files yourself is different: it needs a repository checkout, a Python environment, a CPU-bound quantizing run and enough free disk for the uncompressed model and its output at once. It is still right in a few specific situations, and the tool that makes it possible is llama-quantize.

When to quantize it yourself

Three cases justify the work.

Nobody has published a quant for your model. A model released this week, or a fine-tune of your own, has weight files and nobody’s compressed version. Unsloth’s Qwen3.5-9B repository is the shape of a well-served model: 25 GGUF files totalling 147,803,478,272 bytes (Hugging Face API). A repository with no quants carries none.

You want the importance matrix built from your own text. The quantizer can weight each tensor by how much it matters on a calibration corpus. The calibration file is whatever text you pass in, so a model meant for one domain can be calibrated on text from that domain instead of the generic Wikitext set.

You need a size nobody publishes. The public ladder is shorter than the quantizer’s menu. The quantizer’s own type table lists 39 names, three of which are aliases (Q3_K, Q4_K and Q5_K point at Q3_K_M, Q4_K_M and Q5_K_M) and one of which is a copy mode that quantizes nothing (quantize.cpp). For one 9B model the published ladder runs from 3.19 GB to 17.92 GB, with the popular points at Q4_K_M (5.68 GB), Q6_K (7.46 GB) and Q8_0 (9.53 GB).

If a published quant sits at the size you want, take it. The rest of this page is the path for when one does not.

The pipeline: convert first, quantize second

llama-quantize takes a GGUF file as input, not safetensors. Its documentation states the structure plainly: “Quantization is done in two phases:” (llama-quantize docs).

StageCommandResult
Convert the weightspython convert_hf_to_gguf.py --outfile model-bf16.gguf --outtype bf16 --remote <repo>A full-precision GGUF
Convert the projector (multimodal models only)python convert_hf_to_gguf.py --mmproj --outfile mmproj-q8_0.gguf --outtype q8_0 --remote <repo>A separate mmproj file
Build an importance matrixllama-imatrix -m model-bf16.gguf -f calibration.txt -ngl 99imatrix.gguf
Quantizellama-quantize --imatrix imatrix.gguf model-bf16.gguf model-Q4_K_M.gguf Q4_K_MThe compressed model

The two convert lines follow the commands the quantize documentation uses with google/gemma-4-E2B-it; the importance-matrix and quantize lines follow the examples in the imatrix documentation. The converter’s --outtype accepts f32, f16, bf16, q8_0, tq1_0, tq2_0 and auto (convert_hf_to_gguf.py).

Three catches before you start. The Python requirements install transformers 4, and the quantize documentation notes that more and more models, Gemma 4 among them, need transformers 5. The converter is not standalone: it imports the gguf-py package beside it in the repository, so you need the clone rather than the script alone. And disk is not optional, because “As the models are currently fully loaded into memory, you will need adequate disk space to save them and sufficient RAM to load them. At the moment, memory and disk requirements are the same.” For Llama 3.1 that is 32.1 GB for the 8B model and 280.9 GB for the 70B model before anything is compressed.

Picking a type

The quantizer’s source annotates most k-quant and legacy types with a file size and the perplexity penalty each carries; the i-quants get a bits-per-weight figure instead. The rows below are drawn from the Llama-3-8B set, and the same sizes and penalties appear in GiB and against FP16 in the perplexity documentation’s scoreboard; in the same table F16 and BF16 are recorded against Mistral-7B.

TypeSize (GiB)Extra perplexity vs FP16
Q8_07.96+0.0026
Q6_K6.14+0.0217
Q5_K_M5.33+0.0569
Q4_K_M4.58+0.1754
Q4_K_S4.37+0.2689
Q4_04.34+0.4685
Q3_K_M3.74+0.6569
Q3_K_S3.41+1.6321
Q2_K2.96+3.5199

A second table measures Llama-3.1-8B across types, and it is where the trade-off gets interesting: the i-quants are not simply smaller versions of the k-quants. Central values shown; the source reports each speed with an uncertainty.

TypeBits per weightSize (GiB)Prompt t/s @ 512Generation t/s @ 128
IQ2_XXS2.38242.23852.3979.86
Q2_K3.15932.95784.4579.85
Q3_K_M3.99603.74783.4471.68
IQ4_XS4.45974.17771.8077.51
Q4_K_M4.89444.58821.8171.93
Q5_K_M5.70365.33758.6967.23
Q6_K6.56336.14812.0158.67
Q8_08.50087.95865.0950.93
F1616.000514.96923.4929.17

At 2.38 bits per weight, IQ2_XXS generates at 79.86 tokens per second in that table, ahead of the 4.89-bit Q4_K_M at 71.93. Smaller is not automatically slower, but the numbers hold for one model, and the table does not name the hardware they were measured on. The perplexity documentation adds a general warning about the project’s figures: “llama.cpp numbers are not directly comparable to those of other projects because the exact values depend strongly on the implementation details” (llama-perplexity docs).

Building the importance matrix

An importance matrix holds per-tensor activation statistics collected by running the model over calibration text. The quantizer uses it to spend bits where they matter.

The model and a calibration file are both mandatory, and the documentation’s example calibration file is wiki.train.raw. Offloading to the GPU is the documented speed path: “For faster computation, make sure to use GPU offloading via the -ngl | --n-gpu-layers argument” (llama-imatrix docs).

Two details are worth knowing. Statistics for output.weight, and for token_embd.weight when the model ties its embeddings to the output, are not collected by default, and the documentation says it is “Typically, it is better not to utilize the importance matrix when quantizing these tensors”. And several runs can be merged with --in-file, described as “Useful for merging files from multiple runs/datasets”. Recent versions write GGUF by default, with the legacy binary format still available through --output-format dat.

The instinct that a bigger calibration corpus gives a better matrix finds no support in the project’s own Llama 3 8B scoreboard: “There seems to be no consistent improvement from using more Wikitext tokens for the importance matrix.”

Measuring what the compression cost you

llama-perplexity is the measuring instrument. The convention among contributors is Wikitext-2, and the repository ships a script that downloads it and prints the invocation to use.

QuantImportance matrixSize (GiB)PerplexityKL divergenceMean change in token probability
Q8_0none7.966.2342840.001355-0.019%
Q5_K_Mnone5.336.2886070.010762-0.114%
Q4_K_MWikitext 10m4.586.3829370.028152-0.389%
Q4_K_Mnone4.586.4071150.031273-0.596%
Q3_K_MWikitext 10m3.746.7342550.084358-1.198%
Q3_K_Mnone3.746.8884980.101913-1.990%
Q2_KWikitext 10m2.968.6478250.332223-6.500%
Q2_Knone2.969.7515680.445132-9.123%
IQ2_XXSWikitext 10m2.2414.0917820.812022-14.363%

Those rows are the Llama 3 8B scoreboard from the perplexity documentation, measured on CUDA with one RTX 4090; the matrices in it are labelled WT, meaning Wikitext token counts. Two things fall out. The matrix helps at every size, and most where the damage is steepest: at Q4_K_M the KL divergence is 0.028152 with it against 0.031273 without, and at Q2_K it is 0.332223 against 0.445132. And the 2-bit cliff is real: IQ2_XXS carries a perplexity of 14.091782 against an FP16 baseline of 6.233160.

Producing the KL divergence column takes an extra step, because the comparison needs the FP16 logits. The tool records them to a binary file, and “The logit file will be very large, 11 GiB for LLaMA 2 or 37 GiB for LLaMA 3 when using the Wikitext-2 test set.” That file is the --kl-divergence-base argument for the quantized run, which then adds --kl-divergence.

Two limits on the measurement. Perplexity “is not directly comparable between models, especially if they use different tokenizers”, so the numbers only mean something against a baseline with the same tokenizer. And the documentation notes that finetunes “typically result in a higher perplexity value even though the human-rated quality of outputs increases”, so a worse score on a fine-tune is not automatically a failed quant.

Getting the tools, and shipping the result

The rolling build archives published by the project contain the tools you need. The Linux x64 archive for build b12345, published 13 November 2026, contains llama-quantize, llama-imatrix, llama-perplexity, llama-cli and llama-server (llama-b12345-bin-ubuntu-x64.tar.gz). It holds no Python converter, because the HF-to-GGUF script needs the repository around it. The build number changes with every build, several times a day, so take the current one from the releases list.

Building from source is the alternative; for an NVIDIA GPU the build documentation gives cmake -B build -DGGML_CUDA=ON followed by cmake --build build --config Release (build docs). The third option needs neither: the GGUF-my-repo space on Hugging Face builds quants in the browser, and the quantize documentation notes it “syncs from llama.cpp main every 6 hours”.

One thing to plan for at the end. Ollama will not do this for you: “Ollama does not quantize GGUF models during import.” Its documentation then points you at a GGUF tool to prepare and quantize the file first (Ollama import docs). So quantize first, then point a Modelfile at the result with FROM /path/to/file.gguf, then run ollama create.

The Bottom Line

Quantizing a model yourself is worth it in three situations: no published quant exists, you want the importance matrix calibrated on your own corpus, or you need a size nobody built. Otherwise, download the quant, and use GGUF vs AWQ vs GPTQ vs MLX: Which Quantization to Use to pick the format and What Quantization Costs You in Quality to understand what you will lose.

When you do build one, the sequence is: convert to GGUF, build an importance matrix if you want one, quantize, then measure. Budget disk for the input and the output at once, keep the projector at bf16 or q8_0 because “For these reasons, multimodal components are usually kept in a high-quality format such as bf16 or q8”, and avoid --allow-requantize, whose warning reads “This can severely reduce quality compared to quantizing from 16bit or 32bit”. If the model is a fine-tune, Fine-Tune a Local LLM with LoRA and QLoRA comes first, and the result needs the same verification as any downloaded file.

Other GPUs and tasks: Local AI by VRAM · RSS