7 Inference aur deployment 3 / 8
  1. अध्याय विषय-सूची
  2. Pehla inference service
  3. Decoding aur sampling
  4. KV cache aur inference performance optimization
  5. Prefix caching aur PD separation
  6. Model quantization
  7. LLM inference service
  8. Continuous batching aur PagedAttention
  9. Speculative decoding
सभी अध्याय →

अध्याय 7 · Inference aur deployment 7.3

KV cache aur inference performance optimization

Samjhein KV cache autoregressive decoding ke repeat computation kaise ghataata hai, aur latency, throughput, memory aur service scheduling ke beech ke trade-off systematically pakdein।

मध्यम पढ़ने का समय: 17 मिनट अपडेट किया गया सहायक कोड ↓

यह पृष्ठ पूरा करने पर आप

  • Samjhayein autoregressive generation apne aap dheemi kyun hoti jaati hai।
  • Spasht karein KV cache waqai kya cache karta hai, aur kaun si repeat computation bachaata hai।
  • Samjhayein cache latency kyun ghata hai par memory consumption kyun kaafi badha deta hai।
  • Batayein PagedAttention, continuous batching, quantization jaise system optimizations har ek kaun sa bottleneck solve karte hain।
इस पृष्ठ पर
  1. Problem ka background: ek token generate karna mehnga kyun hota jaata hai
  2. KV cache waqai kya cache karta hai
  3. Yeh itna time kyun bachaata hai
  4. Prefill aur Decode: inference ke do alag phases
  5. Prefill
  6. Decode
  7. Memory formula: cache itni memory kyun kha jaata hai
  8. Khud hisaab lagayein
  9. MHA, MQA, GQA ka cache par asar
  10. MHA
  11. MQA
  12. GQA
  13. PagedAttention: cache fit hona kaafi nahi
  14. Continuous batching: GPU ko khali na baithe dein
  15. Quantization aur KV cache ka gehra rishta
  16. Framework chunte waqt kya dekhein
  17. Production mein kaun se metrics zaroori
  18. Kab KV cache khud bojh ban jaata hai
  19. Recommended reading order
  20. Summary aur exercises
  21. Quick self-test
  22. Aage ki soch
  23. अक्सर पूछे जाने वाले सवाल

Agar aapne Transformer attention basics padh liya hai, to yeh article “attention formula” ko “live inference system” se jodne mein madad karega।

Problem ka background: ek token generate karna mehnga kyun hota jaata hai

LLM inference aam taur par autoregressive hai - har baar ek naya token generate hota hai, context ke aage judta hai, phir agla predict hota hai।

Agar koi cache na ho, to step t par model ko lambai t ki poora prefix dobara process karna padta hai. Iski dikkat:

  • History tokens ki K/V baar-baar recompute hoti hain।
  • Sequence jitni lambi, ek step ki lagat utni zyada।
  • Total generation cost output length ke saath tezi se badhta hai।

System ki nazar se dekhein to inference sirf “ek forward pass” nahi, “ek hi prefix par kai highly repetitive forward passes” hai. KV cache isi waste ko mitaane ke liye bana hai।

KV cache waqai kya cache karta hai

Self-attention mein har layer hidden state ko Q, K, V mein project karti hai. Already-generate hue history tokens ke liye:

  • Un tokens ki K/V aage ke steps mein nahi badalti।
  • Waqai badalne wali cheez sirf “naye token ka query” aur uski nayi K/V hai।

Isliye hum history tokens ki har layer ki K/V save kar sakte hain. Agla step generate karte waqt:

  1. Sirf naye token ki is layer ki Q/K/V compute karo।
  2. Nayi K/V ko cache mein append karo।
  3. Naye token ke Q ko poore cache ki K se attention do।
  4. Weights ke hisaab se cache ki V padho।

Yehi KV cache ka core hai: history recompute nahi, sirf incremental update।

Yeh itna time kyun bachaata hai

