Skip to content

Commit 0c2c568

Browse files
committed
refactor(recipes): flatten GB200 recipe construction
Signed-off-by: chcui <chcui@nvidia.com>
1 parent 2410c65 commit 0c2c568

3 files changed

Lines changed: 75 additions & 22 deletions

File tree

src/megatron/bridge/recipes/nemotronh/gb200/nemotron_3_nano.py

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@
1414

1515
"""GB200 pretraining recipe for Nemotron 3 Nano."""
1616

17-
from megatron.bridge.recipes.nemotronh.nemotron_3_nano import nemotron_3_nano_pretrain_config
17+
import torch
18+
19+
from megatron.bridge import AutoBridge
20+
from megatron.bridge.recipes.common import _pretrain_common
21+
from megatron.bridge.training.comm_overlap import CommOverlapConfig
1822
from megatron.bridge.training.config import ConfigContainer
1923
from megatron.bridge.training.mixed_precision import get_mixed_precision_config
2024
from megatron.bridge.utils.cuda_graph import set_cuda_graph_modules
2125

2226

27+
_NEMOTRON_3_NANO_MODEL_ID = "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16"
28+
29+
2330
def nemotron_3_nano_pretrain_8gpu_gb200_bf16_config() -> ConfigContainer:
2431
"""Return the Nemotron 3 Nano BF16 pretraining config for eight GB200 GPUs.
2532
@@ -31,13 +38,21 @@ def nemotron_3_nano_pretrain_8gpu_gb200_bf16_config() -> ConfigContainer:
3138
Returns:
3239
GB200 BF16 pretraining configuration.
3340
"""
34-
cfg = nemotron_3_nano_pretrain_config()
41+
cfg = _pretrain_common()
42+
43+
cfg.model = AutoBridge.from_hf_pretrained(_NEMOTRON_3_NANO_MODEL_ID).to_megatron_provider(load_weights=False)
44+
cfg.tokenizer.tokenizer_model = _NEMOTRON_3_NANO_MODEL_ID
3545

3646
cfg.model.seq_length = 4096
3747
cfg.dataset.seq_length = 4096
48+
cfg.dataset.blend = None
49+
cfg.dataset.num_workers = 8
50+
cfg.dataset.mmap_bin_files = False
3851

3952
cfg.model.tensor_model_parallel_size = 1
4053
cfg.model.pipeline_model_parallel_size = 1
54+
cfg.model.pipeline_model_parallel_layout = None
55+
cfg.model.pipeline_dtype = torch.bfloat16
4156
cfg.model.virtual_pipeline_model_parallel_size = None
4257
cfg.model.context_parallel_size = 1
4358
cfg.model.sequence_parallel = False
@@ -51,28 +66,68 @@ def nemotron_3_nano_pretrain_8gpu_gb200_bf16_config() -> ConfigContainer:
5166
cfg.model.moe_shared_expert_overlap = False
5267
cfg.model.moe_router_force_load_balancing = False
5368

69+
cfg.train.train_iters = 39735
70+
cfg.train.global_batch_size = 3072
71+
cfg.train.micro_batch_size = 2
72+
cfg.train.manual_gc = False
73+
cfg.train.manual_gc_interval = 0
74+
75+
cfg.model.transformer_impl = "transformer_engine"
76+
5477
# Match the validated GB200 performance recipe's TE-scoped graph set.
5578
cfg.model.cuda_graph_impl = "transformer_engine"
5679
set_cuda_graph_modules(cfg.model, ["attn", "mamba", "moe_router", "moe_preprocess"])
5780
cfg.model.cuda_graph_warmup_steps = 3
5881
cfg.model.use_te_rng_tracker = True
5982
cfg.rng.te_rng_tracker = True
6083

61-
# Retain performance-recipe parity. Nemotron 3 Nano uses no positional
62-
# embeddings, so this remains a no-op unless the architecture changes.
84+
cfg.model.attention_backend = "fused"
85+
cfg.model.moe_router_fusion = False
86+
cfg.model.moe_permute_fusion = True
87+
cfg.model.moe_grouped_gemm = True
88+
cfg.model.cross_entropy_loss_fusion = True
6389
cfg.model.apply_rope_fusion = True
6490
cfg.model.cross_entropy_fusion_impl = "native"
91+
cfg.model.recompute_granularity = None
92+
cfg.model.recompute_modules = None
93+
cfg.model.fine_grained_activation_offloading = False
94+
cfg.model.offload_modules = None
95+
cfg.model.moe_router_padding_for_fp8 = False
6596
cfg.rerun_state_machine.check_for_nan_in_loss = False
66-
cfg.ddp.check_for_nan_in_grad = False
97+
98+
cfg.optimizer.use_precision_aware_optimizer = False
99+
cfg.optimizer.main_grads_dtype = torch.float32
100+
cfg.optimizer.main_params_dtype = torch.float32
101+
cfg.optimizer.exp_avg_dtype = torch.float32
102+
cfg.optimizer.exp_avg_sq_dtype = torch.float32
103+
cfg.optimizer.lr = 1.6e-3
104+
cfg.optimizer.weight_decay = 0.1
105+
cfg.optimizer.min_lr = 1.6e-5
106+
cfg.scheduler.lr_warmup_iters = 333
67107

