Nemo Mbridge Perf Megatron Fsdp

nvidia/skills/skills/nemo-mbridge-perf-megatron-fsdp

作者 nvidiacf5224d14250Apache-2.03.5K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库今天更新

Operational guide for enabling Megatron FSDP in Megatron-Bridge, including config knobs, code anchors, pitfalls, and verification.

仅含说明DevOps & Cloud
AI 生成的概览

在 Megatron-Bridge 中启用 Megatron FSDP 的操作指南,涵盖配置项、代码锚点、常见陷阱与验证方法。

功能
该技能提供在 Megatron-Bridge 中开启 Megatron FSDP 的操作指南。它列出所需的配置覆盖项,指向 Bridge 配置与运行时封装中的相关代码锚点,说明常见陷阱,并给出使用现有 2 卡冒烟测试的验证流程。它产出的是指导与配置片段,而非可执行产物。
适用场景
适用于从 DDP 切换到基于 FSDP 的数据并行时,或在排查由 FSDP 配置变更引起的显存溢出或性能回退时。也适用于处理 use_megatron_fsdp、data_parallel_sharding_strategy 或分片数据并行等相关设置。
运行要求
需要访问 Megatron-Bridge 代码库及其文档。验证过程使用 pytest、torch.distributed 和 uv,并需要至少两块 GPU。该技能不附带脚本,仅为说明性内容。

Megatron 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:

python
cfg.dist.use_megatron_fsdp = Truecfg.ddp.use_megatron_fsdp = Truecfg.ddp.data_parallel_sharding_strategy = "optim_grads_params"cfg.ddp.average_in_collective = Falsecfg.checkpoint.ckpt_format = "fsdp_dtensor"

Example recipe fixup:

python
cfg = llama3_8b_pretrain_config()cfg.dist.use_megatron_fsdp = Truecfg.ddp.use_megatron_fsdp = Truecfg.ddp.data_parallel_sharding_strategy = "optim_grads_params"cfg.ddp.average_in_collective = Falsecfg.checkpoint.ckpt_format = "fsdp_dtensor"cfg.checkpoint.save = "/tmp/fsdp_ckpts"cfg.checkpoint.load = None

Performance harness note:

bash
python scripts/performance/launch.py --use_megatron_fsdp true

Code Anchors

Bridge config definition:

148:154:src/megatron/bridge/training/config.py
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 otherpotential issues."""

Bridge validation:

1533:1578:src/megatron/bridge/training/config.py
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:

217:243:src/megatron/bridge/models/common/unimodal.py
if use_megatron_fsdp:    DP = FullyShardedDataParallelelif use_torch_fsdp2:    DP = TorchFullyShardedDataParallelelse:    DP = DistributedDataParallel...DP(    config=get_model_config(model_chunk),    ddp_config=ddp_config,    module=model_chunk,    ...    pg_collection=pg_collection,)

Perf harness overrides:

74:98:scripts/performance/utils/overrides.py
recipe.ddp.use_megatron_fsdp = Truerecipe.ddp.data_parallel_sharding_strategy = "optim_grads_params"recipe.ddp.keep_fp8_transpose_cache = Falserecipe.ddp.average_in_collective = False...recipe.checkpoint.load = None

Pitfalls

  1. Public recipes often expose use_megatron_fsdp but still default to ckpt_format="torch_dist". If save/load is enabled, switch to fsdp_dtensor.
  2. use_torch_fsdp2 exists, but on the validated branch Bridge still fails before training because _ddp_wrap passes pg_collection.
  3. CPU offloading is only valid when pipeline_model_parallel_size == 1 and activation recomputation is disabled.
  4. Upstream warns that FSDP and TP/CP can want different CUDA_DEVICE_MAX_CONNECTIONS settings on Hopper and earlier.
  5. Megatron FSDP and FSDP2 are mutually exclusive.

Verification

Use the existing 2-GPU functional smoke test:

bash
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

来源与署名

来源:nvidia/skills位于skills/nemo-mbridge-perf-megatron-fsdp提交cf5224d

许可证: Apache-2.0

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架