Lesson 4.9core3 h 30 min

PyTorch II: real training pipelines

Datasets, modules, mixed precision, checkpoints and multi-GPU in one template

Every project from here on — the image classifier of 5.10 Project: train and deploy an image classifier, the character model of 6.8 Project: character-level language model, GPT from scratch in 7.8 Build GPT from scratch (nanoGPT walkthrough), the LoRA fine-tune in 8.13 Project: fine-tune a small LLM with LoRA and ship a chat app — needs the same machinery: an efficient data pipeline, a model built from composable modules, a loop with mixed precision, scheduling, clipping, evaluation and resumable checkpoints, and a way to use more than one GPU. Build it once, properly, and you will spend your time on ideas instead of plumbing.

You'll be able to

  • Build efficient Dataset/DataLoader pipelines and diagnose data-loading bottlenecks
  • Compose models from nn.Module building blocks and manage train/eval modes and state_dicts
  • Write a training loop with AMP, gradient clipping, per-step scheduling, evaluation and resumable checkpoints
  • Use torch.compile and the profiler to find and remove performance bottlenecks
  • Explain how DistributedDataParallel and FSDP split work across GPUs and when to reach for Lightning or Accelerate

Key ideas

  1. Decode and augment inside Dataset.getitem so DataLoader workers run in parallel with the GPU; pin memory and use non_blocking copies.
  2. Register submodules in nn.ModuleList/Sequential (not Python lists); toggle train()/eval(); use inference_mode() for evaluation.
  3. A resumable loop checkpoints model, optimizer, scaler, scheduler step and RNG state; compute the LR from the step so resuming is exact.
Show 4 more key ideas
  1. Apply weight decay to ≥2-D parameters only; unscale before clipping; accumulate sums (not batch means) in evaluation.
  2. torch.compile fuses kernels (1.3–2× typical) but needs static shapes; TF32 matmul precision is a free speedup; profile before optimizing.
  3. DDP = one process per GPU, replicated model, all-reduced gradients, sharded data; FSDP/ZeRO shard parameters and optimizer states when the model doesn't fit.
  4. Test shapes, overfit-one-batch, determinism, gradient flow and checkpoint round-trips; ML bugs are silent.

Suggested path through this lesson