Search

Search papers and lessons

Post-Training Quantization

Level 3 · 12 min read

Post-training quantization converts a trained network's float weights to 8-bit integers using a small sample of inputs to set the grid, with no retraining, trading a little rounding error for a much smaller model.

On this page

  • Colour = topic mix
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Efficiency
  • Calculus
  • Systems
  • ReadReady to readNot yet

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 −4-4 to 33. Each step adds one idea.

  1. Start with the trained weights, which are floats: w=[0.9, −1.8, 0.2, 2.7]w = [0.9,\ -1.8,\ 0.2,\ 2.7]. Check: these are the values the network learned, and nothing about them has changed.
  2. Choose the grid. The largest magnitude is 2.72.7, so map it to the largest integer, 33. The step size, called the scale ss, is s=2.7/3=0.9s = 2.7 / 3 = 0.9. Check: 3×0.9=2.73 \times 0.9 = 2.7.
  3. Divide each weight by the scale and round to the nearest integer. 0.9/0.9=10.9 / 0.9 = 1, −1.8/0.9=−2-1.8 / 0.9 = -2, 0.2/0.9≈0.220.2 / 0.9 \approx 0.22 rounds to 00, and 2.7/0.9=32.7 / 0.9 = 3. The integers are q=[1, −2, 0, 3]q = [1,\ -2,\ 0,\ 3]. Check: all four lie inside −4-4 to 33, so nothing needed clipping.
  4. Store the integers and the one scale. To use the weights again, multiply back: w^=0.9×q=[0.9, −1.8, 0, 2.7]\hat{w} = 0.9 \times q = [0.9,\ -1.8,\ 0,\ 2.7]. Check: three of the four values come back exactly. The weight 0.20.2 came back as 00, an error of 0.20.2.

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.

−3−2−10123−0.4−0.200.20.4weight xx minus its quantized value
  • error, scale 0.9
With scale 0.9 the error never exceeds half a step, 0.45. The weight 0.2 sits close to the grid point 0, so its error is 0.2.
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

Definition: Post-training quantization

Given a trained network with weights ww and a small calibration set of inputs, post-training quantization maps each real value xx to an integer qq, using a scale s>0s \gt 0 and an integer zero point zz fixed from the calibration data. The rule is

q=clip⁡ ⁣(round⁡ ⁣(xs)+z,  qmin⁡,  qmax⁡),x^=s (q−z),q = \operatorname{clip}\!\left(\operatorname{round}\!\left(\frac{x}{s}\right) + z,\; q_{\min},\; q_{\max}\right), \qquad \hat{x} = s\,(q - z),

where qmin⁡q_{\min} and qmax⁡q_{\max} are the smallest and largest integers the bit width allows, and x^\hat{x} 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 ss and zz.

Step 2 in symbols: choosing the scale

Step 2 used a symmetric grid, so z=0z = 0. For a signed bb-bit integer the largest positive value is 2b−1−12^{b-1} - 1, and the scale is

s=max⁡∣x∣2b−1−1.s = \frac{\max |x|}{2^{b-1} - 1}.

For the toy grid, b=3b = 3, so the divisor is 22−1=32^{2} - 1 = 3, and s=2.7/3=0.9s = 2.7 / 3 = 0.9. For real 8-bit weights the divisor is 127127. 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 ss and rounded. The rounding is the only lossy operation. Clipping does nothing here, because a symmetric scale built from max⁡∣x∣\max |x| 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 xx inside the range, rounding moves x/sx / s by at most 12\tfrac{1}{2}. Multiplying by ss moves xx by at most s2\tfrac{s}{2}, so

∣x−x^∣≤s2.|x - \hat{x}| \le \frac{s}{2}.

For the toy grid, s/2=0.45s / 2 = 0.45, and the error 0.20.2 for the weight 0.20.2 is within that bound.

Memory in symbols

A layer with nn weights stored in bb bits takes nb/8n b / 8 bytes, plus one scale for each group of weights. A layer of nn float32 weights takes 4n4n bytes. The ratio is 32/b32 / b, which is 4 for 8-bit integers.

Worked examples

Example
Tiny: round one value to a grid of step 0.5
  1. Set the grid. The scale is s=0.5s = 0.5 and the zero point is z=0z = 0, so the representable values are 0,0.5,1.0,1.5,…0, 0.5, 1.0, 1.5, \dots
  2. Take the value x=1.2x = 1.2. Divide by the scale: 1.2/0.5=2.41.2 / 0.5 = 2.4.
  3. Round to the nearest integer: round⁡(2.4)=2\operatorname{round}(2.4) = 2. So q=2q = 2.
  4. Multiply back: x^=0.5×2=1.0\hat{x} = 0.5 \times 2 = 1.0.
  5. Error: 1.2−1.0=0.21.2 - 1.0 = 0.2. Check: this is within s/2=0.25s / 2 = 0.25.
  6. Now take x=−0.37x = -0.37. Divide: −0.37/0.5=−0.74-0.37 / 0.5 = -0.74. Round: −1-1. So x^=−0.5\hat{x} = -0.5 and the error is 0.130.13, again within 0.250.25.

