an illustrated guide · 13 of 27 chapters
Learning SGLang
Inference engines, from the problems up.
Written and drawn by Wilson Zheng.
One picture per chapter. Each chapter is a single visualization you step through. The captions are the narration; there is almost no other text.
Every chapter ends on a question. The next chapter is the answer. By the end, an inference engine isn't a pile of features but the obvious response to a chain of "so what do we do about that?"
Keys. ← → to step, space to play. The last step of a chapter offers the next one.
The loop
Why every piece of the engine exists.
- 01Bare-minimum inferenceOne function, called in a loop.
- 02Attention, per decode stepWhat a new token needs from the past.
- 03KV cacheKeep k and v. Drop q.
- 04Prefill vs decodeTwo very different workloads.
- 05Batching & continuous batchingSharing a GPU between users.
- 06The schedulerTwo lists, one GPU, one choice per step.
- 07KV memoryPages, and the memory cap.
- 08Prefix cachingSame beginning, one copy.
- 09One request, end to endFrom an HTTP POST to the GPU and back.
- 10The scheduler's loopOne Req through one step.
Making one GPU fast
The loop is correct; now the step is slow for reasons that have nothing to do with the model.
- 11CUDA graphsLaunching a thousand kernels as one.
- 12Piecewise CUDA graphsWhen the graph has to break.
- 13torch.compileFusing the kernels you were about to launch.
- 14Attention backendsWhy there are five kernels for one equation.soon
- 15Speculative decodingGuess several tokens, verify in one step.soon
- 16QuantizationShrinking the read every step pays for.soon
- 17Two-batch overlapHiding communication behind compute.soon
More than one GPU
The model, the cache, or the traffic no longer fits on one card.
- 18Tensor parallelismA model bigger than one GPU.soon
- 19Pipeline & expert parallelismSplitting by layer, and by expert.soon
- 20Data-parallel attentionReplicate the attention side, share the rest.soon
- 21Hierarchical KV cacheWhen 64 GB is not enough.soon
- 22Prefill-decode disaggregationTwo clocks, two machines.soon
- 23The routerMany engines, one front door.soon
Beyond text · SGLang Omni
Speech and vision break the assumptions of Part I, one at a time.
- 24Multimodal inputsA stage in front of the loop.soon
- 25Multi-stage inferenceThinker, talker, and the pipe between them.soon
- 26Streaming audio decodeOutput with a real-time deadline.soon
- 27Full duplexListening while speaking.soon