𝗞𝗩 𝗖𝗮𝗰𝗵𝗲 𝗮𝗻𝗱 𝗣𝗮𝗴𝗲𝗱𝗔𝘁𝘁𝗲𝗻𝘁𝗶𝗼𝗻: 𝗪𝗵𝘆 𝗬𝗼𝘂𝗿 𝗟𝗟𝗠 𝗦𝗲𝗿𝘃𝗲𝗿 𝗦𝗹𝗼𝘄𝘀 𝗗𝗼𝘄𝗻
Your LLM server is running slow.
You deployed a 70B model on four A100 GPUs. Everything looks fine at 8 AM. By lunch, latency doubles. You check your memory. Most of it is taken up by "tensor buffers." These are actually cached states from old conversations.
This is the KV cache problem. It is the biggest bottleneck in production LLM serving.
What is the KV cache?
Every transformer model generates tokens one by one. To create a new token, the model needs the Key and Value tensors from all previous tokens. Recomputing these every time is too slow. Instead, the engine stores them. This storage is the KV cache.
The memory problem:
For a Llama 3.1 70B model, a single 4096-token sequence needs about 1.3 GB of memory.
If you have 256 users at once, you need 336 GB of memory. This is more than four A100 GPUs can hold. The KV cache grows so fast that it often uses more memory than the model weights themselves.
Traditional memory management fails because:
- Internal fragmentation: You allocate space for 4096 tokens but only use 300. You waste 93% of that space.
- No sharing: Two users with the same system prompt each store their own copy of that prompt.
- All-or-nothing eviction: When memory runs out, you must move the entire sequence to the CPU. This stalls the GPU.
How PagedAttention fixes this:
PagedAttention works like an operating system. It divides the KV cache into small, fixed-size blocks called pages.
This solves three main issues:
- On-demand allocation: A sequence only takes up pages as it grows. You do not waste memory on unused capacity.
- Shared prefix support: Multiple users can share the same physical pages for a common system prompt. This uses "copy-on-write" logic to save massive amounts of memory.
- Fine-grained eviction: When memory is full, the system moves small pages to the CPU instead of huge sequences.
The Result:
Using PagedAttention (the tech inside vLLM) can increase throughput by 2x to 4x compared to traditional methods.
When to use it:
- High concurrency.
- Sequences of different lengths.
- Prompts that share the same start.
When to skip it:
- Single-user local inference.
- Very small models.
- Tasks where every sequence is exactly the same length.
KV Cache మరియు PagedAttention: అవి ఏమి చేస్తాయి మరియు అవి ఎందుకు ముఖ్యమైనవి?
Source: https://dev.to/tech_nuggets/kv-cache-and-pagedattention-what-they-do-and-why-they-matter-jce
Large Language Models (LLMs) like GPT-4 are transforming the world. However, running these models efficiently is a massive challenge. Two key technologies that have revolutionized how we handle LLM inference are KV Cache and PagedAttention.
పెద్ద భాషా నమూనాలు (LLMs) అయిన GPT-4 వంటివి ప్రపంచాన్ని మారుస్తున్నాయి. అయితే, ఈ మోడళ్లను సమర్థవంతంగా నడపడం అనేది ఒక పెద్ద సవాలు. LLM ఇన్ఫరెన్స్ (inference) ప్రక్రియను నిర్వహించే విధానంలో విప్లవాత్మక మార్పులు తెచ్చిన రెండు కీలక సాంకేతికతలు KV Cache మరియు PagedAttention.
KV Cache అంటే ఏమిటి?
When an LLM generates text, it does so token by token. For each new token, the model needs to look at all the previous tokens to understand the context. This involves complex mathematical operations called "Attention."
ఒక LLM టెక్స్ట్ను జనరేట్ చేస్తున్నప్పుడు, అది టోకెన్ల వారీగా (token by token) చేస్తుంది. ప్రతి కొత్త టోకెన్ కోసం, సందర్భాన్ని (context) అర్థం చేసుకోవడానికి మోడల్ మునుపటి అన్ని టోకెన్లను చూడవలసి ఉంటుంది. దీనికి "Attention" అని పిలిచే సంక్లిష్టమైన గణిత ప్రక్రియలు అవసరం.
In the attention mechanism, two main matrices are computed for each token: Keys (K) and Values (V). Without a cache, the model would have to recompute these K and V matrices for every single token it generates, which is incredibly wasteful and slow.
అటెన్షన్ మెకానిజంలో, ప్రతి టోకెన్ కోసం రెండు ప్రధాన మ్యాట్రిస్లు (matrices) లెక్కించబడతాయి: Keys (K) మరియు Values (V). ఒక క్యాష్ (cache) లేకపోతే, మోడల్ జనరేట్ చేసే ప్రతి టోకెన్ కోసం ఈ K మరియు V మ్యాట్రిస్లను మళ్ళీ మళ్ళీ లెక్కించాల్సి ఉంటుంది, ఇది చాలా సమయం వృధా చేస్తుంది మరియు ప్రక్రియను నెమ్మదిస్తుంది.
KV Cache solves this by storing the K and V matrices of all previous tokens in memory. This way, the model only needs to compute the K and V for the new token and then retrieve the rest from the cache.
KV Cache ఈ సమస్యను పరిష్కరిస్తుంది. ఇది మునుపటి అన్ని టోకెన్ల K మరియు V మ్యాట్రిస్లను మెమరీలో నిల్వ చేస్తుంది. దీనివల్ల, మోడల్ కేవలం కొత్త టోకెన్ కోసం మాత్రమే K మరియు Vని లెక్కించాల్సి ఉంటుంది మరియు మిగిలిన వాటిని క్యాష్ నుండి పొందవచ్చు.
KV Cache వల్ల కలిగే సమస్యలు
While KV Cache saves computation, it introduces a new problem: Memory Management.
KV Cache గణనను (computation) ఆదా చేసినప్పటికీ, ఇది ఒక కొత్త సమస్యను పరిచయం చేస్తుంది: మెమరీ మేనేజ్మెంట్ (Memory Management).
Memory Fragmentation: LLMs often deal with sequences of varying lengths. When we allocate memory for KV Cache, we often allocate a fixed, large block of memory to accommodate the maximum possible sequence length. This leads to "internal fragmentation," where a lot of memory is reserved but never actually used.
Wasted Space: Because memory is allocated statically, it's difficult to share memory between different requests, leading to inefficient use of GPU memory.
మెమరీ ఫ్రాగ్మెంటేషన్ (Memory Fragmentation): LLMలు తరచుగా వేర్వేరు పొడవుల ఉన్న సీక్వెన్స్లతో పనిచేస్తాయి. మనం KV Cache కోసం మెమరీని కేటాయించినప్పుడు, గరిష్టంగా ఉండగలిగే సీక్వెన్స్ పొడవును దృష్టిలో ఉంచుకుని ఒక పెద్ద మెమరీ బ్లాక్ను కేటాయిస్తాము. దీనివల్ల "ఇంటర్నల్ ఫ్రాగ్మెంటేషన్" ఏర్పడుతుంది, అంటే చాలా మెమరీ కేటాయించబడినప్పటికీ, అది వాస్తవానికి ఉపయోగించబడదు.
వృధా అయ్యే స్థలం: మెమరీని స్టాటిక్గా కేటాయించడం వల్ల, వేర్వేరు రిక్వెస్ట్ల మధ్య మెమరీని పంచుకోవడం కష్టమవుతుంది, దీనివల్ల GPU మెమరీ అస్తవ్యస్తంగా మరియు తక్కువ సామర్థ్యంతో ఉపయోగించబడుతుంది.
PagedAttention అంటే ఏమిటి?
This is where PagedAttention comes in. Inspired by the concept of Virtual Memory and Paging in operating systems, PagedAttention changes how KV Cache is stored in memory.
ఇక్కడే PagedAttention ఉపయోగపడుతుంది. ఆపరేటింగ్ సిస్టమ్స్లోని Virtual Memory మరియు Paging భావనల నుండి ప్రేరణ పొంది, PagedAttention అనేది మెమరీలో KV Cache నిల్వ చేయబడే విధానాన్ని మారుస్తుంది.
Instead of allocating a single, contiguous block of memory for the entire KV Cache, PagedAttention divides the KV Cache into small, fixed-size blocks. These blocks can be stored in non-contiguous (scattered) locations in the GPU memory.
KV Cache మొత్తానికి ఒకే పెద్ద, వరుస మెమరీ బ్లాక్ను కేటాయించకుండా, PagedAttention KV Cacheని చిన్న, స్థిరమైన పరిమాణం కలిగిన బ్లాక్లుగా (blocks) విభజిస్తుంది. ఈ బ్లాక్లను GPU మెమరీలోని వేర్వేరు (scattered) ప్రదేశాలలో నిల్వ చేయవచ్చు.
A "Page Table" is used to keep track of where these blocks are located, much like how an OS manages memory.
మెమరీని ఆపరేటింగ్ సిస్టమ్ ఎలా నిర్వహిస్తుందో, అలాగే ఈ బ్లాక్లు ఎక్కడ ఉన్నాయో ట్రాక్ చేయడానికి ఒక "పేజ్ టేబుల్" (Page Table) ఉపయోగించబడుతుంది.
PagedAttention వల్ల కలిగే ప్రయోజనాలు
Reduced Fragmentation: Since blocks can be placed anywhere, there is almost zero memory fragmentation.
Higher Throughput: Because memory is used more efficiently, we can fit more requests into the GPU at the same time, significantly increasing the number of tokens generated per second.
Dynamic Allocation: Memory is allocated only as needed, allowing for much longer sequences and more efficient handling of varying request lengths.
తగ్గిన ఫ్రాగ్మెంటేషన్: బ్లాక్లను ఎక్కడైనా ఉంచవచ్చు కాబట్టి, మెమరీ ఫ్రాగ్మెంటేషన్ దాదాపు సున్నాగా ఉంటుంది.
అధిక త్రూపుట్ (Higher Throughput): మెమరీని మరింత సమర్థవంతంగా ఉపయోగించడం వల్ల, ఒకే సమయంలో GPUలో ఎక్కువ రిక్వెస్ట్లను ఉంచవచ్చు, దీనివల్ల సెకనుకు జనరేట్ అయ్యే టోకెన్ల సంఖ్య గణనీయంగా పెరుగుతుంది.
డైనమిక్ కేటాయింపు (Dynamic Allocation): అవసరమైనప్పుడు మాత్రమే మెమరీని కేటాయిస్తారు, దీనివల్ల చాలా పొడవైన సీక్వెన్స్లను నిర్వహించడం మరియు వేర్వేరు రిక్వెస్ట్ పొడవులను సమర్థవంతంగా హ్యాండిల్ చేయడం సాధ్యమవుతుంది.
ముగింపు
KV Cache and PagedAttention are fundamental to making modern LLMs fast and scalable. While KV Cache saves us from redundant computations, PagedAttention ensures that we use our precious GPU memory as efficiently as possible.
ఆధునిక LLMలను వేగంగా మరియు స్కేలబుల్గా మార్చడంలో KV Cache మరియు PagedAttention కీలక పాత్ర పోషిస్తాయి. KV Cache మనల్ని అనవసరమైన గణనల నుండి కాపాడితే, PagedAttention మన విలువైన GPU మెమరీని సాధ్యమైనంత సమర్థవంతంగా ఉపయోగించుకునేలా చేస్తుంది.
Optional learning community: https://t.me/GyaanSetuAi