Skip to main content
Quantization reduces the precision of model weights, shrinking model size and speeding up inference with minimal quality loss. This is essential for running large language models on consumer hardware.

What is Quantization?

Quantization converts high-precision model weights (32-bit or 16-bit floats) to lower precision formats (2-8 bits). For example:
  • Original F32: 26 GB for a 7B model
  • F16: 14 GB (50% reduction)
  • Q4_K_M: ~4.5 GB (83% reduction)
  • Q2_K: ~3 GB (88% reduction)
The tradeoff is small accuracy loss, measured in perplexity (ppl). With proper quantization, this loss is often negligible.

Quick Start

The llama-quantize tool converts GGUF models from high precision to quantized formats:

Quantization Types

llama.cpp supports many quantization methods. Here are the most important ones:
Size: ~5.3 GB for 7B model
Quality: +0.06 ppl @ Llama-3-8B
Speed: Slightly slower than Q4
Best for: When quality is more important than size, users with more RAM
Noticeably better quality than Q4 with only ~20% size increase.
Size: ~8 GB for 7B model
Quality: +0.003 ppl @ Llama-3-8B
Speed: Moderate
Best for: When maximum quality is needed, enough RAM available
Minimal quality loss compared to F16, good for validation.
Q2_K Size: ~3 GB for 7B model (+3.5 ppl)
Q3_K_M Size: ~3.7 GB for 7B model (+0.7 ppl)
Speed: Very fast
Best for: Very limited RAM, mobile devices, when size is critical
Noticeable quality degradation but still functional.

Complete Quantization List

For reference, here’s the complete list of supported quantization types:
Perplexity values are from Llama-3-8B benchmarks. Lower perplexity increase = better quality. The “K” variants (Q4_K_M, Q5_K_M) use importance matrix techniques for better quality.

Advanced Quantization

Using Importance Matrix (imatrix)

Importance matrix quantization uses statistical data from real prompts to minimize quality loss:
1

Generate Importance Matrix

First, create an imatrix file by running text through the model:
The calibration data should be representative of your actual use case.
2

Quantize with imatrix

Use the imatrix during quantization for better results:
This typically reduces perplexity by 10-30% compared to naive quantization.
Using an importance matrix is highly recommended for quantization levels below Q5_K_M, as it significantly improves quality.

Advanced Options

Quantize different parts of the model to different levels:
Useful for preserving quality in critical layers while saving size elsewhere.
Control quantization of the output projection:
The output tensor significantly affects generation quality.
Special quantization for token embeddings:
Embeddings can often be more aggressively quantized.
Quantize all tensors to the exact same type:
By default, some tensors use different quantization for quality. --pure disables this.

Requantization

You can requantize an already-quantized model, though quality loss accumulates:
Warning: Requantization severely degrades quality. Always quantize from F16 or F32 when possible.

Complete Workflow Example

Here’s a complete example from raw model to optimized GGUF:

Memory and Disk Requirements

Quantization requires enough memory and disk space for both input and output files:
You need enough disk space for both the input and output files simultaneously. RAM usage is typically close to the output file size.

Online Quantization

If you don’t have sufficient hardware, use the GGUF-my-repo Hugging Face space:
  1. Visit https://huggingface.co/spaces/ggml-org/gguf-my-repo
  2. Enter your model repository
  3. Select quantization levels (multiple at once)
  4. The space converts and quantizes automatically
  5. Results are published to your Hugging Face account
This is free and uses Hugging Face’s infrastructure.

Choosing the Right Quantization

Decision Tree

1

Determine Your Constraints

RAM/VRAM available?
  • <8 GB: Use Q2_K or Q3_K_M
  • 8-16 GB: Use Q4_K_M
  • 16-32 GB: Use Q5_K_M or Q6_K
  • 32+ GB: Use Q8_0 or F16
2

Assess Quality Needs

How important is quality?
  • Maximum quality: Q8_0 or F16
  • High quality: Q5_K_M or Q6_K
  • Balanced: Q4_K_M ⭐
  • Size-constrained: Q3_K_M
  • Extreme compression: Q2_K
3

Consider Use Case

What’s your use case?
  • Production/chat: Q4_K_M or Q5_K_M
  • Development/testing: Q4_K_M
  • Mobile/edge: Q2_K or Q3_K_M
  • Research/benchmarking: Q8_0 or F16

Recommendations by Model Size

Recommended: Q4_K_M or Q5_K_MSmall models are already efficient, so use higher quantization to preserve quality. The size savings from aggressive quantization aren’t as meaningful.
Recommended: Q4_K_MThis is the sweet spot for Q4_K_M quantization. You get ~75% size reduction with minimal quality loss.
Recommended: Q3_K_M or Q4_K_MSize becomes critical for large models. Q3_K_M provides good compression while maintaining usable quality.
Use Q4_K_M if you have the RAM.
Recommended: Q2_K or Q3_K_MFor models this large, aggressive quantization is often necessary just to fit in memory.
Consider using importance matrix to improve Q2_K quality.

Evaluating Quality

Measure quantization quality using perplexity:
Lower perplexity = better quality. A small increase (0.1-0.5) is usually acceptable.

Troubleshooting

Solution: Use a smaller quantization level or quantize on a machine with more RAM. Alternatively, use the online GGUF-my-repo tool.
Possible causes:
  • Quantization level too aggressive (Q2_K or lower)
  • Corrupted quantization process
  • Wrong model format
Solution: Try Q4_K_M or higher, or requantize from original F16.
Error message: error: quantizing already quantized modelSolution: Add --allow-requantize flag, but note this degrades quality. Better to quantize from F16.
Solution: Use more CPU threads:
The last argument specifies thread count.

Next Steps

After quantization:
  1. Test the model to ensure quality is acceptable
  2. Benchmark performance with llama-bench
  3. Deploy using llama-server or integrate into your application
  4. Share your quantized model on Hugging Face for others
See also: