Chapter 6: Quantizing for your production environment
The introduction identifies model quantization as important to LLM inference performance and introduces it as the chapter’s focus.
After this chapter you will be able to
- Quantization and inference performance. Explain that model quantization matters because it is crucial for LLM inference performance.
- FP32 representation and adoption. Explain how FP32 divides its bits and why it became standard for deep-learning training on GPUs instead of FP64.
- FP16 mixed-precision training. Describe FP16’s bit layout and explain why mixed-precision training switches between FP16 and FP32 rather than using only FP16.
- BF16 range tradeoff. Distinguish BF16 from FP16 by its bit allocation, dynamic-range tradeoff, and typical model-performance effect.
- Low-bit integer quantization. State what quantization means in this section and how it differs from choosing a reduced floating-point format.
- Quantization benefits and tradeoffs. Explain how lowering the precision of weights and activations reduces memory use and can speed inference, while introducing quantization error and possible accuracy loss.
- Zero-point INT8 mapping. Trace how the zero-point example uses an input minimum and maximum to scale an asymmetric FP32 distribution into the INT8 range, then quantizes and approximately recovers a weight.
- Absmax INT8 mapping. Describe how absmax quantization uses a tensor’s absolute maximum to map values into the INT8 range and explain the section’s quantization and dequantization example.
- Absmax quantization workflow. Trace how the example starts with a pretrained FP32 GPT-2 model, copies it, and applies absmax quantization to its weights. Describe the function’s three steps and its two returned forms of the values.
- Generation and perplexity checks. Describe how the example compares generated text from the original and quantized models, what perplexity measures, and why its single-run result needs caution.
- Quantization granularity and outliers. Explain why vector-wise quantization is a practical compromise and why large outlier features need special treatment.
- LLM.int8 mixed-precision multiplication. Trace how LLM.int8() separates outliers, multiplies the two groups, and combines their results.
- ONNX conversion approaches. Distinguish the low-, mid-, and high-level approaches to exporting a Transformer model to ONNX, including what Optimum simplifies and when a lower-level approach may be needed.
- Dynamic quantization workflow. Trace how a converted ONNX model is saved and dynamically quantized to 8-bit precision, including the role of Optimum’s ORTQuantizer in the worked example.
- Measured quantization trade-offs. Describe how the banking-intent example compares the FP32 and 8-bit models on file size, classification accuracy, and inference latency.
- GPTQ weight quantization. Explain what GPTQ does to model weights and why it is useful for running large language models on limited hardware.
- AutoGPTQ quantization workflow. Describe the demonstrated sequence for quantizing GPT-2 XL with AutoGPTQ and explain the role of sample sequences.
- ggml for CPU inference. Explain why ggml is an option for running quantized LLMs on CPUs, and contrast its inference focus with GPTQ.
What it builds on
- ONNX for LLMs on CPU (chapter 5) · Provides the earlier PyTorch-to-ONNX conversion example used for comparison.
- Domain-specific evaluation (chapter 3) · Provides earlier context for evaluating a domain-specific model.
- Evaluating the generated content (chapter 4) · The earlier use of the wikitext dataset.
- How to generate content (chapter 4) · The use of a model to generate content during inference.
Section by section
Introduction
The introduction identifies model quantization as important to LLM inference performance and introduces it as the chapter’s focus.
- Quantization and inference performance: Explain that model quantization matters because it is crucial for LLM inference performance.
Why it matters: This gives you the reason the chapter focuses on quantization before getting into specific techniques.
Key terms: model quantization, LLM inference performance
6.1 Transformers precision formats
This section compares floating-point precision formats used with LLMs and introduces low-bit integer quantization.
- FP32 representation and adoption: Explain how FP32 divides its bits and why it became standard for deep-learning training on GPUs instead of FP64.
Why it matters: FP32 provides the baseline for understanding the memory and precision tradeoffs of smaller formats. - FP16 mixed-precision training: Describe FP16’s bit layout and explain why mixed-precision training switches between FP16 and FP32 rather than using only FP16.
Why it matters: Mixed precision explains how training can gain speed without relying entirely on the less stable 16-bit format. - Inference precision tradeoffs: Compare the section’s stated memory-use and performance outcomes for models trained in FP32, FP16, and mixed precision.
Why it matters: The lowest-memory choice is not necessarily the best-performing one. - BF16 range tradeoff: Distinguish BF16 from FP16 by its bit allocation, dynamic-range tradeoff, and typical model-performance effect.
Why it matters: BF16 shows that two 16-bit formats can make different range-versus-precision tradeoffs. - Low-bit integer quantization: State what quantization means in this section and how it differs from choosing a reduced floating-point format.
Why it matters: This distinction connects the precision formats to the chapter’s quantization methods.
Key terms: FP32, FP64, sign, exponent, mantissa, FP16, mixed-precision training, gradients, numerical stability, inference
6.2 8-bit quantization
This section explains why 8-bit quantization helps LLMs run on constrained hardware, distinguishes when it is applied, and illustrates two ways to map weights to INT8 values.
- Quantization benefits and tradeoffs: Explain how lowering the precision of weights and activations reduces memory use and can speed inference, while introducing quantization error and possible accuracy loss.
Why it matters: This tradeoff explains why quantization can make deployment feasible without guaranteeing unchanged model quality. - Distillation and deployment choice: Distinguish knowledge distillation from quantization and explain why the section favors quantization for hardware-specific deployment.
Why it matters: The distinction clarifies why this section changes the representation of a model rather than training a smaller replacement. - Quantization timing and training: Distinguish post-training quantization from quantization-aware training and explain why the book mostly focuses on the former.
Why it matters: The timing distinction identifies which quantization approach fits the book’s pretrained-model workflow. - Zero-point INT8 mapping: Trace how the zero-point example uses an input minimum and maximum to scale an asymmetric FP32 distribution into the INT8 range, then quantizes and approximately recovers a weight.
Why it matters: The example makes the mapping to integers—and the small error on reversal—concrete. - Absmax INT8 mapping: Describe how absmax quantization uses a tensor’s absolute maximum to map values into the INT8 range and explain the section’s quantization and dequantization example.
Why it matters: Absmax provides a contrasting 8-bit mapping that makes the choice of quantization scheme meaningful.
Key terms: quantization, weights, activations, INT8, quantization error, knowledge distillation, hardware-specific deployment, post-training quantization, PTQ, quantization-aware training
6.2.1 Hands-on 8-bit quantization
This subsection walks through 8-bit post-training quantization of GPT-2 and checks its effects by comparing weights, generated text, and perplexity.
- Absmax quantization workflow: Trace how the example starts with a pretrained FP32 GPT-2 model, copies it, and applies absmax quantization to its weights. Describe the function’s three steps and its two returned forms of the values.
Why it matters: This is the concrete workflow whose effects the rest of the example evaluates. - Weight distribution comparison: Explain what comparing weight distributions before and after quantization checks, and what the spike around zero in the example suggests.
Why it matters: The weight plot reveals information loss that is not apparent from simply completing the conversion. - Generation and perplexity checks: Describe how the example compares generated text from the original and quantized models, what perplexity measures, and why its single-run result needs caution.
Why it matters: It prevents a single appealing output or score from being mistaken for a dependable performance comparison.
Key terms: post-training quantization, FP32, INT8, absmax_quantize, model weights, distribution of the model’s weights, histogram, lossy, figure 6.4, top-k sampling
6.2.2 LLM.int8() and quantization
This section explains how LLM.int8() uses mixed precision to handle outliers, then shows how to load and assess an 8-bit GPT-2 model.
- Quantization granularity and outliers: Explain why vector-wise quantization is a practical compromise and why large outlier features need special treatment.
Why it matters: This tradeoff explains why LLM.int8() treats ordinary values and outliers differently. - LLM.int8 mixed-precision multiplication: Trace how LLM.int8() separates outliers, multiplies the two groups, and combines their results.
Why it matters: The split lets the method save memory while preserving precision for unusually large features. - Quantized GPT-2 loading: Describe how the section loads GPT-2 small with LLM.int8() and state the required libraries and hardware.
Why it matters: These are the practical requirements for trying the section’s quantized model. - Memory and evaluation tradeoffs: Explain the reported memory benefit and why the example’s weight plot and single perplexity result do not settle model quality.
Why it matters: A smaller model is useful only if its quality and computational costs are interpreted carefully.
Key terms: per-tensor quantization, vector-wise quantization, outlier features, LLM.int8(), mixed precision, FP16, INT8, dequantizes, Hugging Face Transformers API, Accelerate
6.3 8-bit quantization with ONNX
The section shows how to convert Transformer models to ONNX, dynamically quantize them to 8-bit precision, and compare the resulting model’s size, accuracy, and latency.
- ONNX conversion approaches: Distinguish the low-, mid-, and high-level approaches to exporting a Transformer model to ONNX, including what Optimum simplifies and when a lower-level approach may be needed.
Why it matters: The available conversion route determines how much configuration is needed before quantization. - Dynamic quantization workflow: Trace how a converted ONNX model is saved and dynamically quantized to 8-bit precision, including the role of Optimum’s ORTQuantizer in the worked example.
Why it matters: Conversion and quantization are separate steps, regardless of which supported export route produced the ONNX model. - Quantization hardware distinction: Explain the section’s distinction between where dynamic quantization runs and where the resulting model is used for inference.
Why it matters: The hardware used to create a quantized model should not be confused with its inference performance. - Measured quantization trade-offs: Describe how the banking-intent example compares the FP32 and 8-bit models on file size, classification accuracy, and inference latency.
Why it matters: Checking quality as well as size and speed shows whether the smaller model is useful in practice.
Key terms: torch.onnx, transformers.onnx, Optimum, ORTModelForSequenceClassification, ONNX Runtime quantization API, dynamic quantization, ORTQuantizer, quantization configuration, static quantization, CPU
6.4 4-bit quantization
Introduces 4-bit quantization as the next step after 8-bit quantization, without explaining it yet.
6.4.1 4-bit quantization with GPTQ
This section introduces GPTQ and walks through quantizing GPT-2 XL to 4 bits with AutoGPTQ, then saving and using the result for inference.
- GPTQ weight quantization: Explain what GPTQ does to model weights and why it is useful for running large language models on limited hardware.
Why it matters: This explains how a much smaller model can remain useful for inference. - AutoGPTQ quantization workflow: Describe the demonstrated sequence for quantizing GPT-2 XL with AutoGPTQ and explain the role of sample sequences.
Why it matters: Preparing suitable samples is part of converting the model, not an optional detail of the example. - Quantized model deployment limits: Describe what happens after quantization in the example, including the reported size change and AutoGPTQ loading limitation.
Why it matters: The saved model’s size and loading limits affect where this example can be deployed.
Key terms: GPTQ, one-shot weight-quantization, 4-bit integer quantization, AutoGPTQ, BaseQuantizeConfig, sample sequences, wikitext, offline, 4-bit model, inference
6.4.2 4-bit quantization with ggml
This section presents ggml as a CPU-oriented option for 4-bit LLM quantization, shows how its models can be used from Python, and explains how GGUF differs from the older GGML format.
- ggml for CPU inference: Explain why ggml is an option for running quantized LLMs on CPUs, and contrast its inference focus with GPTQ.
Why it matters: The inference hardware helps determine which quantization option is practical for a deployment. - Python inference with GGML: Describe how the section loads a ggml-quantized model and runs inference from Python.
Why it matters: Using ggml models does not require writing inference code in C. - GGUF versus GGML formats: Distinguish GGUF from the GGML model format using the differences explained in this section.
Why it matters: The file format affects how easily a quantized model can be distributed and extended.
Key terms: ggml, GPTQ, 4-bit post-training quantization, CPU inference, CTransformers, AutoModelForCausalLM, AutoTokenizer, Transformers pipeline, GGUF, llama.cpp
6.5 Summary
Where this is used later
- BitNet (chapter 9) · Precision formats used in Transformers.
- Python code generation with better models (chapter 7) · Assumes the earlier treatment of 8-bit quantization.
- Antibody generation with an SLM (chapter 8) · The concept of 8-bit model quantization
- SmoothQuant (chapter 9) · The earlier LLM.int8() approach to handling outliers with mixed precision.
- Using ONNX with models not supported by Optimum (chapter 7) · Addresses ONNX conversion for models that Optimum does not support.
- Python code generation with CodeGen (chapter 7) · ONNX quantization
- Profiling ONNX-ported LLMs (chapter 10) · Develops profiling of ONNX-ported models beyond this quick latency comparison.
- Coding assistance on commodity hardware (chapter 7) · Relates model compression to running inference on commodity hardware.
Concepts explained in this chapter
- quantizationMethods that reduce numeric precision or representational size of model values to improve efficiency, especially for inference; here the absmax method is a specific quantization procedure.
- int4 quantizationA quantization method that represents model weights or activations with 8-bit numbers to reduce model size and improve efficiency.
- post-training quantizationA quantization method applied after training to reduce model numeric precision, typically to improve deployment efficiency.
- ONNX RuntimeA runtime library that efficiently executes ONNX models across hardware targets such as CPUs, GPUs, and edge devices.
- ONNXA serialized machine-learning model represented in the ONNX format for portable execution outside the training framework.
- OptimumA Hugging Face extension library for optimizing transformer models for training and inference on target hardware, including ONNX workflows.
- bfloat16A 16-bit floating-point format with a wider exponent range and reduced mantissa precision than FP16, used in machine learning.
- mixed precisionUsing reduced-precision numeric formats for some computations or weights to reduce memory use, often with limited loss in performance.
- GPTQA one-shot post-training weight quantization method that uses approximate second-order information to compress generative pretrained transformers with minimal accuracy loss.
- LLM.int8()A quantization approach for large language models that uses mixed-precision INT8 computation with special handling for outlier features to preserve accuracy.
- GGUFA model packaged in the GGUF format for local inference with llama.cpp-style runtimes.
- precisionA numeric representation setting that determines how many bits are used to store or compute model values during serving.
- perplexityA measure of how well a language model predicts tokens in sequence, with lower values generally indicating better predictive fit.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
