4.3.13 · D4Pretraining & Fine-Tuning LLMs

Exercises — Quantization (INT8, INT4, GPTQ)

2,519 words11 min readBack to topic

Throughout, one convention: a real weight is stored as an integer via a scale (how many real units each integer step is worth) and a zero-point (the integer that the real value lands on):

is the dequantized value — our best reconstruction of from the stored integer. The gap is the quantization error.

Figure — Quantization (INT8, INT4, GPTQ)

The picture above is the mental model for every problem: a real-number axis, a ladder of allowed integer levels spaced apart, and each weight snapping to its nearest rung.


Level 1 — Recognition

Recall Solution

The number of levels for bits is .

  • INT8: levels, signed range .
  • INT4: levels, signed range .

Why the range isn't symmetric ( vs ): two's-complement spends one code on , so the negative side gets one extra slot.

Recall Solution
  • (a)–(ii): = real units per integer step.
  • (b)–(iii): = integer that real maps to.
  • (c)–(iv): clamp = keep in the legal range.
  • (d)–(i): dequantize = , integer → real.
Recall Solution
  • GPTQ → PTQ (post-training, no full backprop).
  • AWQ → PTQ.
  • "simulate rounding during training with backprop" → QAT.

Level 2 — Application

Recall Solution

Why symmetric: weights cluster around , so a symmetric grid centered at wastes no range and needs no zero-point add.

  • .
  • :
    • So .
  • .
  • Error .

Why so small: step , so any rounding error is at most .

Recall Solution

The tool and why: affine (asymmetric) quantization is used when the data is not centered on (here it leans positive). We force the two endpoints to line up and solve. Why round : the zero-point must be a real integer so that the value maps exactly to code (important for padding). Check: does map inside range? clamp to 255. This is expected; endpoint rounding can overshoot by one, and clamping fixes it.

Recall Solution
  • .
  • .
  • .
  • Error on : here — lucky, because is an exact multiple of .

Lesson: INT4 error is usually large ( vs for INT8 → ~18× coarser grid), but if a value lands exactly on a rung the error vanishes. Don't generalize from a lucky vector.


Level 3 — Analysis

Recall Solution
  • .
  • Quantize : , so .
  • Error 57% relative error on that weight.

The analysis: the single outlier inflated by ~200×, stretching the grid so wide that all the "normal" small weights fall between just a couple of rungs. 253 of the 256 levels sit unused in the empty range between and . Fix: per-group scales (one per block of 128) so the outlier only spoils its own group — see the parent's granularity section.

Recall Solution

Why : for a uniform distribution on width , the variance is ; here width , giving .

  • INT8: , .
  • INT4: , .
  • Ratio .

The point: memory only halved (8→4 bits), but the noise standard deviation jumped ~18×. Each dropped bit doubles , and the step scaling with the number of levels () makes it geometric, not linear. This is precisely why INT4 needs GPTQ/AWQ compensation while INT8 needs almost nothing.

Figure — Quantization (INT8, INT4, GPTQ)
Recall Solution
  • (a) is the rounding error just committed on weight — the amount we snapped it off the grid.
  • (b) is the "self-sensitivity" of weight ; dividing normalizes the correction so the pushed error exactly cancels weight 's contribution to the output. It's a Newton-style step (see Hessian and Second-Order Methods).
  • (c) The objective minimizes output error . The curvature of that quadratic in the weights depends on how the inputs correlate: . Weights that are hit by correlated inputs must be corrected together — that correlation lives in , not in .

Level 4 — Synthesis

Recall Solution

Why add overhead: per-group scales are the reason real INT4 files exceed the naive GB. Bytes per weight . Interpretation: the extra bit/weight (a 3.1% overhead) buys per-group scales that keep INT4 accurate. Naive GB → real GB.

Recall Solution

The tool: GPTQ tells us output error scales with the input Hessian . A larger (bigger, more energetic inputs) means a given weight rounding error produces a larger output perturbation.

  • Layer A: → its weights are hit by high-energy inputs → rounding errors are amplified more.
  • Layer B: → errors barely propagate. Decision: keep Layer A in INT8 (halve its rounding error there), quantize B hard to INT4. Spend precision where the Hessian says errors are most amplified. This is the seed of mixed-precision quantization; see Mixed-Precision Training (FP16, BF16) and AWQ (Activation-aware Weight Quantization).
Recall Solution

Step 1 — quantize . Nearest rung to on step is . So . Rounding error on weight 1: . Step 2 — spread it. The correction to the remaining weights: We only apply the part for the untouched weight : . Result: , compensated (to be quantized next). We raised to absorb the fact that was pushed up, keeping the layer's output closer to the original.


Level 5 — Mastery

Recall Solution

Substitute and : And the zero-point . So symmetric quantization is genuinely the special case.

Recall Solution

Effective bits :

  • : bits.
  • : bits.
  • : bits.
  • : bits.

Recommendation: . Going from 128→64 costs an extra bit/weight (3% more memory) for a marginal accuracy gain, while 128→256 saves only bit but coarsens the scale so outliers spread across twice as many weights. is the community sweet spot precisely because the overhead curve is already nearly flat there.

Recall Solution

By definition . Let . Rounding to the nearest integer means (with hit at half-integers). Multiply by : Equality is approached when sits exactly halfway between two rungs, i.e. is a half-integer. This bound is why doubling (dropping a bit) doubles the worst-case error — the L3.2 nonlinearity in one line.


Wrap-up

Recall Self-check ladder

One line to recall each level. INT4 has how many levels? ::: . Symmetric scale formula? ::: , with . Why is INT4 error ~18× INT8's at fixed range? ::: step scales as , so 4 fewer... no — 4 bits fewer means coarser grid; concretely . What does do in GPTQ? ::: distributes weight 's rounding error into correlated untouched weights. Effective bits for INT4, , FP16 scales? ::: .

Related: Weight Distributions in Neural Nets · Inference Optimization & KV Cache · LoRA and QLoRA.