CUDA graphs and torch.compile in vLLM
Why vLLM compiles the model and captures CUDA graphs, how it does both at v0.34.0: piecewise compilation split at attention, the five CUDA graph modes and the FULL_AND_PIECEWISE default, batch descriptors and padding to captured sizes, the compile cache, optimisation levels -O0 to -O3, and what all of it costs in startup time and KV cache memory.
By Sandip Maity, Applied AI Engineer
Key takeaways
- At small batches a decode step is hundreds of short kernels. Launching them one by one from Python can take as long as running them; a CUDA graph replays them all with one launch.
- vLLM compiles the model with torch.compile and splits the graph at attention, so the pieces between attention calls can be captured as CUDA graphs for any attention backend.
- The default mode, FULL_AND_PIECEWISE, replays one full graph for uniform decode batches and piecewise graphs for prefill and mixed batches.
- Graphs exist only for captured sizes, so each batch is padded up to the nearest captured size.
- Compilation and capture cost startup time and memory taken from the KV cache. -O0 or --enforce-eager trades speed for fast startup and easier debugging.
Goal: understand what vLLM compiles and captures, why, and what it costs, so you can read the startup log, choose an optimisation level, and explain a step-time difference between eager and graph mode.
You need: a checkout of vLLM at v0.34.0. A GPU only for the optional measurement at the end. Background: kernel fusion and launch overhead and streams, events and CUDA graphs.
Honest limits. The compilation and graph code changes often, and Model Runner V2 replaces some older capture settings with new ones (noted below). Which modes work also depends on the attention backend and the model. Read the startup log and docs/design/cuda_graphs.md for your version.
#Why graphs matter for decode
A decode step at batch size 1 does little work per kernel and runs many kernels: for each layer, norms, projections, rotary embedding, attention, activation and residual adds, plus their small helper kernels. A decoder with a few dozen layers launches hundreds of kernels per step. Each launch from Python through PyTorch costs CPU time, often several microseconds. If the GPU finishes each kernel faster than the CPU can launch the next, the GPU waits, and the step takes as long as the launches, not the work.
A CUDA graph records a sequence of kernels with their arguments once and replays it with a single launch. The catch: shapes and memory addresses are fixed at capture. Inputs must be copied into the graph's own buffers, and a graph captured for 32 tokens only runs 32 tokens.
The 35 to 80 tokens per second project shows the size of this effect from the outside: a baseline far below the bandwidth ceiling with gaps between kernels in the profile.
Model
Kernels per token
388
GPU time per kernel
3.5 µs
GPU busy (eager)
29%
Graph speedup
3.47×
Launch-bound: the GPU finishes each kernel before the CPU can start the next one. Graphs, fusion or a compiled model recover the idle time.
Model: each kernel takes max(GPU time, launch cost). GPU time per kernel = weight bytes ÷ 3.35 TB/s spread over all kernels. Green is GPU work, hatched is the GPU idle while the CPU prepares the next launch. Launch costs are assumptions: measure yours.
The rest is Pro
Keep reading the rest of this lesson
Pro unlocks the rest of this lesson, every advanced lesson, the hands-on labs and completion certificates.
One year
$25
365 days, no auto-renewal
Lifetime Best value
$75
pay once, keep it forever
7-day refund, no questions asked. One payment, no auto-renewal.Compare plansHave a licence key?
Not ready? Keep learning free
Free lessons from vLLM Internals, no sign-up. Or follow the free roadmap →
Not ready? Take the free 5-day email course
One short email a day for five days, each built on a free lesson, then the occasional new lesson or offer. Unsubscribe in one click.
Still to come in this lesson
- Step 1: compile, and split at attention
- Step 2: capture graphs
- What it costs
- Optimisation levels
- Exercises
- Optional: measure it
- Where to go next
You will be able to answer
- Why does vLLM split the compiled graph at the attention operation?
- A step has 37 decoding requests. Captured decode sizes include 32 and 40. What does the runner do?
- When is --enforce-eager (or -O0) a good choice?