1919import pytest
2020import torch
2121import torch .nn .functional as F
22+ from megatron .core .tensor_parallel .random import initialize_rng_tracker
2223from megatron .core .transformer .enums import AttnBackend
2324
2425from megatron .bridge .models .gpt_provider import GPTModelProvider
3839 ValidationConfig ,
3940 runtime_config_update ,
4041)
41- from megatron .bridge .training .fsdp_compat import MCORE_HAS_MEGATRON_FSDP_V2
4242from megatron .bridge .training .gpt_step import forward_step
4343from megatron .bridge .training .pretrain import pretrain
4444from megatron .bridge .training .state import GlobalState
@@ -422,19 +422,47 @@ def test_fsdp_pretrain_basic(self):
422422 torch .distributed .barrier ()
423423
424424 @pytest .mark .run_only_on ("GPU" )
425- @pytest .mark .skipif (not MCORE_HAS_MEGATRON_FSDP_V2 , reason = "MFSDP v2 is unavailable in this MCore revision" )
426425 def test_fsdp_v2_dense_hybrid_pretrain_smoke (self ):
427- """Train a dense two-layer HybridModel with the experimental MFSDP V2 path ."""
426+ """Train a dense two-layer HybridModel with MFSDP V2 in eager mode ."""
428427 initialize_distributed ()
429428 torch .distributed .barrier ()
430429
431430 cfg = create_fsdp_config_container (
432431 seq_length = 128 ,
433432 train_iters = 10 ,
434- optimizer = {"clip_grad" : 0.0 },
433+ optimizer = {"use_precision_aware_optimizer" : True },
434+ )
435+ cfg .model = create_dense_hybrid_smoke_model_config ()
436+ cfg .ddp .megatron_fsdp_version = 2
437+
438+ pretrain (cfg , forward_step )
439+
440+ torch .distributed .barrier ()
441+
442+ @pytest .mark .run_only_on ("GPU" )
443+ def test_fsdp_v2_dense_hybrid_cuda_graph_pretrain_smoke (self ):
444+ """Train a dense HybridModel with MFSDP V2 full-iteration and optimizer graphs."""
445+ initialize_distributed ()
446+ torch .distributed .barrier ()
447+
448+ # Replace the tracker left by eager tests with MCore's graph-safe RNG tracker.
449+ initialize_rng_tracker (use_cudagraphable_rng = True , force_reset = True )
450+
451+ cfg = create_fsdp_config_container (
452+ seq_length = 128 ,
453+ train_iters = 10 ,
454+ optimizer = {"use_precision_aware_optimizer" : True , "optimizer_cuda_graph" : True },
435455 )
436456 cfg .model = create_dense_hybrid_smoke_model_config ()
437457 cfg .ddp .megatron_fsdp_version = 2
458+ # Full-iteration graphs over a dense model: Megatron-LM #7075 made these work
459+ # under MFSDP v2. MoE stays out, its dispatch shapes are not capturable.
460+ cfg .model .cuda_graph_impl = "full_iteration"
461+ # The loss NaN check reads a GPU value on the host and cannot be captured.
462+ cfg .rerun_state_machine .check_for_nan_in_loss = False
463+ # Capturable TE FusedAdam requires the main-gradient and main-weight dtypes
464+ # to match (https://github.com/NVIDIA/TransformerEngine/issues/3358).
465+ cfg .ddp .megatron_fsdp_main_grads_dtype = torch .float32
438466
439467 pretrain (cfg , forward_step )
440468
@@ -455,6 +483,32 @@ def test_fsdp_v2_moe_ep2_pretrain_smoke(self):
455483 cfg .ddp .megatron_fsdp_version = 2
456484
457485 pretrain (cfg , forward_step )
486+
487+ torch .distributed .barrier ()
488+
489+ @pytest .mark .run_only_on ("GPU" )
490+ @pytest .mark .parametrize ("outer_dp_sharding_strategy" , ["no_shard" , "optim" ], ids = ["hsdp" , "hfsdp" ])
491+ def test_fsdp_v2_hybrid_dp_pretrain_smoke (self , outer_dp_sharding_strategy ):
492+ """Train the dense HybridModel over an outer data-parallel axis.
493+
494+ no_shard outer over the optim_grads_params inner axis is HSDP, optim outer is HFSDP.
495+ """
496+ initialize_distributed ()
497+ torch .distributed .barrier ()
498+
499+ cfg = create_fsdp_config_container (
500+ seq_length = 128 ,
501+ train_iters = 10 ,
502+ ddp = {
503+ "num_distributed_optimizer_instances" : 2 ,
504+ "outer_dp_sharding_strategy" : outer_dp_sharding_strategy ,
505+ },
506+ )
507+ cfg .model = create_dense_hybrid_smoke_model_config ()
508+ cfg .ddp .megatron_fsdp_version = 2
509+
510+ pretrain (cfg , forward_step )
511+
458512 torch .distributed .barrier ()
459513
460514 @pytest .mark .run_only_on ("GPU" )
0 commit comments