Sub topic 5
Estimating the memory for weights
This chapter assumes you know what a parameter is in a language model, can do arithmetic with bits, bytes and gigabytes, and can run Python and shell commands. Quantisation and GGUF naming were covered in earlier chapters. Here you use them to turn a model name into a memory number.
The core formula
A model's weights are a long list of numbers. Each number takes a fixed number of bits in the file and in memory. The memory needed is therefore:
bytes = parameters × bits per weight ÷ 8Divide by 8 because there are 8 bits in a byte. Then divide by 1024³ (2³⁰) to get GiB, or by 10⁹ to get GB.
The same estimate works for any format. The only input that changes is bits per weight (bpw).
A worked example
Take an 8-billion-parameter model and compare three formats.
- 16-bit (FP16 or BF16): 8 × 10⁹ × 16 ÷ 8 = 16 × 10⁹ bytes. That is 16 GB, or 14.9 GiB.
- 8-bit (Q8_0, about 8.5 bpw): 8 × 10⁹ × 8.5 ÷ 8 = 8.5 × 10⁹ bytes. That is 8.5 GB, or 7.9 GiB.
- 4-bit (Q4KM, about 4.85 bpw): 8 × 10⁹ × 4.85 ÷ 8 = 4.85 × 10⁹ bytes. That is 4.85 GB, or 4.5 GiB.
A quick shortcut for a model with N billion parameters: the weights take about N × bpw ÷ 8 GB. For 8B at 4.85 bpw, that is 8 × 4.85 ÷ 8 = 4.85 GB.
Why bits per weight is not a round number
A name like "Q4KM" suggests 4 bits, but the real average is higher. Two things cause this.
- Block scales. Quantised formats store weights in small blocks, and each block carries extra values such as a scale factor. Q80 stores 32 weights as 32 bytes of 8-bit integers plus a 2-byte FP16 scale. That is 34 bytes for 32 weights, which is 8.5 bits per weight. Q40 stores 32 weights in 16 bytes plus a 2-byte scale. That is 18 bytes, or 4.5 bpw.
- Mixed precision. The "K" formats do not quantise every tensor the same way. Some tensors, such as parts of the attention and feed-forward layers, get more bits. Embedding and output tensors are often kept at higher precision. The "M" in Q4KM means a medium mix. The figure of about 4.85 bpw is an average across the whole file.
Typical averages are listed below. They are approximate and vary a little between model families.
| Format | Approx. bits per weight | 8B model (GiB) | 70B model (GiB) |
|---|---|---|---|
| FP16 / BF16 | 16.0 | 14.9 | 130.4 |
| Q8_0 | 8.5 | 7.9 | 69.3 |
| Q6_K | 6.56 | 6.1 | 53.5 |
| Q5_K_M | 5.7 | 5.3 | 46.4 |
| Q4_K_M | 4.85 | 4.5 | 39.5 |
| Q4_0 | 4.5 | 4.2 | 36.7 |
Going from FP16 to Q4KM cuts the weights to about 30% of their original size.
GB or GiB?
Two units are in common use, and mixing them up causes errors of about 7%.
- GB (gigabyte) is 10⁹ bytes. Download pages, drive sizes and most hardware specs use it.
- GiB (gibibyte) is 2³⁰ = 1,073,741,824 bytes. Operating system memory monitors and many tools report it.
A "16 GB" GPU holds about 14.9 GiB. A 4.85 GB file is 4.52 GiB. Pick one unit for a calculation and convert at the edges. This tutorial's code uses GiB for memory and GB when comparing against bandwidth figures.
A calculator you can run
The function below is the formula from this chapter. The loop prints the table above.
GIB = 2**30
FORMATS = {
"FP16": 16.0,
"Q8_0": 8.5,
"Q6_K": 6.56,
"Q5_K_M": 5.7,
"Q4_K_M": 4.85,
"Q4_0": 4.5,
}
def weights_gib(params_billion, bits_per_weight):
return params_billion * 1e9 * bits_per_weight / 8 / GIB
for name, bpw in FORMATS.items():
small = weights_gib(8.0, bpw)
large = weights_gib(70.0, bpw)
print(f"{name:7} {bpw:5.2f} bpw 8B: {small:6.1f} GiB 70B: {large:6.1f} GiB")Working backwards from a file
For a GGUF file you have already downloaded, you do not need to guess the bits per weight. The file size is the weight memory, within a small margin. You can also invert the formula to see the real average.
def bits_per_weight(file_bytes, params):
return file_bytes * 8 / params
# A file listed as 4.92 GB for a model with 8.03 billion parameters
print(f"{bits_per_weight(4.92e9, 8.03e9):.2f} bits per weight")This prints 4.90 bits per weight. That is a little above 4.85 because this model has large embedding tensors kept at higher precision.
Where the estimate goes wrong
The formula gives the weights only. Several details make real numbers differ from it.
- Rounded parameter counts. An "8B" model may have 8.03 billion parameters, and a "7B" model may have 6.7 or 7.6 billion. Use the exact count from the model card when you have it.
- Metadata. A GGUF file also holds a header, the tokenizer vocabulary and other metadata. This usually adds tens of megabytes. It matters only for very small models.
- Mixed-precision tensors. The average bpw already includes them, but a table value can be off by a few percent for a specific model.
- Not all weights count the same. Some models tie the input embedding and output layer so they share one tensor. Others store them separately. This shifts the parameter total.
- Loading behaviour. llama.cpp usually memory-maps the file, so weight memory tracks file size. If the model is split between GPU and CPU, the weights are divided between two memory pools, which a later chapter covers.
A practical habit is to compute the weight figure first, then reject any option where weights alone already use more than about 80% of the available memory. This leaves room for the terms the next chapters add.
Putting it together
To estimate weight memory for a model you are considering:
- Find the exact parameter count on the model card.
- Pick a quantisation and look up its approximate bpw, or read the file size from the download page.
- Multiply and convert to GiB.
- Compare the result with the memory you have, keeping in mind that the KV cache and overhead still have to fit.
Exercises
Exercise
A 14-billion-parameter model is stored as Q4KM at about 4.85 bits per weight. How many GiB do the weights need, and can they fit comfortably on a GPU with 8 GiB of VRAM?
Show answer
Bytes = 14 × 10⁹ × 4.85 ÷ 8 = 8.4875 × 10⁹. Dividing by 2³⁰ gives about 7.9 GiB. That is almost all of 8 GiB, so the weights alone leave under 0.1 GiB for the KV cache and runtime buffers. It does not fit comfortably. You would need a smaller quantisation, a smaller model, or partial offload to system RAM.
Exercise
A downloaded GGUF file is 26.4 GB and the model has 32.8 billion parameters. What is the average number of bits per weight, and which format in the table is it closest to?
Show answer
Bits per weight = 26.4 × 10⁹ × 8 ÷ 32.8 × 10⁹ = 6.44. This is closest to Q6_K at about 6.56 bpw. The small difference comes from rounding in the sizes and from the exact mix of tensor precisions in the file.