Skip to content

Quantization: fewer bits per weight

Store weights as small integers with a scale and zero point, see where rounding and clipping errors come from, how GPTQ, AWQ and SmoothQuant reduce them, and when quantization-aware training pays.

Advanced ~16 min10 steps

What runs in CodeSplash

You will be able to

  • Quantize a value with a scale and zero point and find its rounding and clipping error
  • Explain how GPTQ uses calibration inputs to correct rounding errors
  • Explain the exact rescaling behind AWQ and SmoothQuant
  • Choose between post-training quantization and quantization-aware training
  • Find the 8-bit options CodeSplash offers and how they are measured

Read first

01

Integers with a scale

Read

Quantization stores each real weight ww as a small integer qq with a scale and zero pointScale and zero pointInteger storage of real weights: value ≈ scale × (integer − zero point), with the integer clipped to its range.:

q=clip(round(w/s)+z,  qmin⁡, qmax⁡),w^=s (q−z).q = \mathrm{clip}\big(\mathrm{round}(w / s) + z,\; q_{\min},\, q_{\max}\big), \qquad \hat w = s\,(q - z).

Two errors come out: rounding (at most half a step) and clipping, when w/sw/s falls outside the integer range. In the reference's 4-bit example the range is −8-8 to 77 with steps of a tenth, so the largest value comes back as 0.7: clipped, not rounded. Picking the scale trades the two errors: a larger step clips less and rounds more.

DExplorable · Quantization: scale, zero point and error

What does storing weights in 8 or 4 bits cost, and why does one large weight make it worse?

4

Each weight becomes one of 2^bits levels.

Scheme

Symmetric centers the levels on 0; asymmetric shifts them to cover the weights' actual range.

Scale shared by

Smaller groups adapt to local ranges but store more scales.

0.0

Replace the first weight with this value (0 = no outlier).

Scale (first group)
0.5994
Mean squared error
0.03108
Largest error
0.2981
SQNR (dB)
15.60
Bytes
130
  • Puzzle: With an outlier of 10 or more, get SQNR above 18 dB at 4 bits.
  • Puzzle: Store the weights in under a quarter of fp16's bytes with SQNR above 10 dB.
4-bit symmetric quantization with one scale for all 256 weights: scale 0.5994, 11 levels used, SQNR 15.6 dB, largest error 0.298. Storage is 130 bytes, 3.9× smaller than fp16.

4-bit symmetric quantization with one scale for all 256 weights: scale 0.5994, 11 levels used, SQNR 15.6 dB, largest error 0.298. Storage is 130 bytes, 3.9× smaller than fp16.

02

Finer scales, smaller errors

Read

One scale for a whole tensor is set by its largest value, so most weights use few of the available integers. A scale per row, or per small group of weights, fits each part's own range. CodeSplash's 8-bit weights use one scale per row. Local language models in Ollama use group-wise formats such as Q4_K_M, about four and a half bits per weight with the scales included.

03

GPTQ: correct the rounding as you go

Read

Rounding every weight on its own ignores how the layer is used. GPTQGPTQRound a layer's weights to few bits one column at a time, correcting the rest for each error using calibration inputs. quantizes a layer one column at a time and, after each, adjusts the not-yet-quantized columns to cancel that column's error on a set of calibration inputs. It minimizes the change in the layer's outputs, not in its weights.

In the reference's example, plain rounding to tenths changes the output of a two-by-two layer by a squared error of 0.0065. That output error is what GPTQ's correction reduces. Calibration inputs should look like real use: a model calibrated on one kind of text can lose more on another.

04

AWQ and SmoothQuant: an exact rescaling first

Read

A few input channels often carry much larger activations than the rest. AWQAWQScale up the weight channels that meet large activations before rounding, and fold the inverse scale into the input. multiplies the weights that meet those channels by a scale and divides the inputs by the same scale. Before rounding nothing changes, Wx=(W diag(d))(diag(d)−1x)Wx = (W\,\mathrm{diag}(d))(\mathrm{diag}(d)^{-1}x), and the reference's example checks exactly that. After rounding, the important weights are larger relative to the step, so they lose less.

SmoothQuantSmoothQuantMove activation outliers into the weights with a per-channel scale, so both weights and activations fit in 8 bits. uses the same identity the other way: it divides activation outliers down and moves them into the weights, so both activations and weights fit in 8 bits.

05

Quantization-aware training

Read

GPTQ, AWQ and SmoothQuant are post-training: no retraining. Quantization-aware trainingQuantization-aware training (QAT)Train with weights rounded in the forward pass (gradients pass straight through), so the network learns to tolerate rounding. rounds the weights in the forward pass during training and passes gradients straight through the rounding, so the network learns weights that survive it. It costs a training run, and usually only pays off at 4 bits or fewer, where post-training methods lose too much.

06

In CodeSplash

In the app
  • Make a model from this → Compress → 8-bit weights stores a network's weights in 8 bits with one scale per row and measures the result on the rows you choose.
  • Models → Export → ONNX (torch) can add an int8 copy (weights only, or weights and activations, through ONNX Runtime) and reports how far its predictions move from the network's.
  • Settings → AI, for a local provider, estimates the memory a language model needs at its quantization.
07

What runs here

Read
08

Common mistakes

Read
09

Check: quantization

Checkpoint

Check yourself

A 4-bit integer holds −8 to 7. With scale 0.1 and zero point 0, what does the value 1.3 come back as?

Show the answer

Answer: 0.7

1.3 / 0.1 = 13, which clips to 7, and 7 × 0.1 = 0.7. Clipping error dominates here; a larger scale would avoid it at the cost of coarser rounding.

Check yourself

When is quantization-aware training most worth its cost?

Show the answer

Answer: a

QAT needs a training run. At 8 bits post-training methods usually lose little; at 4 bits and below QAT can recover a lot.

Check yourself

What does GPTQ try to keep small?

Show the answer

Answer: a

It quantizes column by column and updates the remaining columns to cancel the output error each rounding causes.

Check yourself

A model was GPTQ-quantized with English news as calibration text. It is then used on source code. What risk follows?

Show the answer

Answer: a

GPTQ's corrections minimize errors on the calibration inputs. Calibrate on data like the real use.

Check yourself

AWQ scales some weight channels up and their inputs down by the same factor. What does that change before rounding?

Show the answer

Answer: a

W·x = (W·diag(d))·(diag(d)⁻¹·x). The gain appears only after rounding, when the important channels lose less.

Check yourself

Which weight channels does AWQ protect?

Show the answer

Answer: a

Activation size, not weight size, decides which channels matter most to the output.

Check yourself

What problem does SmoothQuant solve?

Show the answer

Answer: a

Weights are easy to quantize; activations with a few huge channels are not. Moving the scale into the weights balances the two.

Check yourself

Which of these runs inside CodeSplash?

Show the answer

Answer: c

GPTQ, AWQ, SmoothQuant and QAT are explained here. CodeSplash's networks are small enough that post-training int8 is the practical route.

10

Recap

Recap
  • A quantized weight is scale × (integer − zero point); errors come from rounding and clipping.
  • Per-row or per-group scales cut the error; GPTQ corrects rounding against calibration inputs.
  • AWQ and SmoothQuant apply an exact rescaling before rounding so the important parts lose less.
  • QAT trains through the rounding; worth it at very low bit widths.
  • In CodeSplash: Compress → 8-bit weights, and the ONNX (torch) export's int8 copy.

Concepts in this lesson