4.3.13 · HinglishPretraining & Fine-Tuning LLMs

Quantization (INT8, INT4, GPTQ)

2,014 words9 min readRead in English

4.3.13 · AI-ML › Pretraining & Fine-Tuning LLMs


HUM quantize karte kyun hain?

KYA problem solve ho rahi hai: floats wasteful hain. Trained nets mein weight distributions roughly bell-shaped aur 0 ke paas clustered hoti hain; "roughly 0.03" kehne ke liye humein 16 bits nahi chahiye.


Core operation: floats → integers map karna

aur kaise choose karte hain? Hum chahte hain ki range exactly integer range pe map ho (jaise signed INT8 ke liye ).


Granularity: ek scale kitne numbers ke liye?

Figure — Quantization (INT8, INT4, GPTQ)

INT8 vs INT4


PTQ vs QAT

LLMs ke liye retraining expensive hai, isliye PTQ dominate karta hai — hence GPTQ.


GPTQ — clever part


Worked examples


Common mistakes


Flashcards

Affine quantization mein scale aur zero-point kya hain?
; step size set karta hai (real units per integer step), woh integer hai jis pe real-value 0 map hoti hai.
Quantization scale derive karo.
Force karo aur ; cancel karne ke liye subtract karo: .
Weights ke liye symmetric quantization scale?
, with .
INT4 INT8 se zyaada mushkil kyun hai?
Sirf 16 levels vs 256, toh step ~16× bada → bada rounding error; compensation chahiye (GPTQ/AWQ).
GPTQ kya minimize karta hai?
Layer output error , raw weight error nahi.
GPTQ mein Hessian kya hai aur kyun?
; iska inverse batata hai ki har rounding error ko remaining weights mein kaise redistribute karein taaki output unchanged rahe.
PTQ vs QAT?
PTQ ek trained model ko bina training ke quantize karta hai (GPTQ, AWQ); QAT robustness ke liye training ke dauran quantization simulate karta hai.
Per-tensor vs per-group granularity?
Per-tensor: poori matrix ke liye ek scale (sasta, worse). Per-group: har block ke liye ek scale (jaise 128 weights) — INT4 ke liye standard, outliers handle karta hai.
7B model ki memory FP16 / INT8 / INT4 mein?
14 GB / 7 GB / ~3.5–4 GB.
Quantization ke dauran clamp kyun karna zaroori hai?
Outliers ko integer range se bahar push karte hain; pe clamping overflow rokta hai.

Recall 12-saal ke bachche ko explain karo (Feynman)

Socho sabki height ek ruler se describe kar rahe ho. FP16 ek ruler hai jisme hazaar chhote marks hain — super precise lekin carry karna mushkil. Quantization ise sirf 16 marks wale ruler (INT4) se badal deta hai: halka, lekin agar tum sirf har height ko nearest mark pe snap karo, toh kuch logon ki height badly round ho jaati hai. GPTQ woh clever teacher hai jo, ek bachche ki height thodi upar round karne ke baad, agli bacho ko whisper karta hai "thoda chhote khadhe ho" taaki jab tum group add karo, total sahi rahe. Class ek chhoti notebook mein fit ho jaati hai, lekin important totals preserve rehte hain.

Connections

  • Mixed-Precision Training (FP16, BF16) — quantization ka training-time cousin.
  • LoRA and QLoRA — QLoRA ek 4-bit (NF4) quantized base model ke upar fine-tune karta hai.
  • Inference Optimization & KV Cache — quantization ek key memory-bandwidth win hai.
  • Hessian and Second-Order Methods — GPTQ ka Optimal Brain Surgery reused hai.
  • Weight Distributions in Neural Nets — near-zero, bell-shaped weights quantize kyun well karte hain.
  • AWQ (Activation-aware Weight Quantization) — salient channels protect karta hai; GPTQ ka sibling.

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