TL;DR
Post-training quantization (PTQ) takes a network that has already been trained with 32-bit floats and converts its weights, and often its activations, to 8-bit integers. It does not train anything again. It runs a small sample of typical inputs through the network to learn the range each value lives in, and that range sets the grid of integers. The result is roughly four times smaller than the float32 model, and each value carries a small rounding error that usually changes the model's answers only a little.
Builds on: quantization · Leads to: AWQ
Where it's used
Running large language models on your own hardware. A model with 7 billion parameters stored in 16-bit floats needs about 14 GB just for its weights. Stored in 8-bit integers it needs about 7 GB, which fits on a consumer graphics card that could not hold the float version. Most local model runners ship weights in a quantized form for this reason.
Image classifiers on phones. A mobile app that recognises objects in the camera feed carries a quantized network. The phone's integer units do the multiplications, which uses less battery and less memory bandwidth than running the same network in floats.
Serving at scale. A server holds a model's weights in GPU memory. If each weight takes one byte instead of two or four, one GPU can hold a larger model or more copies of the same model. Each request then costs less, because fewer bytes have to be read for every token generated.
How it works
Take four trained weights and quantize them to a toy 3-bit grid. A 3-bit signed integer, one of the bit patterns of three bits, can take 8 values, from to . Each step adds one idea.
- Start with the trained weights, which are floats: . Check: these are the values the network learned, and nothing about them has changed.
- Choose the grid. The largest magnitude is , so map it to the largest integer, . The step size, called the scale , is . Check: .
- Divide each weight by the scale and round to the nearest integer. , , rounds to , and . The integers are . Check: all four lie inside to , so nothing needed clipping.
- Store the integers and the one scale. To use the weights again, multiply back: . Check: three of the four values come back exactly. The weight came back as , an error of .
The picture below shows the error for any value on this grid. It is a sawtooth: zero on each grid point and largest halfway between two points.
- error, scale 0.9
flowchart LR A["Trained float weights"] --> B["Scale = largest magnitude / 3"] B --> C["Divide by scale, round to integers"] C --> D["Store integers and one scale"] D --> E["Multiply back when needed"]
Notice what PTQ did not do. It did not change any weight by training, and it did not look at labels. The only inputs it needed were the weights and, for activations, a sample of inputs to measure their range. That is why it is cheap.
The math
Definition
Given a trained network with weights and a small calibration set of inputs, post-training quantization maps each real value to an integer , using a scale and an integer zero point fixed from the calibration data. The rule is
where and are the smallest and largest integers the bit width allows, and is the value used at run time.
The map is affine: it scales and then shifts by the zero point. The calibration set is a few hundred to a few thousand ordinary inputs. They are run through the network once, and the smallest and largest value seen at each place is recorded. Those recorded ranges are what set and .
Step 2 in symbols: choosing the scale
Step 2 used a symmetric grid, so . For a signed -bit integer the largest positive value is , and the scale is
For the toy grid, , so the divisor is , and . For real 8-bit weights the divisor is . The scale depends only on the largest magnitude, which is why one large value matters so much, as the common mistakes section explains.
Step 3 in symbols: round and clip
Step 3 divided each weight by and rounded. The rounding is the only lossy operation. Clipping does nothing here, because a symmetric scale built from puts every calibration value inside the range. Clipping matters only for values the calibration set never showed, and for the clipping choices made to avoid outliers.
Step 4 in symbols: the error bound
For any value inside the range, rounding moves by at most . Multiplying by moves by at most , so
For the toy grid, , and the error for the weight is within that bound.
Memory in symbols
A layer with weights stored in bits takes bytes, plus one scale for each group of weights. A layer of float32 weights takes bytes. The ratio is , which is 4 for 8-bit integers.
Worked examples
- Set the grid. The scale is and the zero point is , so the representable values are
- Take the value . Divide by the scale: .
- Round to the nearest integer: . So .
- Multiply back: .
- Error: . Check: this is within .
- Now take . Divide: . Round: . So and the error is , again within .
Answer: becomes and becomes . Each error is under half a step, which is the whole guarantee of rounding to a grid.
Take the weight row and quantize it to signed 8-bit integers with a symmetric scale.
- Largest magnitude: .
- Scale: .
- Quantize : , which rounds to .
- Quantize : exactly, so .
- Quantize : , which rounds to .
- Quantize : , which rounds to .
- Multiply back. , , , .
- Errors: , , , and . Each is below .
Answer: the four weights now take four bytes plus one scale, down from 16 bytes of float32. The largest error is , about a third of a percent of the largest weight. A network with millions of such rows usually keeps its answers nearly unchanged for this reason.
A Transformer layer often has a weight matrix of size , which is weights. Compare two storage formats. Ignore the small bias vector.
- In 16-bit floats each weight takes 2 bytes. The layer takes bytes, about 33.6 MB.
- In 8-bit integers each weight takes 1 byte: bytes, about 16.8 MB.
- PTQ also stores one 16-bit scale per output row, because each row is scaled on its own. That is bytes, about 0.008 MB.
- Total for the 8-bit version: bytes, about 16.8 MB.
- Ratio: . The layer is about half the size of its 16-bit version. Against float32 it is about one quarter.
Answer: the layer shrinks from about 33.6 MB to about 16.8 MB, and the scales add less than a tenth of a percent. Try it: a float32 layer has one million weights, . Quantize it to 4-bit integers with no scales. How many megabytes does it take? Answer: bits per weight gives bits, which is bytes, so MB. The float32 layer took MB, so the ratio is .
Common mistakes and tradeoffs
It is tempting to think the scale should always cover the full range of the weights, so one unusually large value is harmless. In fact the scale is set by the largest magnitude, so a single outlier stretches the whole grid. The ordinary values then share fewer usable steps and their rounding error grows. This is why practical PTQ schemes often clip the largest values or use one scale per row or per group of weights.
It is tempting to think 8-bit integers always run faster. In fact the gain depends on two things: whether the hardware and the software kernels have a fast integer path, and whether the job is limited by memory reads or by arithmetic. When a large model generates one token at a time, the time goes mostly to reading weights from memory, so the speed-up comes from moving fewer bytes.
It is tempting to think quantization loses nothing when the answers look the same on a handful of prompts. In fact a small average error can hide large errors on rare inputs. Measure accuracy on a held-out set that is different from the calibration set, and look at the worst cases as well as the average.
When to use PTQ: you have a trained model, you cannot or do not want to retrain it, and memory or speed is the bottleneck. Eight bits is usually safe for this.
When not to use it: at very low bit widths, such as 4 bits or fewer, simple rounding often loses too much accuracy. Then the usual alternative is quantization-aware training, which simulates the rounding during training so the weights can adjust to it. Pruning and distillation, covered in Model Compression, reduce size in a different way, and they can be combined with quantization.
History
Running neural networks on integer hardware goes back to the 1990s and 2000s, when many embedded chips had no fast floating-point unit. Those systems used fixed-point arithmetic and worked with carefully chosen scales.
Around 2017, Jacob and colleagues at Google published a scheme for integer-only inference that set out the affine rule used today, with a scale and a zero point for each tensor. It made 8-bit networks practical on phones and server chips, and it is the basis of most PTQ tools.
In 2022, Dettmers and colleagues studied very large transformers and found that a few feature dimensions carried values far larger than the rest. These outliers broke simple 8-bit schemes, and their LLM.int8() method handled them separately while quantizing the rest. Later methods built on that observation, including AWQ.
Recap
- A trained float weight is replaced by an integer and one shared scale , using .
- The scale comes from a calibration set, or from the largest magnitude in the weights, so no training is needed.
- Rounding to the nearest grid point gives each value an error of at most .
- The scale depends on the largest value, so outliers are the main source of extra error.
- Next, read AWQ, which chooses how to protect the most important weights before quantizing, and Model Compression for the other ways to shrink a model.
Check yourself
QuestionWhat does the scale measure, in plain words?Show answer
The scale is the real-number distance between two neighbouring integers on the grid. Multiplying an integer by gives the real value it stands for, so a smaller scale means a finer grid and smaller rounding errors, as long as the values stay inside the range.
QuestionWith scale and zero point , quantize . What are , the reconstructed value , and the error?Show answer
Divide: . Round: , so . Multiply back: . The error is , which has size and is within .
QuestionWhy does one unusually large weight make quantization worse for all the other weights?Show answer
The scale is set from the largest magnitude, so one large value makes the scale big. Every weight is rounded to a multiple of that big step, so the typical small weights, which lie between grid points, get larger rounding errors than they would with a finer grid. The fix is to clip the outlier or to use a separate scale for the group that contains it.
QuestionA 3-billion-parameter model is stored in 16-bit floats. Roughly how large are its weights after 8-bit post-training quantization, ignoring the scales? Why is accuracy still worth checking?Show answer
In 16-bit floats the weights take bytes, about 6 GB. In 8-bit integers they take bytes, about 3 GB. Accuracy still needs checking because a small average rounding error can hide large errors on rare inputs, so a held-out set and worst-case inputs should be tested before shipping the model.