
Megatron FSDP
OfficialFreeOptimize distributed training with Megatron FSDP.
Free · Opens the source repo
What Megatron FSDP does
The Megatron FSDP skill provides an operational guide for enabling Megatron's Fully Sharded Data Parallel (FSDP) in the Megatron-Bridge framework. This skill is particularly useful for developers and researchers working with large-scale models who need to efficiently manage memory and computational resources during training. By leveraging FSDP, users can distribute the model's parameters across multiple GPUs, which helps in reducing memory overhead and improving training speed.
To utilize this skill, users can easily configure their training scripts by setting specific flags in the configuration. For instance, enabling FSDP requires setting cfg.dist.use_megatron_fsdp and cfg.ddp.use_megatron_fsdp to True, along with specifying the data parallel sharding strategy. The skill also provides example configurations and performance harness scripts to facilitate quick setup and testing. Users can validate their configurations through existing smoke tests that ensure the proper functioning of the FSDP setup.
However, users should be aware of certain pitfalls when using this skill. Common issues include default checkpoint formats that may not be compatible with FSDP and the mutual exclusivity of Megatron FSDP and FSDP2. Additionally, specific settings related to CPU offloading and CUDA device connections may need to be adjusted based on the hardware being used. This skill is essential for those looking to optimize their distributed training workflows with Megatron FSDP.
When to use it
Use this skill when setting up distributed training for large models with Megatron-Bridge, particularly when memory efficiency is a concern.
When not to use it
This skill is not suitable for users who are not working with Megatron-Bridge or those who do not require FSDP for their training processes.
What you can build with it
Setting Up Distributed Training
Quickly configure your training scripts to enable Megatron FSDP for efficient distributed training.
Validating FSDP Configurations
Run smoke tests to ensure your FSDP setup is functioning correctly before starting extensive training.
Optimizing Memory Usage
Utilize Megatron FSDP to manage GPU memory more effectively during large model training.
How to install Megatron FSDP
View source1. Install with the skills CLI
npx skills add nvidia/skills/nemo-mbridge-perf-megatron-fsdp --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by nvidiaMegatron FSDP Skill
For stable background and recommendation level, see:
- @docs/training/megatron-fsdp.md
- @skills/nemo-mbridge-perf-megatron-fsdp/card.yaml
Enablement
Minimal Megatron FSDP override in Bridge:
cfg.dist.use_megatron_fsdp = True
cfg.ddp.use_megatron_fsdp = True
cfg.ddp.data_parallel_sharding_strategy = "optim_grads_params"
cfg.ddp.average_in_collective = False
cfg.checkpoint.ckpt_format = "fsdp_dtensor"
Example recipe fixup:
cfg = llama3_8b_pretrain_config()
cfg.dist.use_megatron_fsdp = True
cfg.ddp.use_megatron_fsdp = True
cfg.ddp.data_parallel_sharding_strategy = "optim_grads_params"
cfg.ddp.average_in_collective = False
cfg.checkpoint.ckpt_format = "fsdp_dtensor"
cfg.checkpoint.save = "/tmp/fsdp_ckpts"
cfg.checkpoint.load = None
Performance harness note:
python scripts/performance/launch.py --use_megatron_fsdp true
Code Anchors
Bridge config definition:
use_megatron_fsdp: bool = False
"""Use Megatron's Fully Sharded Data Parallel. Cannot be used together with use_torch_fsdp2."""
use_torch_fsdp2: bool = False
"""Use the torch FSDP2 implementation. FSDP2 is not currently working with Pipeline Parallel.
It is still not in a stable release stage, and may therefore contain bugs or other
potential issues."""
Bridge validation:
if self.dist.use_megatron_fsdp and self.dist.use_torch_fsdp2:
raise ValueError(...)
...
assert not self.dist.use_tp_pp_dp_mapping, "use_tp_pp_dp_mapping is not supported with Megatron FSDP"
...
assert self.checkpoint.ckpt_format == "fsdp_dtensor", (
"Megatron FSDP only supports fsdp_dtensor checkpoint format"
)
Runtime wrapper selection:
if use_megatron_fsdp:
DP = FullyShardedDataParallel
elif use_torch_fsdp2:
DP = TorchFullyShardedDataParallel
else:
DP = DistributedDataParallel
...
DP(
config=get_model_config(model_chunk),
ddp_config=ddp_config,
module=model_chunk,
...
pg_collection=pg_collection,
)
Perf harness overrides:
recipe.ddp.use_megatron_fsdp = True
recipe.ddp.data_parallel_sharding_strategy = "optim_grads_params"
recipe.ddp.keep_fp8_transpose_cache = False
recipe.ddp.average_in_collective = False
...
recipe.checkpoint.load = None
Pitfalls
- Public recipes often expose
use_megatron_fsdpbut still default tockpt_format="torch_dist". If save/load is enabled, switch tofsdp_dtensor. use_torch_fsdp2exists, but on the validated branch Bridge still fails before training because_ddp_wrappassespg_collection.- CPU offloading is only valid when
pipeline_model_parallel_size == 1and activation recomputation is disabled. - Upstream warns that FSDP and TP/CP can want different
CUDA_DEVICE_MAX_CONNECTIONSsettings on Hopper and earlier. - Megatron FSDP and FSDP2 are mutually exclusive.
Verification
Use the existing 2-GPU functional smoke test:
CUDA_VISIBLE_DEVICES=0,1 uv run python -m torch.distributed.run --nproc_per_node=2 \
-m pytest tests/functional_tests/training/test_megatron_fsdp.py::TestMegatronFSDP::test_fsdp_pretrain_basic -v -s
Success criteria:
- Pytest reports
1 passed - The log shows finite loss at the last iteration
- The run finishes without a checkpoint format assertion
Frequently asked questions about Megatron FSDP
Similar skills
Heap Snapshot Analysis
Investigate V8 heap snapshots for memory issues.
VS Code Performance Workflow
Automate performance investigations in VS Code.
Memory Leak Audit
Prevent memory leaks with effective coding patterns.
CPU Profile Analysis
Analyze V8 and Chrome performance profiles for optimization.
Chat Performance Testing
Benchmark and validate chat UI performance in VS Code.
Vercel React Best Practices
Optimize your React and Next.js applications for performance.
