Quantization and calibration
Derive a uniform quantizer and evaluate precision changes through payload size, clipping error, representative calibration, and kernel behavior.
On this page
The question for today
What you’ll understand
- Calculate quantization codes, reconstructed values, and payload storage.
- Distinguish quantizer calibration from probability calibration.
- Evaluate weight, activation, and cache precision changes separately.
A lower-bit representation stores a restricted set of numerical values instead of every original floating-point value. The benefit can be smaller weights and less memory traffic; the cost is approximation error and additional representation machinery. Quantization is an engineering hypothesis about an entire execution path. A compressed checkpoint does not automatically imply faster inference, and a small average reconstruction error does not automatically preserve the model's behavior on the tasks users care about.
Define the numerical map before naming the bit width
An affine uniform quantizer rounds a scaled number to an integer code and clips it into a bounded range. A scale converts between code units and real units; a zero point locates real zero. Symmetric quantizers usually use a zero-centered code range, sometimes leaving one signed code unused. State that convention explicitly. Rounding introduces local error, while clipping can introduce much larger error when the represented value falls outside the chosen range.
The granularity of a scale matters. One scale for a whole tensor is cheap but can waste resolution if one channel has outliers. Per-channel or grouped scales use extra metadata while adapting the numerical range more locally. Some methods preserve selected quantities at higher precision. These design choices mean that two checkpoints both described as four-bit can have different memory footprints, approximation properties, and kernel requirements. Count the complete stored representation rather than only packed integer payloads.
Calibration should resemble the intended computation
Post-training quantization uses already trained weights, often with representative inputs to estimate ranges or error-sensitive transformations. GPTQ develops weight quantization using information about layer inputs. SmoothQuant redistributes scaling between activations and weights to make low-precision computation more tractable. These are different mechanisms, not interchangeable labels for shrinking a file. Neither method's original results establish that any arbitrary model, bit width, or workload will preserve quality.
A calibration set should exercise the deployed languages, sequence lengths, formats, and activation patterns without borrowing final test answers. Freeze the transformation before evaluating on held-out tasks. Quantizer calibration concerns numerical ranges or reconstruction; probability calibration concerns whether predicted confidence matches outcome frequency. A quantized model can change both task quality and confidence behavior, so evaluate them separately. Long-context behavior and unusual inputs can expose errors absent from a small collection of short conversational prompts.
Memory savings and speedups need separate evidence
Distinguish weight-only quantization, quantized activations, and quantized key-value caches. A four-bit weight format does not imply that activations or caches use four bits. During execution, kernels may dequantize values, accumulate in a wider type, and hold temporary buffers. A hardware path without efficient support for the format may be slower despite moving fewer weight bytes. Measure latency, throughput, peak memory, and energy at representative batch sizes and lengths.
Use a full-precision or established higher-precision baseline with identical prompts, tokenizer, and decoding configuration. Report absolute quality changes on key slices, not just a relative average or file-size ratio. Include numerical failures, malformed outputs, and calibration drift. A useful deployment decision defines acceptable quality degradation and a measurable resource benefit before experimentation. Retain a compatible fallback and identify the exact quantization artifact, scales, kernels, and runtime versions so a regression can be reproduced.
Work it through
Round and clip with one scale
Use a symmetric signed quantizer with codes −7 through 7, scale 0.2, zero point zero, and nearest rounding. Quantize values 0.93 and 1.8. Separately estimate the packed payload for one billion four-bit parameters.
For 0.93, divide by 0.2 to obtain 4.65 and round to code 5. Reconstruction is 1.0, so signed error is +0.07 and absolute error 0.07, below the unclipped 0.1 bound.
For 1.8, the unbounded rounded code is 9, which clips to 7. Reconstruction is 1.4 and absolute error is 0.4. Clipping invalidates the half-step error bound.
A billion four-bit codes occupy 4,000,000,000 bits, or 500,000,000 bytes of payload. This is approximately 476.84 MiB before scales, zero points, padding, and tensors left at higher precision.
Small rounding error and large clipping error arise from different mechanisms. The payload calculation is a storage floor under the given coding assumptions, not a peak serving-memory prediction.
The living notebook
Explore the model
Interactive lab · network
Sensitivity through one sigmoid
Vary x and w in the exact single-neuron computation σ(wx−0.5). Inspect how changing a weight changes its output at different inputs. The lab has no quantizer, calibration dataset, or Transformer, so calculate rounding separately rather than treating the displayed value as quantization error.
- Product · wx
- 1.2
- Logit · wx − 0.5
- 0.7
- Sigmoid output
- 0.6682
One inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forThe effect of a parameter perturbation depends on the input and local computation, motivating representative numerical evaluation.
Pause & reason
Check your understanding
Your turn
Try it yourself
An unclipped quantizer has scale 0.125. What is its nearest-rounding absolute-error bound? If 2 billion parameters use eight-bit codes and 16 million bytes of metadata, estimate stored size in decimal GB.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
The rounding bound is half a step; one eight-bit code occupies one byte.
Compare with the solution
The unclipped absolute-error bound is 0.0625. Parameter codes occupy 2,000,000,000 bytes; metadata raises the total to 2,016,000,000 bytes, or 2.016 decimal GB. The estimate excludes any other tensors, padding, and runtime state. A value outside the represented interval can exceed the rounding bound through clipping.
Keep the idea
Take it with you
- Specify scale, code range, granularity, rounding, and clipping before comparing precision.
- Representative quantizer calibration and held-out task evaluation serve different purposes.
- Count metadata and execution state, and measure speed on kernels that support the format.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.