liveBook · Manning

Fully sharded data parallel

As explained in Deep Learning with PyTorch, Second Edition

A distributed-training method that shards model state across devices to reduce memory use.

Fully Sharded Data Parallel (FSDP) is a distributed-training technique for models too large for one GPU. It combines data and model parallelism by sharding parameters, gradients, and optimizer state across devices. section 16.12 section 16.7

Each device stores and computes only the subset needed locally. Before a forward computation, all_gather temporarily collects required weights; afterward, extra weights can be released. During backward, gradients are synchronized and returned to shards with reduce_scatter. Communication can overlap computation to limit overhead. section 16.7 section 16.7

A minimal setup specifies the modules to shard and the device mesh:

  • Create a model and initialize a two-device mesh with init_device_mesh(device, (2,)). section 16.7
  • Call fully_shard on each selected linear layer, then call it on the root model. section 16.7
  • Use torch.distributed.fsdp and optionally pass mesh to control the devices used for sharding. section 16.7

Sharding a simple model

from torch.distributed.fsdp import fully_shard model = SimpleModel() mesh = init_device_mesh(device, (2,)) for module in model.modules(): if isinstance(module, nn.Linear): fully_shard(module, mesh=mesh) fully_shard(model, mesh=mesh) section 16.7

Figure 16.10 Pseudocode for FSDP implementation
figure

Use the FSDP pseudocode figure to trace sharding and communication through the implementation. section 16.7

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains parameter sharding, all_gather, reduce_scatter, communication overlap, device meshes, and module-level configuration.
Examples
Uses a two-device mesh, linear-layer sharding, root-model sharding, and pseudocode illustrating FSDP implementation.
What you can do after
Operate/configure FSDP with torch.distributed.fsdp, fully_shard, and DeviceMesh for selected modules and complete models.
How it connects
Connects data parallelism, model parallelism, tensor parallelism, collective communication, DeviceMesh, and ZeRO-3.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.