Pytorch fsdp2

From Orchestra-Research/AI-Research-SKILLs · View interactive version

Pytorch fsdp2 is an AI agent skill from the GitHub repository Orchestra-Research/AI-Research-SKILLs. Adds PyTorch FSDP2 (fully_shard) to training scripts with correct init, sharding, mixed precision/offload config, and distributed checkpointing. Use when models exceed single-GPU memory or when you need DTensor-based sharding with DeviceMesh.

How to use this skill

Skill file

Skill: Use PyTorch FSDP2 (`fully_shard`) correctly in a training script

This skill teaches a coding agent how to add PyTorch FSDP2 to a training loop with correct initialization, sharding, mixed precision/offload configuration, and checkpointing.

FSDP2 in PyTorch is exposed primarily via torch.distributed.fsdp.fully_shard and the FSDPModule methods it adds in-place to modules. See: references/pytorch_fully_shard_api.md, references/pytorch_fsdp2_tutorial.md.


When to use this skill

Use FSDP2 when:

Avoid (or be careful) if:

Alternatives (when FSDP2 is not the best fit)

Reference: references/pytorch_ddp_notes.md, references/pytorch_fsdp1_api.md.


Contract the agent must follow

  1. Launch with torchrun and set the CUDA device per process (usually via LOCAL_RANK).
  2. Apply fully_shard() bottom-up, i.e., shard submodules (e.g., Transformer blocks) before the root module.
  3. Call model(input), not model.forward(input), so the FSDP2 hooks run (unless you explicitly unshard() or register the forward method).
  4. Create the optimizer after sharding and make sure it is built on the DTensor parameters (post-fully_shard).
  5. Checkpoint using Distributed Checkpoint (DCP) or the distributed-state-dict helpers, not naïve torch.save(model.state_dict()) unless you deliberately gather to full tensors.

(Each of these rules is directly described in the official API docs/tutorial; see references.)


Step-by-step procedure

0) Version & environment sanity

Reference: references/pytorch_fsdp2_tutorial.md (launch commands and setup), references/pytorch_fully_shard_api.md (user contract).


1) Initialize distributed and set device

Minimal, correct pattern:

Reference: references/pytorch_device_mesh_tutorial.md (why DeviceMesh exists & how it manages process groups).


2) Build model on meta device (recommended for very large models)

For big models, initialize on meta, apply sharding, then materialize weights on GPU:

Reference: references/pytorch_fsdp2_tutorial.md (migration guide shows this flow explicitly).


3) Apply `fully_shard()` bottom-up (wrapping policy = “apply where needed”)

Do not only call fully_shard on the topmost module.

Recommended sharding pattern for transformer-like models:

Why:

Reference: references/pytorch_fully_shard_api.md (bottom-up requirement and why).


4) Configure `reshard_after_forward` for memory/perf trade-offs

Default behavior:

Heuristics:

Reference: references/pytorch_fully_shard_api.md (full semantics).


5) Mixed precision & offload (optional but common)

FSDP2 uses:

Rules of thumb:

Reference: references/pytorch_fully_shard_api.md (MixedPrecisionPolicy / OffloadPolicy classes).


6) Optimizer, gradient clipping, accumulation

Gradient clipping:

Reference: references/pytorch_fsdp2_tutorial.md.


7) Checkpointing: prefer DCP or distributed state dict helpers

Two recommended approaches:

A) Distributed Checkpoint (DCP) — best default

B) Distributed state dict helpers

Avoid:

References:


Workflow checklists (copy-paste friendly)

Workflow A: Retrofit FSDP2 into an existing training script

Reference: references/pytorch_fsdp2_tutorial.md, references/pytorch_fully_shard_api.md, references/pytorch_device_mesh_tutorial.md, references/pytorch_dcp_recipe.md.

Workflow B: Add DCP save/load (minimal pattern)

Reference: references/pytorch_dcp_recipe.md.

Debug checklist (what the agent should check first)

  1. All ranks on distinct GPUs?

If not, verify torch.cuda.set_device(LOCAL_RANK) and your torchrun flags.

  1. Did you accidentally call forward() directly?

Use model(input) or explicitly unshard() / register forward.

  1. Is fully_shard() applied bottom-up?

If only root is sharded, expect worse memory/perf and possible confusion.

  1. Optimizer created at the right time?

Must be built on DTensor parameters after sharding.

  1. Checkpointing path consistent?

Common issues and fixes

Reference: references/pytorch_fully_shard_api.md, references/pytorch_fsdp2_tutorial.md.


Minimal reference implementation outline (agent-friendly)

The coding agent should implement a script with these labeled blocks:

Concrete examples live in references/pytorch_examples_fsdp2.md and the official tutorial reference.


References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.