Isse pehle ki tum samjho kyun hum K aur V cache karte hain (aur Q nahi), tumhe yeh jaanna zaroori hai ki parent note mein har letter aur picture ka matlab kya hai. Yeh page unhe order mein build karta hai, har cheez pichli cheez pe depend karti hai. Kuch bhi assume nahi kiya gaya.
Picture: ek row of boxes imagine karo, ek per word: "The cat sat". Har box ko numbers ka ek arrow se replace kiya jaata hai jo "meaning space" mein point karta hai. Position t=1 hai "The", t=2 hai "cat", aur aise hi aage.
Topic ko yeh kyun chahiye: poori cost story yeh hai ki "kya hota hai per token". Agar tum yeh nahi jaante ki har token ek vector xt ban jaata hai, toh tum count nahi kar sakte ki uske liye kya compute hota hai.
Figure 1 — teen word-boxes "The", "cat", "sat" positions t=1,2,3 par, har ek ke saath ek neecha arrow numbers ki ek bracketed list mein: length d ka embedding vector xt.
Ek embedding arrow mein components ki sankhya model dimension d hai. Bade models d=768 (GPT-2 small) se lekar hazaron tak use karte hain. Woh akela number d cost formulas mein har jagah aata hai, isliye use abhi pin kar lo.
Notation
xt position t par token ka embedding vector hai; d batata hai ki usme kitne numbers hain.
Attention (6.1.4-multi-head-attention mein built) har token ki embedding xt ko teen naye vectors mein badalta hai, use teen fixed number-grids (matrices) se multiply karke jinhe WQ, WK, WV kehte hain.
Picture — ek library: har past book ki ek spine label (uski Key) aur contents (uski Value) hoti hai. Naya token ek search slip (uski Query) lekar andar aata hai. Woh apni slip ko har spine label se compare karta hai, phir un books se contents uthata hai jo best match karti hain.
Figure 2 — do past-token "books" har ek mein ek green Key label aur ek blue Value content dikha rahi hain, plus ek orange new-token "search slip" Qt jisme dashed red arrows har Key se compare kar rahe hain.
Topic ko yeh kyun chahiye — aur yeh caching ka dil hai: ek past token ka label Ki aur contents Vikabhi nahi badalte ek baar likhne ke baad. Lekin har naya token ek fresh search slip Qt laata hai. Yahi asymmetry exactly woh reason hai kyun hum K aur V cache karte hain lekin Q recompute karte hain.
Recall Hum Q cache kyun nahi kar sakte?
Kyunki Q naye token ka sawaal hai, jo har step par alag hota hai. K aur V purane tokens ke fixed jawab hain. ::: Bilkul sahi.
WK kya hai?
Shape dk×d ki ek fixed matrix (training mein seekhi gayi, generation ke dauran frozen) jo ek embedding xt ko uske Key vector Kt mein map karti hai.
Yeh measure karne ke liye ki search slip spine label se kitna match karti hai, attention dot product use karta hai: matching components ko multiply karo aur jod lo. Bada dot product = strong match.
Shapes ko pin down karna (yahan beginners confuse ho jaate hain): generation step t par ek naya query hai, ek row vector
Qt∈R1×dk
aur t cached keys hain jo ek matrix mein stack hain, ek key per row:
K=K1K2⋮Kt∈Rt×dk.
Query ko har key ke saath ek saath dot karne ke liye, hume har key ek column ke roop mein chahiye. Transpose KT exactly wahi karta hai — woh rows ka t×dk stack ko columns ka dk×t block mein badal deta hai:
KT∈Rdk×t.
Ab shapes matrix multiply ke liye line up hoti hain:
1×dkQtdk×tKT=1×tscores,
har past token ke liye ek score. Inner dk dimensions match karke cancel ho jaate hain — transpose karne ki poori wajah yehi hai.
Picture: query arrow ki shadow jo har key arrow par padti hai — jitni lambi shadow, utna zyada score.
Figure 3 — origin se ek orange query vector Q aur teen key vectors K1,K2,K3; dotted projection lines dikhate hain K3 (green) Q ke saath sabse zyada align karta hai, sabse zyada dot-product score deta hai.
Parent note ke single-layer worked example mein (3rd token generate karna, 4 heads, dk=64), Q3(K1,K2,K3)T exactly 3 scores produce karta hai — ek per past position — jo upar derive ki gayi length t=3 ki row se match karta hai.
dk se divide kyun karte hain?
Scores ko bahut bada hone se rokne ke liye jab dk badhta hai, jo agla step (softmax) bahut spiky bana deta. Yeh ek stabiliser hai.
Picture — attention ka ek pie chart: bada raw score → bada slice, aur saare slices ek poori pie mein sum hote hain. Exponential es leader ko exaggerate karta hai taaki strongest match dominate kare lekin kuch fully ignore na ho.
Figure 4 — left: teen raw scores ka ek bar chart; right: wahi scores softmax ke baad ek pie ke roop mein dikhaye gaye jinke teen slices 1 mein sum hote hain, sabse bada score sabse bada slice le raha hai.
Topic ko yeh kyun chahiye — aur yahan V ki shape enter karti hai: yeh weights cached Values ko blend karke ek output banane ke liye multiply karte hain. t Value vectors ko waise hi stack karo jaise humne Keys stack ki thi:
V=V1V2⋮Vt∈Rt×dk.
Softmax output t weights ki ek row hai, shape 1×t. Shapes multiply karna:
1×tsoftmax scorest×dkV=1×dkoutput.
Inner t cancel ho jaata hai, toh ek 1×t weight row times t×dk Value stack ek ek length-dk output vector deta hai — saari Values ka weighted blend. Toh poora attention formula
Attention(Q,K,V)=softmax(dkQKT)V
left-to-right padhta hai: labels ko score karo → pie mein badlo → contents mix karo.
Ek attention kaafi nahi hai, toh model kaafi heads parallel mein run karta hai — har ek ek head hai.
Ek "layer" kya count hota hai? Ek transformer identical blocks ko ek ke upar stack karta hai; har block ek layer hai (6.1.1-transformer-architecture inhe build karta hai). Ek layer mein apna attention (apne WQ,WK,WV ke saath) plus ek feed-forward network hota hai. GPT-2 small mein 12 aisi layers hain; GPT-3-style model mein 96 hain. N = layers ki sankhya. Har layer apna alag KV-cache rakhti hai, isliye full-model memory N se multiply hoti hai.
Topic ko yeh kyun chahiye: cache memory per head, per layer, per token count ki jaati hai. Ab ki h, dk aur p sab define ho gaye hain, memory formula padhta hai:
Memory (per layer, per token)=2×h×dk×p
2 isliye kyunki hum do cheezein store karte hain: Key stack aur Value stack.
Standard cache per tokenMQA cache per token=2×h×dk2×dk=h1.
h=12 heads ke saath, MQA 12× kam KV-cache store karta hai, kyunki K aur V dono ek baar (length dk par) store hote hain h times ki jagah. Quality typically sirf 1–2% drop hoti hai.
Picture — ek lower-triangular grid: row i (attending token) columns j≤i (khud aur past) fill kar sakta hai lekin upper-right triangle (future) blacked out hai.
Figure 5 — ek L×L grid attending position i (rows) aur key position j (columns) se indexed; green cells j≤i mein 0 (allowed), red upper-right cells j>i mein −∞ (future blocked).
Topic ko yeh kyun chahiye: kyunki mask guarantee karta hai ki ek token sirf positions ≤t attend karta hai, aur woh past K,V frozen hain — yahi exactly unhe cache karne ka license hai. Cache ke hote hue bhi tumhe mask add karna hi hoga, warna model illegally future positions read kar lega.
Topic ko yeh kyun chahiye: poora motivation ek cost comparison hai. Per token projection cost per layer O(d2) hai (dk×d matrix times length-d vector, h heads mein sum karne par hdkd=d⋅d=d2 milta hai). Big-O constant factors N (layers) aur per-head split chhupaata hai, toh neeche clean formulas per-layer projection costs hain; full model ke liye N layers se multiply karo:
Without cache (per layer): With cache (per layer): Speedup: t=1∑LO(td2)=O(L2d2)t=1∑LO(d2)=O(Ld2)Ld2L2d2=L.
100-token sequence ke liye, yeh roughly 100× kam projection work hai. Parent note ka GPT-2-small worked example (jisme dono sides par N=12 layer factor include hai, toh woh cancel ho jaata hai) L=100 ke liye ≈50× real speedup measure karta hai — ideal L se thoda kam kyunki attention khud abhi bhi har step par thoda cost karta hai, lekin same order of magnitude hai.
O(L2d2) vs O(Ld2) ka matlab?
Per-layer projection cost: pehla length mein quadratic hai (naive), doosra linear (cached). Unka ratio L hai, speedup. Pure model ke liye kisi bhi ko N layers se multiply karo.