Weight Quantization Basics › Worked Example: Computing Scale and Zero-Point

Michael BrenndoerferJanuary 10, 20264 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 317Weight Quantization Basics › Worked Example: Computing Scale and Zero-Point
Reading section 6 of 12 · Worked Example: Computing Scale and Zero-Point · 4 minSection 6 of 12 · 4 min

Book contents

Section 6 of 12 · 4 min read

Weight Quantization Basics › Worked Example: Computing Scale and Zero-PointOne section · Sections in this chapter6 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let us work through a concrete numerical example to make the formulas tangible. We will quantize a small tensor using both symmetric and asymmetric approaches, computing every intermediate value by hand.

Suppose we have the following weight tensor (7 values for clarity):

w=[−0.8,−0.4,−0.1,0.0,0.2,0.5,0.9]\mathbf{w} = [-0.8, -0.4, -0.1, 0.0, 0.2, 0.5, 0.9]

We want to represent this using 4-bit signed integers, which range from −8-8 to 77 (giving qmin=−8q_\text{min} = -8, qmax=7q_\text{max} = 7). This tiny tensor will show the mechanics clearly.

Step 1: Find the range.

xmin=−0.8,xmax=0.9x_\text{min} = -0.8, \quad x_\text{max} = 0.9

Step 2a: Compute the symmetric scale.

The symmetric approach uses the maximum absolute value:

max⁡(∣w∣)=0.9\max(|w|) = 0.9 ssym=0.97≈0.1286s_\text{sym} = \frac{0.9}{7} \approx 0.1286

Step 2b: Compute the asymmetric scale and zero-point.

sasym=xmax−xminqmax−qmin=0.9−(−0.8)7−(−8)=1.715≈0.1133s_\text{asym} = \frac{x_\text{max} - x_\text{min}}{q_\text{max} - q_\text{min}} = \frac{0.9 - (-0.8)}{7 - (-8)} = \frac{1.7}{15} \approx 0.1133 zasym=qmin−round ⁣(xminsasym)=−8−round ⁣(−0.80.1133)=−8−(−7)=−1z_\text{asym} = q_\text{min} - \text{round}\!\left(\frac{x_\text{min}}{s_\text{asym}}\right) = -8 - \text{round}\!\left(\frac{-0.8}{0.1133}\right) = -8 - (-7) = -1

Step 3: Quantize each value.

For symmetric (z=0z = 0):

www/ssymw / s_\text{sym}qsymq_\text{sym}w^sym\hat{w}_\text{sym}Error
-0.8-6.22-6-0.7710.029
-0.4-3.11-3-0.3860.014
-0.1-0.78-1-0.1290.029
0.00.0000.0000.000
0.21.5620.2570.057
0.53.8940.5140.014
0.97.0070.9000.000

The maximum error occurs at 0.2, which maps to integer 2 but dequantizes to 0.257, an error of 0.057. This error arises because the symmetric scale is set by the extreme value of 0.9, which coarsens the step size throughout the tensor.

For asymmetric (z=−1z = -1, s≈0.1133s \approx 0.1133):

www/sasym+zw / s_\text{asym} + zqasymq_\text{asym}w^asym\hat{w}_\text{asym}Error
-0.8-7.06 + (-1) = -8.06-8-0.7930.007
-0.4-3.53 + (-1) = -4.53-5-0.4530.053
-0.1-0.88 + (-1) = -1.88-2-0.1130.013
0.00.00 + (-1) = -1.00-10.0000.000
0.21.77 + (-1) = 0.7710.2270.027
0.54.41 + (-1) = 3.4130.4530.047
0.97.94 + (-1) = 6.9470.7930.107

The dequantization here uses w^=sasym⋅(q−z)=0.1133⋅(q−(−1))=0.1133⋅(q+1)\hat{w} = s_\text{asym} \cdot (q - z) = 0.1133 \cdot (q - (-1)) = 0.1133 \cdot (q + 1).

The key insight from this comparison is not that one scheme is uniformly better. The symmetric scheme perfectly represents 0.9 (the anchor of its scale) with zero error, while asymmetric cannot. The asymmetric scheme better represents -0.8 (small error) but assigns its maximum error to 0.9. The average error depends on the full distribution, and asymmetric generally wins when the distribution is not centered at zero. For this tensor, which has a slightly positive skew, asymmetric quantization gives smaller average error for most values.

Step 4: Verify dequantization.

For symmetric, dequantization is w^=ssym⋅q=0.1286⋅q\hat{w} = s_\text{sym} \cdot q = 0.1286 \cdot q. For asymmetric, dequantization is w^=sasym⋅(q−z)\hat{w} = s_\text{asym} \cdot (q - z).

At q=−8q = -8 (asymmetric): w^=0.1133⋅(−8−(−1))=0.1133⋅(−7)=−0.793\hat{w} = 0.1133 \cdot (-8 - (-1)) = 0.1133 \cdot (-7) = -0.793. Compare to the original −0.8-0.8: error of 0.007. The asymmetric mapping distributes its integer range more efficiently across the actual value range, which is why its error for typical values is smaller.