Skip to main content
nanoGPT uses PyTorch Distributed Data Parallel (DDP) to scale training across multiple GPUs and nodes. This guide explains how DDP is implemented and how to configure it for your setup.

How DDP works in nanoGPT

The training script automatically detects and configures DDP based on environment variables set by torchrun.

DDP detection and initialization

From train.py:82-100:

Backend configuration

The default backend is NCCL, optimized for NVIDIA GPUs:
NCCL is recommended for NVIDIA GPU clusters with high-speed interconnects like Infiniband. For CPU training or mixed CPU/GPU setups, use Gloo backend.

Single-node, multi-GPU training

Launch with torchrun

Train on all available GPUs on a single node:

Parameters explained

Example: 4 GPUs

1

torchrun sets environment variables

For each process:
  • RANK: Global rank (0-3)
  • LOCAL_RANK: Local rank on this node (0-3)
  • WORLD_SIZE: Total number of processes (4)
2

Each process initializes

  • Loads the same model
  • Sets different CUDA device based on LOCAL_RANK
  • Uses different random seed (1337 + RANK)
3

Model is wrapped with DDP

4

Gradients are synchronized

During backward pass, DDP averages gradients across all processes

Multi-node training

Two-node example

For training across 2 nodes, each with 8 GPUs:

Multi-node parameters

Infiniband configuration

If your cluster does not have Infiniband interconnect, prepend NCCL_IB_DISABLE=1 to disable Infiniband support. Without this, training will crawl.
With Infiniband:
Without Infiniband:

Benchmark your interconnect

Use iperf3 to test network bandwidth between nodes:
Expect:
  • Infiniband: 100+ Gbps
  • 10GbE: ~10 Gbps
  • 1GbE: ~1 Gbps (will be very slow for multi-node training)

Gradient accumulation with DDP

Automatic scaling

Gradient accumulation steps are automatically divided by world size to maintain the same effective batch size:

Example calculation

With config/train_gpt2.py:
The effective batch size remains constant regardless of the number of GPUs. Each GPU processes fewer gradient accumulation steps.

Gradient synchronization

Efficient sync strategy

From train.py:292-298, gradients are only synchronized on the last micro-step:
This avoids redundant gradient synchronization during gradient accumulation.

Checkpointing and logging

Master process only

Only the master process (rank 0) performs I/O operations:

Unwrap DDP for checkpointing

This ensures the checkpoint contains the model weights without DDP wrapper.

Cleanup

Always destroy the process group when training completes:

Advanced DDP configurations

Custom backend

For CPU training or debugging:

NCCL environment variables

Optimize NCCL performance:

Find available network interfaces

Performance considerations

Scaling efficiency

Due to gradient synchronization overhead, scaling efficiency decreases as you add more GPUs. Expect 80-90% efficiency on 8 GPUs, 60-70% on 64 GPUs.

Batch size tuning

Increase batch_size or gradient_accumulation_steps to:
  • Reduce gradient sync overhead
  • Improve GPU utilization
  • Maintain stable training

Memory optimization

If you run out of memory:
  1. Decrease batch_size
  2. Decrease block_size (context length)
  3. Enable gradient checkpointing (requires code modification)
  4. Use smaller model (n_layer, n_head, n_embd)

Troubleshooting

Common issues

  • Check network connectivity between nodes
  • Verify firewall allows traffic on master port
  • Try NCCL_DEBUG=INFO to see detailed logs
  • Increase timeout: NCCL_TIMEOUT=7200 (seconds)
  • Ensure all GPUs have the same memory
  • Check for memory leaks in data loading
  • Reduce batch_size or block_size
  • Benchmark interconnect with iperf3
  • Disable Infiniband if not available: NCCL_IB_DISABLE=1
  • Check for network congestion
  • Ensure deterministic operations are disabled (default)
  • Check if torch.manual_seed is set correctly
  • Verify all processes load the same initial checkpoint

Next steps

Reproducing GPT-2

Train a 124M parameter model with DDP

Finetuning

Finetune pretrained models on custom data