Advanced

Quantization I: int8, int4, the basics

Quantization represents weights or activations with fewer bits plus scale metadata, reducing memory traffic while introducing rounding and clipping error.

Updated

1

Concept

Model parameters are real-valued numbers, commonly trained and stored in floating-point formats. Quantization maps them—or runtime activations—onto a smaller discrete representation. The goal may be reduced model size, lower memory bandwidth, larger batches, or faster arithmetic. The cost is approximation error plus metadata and kernel complexity.

In symmetric uniform quantization, a real value xx is approximated using an integer code qq and scale ss:

q=clip(round(x/s),qmin,qmax),x^=sq.q=\operatorname{clip}\left(\operatorname{round}(x/s),q_{min},q_{max}\right), \qquad \hat{x}=s q.

Affine quantization adds a zero point, allowing an asymmetric represented range. Rounding maps many real values to the same code; clipping maps extremes to the boundary. Their difference from the original produces quantization error.

“Int8” or “int4” is incomplete. Ask what is quantized: weights, activations, or KV cache? At what granularity: whole tensor, output channel, input group, or token? Are scales floating point, and how often are they stored? Is the representation symmetric? What compute dtype accumulates products? A four-bit weight format can still use higher-precision activations and accumulators.

Granularity controls a trade-off. One scale for an entire tensor has little metadata but must cover the widest magnitude. A single outlier can enlarge the step size, making common small values poorly resolved. Per-channel scales adapt to rows or columns. Groupwise quantization divides a dimension into smaller chunks. Smaller groups usually track local distributions better but store more scales and complicate kernels.

Weights and activations behave differently. Weights are fixed after training and can be calibrated offline. Activations depend on the prompt and can contain channel-wise outliers. SmoothQuant, published in 2022, moves some quantization difficulty from activations into weights through a mathematically equivalent scaling transformation, making weight-and-activation quantization easier for supported models.

Weight-only quantization reduces bytes read for weights, especially valuable during memory-bound decode. The kernel may unpack and dequantize tiles into a compute dtype while multiplying. If the runtime first expands the entire model into floating point, storage shrinks but execution memory and bandwidth do not. Native packed kernels are the difference between a compact file and efficient inference.

Accuracy loss is uneven. Some layers, channels, or tensors are more sensitive. Embeddings and output heads may use different precision. Mixed schemes retain selected weights at higher precision. Perplexity is one signal, but task accuracy, structured-output validity, multilingual behavior, rare-token predictions, and long-context stability can reveal failures averages miss.

Calibration chooses ranges or scales from representative data in many post-training methods. A narrow calibration set can preserve familiar activation patterns and fail elsewhere. Record dataset, sequence lengths, languages, preprocessing, algorithm, and software version. Quantized artifacts are derived models and need independent hashes and evaluations.

The simplest memory estimate divides raw element bits by eight, then adds scales, zero points, indexes, alignment, and any higher-precision tensors. Runtime memory also includes KV cache, activations, workspace, and allocator overhead. A model file that fits in RAM may still fail to run.

The durable model is a measured approximation. Discrete codes save movement and capacity; scales reconstruct useful magnitudes; grouping adapts to local statistics; accumulators preserve arithmetic range. Quantization succeeds when the error stays inside the task’s tolerance and the hardware truly consumes the compact form.

2

Explain it like I am five

A surveyor records mountain elevations. Writing every measurement to the millimeter is bulky; recording each as one of a few thousand steps relative to a local valley is compact. Wide regions need coarse steps, while a separate scale for each small map tile follows terrain more closely. Quantization likewise maps real weights onto discrete codes, and grouping chooses whether one scale must cover an entire mountain range or only a neighborhood.

3

Teach it back

Describe affine quantization with scale and zero point, then explain why group size, outliers, and actual kernel support determine whether fewer bits help.

Minimum: 80 characters and 15 words. Your text stays only in this browser.

Saved only on this device.

Show a model answer

Affine quantization approximates a real value x by a discrete integer q and reconstructs it as scale times q minus a zero point. A scale shared by a large group must cover its full range, so outliers enlarge the step and reduce precision for ordinary values; smaller groups add metadata but adapt locally. Fewer stored bits reduce capacity and bandwidth only when the runtime has kernels that consume the packed representation efficiently—otherwise dequantization or conversion overhead can erase the gain.

4

Check your understanding

1. What creates quantization error?
Answer and explanation

Rounding values to discrete levels and clipping values outside the represented range — A finite codebook cannot represent every real value, and its finite range may clip extremes.

2. Why can per-channel or grouped scales improve accuracy?
Answer and explanation

They adapt the quantization range to smaller subsets with different distributions — Local scales prevent one subset's range or outlier from determining resolution for the entire tensor.

Complete the teach-back and answer the quiz correctly to finish this lesson.

Sources

  1. Guangxuan Xiao et al. (2022). SmoothQuant: Accurate and Efficient Post-Training Quantization for Large Language Models.