Overview

2 Building Quantization from First Principles

Quantization is presented as the practical translation layer between floating-point training and low-bit inference, where neural network values must be mapped onto a finite integer grid to save memory, speed up computation, and reduce energy use. The chapter builds this idea from the ground up by first showing how fixed-point hardware represents numbers, then explaining why a single rigid scale is not enough for modern tensors whose ranges vary widely across weights and activations.

The discussion then develops affine quantization as the more flexible alternative: a real value is reconstructed from an integer by subtracting a zero-point and multiplying by a scale. The scale sets the resolution of the grid, while the zero-point shifts it so that real zero is exactly representable, which is essential for correct zero-padding and preservation of sparsity. The chapter also shows how these parameters are derived from observed minimum and maximum values, and how the resulting mapping trades off representational range against precision.

Finally, the chapter distinguishes granular error, caused by values landing between grid points, from overload error, caused by values exceeding the calibrated range and being clipped. It shows that calibration is really about balancing those two sources of distortion, especially in the presence of outliers. This leads naturally to the comparison of symmetric and asymmetric schemes: symmetric quantization is efficient and fits centered weight distributions well, while asymmetric quantization better matches one-sided activation distributions such as ReLU outputs. The chapter closes by demonstrating a hybrid approach in code, using symmetric weights and asymmetric activations to achieve accurate INT8 inference with manageable arithmetic overhead.

The Q3.4 fixed-point format divides 8 bits into a sign bit, 3 integer bits, and 4 fractional bits. Here, the bit pattern 0 001 1000 encodes +1.5 — the integer bits 001 contribute 1, and the leading fractional bit 1000 contributes 2⁻¹ = 0.5.
The 8-bit signed integer ring. The 256 possible bit patterns wrap around a circle, with 0 at the top and −1 (1111 1111) one step below it. Moving clockwise increments the integer; moving counterclockwise decrements it.
The range-versus-precision tradeoff in Q-format. For a fixed 8-bit container, every bit allocated to the integer part doubles the representable range but halves the precision (step size). Moving the binary point left (more fractional bits) gives finer resolution at the cost of a narrower range; moving it right does the reverse. There is no free lunch: the total information capacity is fixed by the bit-width.
The ruler problem. Fixed-point quantization (left) forces a single rigid ruler on all measurements: a rice grain vanishes between coarse tick marks, while a watermelon extends beyond the ruler's edge and gets clipped. Affine quantization (right) manufactures a custom ruler for each object, with tick spacing (scale) and starting position (zero-point) tailored to the data's actual range.
The granular-versus-overload error tradeoff. Widening the quantization grid (increasing the clipping range) reduces overload error—fewer values are clipped—but coarsens the step size, increasing granular error for values inside the range. Tightening the grid does the reverse. The optimal clipping boundary minimizes total distortion and depends on the shape of the data distribution: heavy-tailed distributions push the boundary outward; tightly concentrated distributions pull it inward.
Symmetric versus asymmetric quantization. Asymmetric quantization (left) shifts the grid to cover only the region the data actually occupies, reclaiming bits that would otherwise encode empty space. The zero-point Z records the shift, and must be accounted for during inference arithmetic. Symmetric quantization (right) pins the grid at zero and mirrors the range, making it ideal for centered distributions like weight tensors but wasteful for one-sided data.
Weight quantization (symmetric)
Activation quantization (affine/asymmetric)
Symmetric vs asymmetric on ReLU activations
FP32 baseline vs INT8 output

Summary

  • Fixed-point arithmetic stores numbers on a rigid discrete grid determined at design time. The Q-format notation makes explicit the fundamental bargain: allocate bits to range or to precision, but never both. Two's complement representation enables negative numbers through overflow, while saturation at the overflow cliff constrains every fixed-point system.
  • The affine transformation r = S(q - Z) bridges floating-point tensors and integer storage. Scale (S) is the ratio of real range to integer range; zero-point (Z) aligns real zero with an integer coordinate. The zero-point nudge forces the calibrated range to include 0.0, preserving sparsity after ReLU and enabling correct zero-padding.
  • Granular error arises when values land between grid points and must be rounded, bounded by S/2. Overload error arises when values fall outside the grid and are clipped, introducing unbounded distortion. Widening the grid reduces clipping but increases rounding; tightening it does the reverse.
  • Symmetric quantization centers the grid on zero, wasting capacity on one-sided distributions but eliminating cross-term overhead. Asymmetric quantization shifts the grid to follow data, reclaiming bits at the cost of runtime correction. The Hybrid Consensus—symmetric weights, asymmetric activations—matches each scheme to its statistical reality.
  • Visualization reveals wasted bits immediately: if half the integer codes cover empty space, the wrong scheme was chosen. The quantized arithmetic, when implemented correctly with zero-point correction, matches FP32 output to within the noise floor of 8-bit representation.
  • Calibration is not a one-time event. Scale and zero-point derivations in this chapter assumed known min/max—true for weights but not activations, whose ranges shift with every batch. Strategies like percentile clipping and entropy minimization replace raw min/max with robust boundaries; when input distributions drift, recalibration may be needed.

FAQ

What is quantization in neural network inference?Quantization is the process of mapping floating-point values, such as weights and activations, onto a finite set of integer levels like int8 or uint8. It reduces memory use, speeds up arithmetic, and lowers energy cost during inference.
How does fixed-point representation work?Fixed-point stores integers in hardware but interprets them as real values by dividing by a constant scale factor. In Q-format notation, the scale is typically a power of two, and the binary point is implied rather than stored explicitly.
Why does 1111 1111 represent -1 in signed 8-bit two's complement?In two's complement, the most significant bit has a negative weight, and the remaining bits have positive weights. For 1111 1111, the values sum to -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1.
What is the affine quantization mapping?Affine quantization uses the equation r = S · (q - Z), where r is the real value, q is the integer value, S is the scale, and Z is the zero-point. This allows the integer grid to be shifted and resized to better match the tensor distribution.
How are the scale and zero-point computed?The scale is computed from the real range and integer range: S = (r_max - r_min) / (q_max - q_min). The zero-point is then derived so that real zero maps exactly to an integer, typically using Z = round(q_min - r_min / S), followed by clamping to the valid integer range.
Why must real zero be exactly representable?Exact zero is necessary for operations like zero-padding in convolutions and for preserving sparsity after activations like ReLU. If zero were only approximated, padding and “zero” activations would introduce noise and reduce accuracy.
What is granular quantization error?Granular error happens when values fall between grid points and are rounded to the nearest representable level. For uniform quantization, its magnitude is bounded by about S/2.
What is overload error?Overload error occurs when values fall outside the quantization range and are clipped to the nearest boundary. Unlike granular error, overload error can become large and unbounded as values move farther beyond the range.
Why is symmetric quantization often used for weights?Weight tensors are usually centered around zero and roughly symmetric, so a symmetric grid fits them well. Setting the zero-point to 0 also removes cross-terms in dot-product arithmetic, making inference cheaper.
Why are activations often quantized asymmetrically?Activation tensors, especially after ReLU, are often one-sided and non-negative. Asymmetric quantization shifts the grid to use the full integer range efficiently, reducing wasted codes and improving precision for these skewed distributions.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Quantization and Fast Inference ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Quantization and Fast Inference ebook for free