Weight Quantization Basics › The Quantization Mapping
Language AI HandbookAll chapters Weight Quantization Basics › The Quantization MappingOne section · Sections in this chapter3 / 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
The process of mapping values from a continuous (or high-precision) representation to a discrete (or lower-precision) representation. In neural network contexts, this typically means converting 32-bit or 16-bit floating-point numbers to 8-bit or smaller integers.
Quantization addresses a basic tension between precision and efficiency. Neural networks learn weights as floating-point numbers, which can represent an enormous range of values with fine granularity. However, this precision comes at a cost: each 32-bit float consumes four bytes of memory, and operations on floating-point numbers require specialized hardware circuits. Quantization works because neural networks can tolerate small perturbations in their weights. We do not need exact values; we need values that are close enough to preserve the network's learned behavior. The question is: how close is close enough, and how do we achieve the best approximation with a fixed bit budget?
Think of the process as converting a high-resolution photograph to a lower-resolution version. The original has fine detail, and the compressed version loses some of that detail. If you compress carefully, preserving the most important features, the result looks nearly identical at a casual glance. If you compress carelessly, throwing away structure that matters, the result looks blurry or distorted. Quantization is the same process applied to numbers instead of pixels: we want to choose our discrete grid points to best represent the actual values we have.
Consider a weight tensor with values ranging from to . We want to represent these using 8-bit signed integers, which can hold values from to . The mapping must preserve the relative relationships between values while fitting them into our target range. Think of this as creating a ruler where each tick mark represents one integer value, and we need to position values from the original continuous number line onto the nearest tick mark. The spacing of the tick marks (the scale) and where we place the zero mark (the zero-point) together determine the quality of the mapping.
The key insight is that this is a linear mapping problem. We are fitting a straight line between the real-valued domain and the integer domain, and the parameters of that line are the scale and zero-point. Different choices of these parameters distribute the available integer "buckets" differently across the real-valued range, and some distributions represent the actual data better than others.
The standard affine quantization formula maps a real value to a quantized integer :
where:
- : the quantized integer value
- : the input real value (floating-point)
- : the scale factor, determining the step size of the quantization grid
- : the zero-point, an integer value to which real zero is mapped
- : the rounding operation (typically to the nearest integer)
This formula captures the essence of what quantization does. First, we divide the real value by the scale, which converts from real units to "quantization units." The scale determines how much of the real number line each integer step represents. If the scale is 0.01, then adjacent integers represent values that differ by 0.01 in the original space. After scaling, we round to the nearest integer because integers cannot represent fractional values. Finally, we add the zero-point, which shifts the entire mapping so that real zero lands on a particular integer value rather than necessarily on integer zero.
The zero-point serves an important purpose: it allows the quantized representation to efficiently use the available integer range even when the original values are not centered at zero. If all your weights happen to be positive, you would waste half the signed integer range without a zero-point adjustment. The zero-point corrects this misalignment, sliding the entire quantization grid until it best overlaps with the data.
To recover the original value (approximately), we apply the inverse dequantization operation:
where:
- : the reconstructed real value (approximation of the original )
- : the scale factor
- : the quantized integer
- : the zero-point
The dequantization formula simply reverses the quantization process. We first subtract the zero-point to undo the shift, then multiply by the scale to convert back from integer units to real units. Notice the hat notation on , which signals that this is an approximation rather than the exact original value. The reconstructed value will not exactly equal due to rounding, and this difference constitutes quantization error. Our goal is choosing and to minimize this error across the tensor.

