DDP and FSDP architecture
Quick answer
PyTorch DistributedDataParallel (DDP) synchronizes gradients across model replicas. Fully Sharded Data Parallel (FSDP) distributes persistent model state across ranks, reducing per-rank persistent memory at the cost of communication and temporary materialization. Examples use small synthetic inputs and Python or local CPU PyTorch. ByteMind execution is unverified.
Simulate all-gather
shards=[[1,2],[3,4]]print([x for shard in shards for x in shard])Expected output (locally tested): [1, 2, 3, 4]. Both ranks would receive the ordered full vector.
How it works
DDP normally uses one process per device, an initialized process group and compatible model parameter order; it synchronizes gradients during backward and each optimizer applies the same update. DDP does not shard input data itself. FSDP FULL_SHARD and FSDP2 fully_shard use parameter all-gathers for computation and gradient reduce-scatter for sharded updates; optimizer state can be sharded too. Exact APIs and memory behavior depend on release, sharding strategy, wrapping and prefetch. All-gather reconstructs the ordered chunks; reduce-scatter sums corresponding chunks and gives each rank its assigned result chunk. In a toy model with 120 bytes of parameters, 120 bytes of gradients and 240 bytes of optimizer state, ideal four-way sharding yields 120 persistent bytes per rank versus 480 for full replication; this excludes activations, temporary gathered tensors and allocator overhead. Checkpoint format, rank topology and state-dict APIs require separate compatibility testing. Examples below simulate collectives as list operations only; no process group is launched.
Simulate reduce-scatter
local=[[1,2,3,4],[10,20,30,40]]reduced=[sum(col) for col in zip(*local)]print([reduced[:2],reduced[2:]])Expected output (locally tested): [[11, 22], [33, 44]]. Each rank receives one chunk of the elementwise sum.
Common mistakes
Do not treat ideal persistent state savings as measured peak device memory. Do not initialize an accelerator process group in an unrestricted lesson runner. Avoid assuming a state dict from one sharding layout can be restored by another without conversion.
Practice and next steps
Calculate persistent state per rank for two versus four ranks and explain why gathered parameter buffers still need memory.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
DDP and FSDP architecture — knowledge check
Apply ddp and fsdp architecture to small examples and debugging scenarios.
What does ordinary DDP synchronize?
Does DDP automatically partition examples?
What does reduce-scatter deliver?
Why may FSDP peak memory exceed ideal sharded state size?
What must be checked before restoring sharded checkpoints?