4.3.14 · HinglishPretraining & Fine-Tuning LLMs

Knowledge distillation

1,785 words8 min readRead in English

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


Knowledge distillation KYA hai?

Soft targets zyada information kyun carry karte hain? {cat, dog, car} ke upar ek image classifier consider karo. Cat ki photo ke liye hard label hai . Lekin teacher output kar sakta hai . Yeh student ko woh baat bataata hai jo hard label nahi bata sakta: "ek cat ek car se kahin zyada ek dog jaisi dikhti hai." Yeh inter-class similarity structure dark knowledge kehlaata hai.


Mechanism: temperature ke saath softmax

Standard softmax logits ke upar:

Temperature ke saath softmax:

  • : normal softmax.
  • : saare logits ek bade number se divide hote hain probabilities uniform .
  • : one-hot (argmax) ki taraf sharpen ho jaata hai.
Figure — Knowledge distillation

Loss function — pehle principles se derive kiya hua

Hum chahte hain ki student ek saath do kaam kare:

(1) Teacher ke soft targets match kare. Teacher soft probs aur student soft probs ke beech cross-entropy (equivalently KL divergence) use karo, dono temperature par:

(2) Sahi answer phir bhi deta rahe. Hard labels ke saath par standard cross-entropy:

Inhe ek mixing weight ke saath combine karo:

factor kyun? (Yeh woh step hai jise sab bhool jaate hain.) Jab aap softmax-with-temperature ke through backprop karte ho, toh ka gradient student logits ke w.r.t. ki tarah scale karta hai (ek student softmax se, ek logit division ke through chain rule se). Soft-target gradient ki magnitude ko hard-target gradient ke comparable rakhne ke liye, hum se multiply karte hain. Iske bina, badhane se soft loss silently irrelevance mein chali jaati hai.

Gradient sketch (WHY ): Softmax cross-entropy ke liye, . Temperature ke saath, student prob hai aur har logit se scaled hai, isliye Chhote logits ke liye (Taylor expand ), jahan teacher logits hain, jo overall deta hai. Loss ko se multiply karne se yeh cancel ho jaata hai. ✅


Worked example 1 — ek distribution ko soften karna

Teacher logits {cat, dog, car} ke upar.

par: , sum . . Yeh step kyun? Confidence "cat" par crush ho gayi hai; dog/car ki info almost invisible hai.

par: logits ban jaate hain . , sum . . Yeh step kyun? Ab "dog" clearly non-trivial 17.5% hai — student seekh sakta hai ki cats dogs jaisi hoti hain. Yahi dark knowledge surface ho raha hai.


Worked example 2 — combined loss compute karna

Maano , . Student soft probs , teacher , true label = cat, student normal probs .

Soft term: .

Hard term: .

Total: . Yeh step kyun? scaling soft term ko yahan dominate karaata hai — yahi iska purpose hai: student ko teacher ki structure absorb karne par force karo jabki hard label use honest rakhta hai.


LLMs ke liye specifically KD

Common LLM KD variants:

  • Response-based (logit) KD: next-token logit distributions match karo. ← classic Hinton KD.
  • Feature-based KD: intermediate hidden states/attention maps match karo (student seekhta hai ki teacher kaise sochta hai, na sirf kya output karta hai).
  • Sequence-level KD: teacher text generate karta hai, student us generated text pe data ke roop mein train hota hai (instruction-following distillation ke liye heavily use hota hai, jaise Alpaca-style).


Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho ek genius chef (teacher) aur ek student cook. Agar chef sirf yeh kahe "yeh dish 'pizza' hai," toh student thoda seekhta hai. Lekin agar chef kahe "yeh zyaatar pizza hai, kuch had tak calzone jaisa hai, aur soup jaisa bilkul nahi," toh student kahin zyada seekhta hai — including yeh ki khaane kaise relate karte hain ek doosre se. Knowledge distillation chhote model ko bade model ki poori raay use karke sikhaana hai, na sirf uska final answer. "Temperature" aise hai jaise chef se kaho ki slow down karo aur subtle in-between thoughts share karo instead of seedha answer bolne ke.


Recall checkpoints


Flashcards

Knowledge distillation kya hai?
Ek chhote student model ko train karna taaki woh ek bade teacher model ki softened output distribution (soft targets) imitate kare, na ki sirf hard labels.
"Soft targets" kya hote hain?
Teacher ki classes ke upar poori probability distribution (temperature se soften ki hui), jo inter-class similarity info carry karta hai.
"Dark knowledge" kya hai?
Woh relational information jo teacher ki non-argmax probabilities mein chupi hoti hai (jaise cat dog se zyada milti hai car se nahi).
Temperature ke saath softmax likho.
hone par kya hota hai?
Distribution uniform ho jaata hai (har ek ).
hone par kya hota hai?
Distribution one-hot (argmax) ki taraf sharpen ho jaata hai.
Distillation loss likho.
factor kyun?
Soft-loss gradient ki tarah scale karta hai; se multiply karne se uski magnitude hard-loss gradient ke comparable rehti hai.
kyun use nahi karte?
Ek trained teacher par near one-hot hota hai, isliye soft targets hard targets mein collapse ho jaate hain aur dark knowledge chupi rehti hai.
KD vs label smoothing?
Label smoothing uniform noise add karta hai (koi structure nahi); KD soft targets encode karte hain ki kaun si classes actually similar hain.
Teen LLM distillation variants batao.
Response/logit-based, feature-based (hidden states/attention), sequence-level (teacher-generated text par train karo).
Student ko factually correct kaun sa term rakhta hai?
Hard-label cross-entropy term jo se weighted hota hai.

Connections

Concept Map

produces

contains

lacks

flattens

reveals

matched via

matched via

weighted by alpha and T squared

weighted by 1 minus alpha

trains

gradient scales 1 over T squared

justifies

imitates

Teacher model big

Soft targets

Dark knowledge inter-class similarity

Hard targets one-hot

Temperature T

L_soft KL divergence

L_hard cross-entropy

Distillation loss

Student model small

T squared correction