LLMs are Massive. I Mean, Really Massive.

Because of their sheer size, most LLM optimization techniques focus on one thing: how the model handles memory.

Today, I want to talk about PagedAttention, a technology that has become the gold standard in the field. It’s also the beating heart of vLLM, one of the trendiest serving frameworks out there right now.

But before we dive into how it works, let’s talk about the problem it’s trying to solve.


1. The Real Culprit Isn’t the Model Size

When people hear that LLMs eat up memory, they usually blame the model’s parameters (weights).

They aren’t wrong. A 70B parameter model occupies roughly 140GB of VRAM just by sitting there. However, this is a constant. Once you decide to use a 70B model, that number doesn’t change. It’s a massive chunk of memory, but at least it’s predictable.

The real problem is the variable. The culprit that actually causes memory to explode during production is the KV Cache.

What exactly is a KV Cache?

I’ve covered the nitty-gritty of this in my previous post, “Understanding the KV Cache (feat. Self-Attention),” but for today, let’s keep it simple.

LLMs are essentially “next-word prediction” machines.

“Once upon a time, there was a…”

To generate the next word, “dragon,” the model needs to understand the context of the preceding words. This context is stored in the form of vectors (clusters of numbers) called the KV Cache.

Once “dragon” is generated, the model needs to generate the next word. To do that, it now needs the context for “Once upon a time, there was a dragon.” The KV Cache just grew by one word.

Every time a token is generated, it gets tucked into the KV Cache. It keeps growing and growing. If a conversation gets long, the stored context becomes a mountain of data.

To make matters worse, every user has a different conversation length. One person might just say “Hi,” while another asks the model to write a full-length novel.

This unpredictability — not knowing how much will be input or how much will be generated — is where the inefficiency begins.


2. The Legacy Problem: Too Many “Reserved Seats”

Before PagedAttention, how did deep learning frameworks manage this KV Cache?

Since they didn’t know how much space would be needed, they just reserved the maximum possible amount.

Imagine a restaurant owner who sees one customer walk in. Fearing that the customer might eventually bring 100 friends, the owner reserves a 100-seat long table and refuses to let anyone else sit there.

In LLM terms, the framework would allocate a contiguous block of memory equal to the model’s maximum sequence length (e.g., 2048 tokens) right from the start.

This leads to two types of serious waste:

  1. Internal Fragmentation: You reserved space for 2048 tokens, but the user only generated 100. The remaining 1948 slots are just dead space.
  2. External Fragmentation: Small gaps of memory are left over between different requests, but because they aren’t contiguous, they can’t be used to store a long sequence.

Think of a bar with stools. People come and go, leaving single empty seats scattered around. You see 1 empty seat here, 2 there, and another 1 further down. Total empty seats: 4.

Now, a group of 3 friends walks in. They want to sit together. Even though you have 4 seats available, you can’t seat them because you don’t have 3 consecutive seats.

The KV Cache worked the same way. It demanded contiguous memory. So, even if there was enough total memory available, it would go to waste if it wasn’t in one solid block.

Research shows that in traditional systems, roughly 60% to 80% of GPU memory was being wasted. Imagine buying a $40,000 A100 GPU and using $30,000 of it just to heat the room.


3. Finding Answers in a 30-Year-Old Textbook

To solve this, researchers didn’t look at the latest AI papers. Instead, they cracked open a classic Computer Science textbook: Operating Systems.

Operating systems like Windows, macOS, and Linux faced this exact same problem decades ago. Giving every program a giant “chunk” of memory was wasteful. The solution they found was Virtual Memory and Paging.

The core idea of PagedAttention is beautifully simple:

“Does data really need to be stored in a contiguous block? Why not break it up and shove the pieces wherever there’s a gap?”

The Art of Packing a Suitcase

Imagine you’re playing Tetris.

The long straight blocks are great. You can clear four lines at once. But as anyone who has played knows, you can’t always count on them. Eventually, you end up with weird gaps, and the long blocks just sit on top, taking up space until it’s Game Over.

Have you ever wished you could just snap that long block into three pieces and fit them into those annoying gaps?

Congratulations. You just invented PagedAttention.

While legacy methods treated the KV Cache like that long, rigid Tetris block, PagedAttention breaks the data down:

  1. It splits the KV Cache into small units called Blocks. (Usually, one block holds data for 16 tokens.)
  2. It takes these blocks and puts them anywhere there is a free spot in the GPU memory, regardless of physical location.

By doing this, you don’t need to find a giant contiguous space. You can use every last nook and cranny of the VRAM. Memory waste drops to nearly zero.


4. Keeping it Organized: The Block Table

