RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Custom Training Pipelines
  6. /Ch. 7
Custom Training Pipelines

07. Data Parallelism

Chapter 7 of 18 · 20 min
KEY INSIGHT

DDP's all-reduce synchronizes gradients after every backward pass—slow interconnects or large models increase sync overhead proportionally.

Data parallelism replicates the model across GPUs, splitting batches. It's the most common distributed strategy because it works with any model that fits on a single GPU.

DDP Fundamentals

DistributedDataParallel (DDP) replicates gradients across GPUs through all-reduce operations:

import torch
import torch.nn as nn
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP

def train_with_ddp(config):
    setup_distributed()
    local_rank = int(os.environ["LOCAL_RANK"])
    
    model = build_model(config).cuda(local_rank)
    model = DDP(model, device_ids=[local_rank])
    
    # All processes see the same initialization
    # Forward pass: each GPU processes batch_size // num_gpus samples
    # Backward pass: gradients are all-reduced across GPUs
    # Optimizer step: identical on all GPUs
    
    for epoch in range(config.epochs):
        for batch in train_loader:
            inputs = batch["input"].cuda(local_rank)
            targets = batch["target"].cuda(local_rank)
            
            outputs = model(inputs)
            loss = loss_fn(outputs, targets)
            
            loss.backward()  # Gradients synchronized automatically
            optimizer.step()
            optimizer.zero_grad()
            
            # Only rank 0 logs to avoid duplicate entries
            if local_rank == 0:
                log_metrics({"loss": loss.item()})
    
    cleanup_distributed()

Gradient Bucketing

DDP buckets gradients to overlap communication with computation. The bucket size affects performance—too small creates excessive communication overhead, too large wastes memory:

# Default bucket cap_mb=25MB; tune for your network bandwidth
model = DDP(model, device_ids=[local_rank], bucket_cap_mb=50)

Performance Metrics

Monitor these to diagnose DDP issues:

# Effective throughput
nvidia-smi --query-gpu=utilization.gpu,utilization.memory --format=csv

# Communication time (should be < 10% of step time)
# Check for "NCCL timeout" errors in logs

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Train a model with DDP on 2 GPUs and measure throughput per GPU. Compare to single-GPU throughput. The ratio should approach 2x—significantly lower indicates communication bottleneck.

← Chapter 6
Multi-GPU Training
Chapter 8 →
Model Parallelism