Determining Scale and Zero-Point
With the quantization formula established, we face a practical question: how do we choose the scale and zero-point? These parameters must be selected carefully because they determine both the range of representable values and the precision within that range. The intuition is straightforward: we want to map the full range of actual tensor values onto the full range of available integers, using every bit of precision the integer format provides.
Think of it as a translation and scaling problem. You have a thermometer calibrated from to degrees, and you want to map it onto a ruler with integer markings from to . If you scale correctly, the coldest temperature on the thermometer maps to and the warmest maps to , and every mark on the ruler corresponds to a well-defined temperature. If you scale poorly, the thermometer might only use a portion of the ruler's range, leaving most of the precision wasted.
We derive the quantization parameters by establishing a linear mapping where the real minimum maps to the integer minimum and maps to . This ensures that the smallest and largest values in our tensor precisely hit the boundaries of the integer range, leaving no representable integers unused.
where:
- : the minimum and maximum values in the real (floating-point) tensor
- : the minimum and maximum values in the target integer range (e.g., -128 and 127 for signed 8-bit)
- : the scale factor to be determined
- : the zero-point to be determined
These two equations represent the boundary conditions of our linear mapping. The first equation says that when we dequantize the minimum integer value, we should recover the minimum real value. The second says the same for the maximum. Together, they give us two equations with two unknowns, and , which we can solve algebraically.
Subtracting the first equation from the second eliminates , which is the key insight that makes this system tractable. The zero-point cancels because we are computing the difference between two dequantization equations, and the shift cancels out in the difference:
Solving for yields the scale factor:
where:
- : the scale factor representing the step size (real value per integer increment)
- : the total range of the continuous values
- : the total number of discrete steps available
This formula has a straightforward interpretation: the scale is simply the ratio of the real range to the integer range. If your real values span 1.0 units and you have 256 integers available, each integer step represents real units. The scale tells you exactly how much precision you have: smaller scales mean finer distinctions between values, while larger scales mean coarser quantization. A large scale arises when the real range is wide relative to the integer range, and it forces many real values to map to the same integer.
Substituting back into the first equation allows us to solve for the zero-point :
where:
- : the zero-point (in real arithmetic before rounding)
- : the minimum real value scaled to the integer domain
The zero-point equation determines where real zero falls in the integer range. If the real values are centered around zero, the zero-point will be near the middle of the integer range. If the real values are all positive, the zero-point will be at or near the minimum integer value. Geometrically, the zero-point specifies how many integer steps separate the integer origin from the point where real zero would land.
Since must be an integer, we round the result:
where:
- : the computed scale factor
- : the computed zero-point
- : the minimum value in the floating-point tensor
- : the minimum value of the target integer range
The rounding of the zero-point introduces a small complication: it means that real zero may not map exactly to integer , and the boundary conditions we started with may not be satisfied exactly. In practice, this slight imprecision is negligible compared to the rounding errors inherent in quantization itself. The zero-point rounding shifts the mapping by at most half an integer step, which is the same order as the rounding error affecting every value in the tensor.
For 8-bit unsigned integers, and , giving discrete steps. For 8-bit signed integers, and , giving the same 255 steps (note: not 256, because the range of -bit integers contains values but only steps between adjacent values). This means signed and unsigned 8-bit formats have the same quantization resolution; they differ only in where the range is centered.
Quantization Error
Every quantization operation introduces error because we are fundamentally discarding information. When we round a continuous value to the nearest integer, any fractional part of the scaled value is lost. Understanding the nature and magnitude of this error helps us make informed decisions about quantization parameters and bit widths.
The rounding operation introduces error that depends on where the original value falls relative to the quantization grid. Think of placing a set of points on a number line and then rounding each point to the nearest tick mark on a ruler. Points that fall very close to a tick mark move only slightly and incur small errors. Points that fall exactly halfway between two tick marks must be rounded to one or the other, incurring the maximum possible error of half a step.
For uniformly distributed values, the maximum error from rounding is . This occurs when a value falls exactly halfway between two adjacent quantization levels. On average, the rounding error follows a uniform distribution between and , with mean zero and variance:
where:
- : the variance of quantization rounding error
- : the scale (quantization step size)
This means smaller scales produce smaller errors but can only represent smaller ranges. There is a direct tradeoff: cutting the scale in half cuts the step size in half, which cuts the variance by a factor of four, but it also halves the representable range. If the true value lies outside the representable range, we must clip it to the nearest boundary value, potentially causing much larger errors for outliers.
The mean squared quantization error for a tensor depends on both the value distribution and the choice of and . Values near the center of the distribution incur only rounding error, bounded by . Values at the extremes are handled well when the scale is chosen to just accommodate them. But values beyond the representable range are clipped, and the error for a clipped value can be arbitrarily large. An outlier at gets clipped to , incurring an error of . This observation motivates many of the advanced quantization techniques we will explore in later chapters, which focus on handling outliers gracefully rather than letting them to corrupt the quantization of typical values.
The theoretical relationship between bit width and quantization error is elegant. Each additional bit doubles the number of representable levels, which halves the step size , which reduces the variance of the rounding error by a factor of four. In other words, quantization error decreases by roughly dB (a factor of 4 in variance, or 2 in amplitude) for each additional bit. This is why plots of quantization error versus bit width on a log scale show nearly straight lines with a slope of 4x per bit.