liveBook · Manning

Chapter 16: Training models on multiple GPUs

What you will learn in chapter 16 of Deep Learning with PyTorch, Second Edition

This introduction explains why growing models and datasets can make training on a single GPU infeasible.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why growing models and datasets can make training on a single GPU infeasible.

Key terms: single GPU, large language models, parameters, inference

16.1 Introduction to parallel programming

Introduces parallel programming as a way to speed up training, illustrates the benefit with concurrent work, and distinguishes threads from processes.

Key terms: parallel programming, sequential, concurrently, threads, processes, memory space, distributed training

16.1.1 Distributed computing terminology

Defines the process, machine, and communication terms used to describe distributed training across GPUs.

Key terms: world, world size, rank, node, global rank, local rank, process group, torch.distributed.init_process_group, SPMD, Single Program Multiple Data

16.1.2 Hardware requirements

This subsection explains what hardware and operating system are practical for running the chapter’s distributed-training examples.

Key terms: distributed training, multiple GPUs, CPUs, speedup, PyTorch’s distributed APIs, Windows, Linux, macOS, Windows Subsystem for Linux (WSL)

16.1.3 Initializing a distributed program

This subsection explains how to launch processes and initialize communication for a distributed PyTorch program, either manually or with torchrun.

Key terms: torch.multiprocessing.spawn, process index, num_processes, torch.distributed, TCPStore, MASTER_ADDR, MASTER_PORT, init_process_group, torchrun, mp.spawn

16.2 Collective communication

This section explains how distributed processes communicate tensor data, which backends carry that communication, and what broadcast and allreduce do.

Key terms: distributed process group, torch.distributed.init_process_group(), tensor data, gloo, nccl, mpi, backend, collective communication, broadcast, allreduce

16.3 Introduction to parallelisms

This section explains why GPUs can process separate data shards independently during training but must synchronize gradients before updating copies of the same model.

Key terms: forward pass, backward pass, shard, gradients, optimizer.step(), gradient reduction, allreduce, synchronized gradients

16.4 Data parallelism

This section explains how data-parallel training synchronizes gradients, how PyTorch’s DistributedDataParallel handles that work, and why each device needs a distinct share of the data.

Key terms: data parallelism, loss.backward(), dist.all_reduce, opt.step(), gradients, DistributedDataParallel, DDP, gradient synchronization, DistributedSampler, shard the dataset

16.5 Model parallelism

Explains why models too large for one GPU need model parallelism and how partitions exchange activations and gradients during training.

Key terms: data parallel, model parallelism, replicating the model, forward pass, activations, peer-to-peer (P2P) operations, send(), recv(), backward pass, gradients

16.5.1 Pipeline parallelism

Explains how pipeline parallelism overlaps work across model stages and microbatches, its tradeoffs, and the PyTorch setup shown in the example.

Key terms: pipeline parallelism, stages, microbatches, repeated blocks, transformer layers, communication overhead, bottlenecks, stragglers, torch.distributed.pipelining, split_spec

16.5.2 Tensor parallelism

This section explains how tensor parallelism splits parameters within neural-network modules across devices and uses communication to preserve the model’s output.

Key terms: tensor parallelism, model parallelism, pipeline parallelism, parameters, tensors, linear layer, weights and biases, input tensor, matrix multiplication, intermediate results

16.5.3 Deciding between pipeline and tensor parallelism

Explains how model shape and per-device memory limits guide the choice between pipeline and tensor parallelism.

Key terms: pipeline parallelism, tensor parallelism, skinnier and deeper, wider and shallower, memory constraints

16.6 n-dimensional parallelism

The section explains why large-scale training combines forms of parallelism and how DeviceMesh organizes communication for a combination of model partitioning and replication.

Key terms: n-dimensional parallelism, tensor parallelism, pipeline parallelism, allreduce, DeviceMesh, device mesh, process groups, ranks, replicate

16.7 Fully sharded data parallelism

This section explains how Fully Sharded Data Parallel (FSDP) shards model state across GPUs, gathers it temporarily for computation, and helps train models too large for ordinary data parallelism.

Key terms: Fully Sharded Data Parallel, FSDP, full parameter sharding, all_gather, reduce_scatter, forward pass, backward pass, torch.distributed.fsdp, fully_shard, device mesh

16.8 Large language model–specific parallelisms

This section introduces context and expert parallelism as GPU strategies relevant to large language models, without explaining how they work.

16.8.1 Context parallelism

Explains how context parallelism splits long sequences across ranks to reduce training memory while preserving causal attention at a communication cost.

Key terms: context parallelism, ranks, causal masking, activation memory, intra-node interconnects

16.8.2 Expert Parallelism

Explains how expert parallelism distributes a mixture-of-experts model across devices and uses sparse routing to scale it.

Key terms: Mixture of Experts (MoE), expert parallelism, expert networks, sparse computation, router, top-k, auxiliary loss, all-to-all communication

16.9 Tying all parallelisms together

The section explains why combining distributed-training strategies requires additional engineering and presents TorchTitan as a practical PyTorch reference.

Key terms: parallelism strategies, checkpointing, resource utilization, communication across devices, TorchTitan, native PyTorch APIs, ready-to-use configurations, tokenizers

16.10 Conclusion

The conclusion connects distributed training’s use of multiple GPUs with gradient synchronization and scaling to larger models and datasets.

Key terms: collective communication, synchronizing gradients, distributed training

16.11 Exercises

16.12 Summary

Concepts explained in this chapter

← Chapter 15: Using segmentation to find suspected nodules Chapter 17: Deploying to production →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.