68108
# Keep BF16 compute while reducing gradients in BF16 instead of FP32.
69109
cfg.mixed_precision = get_mixed_precision_config(cfg.mixed_precision)
70110
cfg.mixed_precision.grad_reduce_in_fp32 = False
111+
112+
cfg.comm_overlap = CommOverlapConfig(
113+
tp_comm_bootstrap_backend="nccl",
114+
tp_comm_overlap=False,
115+
)
116+
cfg.comm_overlap.delay_wgrad_compute = False
117+
cfg.comm_overlap.overlap_moe_expert_parallel_comm = False
118+
119+
cfg.checkpoint.save_interval = 200
120+
cfg.checkpoint.ckpt_assume_constant_structure = True
121+
cfg.checkpoint.dist_ckpt_strictness = "log_all"
122+
123+
cfg.ddp.overlap_grad_reduce = True
124+
cfg.ddp.overlap_param_gather = True
125+
cfg.ddp.check_for_nan_in_grad = False
126+
cfg.ddp.use_distributed_optimizer = True
71127
cfg.ddp.grad_reduce_in_fp32 = False
72128

73-
# TP communication overlap requires TP > 1 and sequence parallelism.
74-
if cfg.comm_overlap is not None:
75-
cfg.comm_overlap.tp_comm_overlap = False
129+
cfg.model.init_method_std = 0.0173
130+
cfg.model.use_fused_weighted_squared_relu = True
76131

77132
return cfg
78133

src/megatron/bridge/recipes/qwen/gb200/qwen35.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
from transformers import AutoConfig
2121

2222
from megatron.bridge import AutoBridge
23-
from megatron.bridge.models.gpt_provider import GPTModelProvider
24-
from megatron.bridge.models.hybrid.hybrid_provider import HybridModelProvider
2523
from megatron.bridge.recipes.common import _pretrain_common
2624
from megatron.bridge.training.comm_overlap import CommOverlapConfig
2725
from megatron.bridge.training.config import ConfigContainer
@@ -32,20 +30,15 @@
3230
_QWEN35_35B_A3B_BASE = "Qwen/Qwen3.5-35B-A3B-Base"
3331

3432

35-
def _qwen35_text_provider(model_id: str, architecture: str) -> GPTModelProvider | HybridModelProvider:
36-
"""Build a language-model provider from a unified Qwen3.5 config."""
37-
text_config = AutoConfig.from_pretrained(model_id).text_config
38-
# The nested text config intentionally omits ``architectures``. AutoBridge
39-
# needs it to select the registered causal-LM bridge instead of the VLM.
40-
text_config.architectures = [architecture]
41-
return AutoBridge.from_hf_config(text_config).to_megatron_provider(load_weights=False)
42-
43-
4433
def qwen35_text_9b_pretrain_8gpu_gb200_bf16_config() -> ConfigContainer:
4534
"""Return a text-only Qwen3.5-9B pretraining config for eight GB200 GPUs."""
4635
cfg = _pretrain_common()
4736

48-
cfg.model = _qwen35_text_provider(_QWEN35_9B_BASE, "Qwen3_5ForCausalLM")
37+
text_config = AutoConfig.from_pretrained(_QWEN35_9B_BASE).text_config
38+
# The nested text config intentionally omits ``architectures``. AutoBridge
39+
# needs it to select the registered causal-LM bridge instead of the VLM.
40+
text_config.architectures = ["Qwen3_5ForCausalLM"]
41+
cfg.model = AutoBridge.from_hf_config(text_config).to_megatron_provider(load_weights=False)
4942
cfg.tokenizer.tokenizer_model = _QWEN35_9B_BASE
5043
cfg.dataset.seq_length = 4096
5144
cfg.dataset.blend = None
@@ -114,7 +107,11 @@ def qwen35_text_35b_a3b_pretrain_8gpu_gb200_bf16_config() -> ConfigContainer:
114107
"""Return a text-only Qwen3.5-35B-A3B pretraining config for eight GB200 GPUs."""
115108
cfg = _pretrain_common()
116109

117-
cfg.model = _qwen35_text_provider(_QWEN35_35B_A3B_BASE, "Qwen3_5MoeForCausalLM")
110+
text_config = AutoConfig.from_pretrained(_QWEN35_35B_A3B_BASE).text_config
111+
# The nested text config intentionally omits ``architectures``. AutoBridge
112+
# needs it to select the registered causal-LM bridge instead of the VLM.
113+
text_config.architectures = ["Qwen3_5MoeForCausalLM"]
114+
cfg.model = AutoBridge.from_hf_config(text_config).to_megatron_provider(load_weights=False)
118115
cfg.tokenizer.tokenizer_model = _QWEN35_35B_A3B_BASE
119116
cfg.dataset.seq_length = 4096
120117
cfg.dataset.blend = None

tests/unit_tests/recipes/test_nemotronh_recipes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def to_megatron_provider(self, *args, **kwargs):
5959

6060
@pytest.fixture(autouse=True)
6161
def _patch_hf_backed_recipe_providers(monkeypatch: pytest.MonkeyPatch) -> None:
62-
"""Keep Super and Ultra recipe construction deterministic and offline."""
62+
"""Keep AutoBridge-backed recipe construction deterministic and offline."""
6363
for module_name in (
64+
"megatron.bridge.recipes.nemotronh.gb200.nemotron_3_nano",
6465
"megatron.bridge.recipes.nemotronh.nemotron_3_super",
6566
"megatron.bridge.recipes.nemotronh.nemotron_3_ultra",
6667
):

0 commit comments

Comments
 (0)