Quantization Fundamentals

Volume IV, Chapter 18 — Part I. Number representation, uniform and non-uniform quantization, quantization error analysis, affine mapping INT8, and post-training vs. quantization-aware training theory.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. Number Representation
  6. Uniform Quantization
  7. Quantization Error Analysis
  8. Affine Quantization Mapping
  9. Per-Tensor vs. Per-Channel Quantization
  10. Post-Training vs. Quantization-Aware Training
  11. Weight vs. Activation Quantization
  12. Worked Examples
  13. Connection to the Broader Curriculum
  14. Common Pitfalls and Misconceptions
  15. Research Perspective
  16. Summary of Takeaways
  17. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. Define uniform quantization with scale ss and zero-point zz.
  2. Derive affine mapping q=round(x/s)+zq = \text{round}(x/s) + z and dequantization x^=s(qz)\hat{x} = s(q - z).
  3. Bound quantization error xx^s/2|x - \hat{x}| \leq s/2 for uniform quantizers.
  4. Compare PTQ vs. QAT theoretically.
  5. Explain why per-channel quantization preserves accuracy for weight matrices.

Prerequisites


Notation

  • x,x^x, \hat{x} — Full-precision and quantized values
  • s,zs, z — Scale and zero-point
  • bb — Bit width (INT8, INT4, etc.)
  • round()\mathrm{round}(\cdot) — Quantization rounding operator
  • ϵq\epsilon_q — Quantization error

Core Intuition

Neural networks use FP32/FP16 weights — 32 or 16 bits per parameter. Quantization maps weights and activations to low-bit integers (INT8, INT4), reducing memory and enabling faster integer arithmetic.

A 70B model in FP16: 140 GB. INT4: ~35 GB — runnable on consumer GPUs. Combined with LoRA (QLoRA) and Flash Attention, quantization is essential for deployment.

Series context. Volume IV, Chapter 18 (Quantization).

Quantization Fundamentals (Inference)

8-bit grid | MSE: 0.00000 | 4× compression
Bits
8
WeightsQuantized
Explore: Inference quantization maps continuous weight distributions to discrete grids. Error distribution shows outliers suffer most — per-channel quantization mitigates this.

Number Representation

Definition 1 (Floating Point). FP16: 1 sign + 5 exponent + 10 mantissa bits. Dynamic range via exponent.

Definition 2 (Fixed Point / Integer). INT8: values in {128,,127}\{-128, \ldots, 127\}. Fixed scale.

Proposition 1. INT8 has 4× fewer bits than FP32 → 4× memory reduction (ignoring overhead).


Uniform Quantization

Definition 3 (Uniform Quantizer). Map real x[xmin,xmax]x \in [x_{\min}, x_{\max}] to integer q{0,,2b1}q \in \{0, \ldots, 2^b - 1\}:

q=clip(round(xs)+z,0,2b1),(1)q = \text{clip}\left(\text{round}\left(\frac{x}{s}\right) + z, 0, 2^b - 1\right), \tag{1} x^=s(qz),(2)\hat{x} = s(q - z), \tag{2}

where ss is scale, zz is zero-point, bb is bit-width.

Definition 4 (Symmetric Quantization). z=0z = 0, s=max(x)/(2b11)s = \max(|x|) / (2^{b-1} - 1).


Quantization Error Analysis

Theorem 1 (Uniform Quantization Error). For round-to-nearest uniform quantization:

xx^s2.(3)|x - \hat{x}| \leq \frac{s}{2}. \tag{3}

Proof. Rounding error bounded by half quantization bin width. \blacksquare

Proposition 2 (MSE Error). For uniformly distributed xx in bin: E[(xx^)2]s2/12\mathbb{E}[(x-\hat{x})^2] \leq s^2/12.

Corollary 1. Smaller scale ss (more bits or narrower range) → lower error.


Affine Quantization Mapping

Definition 5 (Scale and Zero-Point). For asymmetric INT8:

s=xmaxxmin2b1,z=round(xmins).(4)s = \frac{x_{\max} - x_{\min}}{2^b - 1}, \quad z = \text{round}\left(-\frac{x_{\min}}{s}\right). \tag{4}

Maps xmin0x_{\min} \to 0, xmax255x_{\max} \to 255 exactly.

Proposition 3 (Quantized Matmul). Integer matrix multiply with rescaling:

C=ABsAsB(QAQBzAzB11T).(5)C = AB \approx s_A s_B \cdot (Q_A Q_B - z_A z_B \cdot \mathbf{1}\mathbf{1}^T). \tag{5}

Enables INT8 GEMM on hardware accelerators.


Per-Tensor vs. Per-Channel Quantization

Definition 6 (Per-Channel). Separate (s,z)(s, z) per output channel of weight matrix WRdout×din\mathbf{W} \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}}.

Proposition 4. Per-channel preserves accuracy for weights with heterogeneous magnitude distributions across channels — critical for Self-Attention projection matrices.


Post-Training vs. Quantization-Aware Training

Definition 7 (PTQ). Quantize after training using calibration data to estimate xmin,xmaxx_{\min}, x_{\max}.

Definition 8 (QAT). Simulate quantization during training with straight-through estimator (STE):

qx1(STE).(6)\frac{\partial q}{\partial x} \approx \mathbf{1} \quad \text{(STE)}. \tag{6}

Proposition 5. QAT allows model to adapt to quantization noise; PTQ is faster but lower accuracy at low bit-widths.


Weight vs. Activation Quantization

  • Weights — Static (known at load) — Outlier channels
  • Activations — Dynamic (runtime) — Range varies per input

Definition 9 (Weight-Only Quantization). Quantize W\mathbf{W} to INT4; activations remain FP16 — used in GPTQ, AWQ for LLMs.


Worked Examples

Example 1: INT8 Mapping

x[0,10]x \in [0, 10], b=8b=8: s=10/2550.039s = 10/255 \approx 0.039, x=5q=127x=5 \to q = 127, x^4.96\hat{x} \approx 4.96.

Example 2: Memory

7B params × 4 bits = 3.5 GB vs. 14 GB FP16.


Connection to the Broader Curriculum


Common Pitfalls and Misconceptions

Pitfall 1: INT8 always 4× faster (depends on hardware/kernel).

Pitfall 2: Ignoring outliers in activation ranges.

Pitfall 3: PTQ sufficient for INT4 (often needs QAT or advanced PTQ).

Pitfall 4: Quantizing KV Cache separately from weights.


Research Perspective

GPTQ (Frantar et al., 2022). AWQ (Lin et al., 2023). FP8 training (NVIDIA H100). LLM.int8() mixed precision.


Summary of Takeaways

  • Quantizeq=round(x/s)+zq = \text{round}(x/s) + z
  • Dequantizex^=s(qz)\hat{x} = s(q - z)
  • Error boundxx^s/2 — x - \hat{x} — \leq s/2
  • PTQ vs QAT — Calibration vs. STE training

Next: Data Parallelism


Exercises

Exercise 1. Derive (4) for asymmetric INT8.

Exercise 2. Prove error bound (3).

Exercise 3. Compute memory for 70B model at INT4 vs FP16.

Exercise 4. Why per-channel for weights?

Exercise 5. STE gradient justification.

Exercise 6. Quantized matmul (5) derivation.

Exercise 7. Outlier effect on scale ss.

Exercise 8. QLoRA: compose LoRA + INT4.