KV cache na ho to generation ke har step par poore prefix ki projection aur layer propagation dobara hoti hai; KV cache ho to history tokens ki zyada tar intermediate results reuse ho jaate hain।

Roughly samjhein:

  • Bina cache: step 1 par 1 token, step 2 par 2 recompute, step 3 par 3 recompute…
  • Cache ke saath: step 1 par 1 token, step 2 par sirf 1 naya, step 3 par bhi sirf 1 naya…

Bilkul, attention scores ke liye current query ko saare history keys dekhna padta hai, isliye decode phase ki per-step cost sequence lambi hone par badhti rehti hai. Par “poora prefix full recompute” se comparison mein cache ki bachat bahut badi hai।

Prefill aur Decode: inference ke do alag phases

KV cache samajhte waqt do phases alag karna zaroori hai:

Prefill

Model pehli baar poora prompt padhta hai aur saare history tokens ki K/V banata hai. Is phase mein parallelism aur throughput high hote hain, par compute bahut hota hai।

Decode

Model token-by-token generate karta hai; har step sirf ek naya token, aur cache ka baar-baar istemal. Is phase mein compute granularity chhoti, memory access baar-baar - bottleneck prefill se kaafi alag hai।

Asli services mein bahut optimizations in do phases ke liye alag-alag design hoti hain, jaise:

  • Prefill: large-batch throughput aur lambe prompts ki processing efficiency zyada maayne rakhti hai।
  • Decode: per-token latency, scheduling aur cache management zyada maayne rakhte hain।

Dono phases aur cache ke badhte hue size ko ek diagram mein dekhein to yeh samajhna aasaan hai ki KV cache aane ke baad inference ka bottleneck khatam nahi hota, jagah badal jaata hai:

Prefill, Decode aur KV cache ka badhta size Baen or prefill phase poora prompt ek saath padhkar cache banata hai; beech decode phase har baar sirf ek naya token aur ek chhota key/value jodta hai; dahine sequence badhne ke saath cache ka size dheere-dheere chadhta hai aur memory aur scheduling par pressure dalta hai। KV cache recompute ko incremental banata hai, par memory ki keemat bhi lagti hai Phase 1: Prefill Poora prompt ek saath andar History K/V banao Khaasiyat: high parallelism, bahut compute Phase 2: Decode Naya token Naya Q/K/V History cache mein jodo Khaasiyat: har step sirf increment, parhar step poora history cache padhta hai Cache lagatar badhta hai seq_len lamba, KV cache bada Seedha asar: memory use,fragmentation, batching schedule
Prefill ka kaam history ek saath padhna aur cache banana; decode ka kaam har baar sirf thoda sa naya compute. KV cache repeated projection solve karta hai, par memory management, memory-access efficiency aur scheduling naye system core ban jaate hain।

Memory formula: cache itni memory kyun kha jaata hai

KV cache ki speed wali bachat free nahi hai; iski keemat kaafi memory hoti hai. Ek aam rough formula:

KV cache bytes≈2×L×B×S×Hkv×dhead×b\text{KV cache bytes} \approx 2 \times L \times B \times S \times H_{kv} \times d_{\text{head}} \times b

Jismein LL layers ki ginti, BB concurrent sequences (batch), SS sequence length, HkvH_{kv} KV heads, dheadd_{\text{head}} har head ka dim, bb har element ke bytes; shuru ka 22 matlab key aur value dono store hote hain।

Formula se seedha dikhta hai ki cache cost in cheezon ke saath linearly badhta hai:

  • Layers zyada, cache bada।
  • Batch bada, cache bada।
  • Context lamba, cache bada।
  • Precision high (FP16 vs INT8), space zyada।

Ek concrete misaal se scale mehsoos karein: Llama 3 8B mein 32 layers, 8 KV heads, head dim 128; FP16 storage mein har token chahiye

