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
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
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.
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.
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.
Jacob et al. (2018). Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference. CVPR 2018Not verified
Nagel et al. (2021). A White Paper on Neural Network Quantization. arXivNot verified
Gholami et al. (2021). A Survey of Quantization Methods for Efficient Neural Network Inference. arXivNot 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
Assistant Professor of Artificial Intelligence and Deep Learning
University of Al-Qadisiyah
About
Researcher and academic specialising in artificial intelligence (AI), deep learning, computer vision and natural language processing (NLP). The “Deep Learning in Arabic” project aims to enrich Arabic scientific and technical content with sound, free references and software for researchers and learners across the Arab world.
Edge AI Assistant
I am the Edge AI Assistant. Ask about a concept, technology, board or project and I will answer from the lab content with links.
Edge AI Assistant
Local Database Mode
Answers from the lab knowledge base only, without sending your question to any external party. Conversations are not stored.
Smart assistant
Answers from the site’s content
The assistant can make mistakes, so check the attached sources.
Welcome! I am Dr. Alaa Taima, and this is my smart assistant on “Deep Learning in Arabic”. Ask it about any term or topic in artificial intelligence, and it will answer from the site's content with links to the sources.
DL Bot
Your smart assistant · Connecting…
Enter to send · Shift + Enter for a new line · The assistant can make mistakes, so check the sources.