Fewer bits, the same number of weights

INT8 quantization, scale and the cost of clipping

INT8 changes how weights are represented. Scale, rounding and clipping explain both the storage benefit and the accuracy trade-off.

A million dense FP32 weights require four million bytes. Representing them as INT8 requires one million bytes before scales and other metadata. There are still a million weights.

Start with batch size and model memory.

An integer needs a ruler

Signed INT8 has integer values from -128 to 127. A real weight such as 0.37 needs a mapping, not just a cast.

A simple affine quantizer uses positive scale s and integer zero point z:

The scale is the spacing in original units; the zero point represents zero. This example uses z=0. Runtime formats differ; TensorRT's documented operator requires a zero zero-point.

With s=0.1:

| Original | Stored q | Reconstructed | Absolute error | |---:|---:|---:|---:| | 0.37 | 4 | 0.4 | 0.03 | | -0.26 | -3 | -0.3 | 0.04 | | 0 | 0 | 0 | 0 | | 20 | 127 | 12.7 | 7.3 |

The first rows show rounding. The last shows clipping: 200 does not fit. Without clipping, nearest rounding has error at most half a step here, 0.05. That is not a bound on the whole network's output error.

Range trades against resolution

A wider scale can accommodate outliers but gives coarser spacing. One scale per tensor may fit its outliers poorly; per-channel or per-group scales allow more local ranges at the cost of metadata and implementation complexity.

Activations depend on inputs. In ONNX Runtime's terminology, static quantization uses representative calibration inputs to choose activation quantization parameters ahead of time; dynamic quantization computes certain parameters at runtime, with overhead.

Keep the final test set out of repeated calibration and selection.

Smaller is not automatically faster

Speed depends on hardware, operator coverage, kernels and conversions. If only weights are quantized, activations and workspace may stay in other formats. Neither peak service memory nor latency must fall fourfold.

Compare quality, peak memory and latency on the same hardware, inputs and realistic load. Treat the quantized result as a model version to evaluate.

Try the arithmetic

This only demonstrates mapping; it runs no INT8 inference kernel and measures no acceleration.

Change s to 0.2: 20 becomes exactly representable as 100, while 0.37 still reconstructs to 0.4. Coarser resolution does not mean every individual value necessarily gets worse.

Tagged: ai, quantization, inference, performance

Area: AI

Related notes

עברית