2×32×8×128×2 bytes=131,072 bytes=128 KiB2 \times 32 \times 8 \times 128 \times 2\ \text{bytes} = 131{,}072\ \text{bytes} = 128\ \text{KiB}

Ek 8192-token sequence matlab 128 KiB×8192=1 GiB128\ \text{KiB} \times 8192 = 1\ \text{GiB}; 32 aisi sequences serve karne par sirf cache 32 GiB - model weights (lagbhag 16 GB) se bhi zyada. Tulna mein, same scale par traditional multi-head attention (32 KV heads) wala Llama 2 7B har token 512 KiB leta hai; wahi 8192-token sequence 4 GiB - chaar guna।

Khud hisaab lagayein

Alag model, cache precision, sequence length aur concurrency chun kar cache ka size dekhein, aur model weights se compare karein:

Isliye inference system ki bahut saari engineering problems ka core sawaal yahi hai: quality par saaf asar dahe bina, is cache ko zyada sasta, stable aur flexible kaise manage karein।

MHA, MQA, GQA ka cache par asar

KV cache ka size attention head design se gehri tarah juda hai।

MHA

Traditional Multi-Head Attention mein har head ki apni K/V hoti hai - expressive power zyada, par cache overhead bhi bada।

MQA

Multi-Query Attention kai query heads ko ek hi K/V set share karne deta hai; cache size aur memory access kaafi ghat jaata hai - inference ke liye dost।

GQA

Grouped-Query Attention dono ke beech: kuch query heads ek K/V set share karte hain; quality aur efficiency ka santulan।

Aaj deployment-optimized models GQA ya MQA ko original Transformer ke MHA se pehle chunte hain. K/V heads ghatane ki quality keemat setting par nirbhar hai; DeepSeek-V2 ki MLA doosra raasta leti hai: sirf compressed latent vectors cache hoti hain, details efficient attention mein, jahan MHA, GQA, MQA aur MLA compare karne wala widget bhi hai।

PagedAttention: cache fit hona kaafi nahi

Sirf cache karna kaafi nahi; asal baat cache ka management hai. Asli service mein request ki lambai kaafi alag-alag hoti hai; har request ke liye ek bada continuous memory block reserve karoge to:

  • bahut fragmentation
  • allocate par underused
  • lambe-chhote requests mixed hone par utilization kam

PagedAttention ka core idea: KV cache ko chhote page blocks mein tod kar manage karna, har request ko ek badi continuous memory mangne ki bajaye. Fayde:

  • Memory allocation zyada flexible
  • Fragmentation kam
  • Request khatam hone par recycle aasaan
  • Multi-request concurrency ke liye behtar

Yeh optimizations model ke output logic nahi badalti, par asli throughput kaafi badha deti hain - inference systems engineering ki classic “low-level change, badi bachat”. Block size, block table aur max-length reservation vs paging ka memory hisaab continuous batching aur PagedAttention mein।

Continuous batching: GPU ko khali na baithe dein

Static batch ki ek aam dikkat: ya to poora batch bharne ka intezaar, ya batch ka sabse lamba request baaki ko drag karta hai।

Continuous batching ka idea:

  • Naye request aaye to current batch ke poora khatam hone ka intezaar nahi।
  • Complete hue request kabhi bhi nikal sakte hain।
  • Khaali compute slot turant naye request se bhar jaata hai।

Isse GPU utilization kaafi badhta hai - online services ke liye khaas achha. Keemat: scheduling complex hoti hai; cache management, sequence state aur fairness policy par zyada demand. Real inference server par static vs continuous batching compare karne aur parameter drag karne wale scheduler simulator ke liye continuous batching aur PagedAttention dekhein।

Quantization aur KV cache ka gehra rishta

Bahut log quantization ko “sabse zyada weights ki memory bachati hai” samajhte hain; par long-context inference ke liye KV cache utna hi important hai।

