GGUF model quantization with llama.cpp starts by converting a supported checkpoint into a high-precision GGUF, then producing lower-precision versions from that source. The project’s quantization instructions describe this two-stage process and an optional importance matrix for reducing quantization error. Keep the source file so each candidate can be compared against the same baseline.
This guide follows that workflow through calibration, evaluation, local serving and app import. Its reusable script creates a new output directory for each run, stops when a command fails and avoids silently reusing calibration from another checkpoint. The final choice is the smallest candidate that still performs acceptably on the tasks you need.
Don't miss new tech stories on Google
Add Tech Insider once in the Google app and our stories appear in your news suggestions.
Choose the checkpoint and budget for the conversion
Hugging Face’s GGUF format documentation distinguishes GGUF from tensor-only storage: a GGUF contains tensors and standardized model metadata. A GGUF can hold high-precision or quantized tensors. The filename extension therefore identifies the container, while a label such as Q4_K_M describes the quantization choice.
Choose a model architecture supported by the llama.cpp checkout you will use, and retain its tokenizer and configuration files when downloading the checkpoint. The example below uses google/gemma-4-E2B-it because it is the model in the upstream converter example. Our small open-model comparison addresses model selection; conversion cannot compensate for choosing a model unsuited to the task.
Budget for the original checkpoint, the high-precision GGUF, calibration data and every quant you keep. The quantizer documentation says its model-loading process needs enough RAM for the input and enough disk for intermediate files. A final file that fits your GPU does not imply that the conversion can run within the same memory budget.
| Quantization | Average bits per weight | Model file size |
|---|---|---|
| Q2_K | 3.1593 | 2.95 GiB |
| Q3_K_M | 3.9960 | 3.74 GiB |
| Q4_K_M | 4.8944 | 4.58 GiB |
| Q5_K_M | 5.7036 | 5.33 GiB |
| Q6_K | 6.5633 | 6.14 GiB |
| Q8_0 | 8.5008 | 7.95 GiB |
| F16 | 16.0005 | 14.96 GiB |
The example’s Q4_K_M file is about 69.4% smaller than its F16 file, calculated from 4.58 GiB and 14.96 GiB. That is useful for planning this model’s storage. It is not a promise that every checkpoint will shrink by the same proportion. Hugging Face lists the underlying Q4_K block representation at 4.5 bits per weight; a mixed whole-model quantization can have a different average, as the table shows.
Build the tools, then download a fixed model revision
Use Bash on Linux or macOS for the commands below. Start with Git, Python, CMake and a working compiler. Clone the llama.cpp repository before installing its converter requirements, so the requirements file is resolved from the correct directory. Record the commit used to build the binaries.
git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python -m pip install -U huggingface_hub transformers
git rev-parse HEAD > llama-build-commit.txt
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j 4
./build/bin/llama --help
./build/bin/llama-quantize --help
./build/bin/llama-imatrix --help
The project enables Metal by default on macOS. For NVIDIA acceleration, install the CUDA toolkit and configure the build with cmake -B build -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release before the build command. Check the configuration output rather than assuming that the intended backend was enabled. For CPU-only evaluation, the inference tools accept a GPU-layer count of zero.
The quantizer documentation notes that Gemma 4 needs Transformers 5, while the requirements can install Transformers 4. That is why the setup upgrades Transformers. Keep the installed dependency versions with your build record when you settle on a reproducible configuration.
The Hugging Face CLI guide documents hf auth login, hf download, revision selection and a dry-run mode. Authenticate through its interactive flow when access is required. Copy the chosen model repository’s commit identifier into the prompt below; the same revision is used for the size preview and download.
hf auth login
MODEL_ID='google/gemma-4-E2B-it'
read -r -p 'Model repository commit: ' MODEL_REV
: "${MODEL_REV:?A model revision is required}"
mkdir -p models
hf download "$MODEL_ID" --revision "$MODEL_REV" --dry-run
hf download "$MODEL_ID" --revision "$MODEL_REV" --local-dir ./models/source-checkpoint
printf '%s
%s
' "$MODEL_ID" "$MODEL_REV" > model-source.txt
A failed download should be resolved before conversion. Preserve the full model directory rather than selecting only weight files: the converter needs the model’s accompanying information. If the architecture is unsupported, check upstream support and choose a compatible revision or model before proceeding.
Convert the language model and its multimodal components
The converter script is convert_hf_to_gguf.py. For the downloaded model directory, create a BF16 reference file using the same output type as the upstream Gemma example. The converter and quantizer reference also supports automatic type selection for the usual high-precision checkpoint workflow.
python convert_hf_to_gguf.py ./models/source-checkpoint --outfile ./models/source-bf16.gguf --outtype bf16
For image or audio use, the language-model weights are only one part of the conversion. The upstream guide describes a separate multimedia-projector GGUF, which can contain encoders as well as projection components. It recommends keeping those components at relatively high precision because they prepare the inputs presented to the language model.
python convert_hf_to_gguf.py ./models/source-checkpoint --mmproj --outfile ./models/mmproj-q8_0.gguf --outtype q8_0
| Artifact | Purpose | When to retain it |
|---|---|---|
| Source checkpoint directory | Original weights, tokenizer and configuration | For provenance and a fresh conversion |
| High-precision model GGUF | Common input for each quantization candidate | For comparison and re-quantization from the source |
| Importance-matrix GGUF | Calibration information for that checkpoint and corpus | With the calibration and build records |
| Quantized model GGUF | Candidate used for inference | After evaluating its intended tasks |
| Multimedia-projector GGUF | Required encoders/projectors for supported media input | Alongside the matching multimodal model |
Calibrate for the checkpoint, then create each quant separately
The importance-matrix tool takes a model and a text corpus. Prepare a nonempty calibration file representative of the language and tasks you expect, and keep a separate evaluation corpus for comparison. The command below limits processing to 200 chunks as an example setting; the documented default is to process all available chunks.
mkdir -p calibration
test -s ./calibration/calibration-data.txt
./build/bin/llama-imatrix -m ./models/source-bf16.gguf -f ./calibration/calibration-data.txt -o ./models/source-imatrix.gguf --output-format gguf --chunks 200
for QUANT in Q4_K_M Q5_K_M Q6_K Q8_0; do
./build/bin/llama-quantize --imatrix ./models/source-imatrix.gguf ./models/source-bf16.gguf "./models/model-${QUANT}.gguf" "$QUANT"
done
Create each candidate from the high-precision reference. The quantizer warns that enabling re-quantization of already-quantized tensors can substantially worsen quality. Reuse the importance matrix across these candidates only while the checkpoint and calibration setup remain the same; a familiar filename alone is not evidence that an old matrix belongs to a new model.
For repeated work, save the following as quantize-local.sh in the repository root. It accepts a downloaded checkpoint directory, a calibration text file and a new output-directory path. It deliberately refuses an existing output directory, records the build commit and removes an incomplete output on failure while leaving completed artifacts available for inspection.
#!/usr/bin/env bash
set -euo pipefail
checkpoint=${1:?Pass the local checkpoint directory}
calibration=${2:?Pass the calibration text file}
output=${3:?Pass a new output directory}
test -d "$checkpoint"
test -s "$calibration"
test -f convert_hf_to_gguf.py
test -x ./build/bin/llama-quantize
test -x ./build/bin/llama-imatrix
mkdir "$output"
pending=''
trap 'if [[ -n "$pending" ]]; then rm -f -- "$pending"; fi' EXIT
git rev-parse HEAD > "$output/llama-build-commit.txt"
printf '%s
' "$checkpoint" > "$output/checkpoint-path.txt"
cp -- "$calibration" "$output/calibration.txt"
pending="$output/source.partial.gguf"
python convert_hf_to_gguf.py "$checkpoint" --outfile "$pending" --outtype bf16
mv -- "$pending" "$output/source-bf16.gguf"
pending=''
pending="$output/imatrix.partial.gguf"
./build/bin/llama-imatrix -m "$output/source-bf16.gguf" -f "$output/calibration.txt" -o "$pending" --output-format gguf --chunks 200
mv -- "$pending" "$output/imatrix.gguf"
pending=''
for quant in Q4_K_M Q5_K_M Q6_K Q8_0; do
pending="$output/model-${quant}.partial.gguf"
./build/bin/llama-quantize --imatrix "$output/imatrix.gguf" "$output/source-bf16.gguf" "$pending" "$quant"
mv -- "$pending" "$output/model-${quant}.gguf"
pending=''
done
bash quantize-local.sh ./models/source-checkpoint ./calibration/calibration-data.txt ./models/quant-run
A smaller bit label is not a universal quality score. The project’s perplexity documentation even reports no consistent improvement from adding more Wikitext calibration tokens in its published comparison. Treat corpus selection and chunk count as settings to evaluate, rather than assuming that a longer calibration run always produces the better file.
Evaluate the quant on both speed and the intended task
Use llama-bench’s documented tests to measure prompt processing and generation separately. It averages repeated runs and can write individual samples in JSON. These measurements exclude tokenization and sampling time, so they describe model execution rather than complete application latency.
test -s ./evaluation.txt
for model in ./models/quant-run/source-bf16.gguf ./models/quant-run/model-*.gguf; do
./build/bin/llama-bench -m "$model" -p 512 -n 128 -r 5 -o json > "${model}.bench.json"
./build/bin/llama-perplexity -m "$model" -f ./evaluation.txt > "${model}.perplexity.txt" 2>&1
done
Use the same hardware, backend, offload settings and evaluation text across the candidates. The example makes the prompt length, generated length and repetitions explicit. To examine a longer existing conversation, the benchmark’s -d option prefills the KV cache to the specified token depth; use a depth representative of the application rather than extrapolating from an empty context.
The perplexity evaluation guide defines lower perplexity as better next-token prediction, but warns against comparing different models or tokenizers as though the values shared one scale. Compare your quant against its own source on the same corpus. Then exercise the actual application: structured output, a coding task or a conversation can expose a failure that a single aggregate score would conceal.
Our recommendation is to start with Q4_K_M as one candidate, not as an automatic winner. Move to a larger quant when the smaller file fails a relevant task, and retain evidence for the decision. The model-size table helps narrow the candidates; the results you collect determine whether the chosen file and context fit the machine. For the separate hardware choice, see our DGX Spark and Mac Studio comparison.
Run the file locally or import it into an app
The current command interface is llama cli for a terminal conversation and llama serve for the local web interface and compatible API. Liquid AI’s llama.cpp deployment documentation shows both interfaces with local files. The commands below use the newly quantized model and an explicit context size.
./build/bin/llama cli -m ./models/quant-run/model-Q4_K_M.gguf -c 4096 -st -p 'Write a Python function that reverses a list.'
./build/bin/llama serve -m ./models/quant-run/model-Q4_K_M.gguf -c 4096 --host 127.0.0.1 --port 9931
Use the same prompt and settings with source-bf16.gguf when checking a candidate’s output. For media input, load the matching projector using --mmproj. Once the local server starts, a chat-completions request can use its loopback endpoint:
curl --fail-with-body http://127.0.0.1:9931/v1/chat/completions -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Say hello in one sentence."}]}'
Ollama’s import documentation says that importing a GGUF does not quantize it. Point a Modelfile at the completed quant, then create and run the local model. Keep the model’s expected chat behavior rather than inserting a stop token copied from a different architecture.
printf 'FROM ./models/quant-run/model-Q4_K_M.gguf
' > Modelfile
ollama create my-local-quant
ollama run my-local-quant
For a split GGUF, Ollama instructs users to retain the original shard names and use a wildcard matching all shards in the FROM path. LM Studio’s local import guide documents its experimental lms import command, followed by an interactive import prompt:
lms import ./models/quant-run/model-Q4_K_M.gguf
Check the destination runtime as well as the file. For example, Transformers’ GGUF loading documentation describes a packed path for Qwen3.5 and its MoE variant with the appropriate kernel, while other architectures use a loader that dequantizes the weights. The same GGUF file can therefore lead to a different memory footprint in a different runtime.
Share the quant with enough information to reproduce it
Use the Hub upload command only after selecting the file you want to distribute. The destination below is entered interactively, and the upload creates the repository if it does not already exist. Check the intended repository visibility and the model’s redistribution terms before uploading.
read -r -p 'Destination Hub repo (owner/name): ' DESTINATION
: "${DESTINATION:?A destination repository is required}"
hf upload "$DESTINATION" ./models/quant-run/model-Q4_K_M.gguf model-Q4_K_M.gguf
Include the base-model identifier and revision, llama.cpp commit, quantization type, calibration description and the evaluation conditions with the artifact. Keep the source revision record produced during download alongside the script’s build record. If you distribute a multimodal quant, explain which matching projector it requires.
This leaves a traceable result: a quant made from a specific checkpoint, calibrated for a recorded corpus and compared with its own high-precision reference. Repeat the checks when changing the model, calibration or runtime. A completed conversion is the beginning of that decision, not proof that every task survived the reduction in precision.





