Skip to content

Commit 974afe6

Browse files
committed
feat(training): support MFSDP V2 expert parallelism
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
1 parent dd150c1 commit 974afe6

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/megatron/bridge/training/config.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,6 @@ def _validate_and_apply_megatron_fsdp_v2_configs(self) -> None:
11401140
"tensor_model_parallel_size",
11411141
"pipeline_model_parallel_size",
11421142
"context_parallel_size",
1143-
"expert_model_parallel_size",
11441143
)
11451144
configured_parallelisms = [
11461145
f"{name}={getattr(self.model, name)}"
@@ -1149,11 +1148,11 @@ def _validate_and_apply_megatron_fsdp_v2_configs(self) -> None:
11491148
]
11501149
if configured_parallelisms:
11511150
raise ValueError(
1152-
"MFSDP V2 currently supports DP-only training; unsupported settings: "
1153-
+ ", ".join(configured_parallelisms)
1151+
"MFSDP V2 requires TP=PP=CP=1; unsupported settings: " + ", ".join(configured_parallelisms)
11541152
)
1155-
if self.model.num_moe_experts is not None:
1156-
raise ValueError("MFSDP V2 does not currently support MoE models.")
1153+
if self.model.expert_model_parallel_size > 1:
1154+
if self.model.num_moe_experts is None:
1155+
raise ValueError("MFSDP V2 expert parallelism requires an MoE model.")
11571156
if self.model.virtual_pipeline_model_parallel_size is not None:
11581157
raise ValueError("MFSDP V2 does not currently support multiple model chunks.")
11591158
if self.dist.use_tp_pp_dp_mapping:

tests/functional_tests/test_groups/megatron_fsdp/test_megatron_fsdp.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pytest
2020
import torch
2121
import torch.nn.functional as F
22+
from megatron.core.transformer.enums import AttnBackend
2223

2324
from megatron.bridge.models.gpt_provider import GPTModelProvider
2425
from megatron.bridge.models.hybrid.hybrid_provider import HybridModelProvider
@@ -111,6 +112,19 @@ class DenseHybridSmokeModelProvider(HybridModelProvider):
111112
gradient_accumulation_fusion: bool = False
112113

113114

115+
@dataclass
116+
class MLAMoEHybridSmokeModelProvider(HybridModelProvider):
117+
"""Small MLA/MoE HybridModel configuration for the MFSDP V2 EP smoke test."""
118+
119+
attention_backend: AttnBackend = AttnBackend.auto
120+
seq_length: int = 128
121+
hidden_size: int = 128
122+
multi_latent_attention: bool = True
123+
hybrid_layer_pattern: str = "+E"
124+
num_moe_experts: int = 4
125+
expert_model_parallel_size: int = 2
126+
127+
114128
def create_fsdp_model_config(seq_length: int, bf16: bool = True, **kwargs) -> Llama3FSDPTestModelProvider:
115129
"""Create a standardized FSDP model configuration."""
116130
base_config = {
@@ -427,6 +441,23 @@ def test_fsdp_v2_dense_hybrid_pretrain_smoke(self):
427441

428442
torch.distributed.barrier()
429443

444+
@pytest.mark.run_only_on("GPU")
445+
def test_fsdp_v2_mla_moe_ep2_pretrain_smoke(self):
446+
"""Train a small MLA/MoE HybridModel with MFSDP V2 and EP=2."""
447+
initialize_distributed()
448+
torch.distributed.barrier()
449+
450+
cfg = create_fsdp_config_container(
451+
seq_length=128,
452+
train_iters=10,
453+
optimizer={"clip_grad": 0.0},
454+
)
455+
cfg.model = MLAMoEHybridSmokeModelProvider()
456+
cfg.ddp.megatron_fsdp_version = 2
457+
458+
pretrain(cfg, forward_step)
459+
torch.distributed.barrier()
460+
430461
@pytest.mark.run_only_on("GPU")
431462
def test_fsdp_pretrain_save_resume(self, tmp_path):
432463
"""

0 commit comments

Comments
 (0)