Quantization

Representing weights and activations with lower-precision numbers (e.g. INT8) to cut size and speed up execution, with scale and zero-point rules.

Intermediate12 min read

Educational draft content that needs expert review before it is used as an academic reference. Specifications, versions and licences change; check the official source. No experimental result is shown here unless it was actually run.

Learning objectives

  • Apply the quantization and dequantization formulas
  • Distinguish PTQ vs QAT and per-tensor vs per-channel
  • Explain the effect of outliers

Quantization maps real numbers x to integers q with q = round(x / s) + z and approximately restores them with x̂ = s · (q − z). The scale s and zero-point z are chosen from the value range [min, max]. In symmetric quantization z = 0; asymmetric quantization uses z to cover a range not centred on zero.

Types: post-training quantization (PTQ) is fast and needs a small calibration set; quantization-aware training (QAT) simulates quantization during training and recovers more accuracy. Granularity: per-tensor or per-channel — the latter is more accurate for weights because it isolates channels with different ranges. Outliers widen the range and increase the error of the other values.

Benefits: about 4× smaller weights than FP32 at INT8 and the possibility of faster integer hardware. Caveats: possible accuracy loss, varying hardware and operator support, and not every layer may be quantized.

Diagram

Quantization: mapping continuous values to discrete levelsA number line with equally spaced discrete levels and real values each rounded to the nearest level.-4-3-2-101234Real values (such as weights)Integer levels: q = round(x / scale) + zero_point
The gap between a value and its level is the quantization error.

Terminology (with Arabic equivalents)

Scaleمعامل المقياس
Zero-pointنقطة الصفر
Calibrationالمعايرة
PTQالتكميم بعد التدريب
QATالتدريب الواعي بالتكميم
Per-channelلكل قناة
Outlierقيمة شاذة

Worked example

Weight values between −1.0 and +1.0 with symmetric quantization to INT8 (−127..127): s = 1.0/127 ≈ 0.00787. The value 0.5 becomes q = round(0.5/0.00787) = 64, and is restored as 64×0.00787 ≈ 0.5039, so the error ≈ 0.0039. If an outlier of 10.0 exists, s = 10/127 ≈ 0.0787 and the error of the other values grows tenfold.

Code snippet

PythonNumPy, illustrative
import numpy as np

def quant_sym(x, bits=8):
    qmax = 2 ** (bits - 1) - 1
    s = np.abs(x).max() / qmax
    q = np.clip(np.round(x / s), -qmax, qmax).astype(np.int32)
    return q, s

x = np.random.default_rng(0).normal(0, 0.1, 1000)
q, s = quant_sym(x)
xh = q * s
print('MSE', float(np.mean((x - xh) ** 2)))

An illustrative snippet that was not executed in the lab environment; check library versions in their official documentation before use.

Check yourself

  1. What is the value of z (zero-point) in symmetric quantization?

    • Zero
    • One
    • The largest value
    • The number of bits
    Answer and explanation

    Zero — Symmetric quantization makes z = 0.

  2. What effect do outliers have on per-layer quantization?

    • They reduce the error
    • They enlarge the range so the error of the other values grows
    • No effect
    • They cancel the scale
    Answer and explanation

    They enlarge the range so the error of the other values grows — A larger range means a larger quantization step.

  3. What is the advantage of QAT over PTQ?

    • It needs no training
    • It simulates quantization during training and so often recovers more accuracy
    • It is always faster
    • It needs no hardware
    Answer and explanation

    It simulates quantization during training and so often recovers more accuracy — QAT trains the model to tolerate quantization error.

References

Suggested reading (links not verified yet)

  • Jacob et al. (2018). Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference. CVPR 2018 Not verified
  • Nagel et al. (2021). A White Paper on Neural Network Quantization. arXiv Not verified
  • Gholami et al. (2021). A Survey of Quantization Methods for Efficient Neural Network Inference. arXiv Not verified

Image and audio processing in the experiments runs inside your browser. Your images and audio are never uploaded and conversations are not stored. Privacy