Kyunki sequence lambi aur concurrency high hone par bottleneck weights nahi, khud cache ho sakta hai. Tab cache ko lower-bit representation dena ya zyada space-efficient head design lena seedha asar deta hai:

  • Kitne concurrent requests fit ho sakte hain
  • Kitna lamba context support ho sakta hai
  • Paging/recycle kitni baar chahiye

Quantization ki apni precision aur stability wali keemat hai, isliye task type, model architecture aur service goals ke saath milkar evaluate karna chahiye।

Framework chunte waqt kya dekhein

Aakhir mein vLLM, SGLang, llama.cpp, Ollama ya apni service - decision criteria sirf “chal jaata hai” nahi hona chahiye; in dimensions par dekhein:

  • Efficient KV cache management supported hai?
  • Continuous batching capability hai?
  • Lambe context par throughput aur latency stable hain?
  • Multi-model, multi-tenant mein resource isolation clear hai?
  • Monitoring, logs aur replay kaafi hain?

Production inference optimization ek system problem hai, single-point algorithm problem nahi।

Production mein kaun se metrics zaroori

Service stability ka zimma aapke paas hai to yeh metrics lambe samay tak dekhein:

  • First token latency (TTFT)
  • Average generation speed (tokens/s)
  • p95 / p99 latency
  • Memory use aur fragmentation rate
  • Request queue time
  • Lambe-chhote prompts mixed hone par throughput ka badlav

Bahut performance problems ka asal kaaran “model dheema hai” nahi hota - cache allocation, batch scheduling aur request shape ka mila-jula natija hota hai।

In metrics ko aam symptoms ke saath jodkar dekhein, to real service ki troubleshooting order jaisa lagta hai:

Production symptoms se KV cache bottleneck tak troubleshooting map High first-token latency, kam generation speed, lambe context par memory explosion aur p95 latency jitter jaise symptoms ko prefill compute, cache size, PagedAttention aur continuous batching wale alag-alag layer ke bottlenecks se joda gaya hai। KV cache problems: symptoms se batao prefill, cache ya scheduling kaun si layer ka sawaal Production symptoms TTFT kaafi high tokens/s bahut low Lambe context par OOM p95/p99 bahut jitter Pehle yeh layers check karein Prefill compute aur lambe prompt ki processing Pehle prompt length distribution, TTFT, prefill throughput aur operator efficiency dekhein Decode memory access aur continuous batching GPU khali baithe hai, batch lambi requests se drag ho rahi hai, cache read bottleneck hai? Cache size, GQA/MQA aur cache quantization Yahan seq_len, batch, layers, head design dekhein, sirf weight size nahi PagedAttention, fragmentation aur request-mix policy Tail latency ka jitter aam taur par paging, recycle aur batching policy ke na milne se Aksar "model dheema hai" jaisi problems ka asal kaaran: prefill, cache size aur scheduling ko alag-alag observe nahi kiya gaya।
KV cache troubleshooting mein sab kuch ek saath pakadna sabse buri aadat hai. TTFT, tokens/s, OOM aur tail latency ko alag-alag prefill, decode, cache size aur paging scheduling par map karein, to bottleneck jaldi milta hai।

Kab KV cache khud bojh ban jaata hai

KV cache aankhein band karke on karna kaafi nahi. In situations mein khaas dhyan:

  1. Context bahut lamba, concurrency bahut high - cache memory weights se pehle bottleneck ban jaata hai।
  2. Requests bahut chhoti - cache ki bachat kam, par scheduling complexity aur memory management ka kharcha aa jaata hai।
  3. Multi-turn sessions bahut saare - cache lifecycle manage nahi kiya to memory purani sessions se ghiri rehti hai।
  4. Edge deployment mein memory bahut tight - tab quantization, context trim ya layer-wise offloading pehle sochein।

Ek aur raasta: cache ko length ke saath badna hi na do - linear attention, state space models aur hybrid architectures zyada tar layers ki KV ko fixed-size state se badal dete hain; keemat yeh hai ki lossy hota hai. State space models aur hybrid architectures page Jamba, Nemotron-H, Qwen3-Next ka cache hisaab lagata hai।