Answer: 1.21.2 becomes 1.01.0 and −0.37-0.37 becomes −0.5-0.5. Each error is under half a step, which is the whole guarantee of rounding to a grid.

Example
Standard: quantize four weights to 8 bits

Take the weight row w=[0.31, −0.84, 0.05, 0.62]w = [0.31,\ -0.84,\ 0.05,\ 0.62] and quantize it to signed 8-bit integers with a symmetric scale.

  1. Largest magnitude: max⁡∣w∣=0.84\max |w| = 0.84.
  2. Scale: s=0.84/127≈0.0066142s = 0.84 / 127 \approx 0.0066142.
  3. Quantize 0.310.31: 0.31/s≈46.90.31 / s \approx 46.9, which rounds to q=47q = 47.
  4. Quantize −0.84-0.84: −0.84/s=−127-0.84 / s = -127 exactly, so q=−127q = -127.
  5. Quantize 0.050.05: 0.05/s≈7.560.05 / s \approx 7.56, which rounds to q=8q = 8.
  6. Quantize 0.620.62: 0.62/s≈93.70.62 / s \approx 93.7, which rounds to q=94q = 94.
  7. Multiply back. 47×s≈0.310847 \times s \approx 0.3108, −127×s=−0.84-127 \times s = -0.84, 8×s≈0.05298 \times s \approx 0.0529, 94×s≈0.621794 \times s \approx 0.6217.
  8. Errors: 0.00090.0009, 00, 0.00290.0029, and 0.00170.0017. Each is below s/2≈0.0033s / 2 \approx 0.0033.

Answer: the four weights now take four bytes plus one scale, down from 16 bytes of float32. The largest error is 0.00290.0029, 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.

Example
Realistic: a 4096 by 4096 linear layer

A Transformer layer often has a weight matrix of size 4096×40964096 \times 4096, which is n=16,777,216n = 16{,}777{,}216 weights. Compare two storage formats. Ignore the small bias vector.

  1. In 16-bit floats each weight takes 2 bytes. The layer takes 2×16,777,216=33,554,4322 \times 16{,}777{,}216 = 33{,}554{,}432 bytes, about 33.6 MB.
  2. In 8-bit integers each weight takes 1 byte: 16,777,21616{,}777{,}216 bytes, about 16.8 MB.
  3. PTQ also stores one 16-bit scale per output row, because each row is scaled on its own. That is 4096×2=8,1924096 \times 2 = 8{,}192 bytes, about 0.008 MB.
  4. Total for the 8-bit version: 16,777,216+8,192=16,785,40816{,}777{,}216 + 8{,}192 = 16{,}785{,}408 bytes, about 16.8 MB.
  5. Ratio: 33,554,432/16,785,408≈2.033{,}554{,}432 / 16{,}785{,}408 \approx 2.0. 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, n=1,000,000n = 1{,}000{,}000. Quantize it to 4-bit integers with no scales. How many megabytes does it take? Answer: 44 bits per weight gives 4,000,0004{,}000{,}000 bits, which is 500,000500{,}000 bytes, so 0.50.5 MB. The float32 layer took 44 MB, so the ratio is 88.

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 qq and one shared scale ss, using x^=s (q−z)\hat{x} = s\,(q - z).
  • 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 s/2s / 2.
  • 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 ss measure, in plain words?Show answer

The scale is the real-number distance between two neighbouring integers on the grid. Multiplying an integer qq by ss 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 s=0.5s = 0.5 and zero point z=0z = 0, quantize x=0.9x = 0.9. What are qq, the reconstructed value x^\hat{x}, and the error?Show answer

Divide: 0.9/0.5=1.80.9 / 0.5 = 1.8. Round: 22, so q=2q = 2. Multiply back: x^=0.5×2=1.0\hat{x} = 0.5 \times 2 = 1.0. The error is 0.9−1.0=−0.10.9 - 1.0 = -0.1, which has size 0.10.1 and is within s/2=0.25s / 2 = 0.25.

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 3×109×2=6×1093 \times 10^9 \times 2 = 6 \times 10^9 bytes, about 6 GB. In 8-bit integers they take 3×1093 \times 10^9 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.

Sources