The PyTorch Training Loop from Scratch
Tensors, data loading, the canonical loop, mixed precision, checkpoints and the bugs that hide in them
Write and debug a single-GPU PyTorch training loop: tensor dtype/device rules, broadcasting traps, views vs copies and in-place autograd errors, the .item() graph leak, Dataset/DataLoader configuration (num_workers, pin_memory, drop_last, collate_fn, samplers), nn.Module parameters vs buffers and train/eval, the annotated canonical loop with correct weighted metric accumulation, autocast and GradScaler with fp16 vs bf16, checkpoints that resume exactly (optimizer, scheduler, scaler, RNG and sampler state), seeding and deterministic flags, torch.profiler and dataloader-bound diagnosis, a bug catalogue, and a worked example with real output and a find-the-five-bugs exercise with answers.
Practice questions (6)
-
View →
Write the Training Step for a Padded Token Classifier
Intermediate · Free -
View →
Why fp16 Training Stalls Without a GradScaler
Intermediate · Free -
View →
Design Checkpoints for a Preemptible Training Job
Advanced -
View →
The GPU Is 70% Idle: Find the Bottleneck
Intermediate -
View →
Seeds Fixed, Validation Accuracy Still Differs Between Runs
Advanced