Zero Labs
Industry News / AI

The 1960s Operating System Trick That Makes vLLM 4x Faster

Source · Towards AI By D RAHUL PRASANTH

From the publisher’s feed, collected in the ZeroLabs newsroom.

How vLLM borrowed virtual memory paging to solve AI’s multi-billion dollar GPU memory crisis and why it leaves local runtimes in the dust.Imagine you go to a restaurant, and before you even order a drink, the host forces you to reserve and pay for a 12-course banquet table. Even if you only end up ordering a single side salad, that entire table remains locked. Nobody else can sit there.This sounds like a logistical nightmare, yet this is exactly how standard Large Language Model (LLM) serving engines used to treat GPU memory.Every time a user prompts an LLM, the system has to generate tokens one by one autoregressively. To avoid recalculating the history of the conversation over and over, the system saves the mathematical “history” of the sequence in GPU memory. This is known as the Key-Value (KV) Cache.In legacy systems, this KV Cache had to be stored as a single, contiguous block of memory. Because the system has no idea how long a user’s response will be, it statically pre-allocates space for the maximum possible sequence length (e.g., 2,048 tokens) upfront.The result? A catastrophic waste of up to 60% to 80% of your precious GPU memory. You are paying for a luxury hotel suite but only using the pillow.In my previous article, Stop Wasting GPU Memory: A Deep Dive Into vLLM’s PagedAttention, we went deep into the math behind the PagedAttention algorithm. Today, we are taking a step back to look at the bigger picture: how vLLM wraps this algorithm into an entire operating system-like architecture to achieve a staggering 2x to 4x throughput boost in enterprise deployments.1. The OS Secret: Virtual Memory for LLMsTo solve this memory waste, the creators of vLLM borrowed a page from classic operating systems: Virtual Memory Paging.When your computer runs out of RAM, it doesn’t crash. It breaks your data down into tiny, non-contiguous “pages” and scatters them wherever they fit in physical memory, using a virtual page table to keep track of where everything actually is.PagedAttention does the exact same thing for LLM inference.Instead of allocating a massive, rigid, continuous block of memory for each user, PagedAttention partitions the KV Cache into small, flexible KV Blocks (typically sized to hold exactly 16 tokens).┌────────────────────────────────────────────────────────┐│ How vLLM Paging Works │├────────────────────────────────────────────────────────┤│ Logical Blocks (What the LLM thinks it has): ││ [ Block 0 (Tokens 1-16) ] ───► [ Block 1 (Tokens 17-32) ]││ ││ Physical Blocks (Where they actually live in VRAM): ││ [ GPU VRAM Address 0x9F ] ◄── Holds Block 0 ││ [ GPU VRAM Address 0x12 ] ◄── Holds Block 1 │└────────────────────────────────────────────────────────┘By mapping continuous logical blocks to scattered physical memory pages, vLLM dynamically allocates GPU memory only as new tokens are generated. This completely wipes out reservation waste, shrinking memory waste from ~70% to virtually zero (under 4%).2. See It in Action: The Macro OrchestratorBut managing these physical block maps is a massive coordination challenge. How does a server keep track of hundreds of users, all generating tokens at different speeds, without descending into chaos?This is where the centralized orchestrator of vLLM comes in. Below, we can see a live simulation of the engine processing incoming client prompts:An end-to-end simulation of the vLLM engine, demonstrating how the centralized scheduler maps logical block tables to physical GPU/CPU memory pools on the fly.Let’s break down exactly what is happening in this orchestrator dashboard:The FCFS Request Queue (Left): Client requests (such as Req 1, 2, and 3) arrive at different times with variable lengths. vLLM schedules them at the iteration level using a First-Come-First-Serve policy to guarantee fairness.The Translation Map (Center): As tokens generate, the Block Table maps contiguous logical segments to scattered physical GPU locations.The Memory Pools (Right): VRAM is divided into a clean grid of physical blocks. If a surge of traffic overcommits the GPU memory, the scheduler preempts active sequences and swaps their blocks to the CPU Swap Space (the bottom-right grid) to prevent the server from crashing.The Live Metrics: Notice how PagedAttention keeps GPU Waste at 0.0%, translating into a massive 4x throughput multiplier over traditional baseline allocators.3. The Three Superpowers of Paged MemoryWith an OS-style page table managing our GPU memory, we can now perform tricks that were previously impossible. Let’s break down the three core generation scenarios that make vLLM so incredibly fast:Superpower #1: The Autoregressive “Pay-As-You-Go” PlanWhen a single sequence generates text, the logical-to-physical block table updates dynamically. Instead of reserving memory for 2,000 tokens upfront, the engine requests a single 16-token physical block. It only requests block #2 once block #1 is completely full.Watch how physical blocks are dynamically grabbed from the free VRAM pool on-demand as new tokens generate.Superpower #2: Parallel Sampling & “Copy-on-Write” (CoW)What if you ask an LLM to generate 5 different email drafts from the same prompt? Traditionally, you’d have to copy-paste the prompt’s KV cache 5 times.With vLLM, all 5 output streams share the exact same physical memory blocks for the prompt. The moment the drafts begin to diverge, vLLM triggers a Copy-on-Write (CoW) event. It duplicates only the specific block that is changing, leaving the shared prompt blocks untouched. This allows you to share up to 30% of your memory effortlessly.Diverging paths? No problem. The system only duplicates blocks when their paths actually branch off.Superpower #3: Beam Search (Dynamic Tree Pruning)In advanced decoding like beam search, the model evaluates multiple candidate “beams” and dynamically prunes the low-probability ones. Because candidates with a shared history map to the exact same physical memory blocks, pruning a candidate simply decrements the block’s “reference count”. If a block’s reference count hits zero, it is instantly garbage-collected back into the free VRAM pool, unlocking 37% to 55% memory savings.Watch how pruned branches immediately release their physical blocks back to the system pool.4. The Ultimate Safety Net: Swapping vs. RecomputationWhat happens when your server experiences a sudden, massive surge in users and your physical GPU blocks run out completely?In legacy frameworks, this triggers the dreaded CUDA Out-Of-Memory (OOM) error, crashing the entire server. vLLM is smarter. It handles memory overcommitment with a strict First-Come-First-Serve (FCFS) scheduler.When VRAM is completely full, the scheduler temporarily pauses (preempts) the most recent requests. To free up space, vLLM has two options to handle the paused request’s KV Cache:Swapping: It packages the request’s blocks and ships them across the PCIe bus to host CPU memory. When the GPU has free space again, they are swapped back to VRAM to resume decoding.Recomputation: It simply discards the cached blocks. When rescheduled, the model runs a single, lightning-fast prefill pass over the generated tokens to recreate the cache.┌────────────────────────────────────────────────────────┐│ Preemption Recovery: The Trade-off │├────────────────────────────────────────────────────────┤│ For Small Blocks: Recomputation is faster because ││ shipping small data blocks over PCIe bottlenecks ││ the bus with communication overhead [4, 40]. ││ ││ For Large Blocks: Swapping is faster because it ││ perfectly saturates the PCIe transfer bandwidth [4, 40].│└────────────────────────────────────────────────────────┘5. Ollama vs. vLLM: Which One Do You Actually Need?If you are developing LLM applications, you have likely run into both Ollama and vLLM. But comparing them is like comparing a cozy local commuter train to a high-speed bullet train. They are built for completely different worlds.Ollama: The Local PlaygroundThe Philosophy: Designed for absolute ease of use on consumer laptops (e.g., your MacBook).The Engine: Uses llama.cpp under the hood, heavily optimized for unified memory architectures (like Apple Silicon) and single-user pipelines.The Limitation: It uses traditional static batching. If you try to run an Ollama instance to serve an API to hundreds of concurrent users, its performance will grind to a halt.vLLM: The Enterprise WorkhorseThe Philosophy: Designed to maximize token throughput on dedicated enterprise GPUs (like NVIDIA A100/H100 clusters) in cloud data centers.The Engine: Orchestrated by a centralized scheduler, running custom CUDA kernels to bypass paging overhead, and scaling across multiple GPUs using Megatron-LM tensor parallelism.The Trade-off: It is highly complex to set up locally, but it delivers the massive high-throughput performance required to run production-grade APIs.Wrapping Up: Memory is the Real BottleneckAs Large Language Models become the backbone of autonomous agents and complex multi-sequence workloads, raw compute speed is no longer the primary constraint. The real battle is memory bottlenecking.By applying classic operating system paging to the modern AI stack, vLLM proved that software architecture can unlock just as much performance as hardware scaling. If you’re building for scale, look past the weights — and start looking at how you manage your blocks.Further Reading & ReferencesFor a deep dive into the underlying mathematical equations of PagedAttention, check out my previous piece: Stop Wasting GPU Memory: A Deep Dive Into vLLM’s PagedAttention (Towards AI).SOSP ’23 Paper: Efficient Memory Management for Large Language Model Serving with PagedAttentionThe 1960s Operating System Trick That Makes vLLM 4x Faster was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.