liveBook · Manning

Quantization

As explained in LLMs in Production

Quantization is the conversion of model parameters or computations from higher-precision numerical representations to lower-precision representations.

A model that runs comfortably during training may be too large to serve. Quantization reduces numerical precision to lower memory requirements, making compression a practical first step when disk space, RAM, GPU memory, or serving cost is the constraint. Less precision is not free: stronger compression can raise perplexity and make an LLM’s behavior less stable or predictable. section 3.3.1 section 9.3.1 section 11.4.2

Think of reporting a ruler measurement to far more decimal places than the ruler can verify. Keeping every digit does not necessarily make the measurement more useful. Model numbers pose a harder version of that question: reducing precision saves space, but reducing it too far increases error. The useful setting is therefore the one that meets the hardware limit while preserving acceptable performance—not simply the fewest bits available. section 3.3.1 section 11.4.2

Choose a route based on what you are deploying:

  • For a trained PyTorch model, quantize after training and compare the resulting artifact with the original. A dynamic INT8 example applies configurations to embedding and linear layers and reports a drop from about 716 MB to 18 MB on disk; check model behavior as well as size. section 9.3.1
  • For a llama.cpp edge deployment, convert the model to a GGUF checkpoint, then quantize it—or download an already-quantized GGUF file. A 7B model in Q4_K_M is about 4.37 GB on disk and requires up to 6.87 GB RAM in the comparison table. section 11.2 section 11.4.2
  • If the model still will not fit, compare a smaller model with a lower-bit format. Q2_K is smaller, but carries significant quality loss and is not recommended for most purposes. Account for VRAM as well as RAM when using a GPU. section 11.4.2
Listing 9.5 Quantization
llama.to("cpu")
qconfig_dict = {
    torch.nn.Embedding: torch.quantization.float_qparams_weight_only_qconfig,
    torch.nn.Linear: torch.quantization.default_dynamic_qconfig,
}
dynamic_quantized_llama = torch.quantization.quantize_dynamic(    #1
    llama, qconfig_dict, dtype=torch.qint8
)
#SimpleLlama size: 716.504MB
#SimpleLlama size: 18.000MB

The PyTorch listing isolates the dynamic INT8 call and shows the before-and-after disk sizes for a trained model. section 9.3.1

Table 11.1 Comparison of key attributes for different llama.cpp quantization methods for a 7B parameter model
Quant method
Bits
Size (GB)
Max RAM required (GB)
Use case
Params (billions)
Q2_K
2
2.72
5.22
Significant quality loss; not recommended for most purposes
7
Q3_K_S
3
3.16
5.66
Very small, high loss of quality
7
Q3_K_M
3
3.52
6.02
Very small, high loss of quality
7
Q3_K_L
3
3.82
6.32
Small, substantial quality loss
7
Q4_0
4
4.11
6.61
Legacy; small, very high loss of quality; prefer using Q3_K_M
7
Q4_K_S
4
4.14
6.64
Small, greater quality loss
7
Q4_K_M
4
4.37
6.87
Medium, balanced quality; recommended
7
Q5_0
5
5.00
7.50
Legacy; medium, balanced quality; prefer using Q4_K_M
7
Q5_K_S
5
5.00
7.50
Large, low loss of quality; recommended
7
Q5_K_M
5
5.13
7.63
large, very low loss of quality; recommended
7
Q6_K
6
5.94
8.44
Very large, extremely low loss of quality
7
Q8_0
8
7.70
10.20
Very large, extremely low loss of quality; not recommended
7

Use the 7B-format comparison to check bits, disk size, maximum RAM, and stated quality before choosing a llama.cpp format. section 11.4.2

Quantization can make inference feasible on constrained hardware, but fitting is not the same as serving well. An LLM on a Raspberry Pi may still be too slow for its intended use. Test output quality and inference speed in the target environment before treating a smaller checkpoint as production-ready. section 11.5 section 9.3.1

How LLMs in Production covers it

How deep it goes
Defines the precision trade-off and works through PyTorch and llama.cpp procedures, without deriving the underlying numerical mathematics.
Examples
Uses a dynamic INT8 PyTorch model and a 7B GGUF model on a Raspberry Pi to compare sizes and constraints.
What it assumes
Assumes a trained model for PyTorch quantization or a model checkpoint and prepared llama.cpp environment for GGUF conversion.
What you can do after
Apply it via PyTorch or llama.cpp, then assess whether the smaller model fits hardware without unacceptable quality or speed losses.
How it connects
Links model compression to post-training quantization, GGUF, perplexity, edge inference, and GPU memory limits.

Also explained in

Compare how 13 books explain quantization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.