You might be thinking: “Splitting them up is fine, but if the data is scattered everywhere, how do you keep the sentence in order?”

For an LLM, sequence is everything. If the data gets mixed up, you’ll get gibberish.

This is where the Block Table comes in. Think of it as an address book. It maps two things:

  • Logical Blocks: The order of the sentence as the user sees it (Block 0, Block 1…).
  • Physical Blocks: The actual address in the GPU memory (Block 0 is at Address 100, Block 1 is at Address 5…).

With this address book, the system can always find the correct token data, no matter where it’s physically hiding in the VRAM.

The Attention Calculation Flow

Let’s look at the “under the hood” process:

  1. A user sends a prompt.
  2. As the model starts computing, the PagedAttention kernel kicks in.
  3. The kernel checks the Block Table: “Okay, the first part of the context is in that corner, and the second part is over there.”
  4. It fetches those scattered pieces and performs the Attention calculation instantly.

This process is incredibly efficient. There’s almost no overhead in finding and retrieving the data.

One reason it’s so fast is that the vLLM team implemented a custom CUDA kernel at the GPU low-level. Instead of using complex conditional logic in high-level code, PagedAttention handles the Block Table lookup and Attention calculation simultaneously across thousands of GPU cores.

Because of this, we can trick the model into thinking the data is contiguous while wasting zero memory.


5. Memory Sharing: The Real Game Changer

The benefits of PagedAttention don’t stop at filling gaps. By managing memory in blocks, a revolutionary feature becomes possible: Memory Sharing.

Imagine this scenario:

User: “Explain the philosophy of G.W.F. Hegel.”
System: (To ensure a high-quality answer, the system uses Parallel Sampling to generate three different responses at once.)

In a traditional system, the KV Cache for the prompt (“Explain the philosophy of G.W.F. Hegel”) would be copied three times — once for each response process. That’s 3x the memory for the exact same data.

With PagedAttention:

  1. Reference: It creates only one set of KV Cache blocks for the prompt.
  2. Mapping: All three processes point to that same block in their block tables (Read-Only).
  3. Copy-on-Write: Only when the three responses start to diverge (generating different words) does the system allocate new, separate blocks for each.

This is the exact same Copy-on-Write (CoW) technique used when Linux forks a process. It’s a classic programming pattern.

Thanks to this, advanced decoding techniques like Beam Search or Parallel Sampling can run with a fraction of the memory they used to require.


6. So, How Much Faster Is It?

All these techniques are packed into the vLLM library. So, what does the performance look like?

Compared to the HuggingFace Transformers library (the previous standard), vLLM is anywhere from 2x to 24x faster in terms of throughput.

A quick clarification: this doesn’t mean a single user gets their answer 24x faster (Latency). It means the number of users the system can handle at the same time (Throughput) increases by up to 24x.

Why does throughput increase?

It’s simple math:

  1. PagedAttention eliminates memory waste.
  2. The “saved” memory can be used to squeeze in more simultaneous requests (Increased Batch Size).
  3. More users are processed in a single GPU compute cycle.
  4. Consequently, the traffic a single server can handle skyrockets.

For a business, this translates directly to cost savings. Instead of running 10 GPU servers, you might be able to handle the same load with just one or two using vLLM.


7. Swap-out / Swap-in: The Safety Net

What happens if the server is so popular that even with PagedAttention, the GPU memory hits its limit? Normally, the system would crash with an “Out Of Memory” (OOM) error.

But PagedAttention borrows one more piece of wisdom from the OS: Swapping.

Blocks that aren’t immediately needed are “swapped out” of the GPU memory and moved to the CPU memory (RAM). When those blocks are needed again, they are “swapped in” back to the GPU.

While moving data between the GPU and CPU is slower, it prevents the worst-case scenario: a server crash. It ensures the service remains stable even under extreme load.


8. Conclusion: Innovation through Reinterpretation

If you’ve ever taken an Operating Systems course, this might all feel strangely familiar. That’s because PagedAttention is a brilliant application of 30-year-old OS paging concepts to the modern problem of LLM serving.

To recap:

  • The Problem: Memory fragmentation and waste due to unpredictable KV Cache sizes.
  • The Solution: PagedAttention, which breaks memory into blocks for non-contiguous storage.
  • The Tool: A Block Table that bridges logical and physical addresses.
  • The Bonus: Massive throughput gains and memory sharing for complex decoding.

Looking at the lightning-fast evolution of AI, it’s easy to get overwhelmed by new theories. But often, the best innovations come from applying the “old” wisdom of Computer Science to new problems. It almost makes me wish I’d paid a little more attention in my undergraduate CS classes!