4.3.13 · AI-ML › Pretraining & Fine-Tuning LLMs
Intuition Ek-sentence idea
Ek neural net apne weights ko high-precision floats (FP16/FP32) mein store karta hai, lekin un bits mein se kaafi redundant precision hoti hai. Quantization un floats ko low-bit integers (INT8, INT4) ke ek chote set pe map kar deta hai, taaki model kam memory use kare aur faster chale — aur ek careful mapping se accuracy almost unchanged rehti hai.
Ek 7B-parameter model FP16 mein sirf weights ke liye 7 × 1 0 9 × 2 bytes = 14 GB chahta hai. INT4 mein yeh 7 × 1 0 9 × 0.5 bytes = 3.5 GB ban jaata hai — ek 4× shrink. Yeh farq hai "GPU pe load hi nahi hoga" aur "laptop pe chal jaayega" ke beech. Memory se move hone waale kam bytes → faster inference bhi, kyunki LLM inference memory-bandwidth-bound hoti hai.
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.
Definition Affine (asymmetric) quantization
Ek real value x ko b bits mein store karne ke liye, ek scale s aur zero-point z chuno:
q = round ( s x ) + z , x ^ = s ( q − z )
jahan q stored integer hai (clamped to [ q m i n , q m a x ] ) aur x ^ ka x ka dequantized approximation hai.
s aur z kaise choose karte hain? Hum chahte hain ki range [ x m i n , x m a x ] exactly integer range [ q m i n , q m a x ] pe map ho (jaise signed INT8 ke liye [ − 128 , 127 ] ).
Definition Symmetric quantization
Agar hum z = 0 set karein aur symmetric range x m a x = − x m i n = max ∣ x ∣ use karein:
s = q m a x m a x ∣ x ∣ , q = round ( s x )
Yeh sasta hai (zero-point add nahi) aur weights ke liye standard hai, jo 0 ke around near-symmetric hote hain.
Intuition Per-tensor vs per-channel vs per-group
Ek single outlier weight max ∣ x ∣ ko blow up kar sakta hai aur tumhare saare integer levels empty range pe waste kar sakta hai. Fix: zyaada, chhote scales use karo.
Per-tensor: poori matrix ke liye ek s — sabse sasta, sabse kam accurate.
Per-channel: har output row ke liye ek s — kaafi better.
Per-group (group size g , jaise 128): g weights ke har block ke liye ek s — INT4 ke liye standard.
INT8: 256 levels, signed range [ − 128 , 127 ] . Post-training quantization usually "just works" (<1% quality loss). FP16 se ~2× memory saving.
INT4: 16 levels, range [ − 8 , 7 ] . Itne kam levels hain ki naive rounding hurt karta hai — accurate rehne ke liye smart methods (GPTQ, AWQ) chahiye. ~4× saving.
Common mistake "Zyaada bits saved hone chahiye toh proportionally zyaada error hona chahiye"
Kyun sahi lagta hai: 8→4 se bits halve karna memory halve karta hai, toh surely error ~double ho?
Sachaai: error nonlinearly badhta hai. Har drop hua bit quantization step s double kar deta hai , toh rounding error (roughly uniform on [ − s /2 , s /2 ] ) bhi badhta hai, lekin model output pe impact is baat pe depend karta hai ki kaunse weights hain aur tumne compensate kiya ya nahi. Isi liye INT4 ko GPTQ-style compensation chahiye, jabki INT8 ko almost kuch nahi chahiye.
Post-Training Quantization (PTQ): already-trained model ko quantize karo, bina gradient training ke. Fast, cheap. GPTQ/AWQ yahan aate hain.
Quantization-Aware Training (QAT): training ke dauran quantization simulate karo taaki model robust banana sikhe. Zyaada accurate, lekin poora training compute chahiye.
LLMs ke liye retraining expensive hai, isliye PTQ dominate karta hai — hence GPTQ.
Intuition Naive rounding dumb kyun hai
Har weight ko nearest grid point pe independently round karna yeh ignore karta hai ki weights interact karte hain. Agar tum weight w 1 ko upar round karte ho, toh tum compensate kar sakte ho abhi-tak unquantized weights ko nudge karke taaki layer ka output sahi rahe. GPTQ exactly yahi karta hai: weights ko ek ek karke quantize karo, aur har ek ke baad, baaki walo ko update karo error absorb karne ke liye.
Definition GPTQ objective
Ek layer ke weight matrix W aur calibration inputs X ke liye, output error minimize karo, weight error nahi:
min W ^ ∥ W X − W ^ X ∥ 2 2
Yeh Optimal Brain Quantization hai layer-wise apply kiya gaya. Magic quantity hai Hessian:
H = 2 X X ⊤
Intuition GPTQ ka Feynman summary
Weights ko greedily, left to right quantize karo. Jab bhi tum ek weight ko grid pe snap karte ho, ek chhoti si error hoti hai — usi waqt us error ko un neighbors mein spread karo jo abhi untouched hain , is hisaab se ki inputs kitne correlated hain (H − 1 ). End mein, layer ka behavior preserve hota hai chahe har weight ab ek 4-bit integer ho.
Worked example 1 — INT8 symmetric quantize ek chhota weight vector
Weights w = [ − 0.9 , 0.1 , 0.6 ] , signed INT8 toh q m a x = 127 .
max ∣ w ∣ = 0.9 ⇒ s = 0.9/127 = 0.00709 . Kyun? Symmetric scale largest magnitude use karta hai.
q = round ( w / s ) = [ round ( − 127 ) , round ( 14.1 ) , round ( 84.6 )] = [ − 127 , 14 , 85 ] .
Dequantize: w ^ = s ⋅ q = [ − 0.900 , 0.0993 , 0.6026 ] .
Error: ≈ [ 0 , 0.0007 , 0.0026 ] — bahut chhota. Chhota kyun? Range [ − 0.9 , 0.9 ] mein 256 levels step ≈ 0.007 deta hai, jo values se kaafi kam hai.
Worked example 2 — Same vector INT4 mein (kyun takleef deta hai)
INT4 signed: q m a x = 7 , toh s = 0.9/7 = 0.1286 .
q = round ( w / s ) = [ − 7 , round ( 0.78 ) , round ( 4.67 )] = [ − 7 , 1 , 5 ] .
w ^ = s ⋅ q = [ − 0.9 , 0.1286 , 0.6429 ] .
0.6 pe error: 0.043 — INT8 se 16× bada . Kyun? Sirf 16 levels matlab step 0.13 ; rounding noise bada hai. Isi liye INT4 ko GPTQ compensation chahiye.
Worked example 3 — 7B model ke liye memory math
FP16: 7 B × 2 = 14 GB.
INT8: 7 B × 1 = 7 GB.
INT4 (+ per-group scales, ~0.5–1 bit overhead): ≈ 3.5 –4 GB.
Yeh kyun matter karta hai: 6 GB consumer GPU pe fit ho jaata hai. Scale-overhead hi reason hai ki real INT4 files naive 3.5 GB se thodi upar hoti hain.
Common mistake Clamp karna bhool jaana
Galat idea: bas x / s round karo. Kyun sahi lagta hai: rounding hi core op hai. Fix: ek outlier q ko [ q m i n , q m a x ] ke bahar push kar sakta hai; tumhe zaroor clamp karna hai warna integer overflow / garbage milta hai. Clamping hi range ko finite banata hai.
Common mistake Activations ko weights ki tarah quantize karna
Galat idea: sab ke liye symmetric per-tensor use karo. Kyun sahi lagta hai: simpler, uniform pipeline. Fix: activations mein huge outliers hote hain (kuch channels 100× bade). Per-token/per-channel use karo ya outlier channels ko FP16 mein rakho (LLM.int8()/AWQ idea). Weights tame hote hain; activations wild hote hain.
Common mistake Yeh maanna ki GPTQ model ko retrain karta hai
Galat idea: GPTQ = 4 bits pe fine-tuning. Fix: GPTQ puri net pe koi backprop nahi karta; yeh ek chhote calibration set (~128 samples) use karke closed-form layerwise least-squares solve karta hai. Yeh PTQ hai, QAT nahi.
Affine quantization mein scale s aur zero-point z kya hain? q = round ( x / s ) + z ; s step size set karta hai (real units per integer step), z woh integer hai jis pe real-value 0 map hoti hai.
Quantization scale s derive karo. Force karo x m i n → q m i n aur x m a x → q m a x ; z cancel karne ke liye subtract karo: s = ( x m a x − x m i n ) / ( q m a x − q m i n ) .
Weights ke liye symmetric quantization scale? s = max ∣ x ∣/ q m a x , with z = 0 .
INT4 INT8 se zyaada mushkil kyun hai? Sirf 16 levels vs 256, toh step s ~16× bada → bada rounding error; compensation chahiye (GPTQ/AWQ).
GPTQ kya minimize karta hai? Layer output error ∥ W X − W ^ X ∥ 2 2 , raw weight error nahi.
GPTQ mein Hessian kya hai aur kyun? H = 2 X X ⊤ ; 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 q ko integer range se bahar push karte hain; [ q m i n , q m a x ] 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.
Mnemonic Pipeline yaad rakho
"Scale, Zero, Round, Clamp, Dequant" → SZ-RCD = "See Zebras Run, Cheetahs Dash." Aur do INT4 helpers ke liye: GPTQ = "Greedily Push The Quantization-error onward."
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 H = 2 X X ⊤ 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.
Match xmin xmax to qmin qmax
Symmetric quant for weights