liveBook · Manning

Chapter 6: Quantizing for your production environment

What you will learn in chapter 6 of Domain-Specific Small Language Models

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

What it builds on

Section by section

Introduction

The introduction identifies model quantization as important to LLM inference performance and introduces it as the chapter’s focus.

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.

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.

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.

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.

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.

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.

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.

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

Concepts explained in this chapter

← Chapter 5: Exploring ONNX Chapter 7: Generating Python code →

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.