𝗞𝗩 𝗖𝗮𝗰𝗵𝗲 𝗮𝗻𝗱 𝗣𝗮𝗴𝗲𝗱𝗔𝘁𝘁𝗲𝗻𝘁𝗶𝗼𝗻: 𝗪𝗵𝘆 𝗬𝗼𝘂𝗿 𝗟𝗟𝗠 𝗦𝗲𝗿𝘃𝗲𝗿 𝗦𝗹𝗼𝘄𝘀 𝗗𝗼𝘄𝗻

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.

ਸਰੋਤ: https://dev.to/tech_nuggets/kv-cache-and-pagedattention-what-they-do-and-why-they-matter-jce

ਵਿਕਲਪਿਕ ਲਰਨਿੰਗ ਕਮਿਊਨਿਟੀ: https://t.me/GyaanSetuAi