fsdp: avoid double sharding of MoE experts when EP is enabled - #2833
fsdp: avoid double sharding of MoE experts when EP is enabled#2833CodersAcademy006 wants to merge 15 commits into
Conversation
|
Hi, thank you for this work.
|
|
@zhujian19891203 Thanks for testing this and for the detailed trace. You’re right: while the current change correctly skips expert parameters during FSDP parameter grouping to avoid double sharding, the same exclusion needs to be applied in the FSDP replacement path. In particular, I’ll push a follow-up fix shortly |
- Move nv-grouped-gemm from dev/lts extras to new moe extra - Users can now install megatron-core[dev] without build failures - Add comprehensive error messages with installation instructions - Update README with MoE dependencies documentation This resolves the installation failure when nv-grouped-gemm cannot build due to missing CUTLASS headers. Users who need MoE with grouped GEMM can now explicitly install it with megatron-core[moe], while others can install dev/lts extras without encountering build errors. Fixes NVIDIA#2541
…r RL workflows. Fixed imports, ensured native RL loop support, and validated no errors in touched files. Ready for review and CI on supported platforms.
…lag, gate all logic on --rl-amem-offload-during-rollout, encapsulate env setup, and update docs/examples per review
|
I can split AMem NCCL into a follow-up PR if reviewers prefer |
Hi @CodersAcademy006 — that's awesome! 🎉 Please open a separate PR for AMem NCCL integration. We’d also really appreciate it if you could include a brief design document outlining how this feature work in RL training and its expected performance impact. @HaochenYuan is our RL expert here can help review your code. |
…m-nccl flag, gate all logic on --rl-amem-offload-during-rollout, encapsulate env setup, and update docs/examples per review" This reverts commit ee9962c.
Remove all AMem NCCL-related code and documentation from this PR: - Delete megatron/core/amem_nccl.py - Delete examples/rl/train_with_amem.sh - Revert megatron/rl/rl_utils.py to upstream (remove AMem hooks) - Revert megatron/training/arguments.py to upstream (remove AMem flags) - Revert megatron/training/initialize.py to upstream (remove AMem setup) - Revert pyproject.toml to upstream (remove AMem dependencies) - Revert scripts/check_api_backwards_compatibility.py to upstream - Remove AMem documentation section from README.md This PR should focus only on FSDP/EP double-sharding fixes. AMem NCCL integration will be submitted as a separate PR as requested by reviewers.
- Add assert_not_fsdp_wrapped_ep_param() helper to enforce the invariant - Call assertion in _replace_param_with_distributed_if_needed() - Document invariant in MoE layer where expert_parallel_enabled is set This prevents future double-sharding regressions by making violations loud instead of silent.
|
Added a small invariant guard to ensure EP-owned parameters are never wrapped or replaced by FSDP in any code path. This should prevent future double-sharding regressions. |
| # Exclude expert-parallel params from FSDP bookkeeping | ||
| self.param_to_name = {} | ||
| self.raw_param = {} | ||
| from megatron.core.utils import is_ep_owned_param |
There was a problem hiding this comment.
Since Megatron-FSDP is also standalone pip-installable, mind guarding this with a try/except MCore import, or simply having these utilities live natively under Megatron-FSDP? (After all, this is an Megatron-FSDP-specific interaction with Megatron-LM.)
There was a problem hiding this comment.
@CodersAcademy006 Can you elaborate a bit more on the "double sharding"? Megatron-FSDP should be able to shard expert parameters per local expert per Expert TP rank. Are you suggesting that FSDP sharding is useless here due to the memory inflation of using FSDP with EP? (I also wonder about the loss curves you're seeing - since I believe if you detach from FSDP, no reductions would be happening on the DP-Shard / intra-DP domain right?)
#2772 seems to be resolved by a TE issue as well, looking like some bug that un-distributed the heavyweight optimizer state (which coincidentally, becomes a major issue as model size increases), so wanted clarity on whether that problem has already been solved in a less radical way.
Could you provide more technical justification on the problem between the interaction of MoE layers in MLM with Megatron-FSDP? 🙏🏻 Or maybe this PR is just out-dated and the root cause of the memory issue was resolved.
|
hey @cspades Thanks for the review. This PR is not addressing the TE Please correct me otherwise. |
|
When enabling EP, expert parameters may still have replicas. If the DP world size > the EP world size, some expert parameters will have data-parallel neighbors—this setup is known as Expert Data Parallelism (EDP). Applying FSDP on top of EDP can still yield meaningful memory savings. Conceptually, FSDP is transparent to all parameters. Each parameter maintains its original copy; we generally don't modify it directly—instead, we create shadow shards and unshard them as needed. The function that restores module parameters to their original form is located here: megatron_fsdp.py#L1159. Regarding your point, I believe our aim should be to maintain broad compatibility between FSDP and DTensor—fixing issues in the ecosystem where needed—rather than avoiding either FSDP or DTensor altogether. This PR unintentionally cancels out FSDP’s effect on expert parameters, which isn’t ideal. |
|
@CodersAcademy006 Just to conclude, maybe the ask is for better documentation on how to use EP-EDP-ETP distribution for MoE layers, or support for specific layers, with appropriate advice for how to actually reduce memory utilization? We definitely don't want users to try this code and come to the initial conclusion that the easiest way to get it to work is just to get rid of FSDP for MoE. Otherwise, good to close this PR? I'll also try to improve the user experience with EP outside of Megatron-LM or custom training FW's, so hopefully all of this is better documented. |
|
Thanks for the thorough review @cspades @shjwudp. You're right — I was reasoning about the pure EP case and missed the Given that #2772 was resolved upstream and the broader fix requires Also thank you for correcting my reasoning and helping me understand better with real exposure. |

When Expert Parallelism (EP) is enabled, MoE expert parameters are already
partitioned across ranks. However, current FSDP auto-wrapping logic still
wraps these expert modules, causing parameters to be effectively tracked
and sharded twice.
This results in:
What this PR does
by Expert Parallelism
expert_parallel_enabled) to MoE layersto avoid heuristic or name-based checks
Impact
Reproduction
This issue is reproducible on large MoE models and is reported in #2772.
The change removes the redundant sharding path responsible for the memory
regression.
Fixes #2772