Foundations to Frontier
Tests
E. Deep Learning › E19 Continual, Multi-Task and Transfer Learning

Quantization: Post-Training (PTQ) and Quantization-Aware Training (QAT)

Quantization stores and computes a network with low-precision integers instead of 32-bit floats: 8-bit is standard, and 4-bit or lower is used for large language models. This cuts memory 4–8× and enables fast integer hardware. Post-training quantization (PTQ) rounds a trained model's weights (and activations), using a little calibration data. Quantization-aware training (QAT) simulates the rounding during training, with the straight-through estimator, so the model learns to tolerate it. This page covers uniform symmetric and affine schemes, scales and zero points, per-tensor versus per-channel scaling, rounding noise, PTQ, QAT and modern LLM methods. In the exercises you implement int8 and affine quantization, then quantize a trained network (float accuracy 0.814). 8- and 4-bit PTQ of the weights were free (0.815) and 3-bit nearly so (0.802). 2-bit (ternary) PTQ fell to 0.586. Per-channel scales recovered 0.733, and 100 steps of QAT recovered 0.790.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Fewer bits per number

A network’s weights are usually stored as 32-bit decimal numbers, but most of that precision is wasted. Rounding them to 8-bit integers (256 possible values) makes the model 4× smaller and lets cheap integer hardware run it, usually with no loss in accuracy. Pushing to 4 or 2 bits saves more but starts to hurt, unless the model is trained knowing it will be rounded. The demo measures the rounding error as you change the number of bits.

Transfer and efficiency lab
4-bit uniform quantisation: 16 levels, step 0.1333, measured MSE 1.47e-3 vs Δ²/12 = 1.48e-3. Each extra bit cuts the error by 4×.
Why engineers care

Microcontrollers, FPGAs and neural accelerators on drones, satellites and phones run int8 (or lower) much faster and with far less energy than floating point. Quantization also lets large language models fit on a single GPU.

📝 Quick check (Level 0)
0/3 answered
  1. 1. 8-bit quantization makes a float32 model about…
  2. 2. 8-bit quantization usually costs…
  3. 3. Training while simulating rounding is called…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Uniform quantization: x̂ = s·(clip(round(x/s + z), q_min, q_max) − z), with scale s and zero point z.
    • Symmetric signed (z = 0): s = max|x| / (2^(b−1) − 1). For 8 bits the integers run from −127 to 127; 2-bit symmetric has only 3 levels (ternary).
    • Affine (asymmetric) unsigned: s = (hi − lo)/(2^b − 1), z = round(−lo/s). Zero is represented exactly, which matters for ReLU outputs and padding.
  • Rounding noise: about uniform with variance Δ²/12. Each extra bit halves Δ and quarters the error (SQNR ≈ 6.02 b + 1.76 dB, 50 dB at 8 bits).
  • Granularity: per-tensor uses one scale; per-channel uses one scale per output channel. Per-channel is better when channel ranges differ.
  • PTQ: quantize weights directly; calibrate activation ranges on a few hundred samples (min–max, percentile, or MSE-optimal).
  • QAT: fake-quantize in the forward pass and use the straight-through estimator (STE) in the backward pass: treat round() as the identity inside the clipping range.
  • Memory: a 7B-parameter model takes 14 GB in fp16 and 3.5 GB in 4-bit.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Distinct values of symmetric 2-bit quantization:
  2. 2. One more bit divides the rounding error variance by…
  3. 3. The STE treats round() in the backward pass as…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Quantization in code

Exercise 1.

  • Symmetric int8 codes stay within ±127, with error at most half a step.
  • Affine uint8 represents zero exactly.

Practice.

  • PyTorch torch.ao.quantization (eager or FX/PT2E) provides PTQ and QAT. TensorRT, ONNX Runtime and TFLite deploy int8 models.
  • For LLMs use GPTQ, AWQ or bitsandbytes (4-bit NF4), which handle outlier channels.
  • Quantize weights per channel and activations per tensor, and calibrate on representative data.
  • Keep first and last layers and sensitive operations (softmax, layer norm) in higher precision if accuracy drops.
  • Switch to QAT when PTQ loses too much at your target bit width.
