Fusion and graphs
Two more tricks fast engines use: fewer trips to memory, and fewer launches from the CPU.
Kernel fusion
Kernel fusion merges several small steps on the same data into one kernel. Three separate steps read and write HBM three times; fused, it's one read and one write. The math is the same, but the memory traffic drops. It helps memory-bound steps most.
Compilers
Fusing by hand is slow, careful work. torch.compile reads a PyTorch model, finds steps that can be fused, and writes fused kernels for you, often in Triton. Compiling makes the first run slow, like a build step. So engines compile and warm up at startup, before real users arrive.
CPU overhead
The CPU launches each kernel on the GPU, one at a time. In decode, each kernel's work is small, so the GPU can finish before the next launch arrives. Then the step is CPU-bound, and a faster GPU won't help. This shows most with small models and small batches.
CUDA graphs
A CUDA graph records a whole step's kernel launches once, then replays them with a single launch. A step that needed 300 launches now needs one. CPU work drops, the gaps close, and small-batch decode gets faster. Unlike fusion, a graph still runs every kernel, so it doesn't save HBM traffic.
Limits of graphs
A graph replays exactly what was recorded, so the shapes, like batch size, must match. Engines capture graphs for a few batch sizes, like 1, 2, 4, 8 and 16. Each batch is padded up to the next size: 5 requests run as 8. Each capture costs startup time and memory, so engines keep the list short.
Remember
- Fusion: one trip to HBM instead of many.
- torch.compile fuses for you; warm up before traffic.
- Small decode steps can be CPU-bound.
- CUDA graphs: record once, replay with one launch.
- Graphs need fixed shapes, so batches are padded.