1414# limitations under the License.
1515
1616"""
17- Generic Training Script for GPT-based Models
17+ Generic Training Script for LLM and diffusion models
1818
1919This script works with any model family that uses GPT-style training
20- (Llama, Gemma, Qwen, GPT, etc.). It dynamically loads recipes and supports
20+ (Llama, Gemma, Qwen, GPT, etc.) and with diffusion models (e.g. FLUX, WAN) . It dynamically loads recipes and supports
2121CLI overrides. The --dataset flag selects the dataset type and automatically
2222infers pretrain vs finetune mode.
2323
5050 --dataset llm-finetune-preloaded \\
5151 dataset.dataset_root=/data/my_finetune_data
5252
53+ Diffusion pretrain:
54+ uv run torchrun --nproc_per_node=8 run_recipe.py \
55+ --recipe wan_1_3B_pretrain_config \
56+ --step_func wan_step
57+
58+ Diffusion SFT (full finetuning):
59+ uv run torchrun --nproc_per_node=8 run_recipe.py \
60+ --recipe wan_1_3B_finetune_config \
61+ --step_func wan_step
62+
5363 VLM with HF dataset:
5464 uv run torchrun --nproc_per_node=8 run_recipe.py \\
5565 --recipe qwen3_vl_8b_peft_config \\
8393 --dataset llm-pretrain-mock \\
8494 train.train_iters=5000 \\
8595 optimizer.lr=0.0003
86-
87- Diffusion (FLUX) pretrain:
88- uv run torchrun --nproc_per_node=8 run_recipe.py \\
89- --recipe flux_12b_pretrain_config \\
90- --step_func flux_step
9196
9297Recipe Arguments:
9398 Generic scripts call recipes with no arguments: recipe().
102107
103108import megatron .bridge .recipes as recipes
104109
105- # Diffusion (FLUX) forward step : use class instance so it can be passed as forward_step_func
110+ # Diffusion forward steps : use class instances so they can be passed as forward_step_func
106111from megatron .bridge .diffusion .models .flux .flux_step import FluxForwardStep
112+ from megatron .bridge .diffusion .models .wan .wan_step import WanForwardStep
107113from megatron .bridge .models .qwen_vl .qwen3_vl_step import forward_step as qwen3_vl_forward_step
108114from megatron .bridge .recipes .utils .dataset_utils import (
109115 DATASET_TYPES ,
124130 "vlm_step" : vlm_forward_step ,
125131 "qwen3_vl_step" : qwen3_vl_forward_step ,
126132 "llava_step" : llava_forward_step ,
127- "flux_step" : FluxForwardStep (),
133+ "flux_step" : FluxForwardStep ,
134+ "wan_step" : WanForwardStep ,
128135}
129136
130137TRAIN_FUNCTIONS = {
@@ -175,7 +182,8 @@ def parse_args() -> tuple[argparse.Namespace, list[str]]:
175182 type = str ,
176183 default = "gpt_step" ,
177184 choices = sorted (STEP_FUNCTIONS .keys ()),
178- help = "Step function: gpt_step (text-only), vlm_step (vision-language), llava_step (LLaVA), flux_step (diffusion/FLUX)" ,
185+ help = "Step function: gpt_step (text-only), vlm_step (vision-language), llava_step (LLaVA), "
186+ "flux_step (FLUX diffusion), wan_step (WAN diffusion, hyperparameters selected by --mode/recipe name)" ,
179187 )
180188 parser .add_argument (
181189 "--peft_scheme" ,
@@ -273,12 +281,17 @@ def load_recipe(
273281 return config_builder ()
274282
275283
276- def load_forward_step (step_type : str ) -> Callable :
284+ def load_forward_step (step_type : str , mode : str | None = None ) -> Callable :
277285 """Load forward_step function based on the requested step type."""
278286 step_key = step_type .lower ()
279287 if step_key not in STEP_FUNCTIONS :
280288 raise ValueError (ERR_UNKNOWN_STEP .format (step_type = step_type , choices = ", " .join (STEP_FUNCTIONS )))
281- return STEP_FUNCTIONS [step_key ]
289+ step = STEP_FUNCTIONS [step_key ]
290+ if inspect .isclass (step ):
291+ if "mode" in inspect .signature (step .__init__ ).parameters :
292+ return step (mode = mode )
293+ return step ()
294+ return step
282295
283296
284297def infer_train_mode (recipe_name : str ) -> str :
@@ -330,10 +343,10 @@ def main() -> None:
330343 if hasattr (config .dataset , "seq_length" ) and config .model .seq_length != config .dataset .seq_length :
331344 config .model .seq_length = config .dataset .seq_length
332345
333- forward_step = load_forward_step (args .step_func )
346+ forward_step = load_forward_step (args .step_func , mode = mode )
334347 train_func = TRAIN_FUNCTIONS [mode ]
335348 train_func (config = config , forward_step_func = forward_step )
336349
337350
338351if __name__ == "__main__" :
339- main ()
352+ main ()
0 commit comments