liveBook · Manning

DistributedDataParallel

As explained in Designing Deep Learning Systems: A guide for software engineers

PyTorch's DistributedDataParallel library component that runs a model across processes or machines and coordinates distributed updates.

DistributedDataParallel (DDP) wraps a PyTorch model so synchronized data-parallel training can run across processes on one machine or across machines. Each process joins a training group, while DDP coordinates distributed communication, gradient aggregation, and local parameter updates. section 4.2.3

A typical DDP setup has these responsibilities:

  • Initialize the process group: provide each process's rank, the total world size, and the master's address and port; processes wait until the group is complete. section 4.2.3
  • Place the model on its device and wrap it with DDP, such as DDP(model, device_ids=[rank]). section 4.2.3
  • Run the forward pass and call backward; when backward returns, parameter gradients contain synchronized tensors across workers. section 4.2.3
  • Tune gradient bucket size when needed: smaller or larger buckets trade off device saturation against network saturation during synchronization. section 4.2.2
def setup(rank, world_size):
  os.environ['MASTER_ADDR'] = 'xxxx'
  os.environ['MASTER_PORT'] = 'xxx'
 
  # initialize the process group, "gloo" is one of the communication 
  # backends Pytorch supports, it also supports MPI and NCCL. 
  # rank is the process’s rank, it's a globally unique id 
  # for this process. rank=0 means  master process.
  # world_size is the total number of processes in this training group.
  dist.init_process_group("gloo", rank=rank, world_size=world_size)
 
def cleanup():
  dist.destroy_process_group()

Use the setup listing to inspect environment configuration, process-group initialization, supported communication backends, and cleanup. section 4.2.3

import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
 
# create model and move it to GPU
model = DpModel().to(device)
 
# wrap the model with DDP
ddp_model = DDP(model, device_ids=[rank])
outputs = ddp_model(data)                    #1
 
# compute the loss and sync gradient with other workers.
# when 'backward' function returns, the param.grad already 
# contains synchronized gradient tensor
loss_fn(outputs, labels).backward()

Use the model listing to see device placement, DDP wrapping, forward execution, and synchronized gradients after backward. section 4.2.3

How Designing Deep Learning Systems: A guide for software engineers covers it

How deep it goes
Explains process registration, model wrapping, gradient synchronization, and bucket tuning without descending into custom synchronization internals.
Examples
Uses process-group setup, GPU model wrapping, synchronized backward, and gradient-bucket sizing as implementation examples.
What it assumes
Assumes single-device training code and introduces distributed process registration, ranks, world size, and master addressing.
What you can do after
Apply it via PyTorch by initializing a process group, wrapping a model with DDP, and training with synchronized gradients.
How it connects
Connects distributed training with data parallelism, process groups, gradient aggregation, and communication backends.

Also explained in

Compare how 2 books explain DistributedDataParallel →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Designing Deep Learning Systems: A guide for software engineers. The section links point to the parts of the book it draws on.