an illustrated guide · 21 of 29 chapters
Learning SGLang
Inference engines, from the problems up.
Written and drawn by Wilson Zheng.
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.
- 10Scheduler deep diveOne 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.
Beyond text · SGLang Omni
Speech and vision break the assumptions of Part I, one at a time.
More from one GPU
Every pass still reads all of the weights: make that read smaller, and keep the GPU busy while it waits.
- 22QuantizationShrinking the read every step pays for.soon
- 23Two-batch overlapHiding communication behind compute.soon
More than one GPU
The model, the cache, or the traffic no longer fits on one card.
- 24Tensor parallelismA model bigger than one GPU.soon
- 25Pipeline & expert parallelismSplitting by layer, and by expert.soon
- 26Data-parallel attentionReplicate the attention side, share the rest.soon
- 27Hierarchical KV cacheWhen 64 GB is not enough.soon
- 28Prefill-decode disaggregationTwo clocks, two machines.soon
- 29The routerMany engines, one front door.soon