Sub topic 4
Reading GGUF files and names like Q4_K_M
This chapter assumes you know that a model's weights are stored as numbers, that quantisation stores those numbers with fewer bits, and that you can do arithmetic on bits, bytes and GB. Here you will learn what a GGUF file holds and how to read the quantisation label in its name.
What a GGUF file is
GGUF is a single-file format used by llama.cpp and tools built on it. One file holds three things:
- The weights, stored as tensors, the named arrays of numbers that make up the model.
- Metadata, such as the architecture, layer count, number of KV heads and the context length the model was trained for.
- The tokenizer, so the file needs no side files to run.
Because the file stores the model's shape, a tool can read it and work out memory needs without loading the weights. The file size on disk is also close to the memory the weights need, which makes it the most useful number for planning.
Anatomy of a name
File names follow a loose convention. Take this one:
Llama-3.1-8B-Instruct-Q4_K_M.gguf| Part | Meaning |
|---|---|
| Llama-3.1 | Model family and version |
| 8B | About 8 billion parameters |
| Instruct | Tuned to follow instructions, not just continue text |
| Q4_K_M | Quantisation scheme |
| .gguf | File format |
The naming is a habit, not a rule. Always check the model page if a name looks unusual.
Decoding Q4_K_M
The label has three parts.
- Q4 is the approximate bits per weight. Q4 is about 4 bits, Q5 about 5, Q8 about 8.
- K marks the "K-quant" family. Weights are grouped into blocks, and each block stores its own scale factors. Blocks are grouped into larger super-blocks, which keeps the extra storage small.
- M is the size variant: S (small), M (medium) or L (large). Larger variants keep a few sensitive tensors at higher precision.
Older names such as Q40 and Q80 have no K. They use simpler blocks, and they are still common.
Names starting with "IQ", such as IQ3_XS, use a different scheme. It targets very low bit counts and needs more compute to decode.
Why "4" really means about 4.85
Scales and mixed-precision tensors add storage. A Q4KM file therefore averages roughly 4.85 bits per weight, not 4.0. The table gives typical figures. Treat them as approximate, because they vary by model.
| Label | Approx. bits per weight | Size of an 8B model |
|---|---|---|
| Q8_0 | 8.5 | about 8.5 GB |
| Q6_K | 6.6 | about 6.6 GB |
| Q5_K_M | 5.7 | about 5.7 GB |
| Q4_K_M | 4.85 | about 4.9 GB |
| Q3_K_M | 3.9 | about 3.9 GB |
You can turn bits per weight into a file size yourself:
def gguf_size_gb(params_billion, bits_per_weight):
return params_billion * bits_per_weight / 8
for label, bits in [("Q8_0", 8.5), ("Q5_K_M", 5.7), ("Q4_K_M", 4.85)]:
print(f"{label}: {gguf_size_gb(8.0, bits):.1f} GB")Common errors
- Reading Q4 as exactly 4 bits. This underestimates size by about 20 percent.
- Ignoring split files. Large models may come as parts named like
model-00001-of-00003.gguf. Download all parts and point the tool at the first one. - Assuming the file is the whole memory cost. The KV cache and runtime overhead come on top of it.
Exercise
A file named Mistral-7B-Q5KM.gguf holds a 7-billion-parameter model at about 5.7 bits per weight. Roughly how large is the file in GB, and what do the Q5, K and M parts tell you?
Show answer
7 × 5.7 / 8 ≈ 5.0 GB. Q5 means about 5 bits per weight, K means the K-quant block scheme with per-block scales, and M means the medium variant, which keeps some sensitive tensors at higher precision.