forked from NVIDIA-NeMo/NeMo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretrain_vlm_llama4_e16.py
More file actions
162 lines (139 loc) · 5.22 KB
/
pretrain_vlm_llama4_e16.py
File metadata and controls
162 lines (139 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from os.path import basename, splitext
import nemo_run as run
from nemo.collections.common.tokenizers import AutoTokenizer
from nemo.collections.llm.recipes.precision.mixed_precision import bf16_with_fp8_mixed
from nemo.collections.vlm.recipes.llama4_omni_e16 import pretrain_recipe
from nemo.lightning.run.plugins import NsysPlugin, PerfEnvPlugin
from ..argument_parser import parse_cli_args
from ..executors import slurm_executor
from ..helpers import args_sanity_check, get_user_configs, set_exp_logging_configs, set_primary_perf_configs
def override_recipe_configs(
args: str,
num_nodes: int,
mbs: int,
gbs: int,
tp_size: int,
pp_size: int,
cp_size: int,
vp_size: int,
ep_size: int,
etp_size: int,
enable_cuda_graphs: bool,
):
"""
Llama4 16-Experts (Scout) VLM pre-train recipe aimed at achieving best possible performance.
NOTE: Use fp8 precision training with caution. It might not give desirable results.
"""
recipe = pretrain_recipe(performance_mode=True)
recipe.data.tokenizer = run.Config(
AutoTokenizer, pretrained_model_name='meta-llama/Llama-4-Scout-17B-16E-Instruct'
)
recipe = set_primary_perf_configs(
recipe,
"pre_train",
num_nodes,
args.gpus_per_node,
mbs,
gbs,
args.max_steps,
tp_size,
pp_size,
cp_size,
vp_size,
ep_size,
etp_size,
enable_cuda_graphs=enable_cuda_graphs,
compute_dtype=args.compute_dtype,
use_mcore_fsdp=args.use_mcore_fsdp,
use_fsdp_double_buffer=args.use_fsdp_double_buffer,
use_user_buffer_registration=args.use_user_buffer_registration,
)
recipe = set_exp_logging_configs(
recipe,
"pre_train",
"vlm",
"vlm_llama4",
args.tensorboard,
args.wandb,
args.wandb_prj_name,
args.wandb_job_name,
)
# compute dtype configs
if args.compute_dtype.lower() == "fp8":
recipe.trainer.plugins = bf16_with_fp8_mixed()
recipe.trainer.plugins.grad_reduce_in_fp32 = False
recipe.model.config.language_transformer_config.cross_entropy_fusion_impl = "te"
recipe.model.config.language_transformer_config.cross_entropy_loss_fusion = True
recipe.model.config.language_transformer_config.apply_rope_fusion = True
recipe.model.config.language_transformer_config.moe_permute_fusion = True
recipe.model.config.vision_transformer_config.gradient_accumulation_fusion = True
# enable cudagraph
recipe.model.config.vision_transformer_config.enable_cuda_graph = True
recipe.model.config.enable_cuda_graph = True
recipe.trainer.strategy.use_te_rng_tracker = True
recipe.model.config.language_transformer_config.enable_cuda_graph = enable_cuda_graphs
return recipe
if __name__ == "__main__":
args = parse_cli_args().parse_args()
args_sanity_check(args)
kwargs = get_user_configs(args.gpu.lower(), "pre_train", "vlm_llama4", "e16", args)
num_nodes, mbs, gbs, tp_size, pp_size, cp_size, vp_size, ep_size, etp_size, enable_cuda_graphs, _, _, _ = kwargs[
0:13
]
recipe = override_recipe_configs(
args, num_nodes, mbs, gbs, tp_size, pp_size, cp_size, vp_size, ep_size, etp_size, enable_cuda_graphs
)
exp_config = (
f"{num_nodes}nodes_tp{tp_size}_pp{pp_size}_cp{cp_size}_vp{vp_size}_ep{ep_size}_etp{etp_size}_{mbs}mbs_{gbs}gbs"
)
exp_name = f"{splitext(basename(__file__))[0]}_{args.compute_dtype}_{exp_config}"
executor = slurm_executor(
args.gpu.lower(),
args.account,
args.partition,
args.log_dir,
num_nodes,
args.gpus_per_node,
args.time_limit,
args.container_image,
custom_mounts=args.custom_mounts,
custom_env_vars={},
hf_token=args.hf_token,
nemo_home=args.nemo_home,
wandb_key=args.wandb_key,
)
if args.gpu.lower() in ['gb200'] and "PYTORCH_CUDA_ALLOC_CONF" in executor.env_vars:
del executor.env_vars["PYTORCH_CUDA_ALLOC_CONF"]
plugins = [
PerfEnvPlugin(
enable_vboost=True,
nccl_pp_comm_chunksize=2097152 if pp_size > 1 else None,
gpu_sm100_or_newer=(args.gpu.lower() in ['b200', 'gb200']),
)
]
if args.enable_nsys:
plugins.append(NsysPlugin(start_step=5, end_step=6, gen_shape=True))
with run.Experiment(exp_name) as exp:
exp.add(
recipe,
executor=executor,
name=exp_name,
plugins=plugins,
)
if not args.dryrun:
exp.run(sequential=True, detach=True)
else:
exp.dryrun()