KV caching, sampling kernels and CUDA graphs, one step at a time
Abstract
Most of us call an inference server and never look inside it. In this talk I started from Karpathy's nanoGPT, which recomputes everything for every new token, and added the optimizations that real inference engines rely on, one at a time. After each step we looked at what happened to throughput, time to first token and memory.
AI Tinkerers has a strict format: fifteen minutes, no slides, live code only. So this was a walk through nanoGPT-inference, a small fork of nanoGPT with a few hundred lines of extra inference code. Everything runs on GPT-2 weights, so you can reproduce the benchmarks on a single GPU.
Technical talk, in English. Assumes you roughly know how a transformer works.
Walkthrough
Resources
nanoGPT-inference on GitHub
KV caching, sampling kernels and CUDA graphs on top of nanoGPT, with a benchmark script
nanoGPT by Andrej Karpathy
The model implementation this repo forks
FlashInfer: sorting-free GPU kernels for LLM sampling
The rejection-sampling kernels used in the sampling step
llmq
My library for distributed LLM inference