Quantization, Step by Step

Quantization, Step by Step

A model is billions of numbers. Quantization stores each one with fewer bits. These five stations follow one number into its bits, then a few numbers into integer codes and back, and show what gets lost on the way, and one way to lose less.

What you will learn on this page

  1. A number in 16 bits is three fields read separately: a sign, an exponent (the scale) and a mantissa (the detail). FP16 and BF16 split the bits differently, and the space to the next number that can be stored grows with the number. → section 1
  2. Absmax: the largest absolute value sets one scale; every value is multiplied by it and rounded to an integer code. With fewer bits there are fewer codes, and different values start to share one. → section 2
  3. One outlier stretches the range, so the ordinary values share a few codes. Clipping caps the range: one large error for the outlier, smaller errors for all the others. → section 3
  4. GPTQ rounds the weights one at a time and moves each rounding error into the weights not yet rounded (here, simply the next one), so the output stays closer than when each weight is rounded on its own. → section 4
  5. A scale for each block of weights keeps the small weights from rounding to 0; each extra scale costs 16 bits, as in GGUF files. → section 5

1One number in 16 bits

Click a bit to flip it, or type a number. The three colored fields are read separately, then multiplied.

Examples:

Sign

Exponent: the scale

Mantissa: the detail

Play walks the exponent: 1.5, then × 2 and ÷ 2, one exponent step at a time. The line below is a scale of sizes: the mark 2^k stands for 2 to the power k, and one power to the right is twice as large. Watch the space to the next number.

2Absmax: from floats to integer codes

Nine values, stored as signed integers. The largest absolute value, α = 10.8, is mapped to the largest code. Every value is multiplied by the same scale s and rounded. Fewer bits: fewer codes, and values start to share one.

Bits:
a value a code shared by two values: they can no longer be told apart

Click a row to see its arithmetic

3One outlier stretches the range

Twenty-four ordinary values between −2 and 2, and one value of 256. The top number line shows all of them, with the range in orange. The lower one zooms in on the ordinary values, with the codes as orange ticks: with the outlier, absmax spreads the codes so far apart that very few of them fall there. Clipping shrinks the range, and the codes move closer together.

Bits:
Clip at:

Clipping trades one error for many: the outlier is stored as the clipping value, and every ordinary value gets closer to a code. Which trade is right depends on how much the outlier matters.

4GPTQ: round one weight, then correct the next

One output of a learned table: six weights (the model's learned values, its parameters), one input of six numbers x₁ … x₆, and one output, their dot product y = w₁x₁ + … + w₆x₆. The weights can only be stored at quantization levels 0.25 apart (the gray lines). Rounding each weight on its own lets the errors add up. GPTQ rounds the weights one at a time, and moves each rounding error into the next weight, before that one is rounded.

the original weight the weight now, after corrections rounded and stored ▲ the next weight to be rounded

Real GPTQ computes the corrections for all the weights not yet rounded, from a small set of real inputs (calibration data), so that the output stays close on average over those inputs. Here there is one input, and the whole error goes into the next weight.

5Smaller blocks: less error, more bits

How large should a block be? These are the 96 weights of the GGUF slide, stored with 4 bits. Each bar is the code of a weight, from 0 to 7. Pick a block size and compare it with the others in the charts below: smaller blocks store the weights more exactly, and every block adds a 16-bit scale.

Weights per block:
the code of a weight, without its sign a weight stored as code 0