4.3.13Pretraining & Fine-Tuning LLMs

Quantization (INT8, INT4, GPTQ)

2,215 words10 min readdifficulty · medium

WHY do we quantize at all?

WHAT problem is being solved: floats are wasteful. Weight distributions in trained nets are roughly bell-shaped and clustered near 0; we don't need 16 bits to say "roughly 0.03."


The core operation: mapping floats → integers

HOW do we choose ss and zz? We want the range [xmin,xmax][x_{\min}, x_{\max}] to map exactly onto the integer range [qmin,qmax][q_{\min}, q_{\max}] (e.g. [128,127][-128,127] for signed INT8).


Granularity: one scale for how many numbers?

Figure — Quantization (INT8, INT4, GPTQ)

INT8 vs INT4


PTQ vs QAT

For LLMs, retraining is expensive, so PTQ dominates — hence GPTQ.


GPTQ — the clever part


Worked examples


Common mistakes


Flashcards

What are the scale ss and zero-point zz in affine quantization?
q=round(x/s)+zq=\text{round}(x/s)+z; ss sets the step size (real units per integer step), zz is the integer that real-value 0 maps to.
Derive the quantization scale ss.
Force xmin ⁣ ⁣qminx_{\min}\!\to\!q_{\min} and xmax ⁣ ⁣qmaxx_{\max}\!\to\!q_{\max}; subtract to cancel zz: s=(xmaxxmin)/(qmaxqmin)s=(x_{\max}-x_{\min})/(q_{\max}-q_{\min}).
Symmetric quantization scale for weights?
s=maxx/qmaxs=\max|x|/q_{\max}, with z=0z=0.
Why is INT4 much harder than INT8?
Only 16 levels vs 256, so the step ss is ~16× larger → big rounding error; needs compensation (GPTQ/AWQ).
What does GPTQ minimize?
Layer output error WXW^X22\lVert WX-\hat W X\rVert_2^2, not raw weight error.
What is the Hessian in GPTQ and why?
H=2XXH=2XX^\top; its inverse tells how to redistribute each rounding error into remaining weights to keep output unchanged.
PTQ vs QAT?
PTQ quantizes a trained model without training (GPTQ, AWQ); QAT simulates quantization during training for robustness.
Per-tensor vs per-group granularity?
Per-tensor: one scale for whole matrix (cheap, worse). Per-group: one scale per block (e.g. 128 weights) — standard for INT4, handles outliers.
Memory of a 7B model in FP16 / INT8 / INT4?
14 GB / 7 GB / ~3.5–4 GB.
Why must you clamp during quantization?
Outliers push qq past the integer range; clamping to [qmin,qmax][q_{\min},q_{\max}] prevents overflow.

Recall Explain to a 12-year-old (Feynman)

Imagine describing everyone's height using a ruler. FP16 is a ruler with a thousand tiny marks — super precise but bulky to carry. Quantization swaps it for a ruler with only 16 marks (INT4): lighter, but if you just snap each height to the nearest mark, some people get badly rounded. GPTQ is the clever teacher who, after rounding one kid's height up a bit, whispers to the next kids "stand a hair shorter" so that when you add the group up, the total is still right. The class fits in a small notebook, but the important totals are preserved.

Connections

  • Mixed-Precision Training (FP16, BF16) — the training-time cousin of quantization.
  • LoRA and QLoRA — QLoRA fine-tunes on top of a 4-bit (NF4) quantized base model.
  • Inference Optimization & KV Cache — quantization is a key memory-bandwidth win.
  • Hessian and Second-Order Methods — GPTQ's H=2XXH=2XX^\top is Optimal Brain Surgery reused.
  • Weight Distributions in Neural Nets — why near-zero, bell-shaped weights quantize well.
  • AWQ (Activation-aware Weight Quantization) — protects salient channels; sibling of GPTQ.

Concept Map

motivates

maps floats to

reduces

enables

core op

needs

needs

derived from

rounded to

special case z=0

outliers waste range

finest

FP16 weights waste bits

Quantization

Low-bit integers

Memory 4x smaller

Faster inference

Affine quantization

Scale s

Zero-point z

Match xmin xmax to qmin qmax

Valid integer so 0 maps

Symmetric quant for weights

Granularity

Per-group g=128 for INT4

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek LLM ke weights normally FP16 (16-bit float) me store hote hain, jo bahut precise hai par memory bahut khata hai — 7B model ka matlab 14 GB sirf weights ke liye. Quantization ka idea simple hai: in floats ko chhote integers me convert kar do — INT8 (256 levels) ya INT4 (sirf 16 levels). Formula ka core hai q=round(x/s)q=\text{round}(x/s), jahan ss "scale" hai yaani ek integer step kitne real units ka hai. Scale nikalne ke liye hum bas range [xmin,xmax][x_{\min},x_{\max}] ko integer range pe map karte hain, aur do endpoint equations subtract karke s=(xmaxxmin)/(qmaxqmin)s=(x_{\max}-x_{\min})/(q_{\max}-q_{\min}) nikal aata hai.

INT8 me itne levels hain ki error almost zero rehta hai — bas simple round kar do, kaam ho gaya. Par INT4 me sirf 16 levels hain, to har weight ka rounding error bada ho jata hai (roughly 16 guna). Isliye INT4 me naive rounding se accuracy giR jati hai, aur yahin GPTQ kaam aata hai.

GPTQ ki trick genius hai: weights ko ek-ek karke quantize karo, aur jab ek weight ko grid pe snap karo to jo error aaya usko baaki bache huye weights me compensate kar do. Ye compensation Hessian H=2XXH=2XX^\top ke inverse ke through hota hai — matlab input correlations ke hisaab se error ko smartly distribute karta hai. Isse layer ka output almost same rehta hai, chahe har weight ab 4-bit integer ban gaya ho. Aur haan — GPTQ retraining nahi hai, ye sirf ek chhote calibration set (~128 samples) se closed-form solve karta hai, isliye fast hai.

Kyu important hai? Kyunki quantization ki wajah se hi ek bada model tumhare laptop/consumer GPU pe chal pata hai, aur inference bhi tez hota hai (kyunki inference memory-bandwidth bound hai, kam bytes = kam wait). QLoRA jaise techniques bhi isi 4-bit base pe fine-tuning karte hain. Short me: quantization = badे model ko chhota + tez banao, bina brain khoye.

Go deeper — visual, from zero

Test yourself — Pretraining & Fine-Tuning LLMs

Connections