Skip to content

Lesson 13 ยท Memory, Speed and Quantization

Llama Quantization and GGUF

Learn what quantization does, how to read GGUF names such as Q4_K_M and Q8_0, how to convert and quantize with llama.cpp and how to test quality loss.

Quantization stores model numbers with fewer bits. Files get smaller, memory needs drop and speed often improves. The price is some loss of precision. Your job is to find a level that saves enough memory without hurting your task too much.

The basic idea

Instead of storing every weight as a 16-bit number, you store a small integer plus a shared scale for a group of weights. To use a weight, the runtime multiplies the integer by the scale. Rounding creates a small error.

Python Quantize and restore a few numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real methods are smarter: they use small blocks, separate scales and careful choices about which tensors to treat gently. This is only the core idea. See weight quantization for inference.

GGUF

GGUF is the file format used by llama.cpp (and supported by some other tools). A GGUF file holds tensors plus metadata such as the model architecture and tokenizer information. A quantized model often has the type in its file name.

Label Rough meaning
F16 16-bit, the usual high-precision starting point
Q8_0 About 8.5 bits per weight
Q6_K, Q5_K_M Middle levels, smaller than Q8
Q4_K_M A common "4-bit" middle choice
Q3 and Q2 types Very small, larger quality risk
IQ types Another family aimed at low bit rates

The K types use a more refined block layout, and suffixes such as _S and _M indicate size variants. A "4-bit" label does not mean exactly 4 bits per weight.

There is no universally best level. A common starting point is a Q4_K_M file, moving up when quality matters and memory allows. A lower level on a larger model can sometimes beat a higher level on a smaller one, but only your tests can say.

Convert and quantize yourself

llama.cpp provides a conversion script and a quantization tool. These are real commands to run in a llama.cpp setup:

Terminal
# 1. Convert a Hugging Face model folder to a 16-bit GGUF
python convert_hf_to_gguf.py ./llama-1b --outfile llama-1b-f16.gguf --outtype f16

# 2. Quantize it
./build/bin/llama-quantize llama-1b-f16.gguf llama-1b-Q4_K_M.gguf Q4_K_M

Check the current script help, because options change. The quantization tool also supports an importance matrix (--imatrix), which guides which information to protect. The documentation says that a suitable imatrix can reduce accuracy loss. There is also a --dry-run option that simulates the job without writing a model.

Remember the license: a quantized file is still Llama material, and publishing it brings distribution duties.

Other quantization routes

  • bitsandbytes loads Transformers models in 8-bit or 4-bit.
  • GPTQ and AWQ are methods used by several GPU inference tools.
  • Meta published quantized versions of Llama 3.2 1B and 3B. Its model card says those have an 8K context, while the standard versions have 128K.

Formats are not interchangeable. A GGUF file is for tools that read GGUF.

Measure quality loss

Compare levels with the same prompts and settings. A useful trick is to measure agreement with a higher-precision version, and also to score both against correct answers.

Python Stand-in: compare quantization levels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For a real comparison, use more examples, include long inputs and your hardest cases, and check the formats your code depends on, such as JSON and tool calls. A single-prompt test is misleading.

Also record file size, peak memory and speed next to each quality score. The right choice is the point where quality is still acceptable and the file fits comfortably.

Why rounding hurts some tasks more

A small error in one weight rarely matters alone, but a model has billions of them, and the errors combine through many layers. Simple tasks with obvious answers often survive. Tasks that depend on exact details, such as copying numbers, writing code, following a strict format or using a less common language, may show problems earlier. This is a reason to test those cases on purpose and not to rely on a friendly chat.

Choosing a level step by step

  1. Estimate which files fit your memory, including the KV cache.
  2. Pick a middle level and run your test set.
  3. If quality is fine and speed or memory is tight, try one level lower and run it again.
  4. If quality is poor, try one level higher. If that does not help, the model itself may be the limit.
  5. Record the file name, its checksum, the runtime version and the scores.

A checksum is a short fingerprint of a file. With it you can prove later that you are still using the file you tested.

Quantization does not shrink everything

Weight quantization reduces the size of the weights. It does not automatically shrink the KV cache, which has its own data type and settings. With a long context and several users, the cache can become a large part of the total, so check both when memory runs out.

Sharing a quantized file

If you publish your own quantized file, say which base model and revision it came from, which tool and options you used and what you tested.

Keep the original. Keep your high-precision file, or note exactly where to get it again. Always quantize from that source, not from an already quantized file, so rounding errors do not pile up.

Practice idea. Add a 6-bit run to the first browser example and compare its error with the others.

Common mistakes

Do not download a random GGUF and assume it is official or safe. Check the uploader, read the model page, verify the chat template and test before you trust it.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What is the main trade-off in quantization?

    Fewer bits save memory but add rounding error.
  2. 2. Which llama.cpp tool creates quantized GGUF files?

    It converts a high-precision GGUF into a chosen type.
  3. 3. How should you compare two quantization levels?

    Controlled, representative tests reveal real quality differences.

Summary

  • Quantization saves memory by storing fewer bits per weight.
  • GGUF is llama.cpp's file format; names like Q4_K_M describe the type.
  • llama.cpp can convert and quantize models.
  • Measure quality on your own task before you choose.

Next: Llama Speed and Context Settings

Your progress is saved in this browser. No account needed.