import torch.ao.quantization as tq
model.qconfig = tq.get_default_qat_qconfig("x86")
qat_model = tq.prepare_qat(model.train())               # inserts fake-quant modules (STE)
train(qat_model, epochs=2)                              # fine-tune with simulated rounding
int8_model = tq.convert(qat_model.eval())
🐍 Exercise 1: symmetric and affine quantization
Compute a symmetric scale, integer codes and affine quantization with a zero point.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Weights are usually quantized…
  2. 2. LLM 4-bit methods include…
  3. 3. Layers often kept in higher precision:

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Quantization as noise. For fine steps, rounding adds approximately independent uniform noise of variance Δ²/12. A well-trained network sits in a flat region of the loss, so small weight perturbations barely change outputs: 8 bits are almost always safe. At very low bit widths the noise is no longer small, and errors accumulate through layers.

Why per-channel helps. With one scale per tensor, the step is set by the largest weight anywhere. Channels with small weights get only a few levels, or round to zero. Per-channel scales give each channel its full range.

Why QAT helps. The STE gradient lets the float “shadow” weights move so that their rounded versions perform well. The network settles in a region that is robust to the specific quantization grid. Its cost is extra training, plus a gradient approximation that becomes poor at very low bit widths.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Rounding noise variance is about…
  2. 2. Per-tensor scales hurt channels with…
  3. 3. QAT makes the network robust to…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How low can the bit width go, and what recovers the loss?

History.

  • B. Jacob et al., “Quantization and training of neural networks for efficient integer-arithmetic-only inference” (CVPR 2018).
  • Y. Bengio, N. Léonard and A. Courville, “Estimating or propagating gradients through stochastic neurons” (2013; the STE).
  • E. Frantar et al., “GPTQ” (ICLR 2023).

Exercise 2.

  • Task: 3-class classification; a 20 → 128 → 3 network trained in float on 600 examples (test accuracy 0.814).
  • Weight quantization:
    • symmetric uniform, per-tensor or per-channel, at 8, 4, 3 and 2 bits (PTQ);
    • QAT for 100 steps from the float model at 3 and 2 bits (per-tensor, STE).
  • Not covered: activations stay in float, so only the weight effect is measured.
bits PTQ per-tensor PTQ per-channel QAT per-tensor weight MSE (per-tensor)
32 (float) 0.814 — — 0
8 0.815 0.814 — 1.0e-5
4 0.815 0.814 — 3.2e-3
3 0.802 0.807 0.808 1.7e-2
2 (ternary) 0.586 0.733 0.790 0.115
  • 8 and 4 bits are free here: weight MSE grows about 4× per bit removed (1e-5 → 3.2e-3 from 8 to 4 bits), but accuracy does not move.
  • 2-bit PTQ breaks the model: every weight becomes −s, 0 or +s, and accuracy falls to 0.586.
  • Per-channel scales recover part of it (0.733), because each hidden unit keeps its own range.
  • QAT recovers most (0.790), after only 100 steps of training with simulated rounding.
  • Seeds: seeds 1 and 2 gave 2-bit PTQ 0.616/0.685, per-channel 0.741/0.768 and QAT 0.794/0.758 (floats 0.836/0.833). In seed 2, per-channel PTQ beat QAT (0.768 against 0.758), so the advantage of QAT over per-channel PTQ is not guaranteed.
🐍 Exercise 2 (research): how low can the bit width go, and what recovers the loss?
Quantize a trained MLP's weights at 8, 4, 3 and 2 bits, per-tensor and per-channel, and compare with quantization-aware training.

Quantization in AI, robotics and aerospace

  • Edge inference: int8 detectors on drones, rovers and automotive accelerators; TinyML on microcontrollers.
  • Space: quantized networks on radiation-tolerant FPGAs and processors with no floating-point unit.
  • LLMs: 4-bit weights let large models run on a single GPU or laptop.
  • Design lesson: PTQ at 8 bits first; per-channel scales, then QAT, as you go lower.

References

  1. B. Jacob et al. (2018). “Quantization and training of neural networks for efficient integer-arithmetic-only inference.” Proc. CVPR.
  2. R. Krishnamoorthi (2018). “Quantizing deep convolutional networks for efficient inference: a whitepaper.” arXiv:1806.08342.
  3. E. Frantar, S. Ashkboos, T. Hoefler and D. Alistarh (2023). “GPTQ: accurate post-training quantization for generative pre-trained transformers.” Proc. ICLR.
  4. Y. Bengio, N. Léonard and A. Courville (2013). “Estimating or propagating gradients through stochastic neurons for conditional computation.” arXiv:1308.3432.