System optimization mein asli baat “kaun si technique advanced hai” nahi, “kya woh aapka abhi ke sabse mehnge bottleneck solve karti hai” hai।

Is hisse ka gyaan jodna hai to is kram mein aage badhein:

  1. Transformer attention basics
  2. Attention Is All You Need
  3. Lost in the Middle

Isse aap teen cheezein ek saath samjhein ge: attention kaise chalta hai, lamba context mehnga kyun hai, aur itni mehangi keemat dene ke baad bhi model us context ko achhe se use karega hi nahi।

Cache ka idea sirf language models tak seemit nahi: robot policy π0 block-wise causal mask use karta hai, jisse image aur language prefix ki keys/values 10-step flow matching mein sirf ek baar compute hoti hain - prefix caching ka ek instance (paper: π0)।

Cache ko extreme tak dabane ka ek aaj ka misaal DeepSeek-V4.1-Flash hai: har token ki global KV cache sirf 890 bytes (DeepSeek-V1 mein 389,120 bytes); tareeka layer-wise sharing, sequence compression aur FP4 ka guna. Yeh numbers public config se dobara nikale gaye the।

Summary aur exercises

Is page ke mukhya points:

  1. KV cache har layer, har processed token ki key aur value cache karta hai; history recompute nahi, sirf incremental update।
  2. Yeh memory se time khareedna hai: cache bytes lagbhag 2×L×B×S×Hkv×dhead×b2 \times L \times B \times S \times H_{kv} \times d_{\text{head}} \times b; layers, concurrency, length ke saath linear; GQA/MQA aur quantization use chhota karte hain।
  3. Prefill poora prompt parallel process karta hai, compute zyada; decode token-by-token generate karke cache baar-baar padhta hai - dono phases ke bottlenecks alag।
  4. PagedAttention cache “kaise manage karein” solve karta hai, continuous batching GPU “khali na baithe” - dono model ke output logic nahi badalti।

Quick self-test

1. KV cache kya cache karta hai?

  • Har layer ke query vectors
  • Har layer, har processed token ke key aur value vectors
  • Attention weight matrix
  • Model ke weight parameters

History tokens ki K/V aage nahi badalti, isliye save karke reuse ho sakti hai; badalne wali sirf naye token ka query aur nayi K/V. Attention weights har step naye query par nirbhar hote hain - cache ki unki koi value nahi।

2. Llama 3 8B (32 layers, 8 KV heads, head dim 128) FP16 cache mein 32 aisi 8192-token sequences serve karne par sirf KV cache kitni memory lelega?

  • Lagbhag 1 GiB
  • Lagbhag 4 GiB
  • Lagbhag 32 GiB
  • Lagbhag 128 GiB

Har token ke liye 2x32x8x128x2 bytes, yaani 128 KiB; ek 8192-token sequence 1 GiB; 32 sequences 32 GiB - model weights (lagbhag 16 GB) se bhi zyada।

3. Upar wale sawaal mein KV heads 8 se 32 kar dein (MHA par wapas), baaki same - ek 8192-token sequence ka cache kitna?

  • 1 GiB
  • 2 GiB
  • 4 GiB
  • 8 GiB

Cache KV heads ke proportional hai: 32/8 = 4, isliye 1 GiB se 4 GiB; har token 512 KiB. Yehi wajah hai ki GQA deployment ka default ban gaya।

4. Prefill aur decode ke baare mein kaun sa kathan sahi hai?

  • Prefill token-by-token generate karta hai, decode poora prompt ek saath padhta hai
  • Dono phases ka bottleneck same hai, isliye optimization bhi same
  • Prefill poora prompt ek saath parallel process karta hai, compute zyada; decode har step ek naya token, granularity chhoti, par har step poora cache padhta hai
  • KV cache aane ke baad decode ki per-step cost length par nirbhar nahi rehti

