Skip to content

[megatron] feat: support Megatron-FSDP mode for Megatron backend#5423

Open
conver334 wants to merge 30 commits intoverl-project:mainfrom
conver334:megatron-fsdp
Open

[megatron] feat: support Megatron-FSDP mode for Megatron backend#5423
conver334 wants to merge 30 commits intoverl-project:mainfrom
conver334:megatron-fsdp

Conversation

@conver334
Copy link
Copy Markdown
Contributor

@conver334 conver334 commented Feb 27, 2026

What does this PR do?

Support using Megatron-FSDP for SFT and RL.

Add Megatron-FSDP as a new training backend option for the Megatron engine. This is implementation of #5244 .

Key changes:

  • Add example scripts for GRPO examples/grpo_trainer/run_qwen2-7b_math_megatron_fsdp.sh and SFT examples/sft/gsm8k/run_qwen_megatron_fsdp.sh. Run these following the user guide docs/examples/megatron_fsdp_example.rst
  • Add use_megatron_fsdp config flag to McoreEngineConfig, enabling using Megatron-FSDP via a single config option.
  • When enabled, automatically configure the required DDP settings (distributed optimizer, Zero-3 sharding strategy, grad reduce overlap) with sensible defaults, while still allowing fine-grained override via override_ddp_config.

Checklist Before Starting

  • Search for similar PRs. Paste at least one query link here: [megatron, model] feat: qwen3.5 example  #5381
  • Format the PR title as [{modules}] {type}: {description} (This will be checked by the CI)
    • {modules} include fsdp, megatron, veomni, sglang, vllm, rollout, trainer, ci, training_utils, recipe, hardware, deployment, ray, worker, single_controller, misc, perf, model, algo, env, tool, ckpt, doc, data, cfg, reward, fully_async, one_step_off
    • If this PR involves multiple modules, separate them with , like [megatron, fsdp, doc]
    • {type} is in feat, fix, refactor, chore, test
    • If this PR breaks any API (CLI arguments, config, function signature, etc.), add [BREAKING] to the beginning of the title.
    • Example: [BREAKING][fsdp, megatron] feat: dynamic batching

Test

Loss is normal in SFT. Tested on 8*H100 with Moonlight-16B-A3B-Instruct, GSM8K SFT dataset
moonlight_correct

Loss is normal in SFT. Tested on 8*H100 with Qwen2.5-Math-7B, GSM8K SFT dataset
qwen7b

Reward is normal in GRPO. Tested on 8*H100 with Qwen2.5-Math-7B, GSM8K
qwen7b-grpo-reward

MFU
W B Chart 2026_4_13 16_57_39

API and Usage Example

Enable Megatron-FSDP by setting three config flags:

    actor_rollout_ref.actor.megatron.use_mbridge=True \
    actor_rollout_ref.actor.megatron.vanilla_mbridge=False \
    actor_rollout_ref.actor.megatron.use_megatron_fsdp=True \

The FSDP-specific DDP settings (sharding strategy, overlap, etc.) are auto-configured with defaults. Advanced users can override them:

    actor_rollout_ref.actor.megatron.override_ddp_config.data_parallel_sharding_strategy=optim_grads \
    actor_rollout_ref.actor.megatron.override_ddp_config.overlap_grad_reduce=False \

Design & Code Changes

Megatron-FSDP use the same training loop as Megatron.
Conversion between HuggingFace format and Megatron-FSDP DTensor is implemented via NVIDIA-NeMo/Megatron-Bridge#1910.

Checklist Before Submitting

Important

Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for Megatron-FSDP as a new training backend, including configuration flags, automatic DDP settings, and state lifecycle management. The overall implementation is sound, but I've identified a critical issue in the FSDP parameter synchronization logic that could lead to inconsistent model states during inference. Additionally, the new example script has a hardcoded model path, which impacts its portability. I've provided suggestions to fix these issues.

Comment thread verl/utils/megatron_utils.py Outdated
Comment thread examples/grpo_trainer/run_qwen2-7b_math_megatron_fsdp.sh Outdated
@ISEEKYAN ISEEKYAN marked this pull request as draft February 27, 2026 06:41
@ISEEKYAN ISEEKYAN changed the title [BREAKING][megatron] feat: support Megatron-FSDP as a new training backend [megatron] feat: support Megatron-FSDP mode for Megatron backend Mar 4, 2026
conver334 and others added 3 commits April 10, 2026 01:01
Resolve 3 conflicts:
- megatron_utils.py: keep FSDP ddp_config in main's cleaner structure;
  take main's GDN-aware grad buffer handling
- megatron_checkpoint_manager.py: apply FSDP while-loop unwrap in main's
  should_generate_model_sections structure; use main's refactored PEFT
  save path with FSDP skip for HF checkpoint
- run_sft_engine.sh: keep both megatron_fsdp and automodel backends

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@yxs
Copy link
Copy Markdown
Collaborator

yxs commented Apr 10, 2026

I tested both SFT and GRPO (without ALL_OFFLOAD) on 8×H100 with Qwen2.5-0.5B, Megatron-LM main, and Megatron-Bridge PR #1910:

  • SFT: loss decreasing normally (0.695 → 0.543 over 21 steps)
  • GRPO: 447 steps completed, reward improving (0.006 → 0.72), DTensor weight export works correctly

@conver334 All looks good to me now.

@conver334 conver334 marked this pull request as ready for review April 13, 2026 09:10
conver334 and others added 5 commits April 22, 2026 23:34
# Conflicts:
#	verl/workers/megatron_workers.py
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Simiao Zuo <simiaoz@nvidia.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Simiao Zuo <simiaoz@nvidia.com>
Update the Megatron-Bridge pin in the Megatron-FSDP CI job and example doc
to 6fea5bb (merge commit of NVIDIA-NeMo/Megatron-Bridge#3512), which is the
preferred version now that the HF<->Megatron-FSDP weight conversion PR has
landed. Also drop the now-merged Megatron-LM PR3191 and Megatron-Bridge
PR1910 checkouts from the example doc in favor of the same pinned commits
used in CI, and refresh the doc's "Last updated" date.

Co-authored-by: Claude
conver334 and others added 14 commits April 27, 2026 00:48
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: conver334 <conver334@gmail.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: conver334 <conver334@gmail.com>
Co-authored-by: OpenAI Codex
Co-authored-by: OpenAI Codex <codex@openai.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
Save and load Megatron-FSDP trainer checkpoints through PyTorch DCP, including model, optimizer, scheduler, and RNG state. Preserve HF export support through Megatron-Bridge and document the current example assumptions.

Co-authored-by: OpenAI Codex <codex@openai.com>
Signed-off-by: conver334 <conver334@gmail.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants