pytorch-fsdp

Guides Fully Sharded Data Parallel training with PyTorch FSDP and FSDP2.

Updated Jun 5, 2026
One-click install
npx skills add https://github.com/xu1713/openhorse --skill pytorch-fsdp-xu1713
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytorch-fsdp
Source: https://github.com/xu1713/openhorse/tree/main/openhorse/openhorse/optional-skills/mlops/pytorch-fsdp
Command: npx skills add https://github.com/xu1713/openhorse --skill pytorch-fsdp-xu1713

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch>=2.0, transformers, and includes references (resource) components.

What problem does it solve? Training large models exhausts GPU memory when parameters, gradients, and optimizer states are replicated on every device. This Skill provides expert guidance for PyTorch FSDP, which shards model state across data parallel workers to reduce per-device memory usage. ## Core Features & Use Cases - Parameter Sharding Guidance: Explains FSDP1 FullyShardedDataParallel and FSDP2 fully_shard APIs, including DTensor-based per-parameter sharding and bottom-up module application. - Distributed Communication Setup: Covers torch.distributed initialization, backend selection (NCCL, Gloo, MPI), device meshes, and the Join context manager for uneven inputs. - Memory and Performance Techniques: Documents mixed precision, CPU offloading, prefetching control, and communication/computation overlap for large-scale training. - Use Case: When migrating a multi-billion parameter model from DDP to FSDP2, use this Skill to correctly apply fully_shard layer by layer, configure the device mesh, and initialize the optimizer on DTensor parameters. ## Quick Start Ask how to shard a Transformer model across 8 GPUs using PyTorch FSDP2 fully_shard with mixed precision.

Frequently Asked Questions about pytorch-fsdp

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I apply PyTorch FSDP2 fully_shard to a model?

Call fully_shard on each submodule bottom-up before applying it to the root module, so each layer forms its own all-gather group. The optimizer must be initialized with the resulting DTensor parameters, and forward must be invoked via model(input) to trigger the all-gather hooks.

What is the difference between PyTorch FSDP1 and FSDP2?

FSDP2 uses DTensor-based dim-0 per-parameter sharding instead of FSDP1's flat-parameter sharding, giving a simpler sharding representation and communication-free sharded state dicts. FSDP2 also avoids record_stream memory management and exposes manual prefetching and collective scheduling APIs.

Which torch.distributed backend should I use for GPU training?

Use the NCCL backend for distributed training with CUDA GPUs, as it provides the best performance and supports InfiniBand and GPUDirect. Use Gloo for CPU training or as a fallback if NCCL encounters problems.

How do I handle uneven inputs in distributed training?

Use the generic Join context manager from torch.distributed.algorithms, wrapping your Joinable objects such as DDP models and ZeroRedundancyOptimizer. It shadows collective communications of joined processes so training on uneven inputs does not hang or error.

Why does FSDP training hang on exit?

Hangs occur when destroy_process_group is not called by all ranks, since ProcessGroupNCCL's destructor calls ncclCommAbort which must run collectively in a consistent order. Call destroy_process_group() once per trainer process near the end of main().