GPUs and Efficient Training
Using GPUs well: device placement, data loading bottlenecks, mixed precision, memory limits and a first look at multi-GPU training.
Intermediate · about 6 hours · difficulty 3/5
Learn first: PyTorch
Deep learning is compute bound, and GPUs speed up the matrix math by one to two orders of magnitude. Using them well is a practical skill employers expect.
Essentials
- Access: Google Colab and Kaggle notebooks offer free GPUs; cloud GPU rentals for bigger jobs.
- Device placement: move model and each batch to the same device; avoid needless CPU-GPU copies and
.item()calls inside hot loops. - Monitor utilization with
nvidia-smi. Low utilization usually means the data loader is the bottleneck: increasenum_workers, usepin_memory, preprocess ahead of time. - Mixed precision (fp16 or bf16 with autocast) roughly halves memory and speeds up training on modern GPUs.
- Out of memory fixes: smaller batch plus gradient accumulation, mixed precision, gradient checkpointing.
Scaling out (optional for now)
Distributed Data Parallel (DDP) replicates the model on several GPUs; FSDP and DeepSpeed shard large models. You mostly need these for large model work.
Reproducibility
Set seeds for Python, NumPy and the framework, and record GPU type and library versions; exact bitwise reproducibility on GPUs is not guaranteed.
Good enough to move on when you can profile a slow training run, identify whether data loading or compute is the bottleneck, and enable mixed precision.
Key concepts
- Free GPU platforms
- Device placement
- Data loading bottlenecks
- Mixed precision training
- Handling out-of-memory errors
- DDP and FSDP
Mini project
Benchmark CIFAR-10 CNN training on a free Colab or Kaggle GPU with different num_workers settings and with and without mixed precision, and report images per second and peak memory.
Check yourself
nvidia-smi shows your GPU at 25% utilization during training. What is the most likely cause and how would you confirm it?
Free resources
- PyTorch automatic mixed precision (PyTorch)
- PyTorch tutorials (performance and distributed) (PyTorch)