Cache history tokens ki repeated projection bachata hai, par naye token ka query saare history keys se scores nikaalta hai aur saari history values weights ke hisaab se padhta hai - isliye decode ki per-step cost length ke saath badhti rehti hai, aur bottleneck cache padhne ki memory bandwidth hai (kaaran GPU aur roofline model mein)।

5. In mein se kaun se kathan sahi hain? (multiple)

  • PagedAttention KV cache ko chhote pages mein manage karta hai - fragmentation kam, memory utilization zyada
  • Continuous batching mein complete hue request kabhi bhi nikal sakte hain, khaali slot turant naye request se bharta hai
  • PagedAttention approximate attention use karta hai, model output halke se badal jaata hai
  • Continuous batching mein agla batch tabhi shuru hota hai jab poora batch khatam ho

Dono optimizations system-level hain: model ke output logic nahi badalti, sirf memory aur compute resources ke use ka tareeka badalti hain. Aakhri do actually static batch ki problems describe karti hain।

6. Kisi service ka context lamba, concurrency high, aur memory mostly KV cache se bhari hai. Kaun se tareeqe cache seedha chhota karte hain? (multiple)

  • GQA ya MQA jaise kam KV heads wale model par shift
  • KV cache ko INT8 jaise low-bit quantize karna
  • Sirf model weights ko 4-bit quantize karna
  • Batch badhana, taaki har request ka cache hissa kam ho

Cache layers, concurrency, length, KV heads aur per-element bytes se banta hai. Weights quantize karne se cache nahi ghatta; batch badhane se cache aur bada hota hai।

Exercises:

  1. KV cache “history tokens ki repeated projection” kaise bachata hai, par decode phase ko poori tarah constant-cost kyun nahi bana deta?
  2. Lamba-context online service mein GQA traditional MHA se deployment ke liye zyada kyun fit hai?
  3. Agar kisi service ka TTFT normal hai par p99 latency bura hai, to aap pehle kaun si cache ya scheduling problems check karenge?
  4. Jab memory bottleneck weights nahi balki KV cache hai, to kaun se optimizations ki priority badh jaati hai?
  5. Qwen2.5 7B mein 28 layers, 4 KV heads, head dim 128, lagbhag 7.6B parameters. FP16 cache: har token ka cache kitne bytes? 24 GiB GPU par FP16 weights fit karne ke baad bacha memory poora KV cache ko diya jaaye (activations aur framework overhead ignore), to max kitni 8192-token sequences ek saath? Cache INT8 mein ho to?
