16.2 Distributed and serving boundaries
This part of Training and Frameworks develops the mechanisms below. Work the numerical examples or trace the code before checking the answer.
Concepts from other sections (14)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Transformation (JAX): a function such as
jit,grad,vmap, orpmapthat takes a pure function and returns a compiled, differentiated, batched, or parallel version of it. Read more: 16.3 Embeddings, small models, and platforms - inference engine: software that loads weights, schedules requests, manages the KV cache, and generates tokens (vLLM, SGLang, TensorRT-LLM, llama.cpp). Read more: 20.5 LLM Serving Platforms: How They Work and When to Use Them
- PagedAttention: KV-cache management in fixed-size blocks mapped through a block table, like virtual memory. Read more: Chapter 20: Inference Engines and GPU Deployment
- KV cache: per-layer storage of past keys and values, so each decode step processes only the new token. Read more: 21.2 The KV cache, incremental decoding, and generate
- continuous batching: re-forming the batch at every step so finished requests leave and waiting ones join immediately. Read more: 23.3 Continuous batching scheduler with priority and preemption
- Model registry: the store of versioned, signed model weights between evaluation and serving. Read more: 16.3 Embeddings, small models, and platforms
- KEDA: an operator that scales workloads on external metrics by creating and driving an HPA. Read more: 10.7 Durable and worker images and charts in Systems Engineering
- data parallelism (DDP): each rank holds a full model replica, computes the gradient of its batch slice, and all ranks average gradients before stepping. Read more: 17.3 Data parallelism and ZeRO stages 1 to 3
- all-reduce: every rank ends with the elementwise reduction (sum or mean) of all ranks’ arrays. Read more: 17.2 Collectives over processes: ring all-reduce
- Pipeline bubble: time a stage cannot perform useful work because the schedule has not filled or drained. Read more: 17.4 Parallelism, topology, and the global training batch
- ZeRO / FSDP: sharding parameters, gradients, and optimizer state across data-parallel ranks, gathering each layer only when it is needed. Read more: 16.3 Embeddings, small models, and platforms
- BF16: a 16-bit float with float32’s sign and exponent and 7 mantissa bits; the top half of a rounded float32. Read more: 6.7 Safetensors for every dtype, atomic checkpoints, and the token stream
- fp16: a 16-bit format with 5 exponent bits and 10 mantissa bits; finer precision but a maximum of 65504 and a smallest subnormal of . Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- master weights: the float32 copy of the parameters and optimizer state that receives every update. Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
Learning objectives
- Explain which responsibilities belong to the learner and the serving runtime.
Serving frameworks: vLLM and the stack
Section titled “Serving frameworks: vLLM and the stack”A framework defines a model; it does not serve one well. A naive model.generate() loop wastes the GPU, because concurrent, variable-length traffic leaves it idle between requests. Serving engines exist to keep the GPU saturated under that kind of load, and they sit on top of the framework: PyTorch or JAX defines the model, and vLLM, TGI, TensorRT-LLM, or SGLang serves it efficiently. The full treatment of how these products work, when to use each, and their tradeoffs is in LLM Serving Platforms and the rest of LLM Systems & Inference.
vLLM is the default for self-hosting LLMs. It is a high-throughput inference engine built around a PagedAttention KV cache, continuous batching, tensor parallelism, and prefix caching, and it exposes an OpenAI-compatible server. NVIDIA TensorRT-LLM takes a different approach: it compiles the model ahead of time into fused, hardware-specific kernels (with FP8 and in-flight batching) for maximum throughput and minimum latency on NVIDIA GPUs, and it is typically served behind Triton Inference Server. The tradeoff against vLLM is a build step and less flexibility in exchange for peak performance.
Above the engine sits orchestration. Ray is a common choice: Ray Serve wraps a serving engine (vLLM or TensorRT-LLM) with autoscaling, multi-model routing, and request batching across a cluster, while Ray Core’s actor and task model is the distributed-compute substrate underneath and Ray Train handles the training side, discussed next. From the platform’s point of view, the engine is one tier. Around it sit a model registry, an autoscaler (KEDA, Ray Serve, or KServe), a router, and observability; see Cloud Native in Systems Engineering for the substrate and Streaming and SSE for how tokens reach the client.
Distributed training
Section titled “Distributed training”Serving assumes the model already exists. Producing it at scale raises the opposite problem: the model may not fit on one GPU, and even when it does, one GPU is too slow. “Distributed training” is not a single switch. It is a choice of which dimension of the problem to split, and each choice costs a different amount of communication. There are four axes, and real systems combine them; “3D parallelism” is tensor times pipeline times data parallelism.
| Strategy | What it splits | Communication | Use when |
|---|---|---|---|
| Data parallel (DDP) | The batch (full model replica per GPU) | All-reduce gradients each step | Model fits on one GPU, want more throughput |
| Tensor parallel (TP) | Each weight matrix across GPUs | All-reduce per layer (heavy) | Layer too big; keep inside one node (NVLink) |
| Pipeline parallel (PP) | Layers into stages | Activations between stages | Model spans nodes; tolerate pipeline bubbles |
| Expert parallel (EP) | MoE experts across GPUs | All-to-all token routing | Mixture-of-Experts models |
Name the dimension you need to split, and the tool follows.
Sharding the optimizer: ZeRO and FSDP
Section titled “Sharding the optimizer: ZeRO and FSDP”The surprising fact about training memory is that the weights are not the dominant cost. The optimizer state is: Adam keeps two moments per parameter, and together with gradients and activations the total often runs four to eight times the size of the weights in fp32. A model whose weights fit comfortably on a GPU may be impossible to train there.
ZeRO (in DeepSpeed) and FSDP (in PyTorch) attack exactly that cost. They shard parameters, gradients, and optimizer state across the data-parallel ranks, gather a layer’s full weights only for its forward and backward pass, and release them afterward. ZeRO’s stages 1, 2, and 3 shard progressively more of that state. The result is that you can train a model far larger than any single GPU’s memory without resorting to full tensor parallelism. The general lesson is to shard the most expensive thing: find the dominant cost before deciding what to split.
The tooling and the habits
Section titled “The tooling and the habits”A small set of tools does most of this work. DeepSpeed provides ZeRO and offload to CPU or NVMe. Megatron-LM provides tensor, pipeline, and sequence parallelism and is the reference implementation for large pretraining. PyTorch FSDP is the native option, and HuggingFace Accelerate lets one config drive many backends. Ray Train orchestrates multi-node training over Ray Core, handling data loading, fault tolerance, and elastic scaling around PyTorch or DeepSpeed. Underneath all of them is NCCL, the GPU collective-communication library.
Three habits are standard on any serious run. Checkpointing must be distributed, sharded, and asynchronous so a multi-day run survives a node failure; this is the same checkpoint pattern that Durable Orchestration and Workers builds for workflows. Mixed precision (bf16 or fp16 compute with fp32 master weights) cuts memory and time. Gradient (activation) checkpointing recomputes activations during the backward pass instead of storing them, trading compute for memory.
Summary
Section titled “Summary”Explain which responsibilities belong to the learner and the serving runtime. State the assumptions before applying the result to a different workload.
Review questions
Section titled “Review questions”-
Why is a training checkpoint not a complete serving artifact?
Answer
Serving also needs the architecture, tokenizer, template, position rules, precision, and generation contract.
Chapter contents · Next: Embeddings, small models, and platforms