liveBook · Manning

Post-training quantization

As explained in Domain-Specific Small Language Models

A quantization method applied after training to reduce model numeric precision, typically to improve deployment efficiency.

Post-training quantization (PTQ) lowers a trained model’s numeric precision to reduce memory use and potentially speed inference. Unlike quantization-aware training (QAT), PTQ is applied after training. Lower precision introduces quantization error, so deployment choices balance model size and speed against accuracy. section 6.2

Common 8-bit approaches map values to integers in different ways:

  • Zero-point quantization scales values using the input range and accounts for asymmetric distributions. section 6.2
  • Absmax quantization scales values relative to the tensor’s largest absolute value. section 6.2

A practical check compares original and quantized weights, then evaluates model behavior. Weight distributions reveal differences, while generation and perplexity offer ways to assess effects; a sample output alone is not a reliable quality judgment. section 6.2.1

import torch

def absmax_quantize(X):
    scale = 127 / torch.max(torch.abs(X))

    X_quant = (scale * X).round()

    X_dequant = X_quant / scale

    return X_quant.to(torch.int8), X_dequant

Absmax function that returns INT8 values and their dequantized approximation. section 6.2.1

How Domain-Specific Small Language Models covers it

How deep it goes
Defines PTQ against QAT, explains two 8-bit scaling schemes, and demonstrates weight conversion and model evaluation.
Examples
Uses GPT-2 small converted from FP32 to INT8 with absmax scaling, then compares weights, generated text, and perplexity.
What it assumes
Assumes a pretrained language model and distinguishes post-training conversion from training-time quantization.
What you can do after
Apply 8-bit absmax quantization to GPT-2 weights with PyTorch and Transformers, then assess the result with weight plots and perplexity.
How it connects
Connects to quantization-aware training, model weights, inference, and perplexity evaluation.

Also explained in

Compare how 2 books explain post-training quantization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Domain-Specific Small Language Models. The section links point to the parts of the book it draws on.