Reference answers
  1. Cache ke saath history tokens ki K/V dobara project nahi hoti; har step sirf naye token ki Q/K/V projection aur forward hota hai - jo bacha woh poore prefix ki repeat computation hai. Par attention khud nahi bachta: naye token ka query saare history keys se scores nikaalta hai aur saari history values weights ke hisaab se padhta hai; yeh compute aur memory access dono sequence length ke proportional hain, isliye decode ki per-step cost length ke saath badhti hai. Aur har step poora cache memory se padha jaata hai - bottleneck mostly memory access bandwidth hai।

  2. GQA mein kuch query heads ek K/V set share karte hain; KV heads HH se GG par aate hain, cache aur decode ki per-step memory access dono G/HG/H par gir jaate hain (Llama 3 8B: 32 query heads, 8 KV heads - MHA ka sirf 1/4). Lamba context, high concurrency service mein cache weights se pehle memory bottleneck banta hai; chhota cache matlab zyada concurrency aur lamba context, aur quality keemat aam taur par bahut chhoti।

  3. TTFT normal hai to requests time par shuru ho rahi hain, prefill usually theek; p99 kharab hone ka zyada chance decode phase aur scheduling mein. Pehle teen tarah ke signals: memory use aur fragmentation rate (cache paging aur recycle time par?), lambe-chhote prompts mixed hone par throughput aur tail latency ka badlav, aur continuous batching ki scheduling - jaise batch kuch lambi requests se drag to nahi ho rahi. Tail latency ka jitter aam taur par cache paging, recycle aur batching policy ke na-milne se hota hai।

  4. Priority un tareeqon ki badhti hai jo cache khud chhota ya manage karne mein aasaan banate hain: GQA, MQA (ya MLA) jaise cache-saving head design; KV cache ko INT8 jaise low-bit quantize karna; PagedAttention se fragmentation aur waste ghataana; zaroorat par context length trim karna ya cache ko layer-wise offload karna. Sirf weights quantize karna kafi madadgar nahi, kyunki weights bottleneck nahi hain।

  5. Har token ka cache 2×28×4×128×2=57,3442 \times 28 \times 4 \times 128 \times 2 = 57{,}344 bytes, yaani 56 KiB; ek 8192-token sequence 448 MiB. 24 GiB GPU par lagbhag 14.2 GiB weights (7.6×1097.6 \times 10^9 parameters, har ek 2 bytes) ke baad lagbhag 9.8 GiB bachta hai - 22 sequences fit; cache INT8 mein har sequence sirf 224 MiB, 44 sequences fit. Asli service mein activations aur framework ke liye bhi jagah chahiye, fit hone wali ginti aur kam hogi. Verification code:

    def kv_bytes_per_token(layers, kv_heads, head_dim, bytes_per_elem):
        return 2 * layers * kv_heads * head_dim * bytes_per_elem
    
    
    per_token = kv_bytes_per_token(28, 4, 128, 2)
    per_seq_mib = per_token * 8192 / 2**20
    free_mib = (24 - 14.2) * 1024
    print(per_token, per_seq_mib)  # 57344 448.0
    fp16 = int(free_mib // per_seq_mib)
    int8 = int(free_mib // (per_seq_mib / 2))
    print(fp16, int8)  # 22 44

Aage ki soch

Inference optimization mein sabse aasaan bhram “jargon ka dher” ka hai; par asli useful criteria bilkul simple hai:

Aapke system ka bottleneck waqai kahan hai - compute, memory access, memory, ya scheduling?

KV cache sirf shuruaat hai. Isse samajh lene ke baad hi aap LLM systems engineering ke core mein ghuse hain।

  • Weights wahi rakh kar KV size aur ghatane ke do raaste: Liu adi ka KIVI (2024; key full-precision, value low-bit asymmetric quantization) aur Zhang adi ka ZipCache (2024; importance ke hisaab se token-level compression); CLA (2024) aur YOCO (2024) paas wali layers ko K/V share karwa dete hain - cache ka layer dimension hi kaat dete hain।
  • Requests ke beech reuse aur cluster-level cache organization ke liye prefix caching aur PD separation dekhein: RadixAttention, CacheBlend, DistServe, Mooncake।

अक्सर पूछे जाने वाले सवाल

KV cache kya cache karta hai?
Self-attention ki har layer mein, har processed token ke key aur value vectors. Naya token generate karte waqt sirf naye token ka query, key, value nikaalna padta hai, aur cache ki hui historical K/V se attention kiya jaata hai - pura history dobara linear project karne ki zaroorat khatam।
KV cache kitni memory leta hai, kaise andaza lagayein?
Har token ke cache bytes lagbhag 2 × layers × KV heads × head dim × bytes per element, phir sequence length aur concurrency se multiply. Misal ke liye Llama 3 8B (32 layers, 8 KV heads, head dim 128) par FP16 mein har token lagbhag 128 KiB; ek 8192-token sequence lagbhag 1 GiB।
GQA aur MQA memory kyun bachaate hain?
Standard multi-head attention mein har query head ke apne K/V heads hote hain; MQA mein sab query heads ek hi K/V set share karte hain, GQA mein kuch query heads ek set share karte hain. KV heads kam hote hain to cache aur memory traffic anupaat se ghatta hai, aur model quality par asar chhota rehta hai।