Skip to content

Wan 2.2 training - #470

Open
Toshi-31 wants to merge 1 commit into
AI-Hypercomputer:mainfrom
Toshi-31:wan-2.2-training
Open

Wan 2.2 training#470
Toshi-31 wants to merge 1 commit into
AI-Hypercomputer:mainfrom
Toshi-31:wan-2.2-training

Conversation

@Toshi-31

@Toshi-31 Toshi-31 commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Title:
Add Wan 2.2 Training Pipeline with Joint Timestep Routing

Description:
This PR introduces full training support for the Wan 2.2 models into MaxDiffusion.

1. Joint Timestep Routing
This implementation utilizes a unified, joint-pipeline training strategy. During each training step, the pipeline dynamically samples a timestep and routes the forward pass to either the high-noise or low-noise transformer based on the configured boundary ratio:

is_high_noise = jax.random.uniform(cond_rng) > config.boundary_ratio
This ensures a seamless and efficient training execution graph without needing separate high/low training loops.

2. Pipeline Inheritance & Code Reuse
The new wan_trainer_2_2.py relies directly on the WanPipeline2_2 class to bootstrap the models and load checkpoints. Because WanPipeline2_2 cleanly inherits from the base WanPipeline, the training loop seamlessly reuses all existing weight conversion and fast-loading logic that was previously established for inference.

3. Native Training Quantization Support
Because the trainer hooks directly into the unified WanPipeline.from_pretrained() (and from_checkpoint()) initialization sequence, quantization is automatically supported out-of-the-box for training. By passing use_qwix_quantization=True (and e.g., quantization="fp8"), the pipeline's innate quantize_transformer() loop executes before handing the model over to the trainer. This seamlessly delivers a quantized transformer straight to the optimizer with zero additional training code overhead. (Note: LoRA remains inference-only).

The loss graphs were plotted for around 260 steps, and show a clear downward trend.

For graphs and other artefacts: https://docs.google.com/document/d/1svzC8cVZxb2XxyypeFcoJig13_1ptu6wYIe5QwnC_Lo/edit?usp=sharing

Step time: Around 39.7 seconds per device.

@Toshi-31
Toshi-31 requested a review from entrpn as a code owner September 1, 2026 10:32

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces training support for the Wan 2.2 model, including a new trainer (WanTrainer2_2), training script, configuration updates, and import smoke tests. It also switches GCS model downloads to use gcloud storage to prevent SSL segfaults, and optimizes disk usage during shard conversion by deleting shards after processing. The code review identified several critical issues and improvement opportunities: a logical error in the dataset validation check in WanTrainer2_2 where 'and' was used instead of 'or'; a JAX purity violation caused by mutating the input dictionary in-place inside the JIT-compiled train_step_2_2; incorrect evaluation routing for mixed-timestep batches, which should be resolved using jnp.where instead of checking only the first timestep; potential out-of-memory errors from downloading large models to /dev/shm instead of /tmp; performance overhead from recreating a ThreadPoolExecutor inside a loop during shard conversion; and the use of a mutable default argument in training_loop_2_2.

Comment thread src/maxdiffusion/trainers/wan_trainer_2_2.py Outdated
Comment thread src/maxdiffusion/trainers/wan_trainer_2_2.py Outdated
Comment thread src/maxdiffusion/trainers/wan_trainer_2_2.py Outdated
Comment thread src/maxdiffusion/pyconfig.py Outdated
Comment thread src/maxdiffusion/models/wan/wan_utils.py Outdated
Comment thread src/maxdiffusion/trainers/wan_trainer_2_2.py Outdated
@prishajain1

Copy link
Copy Markdown
Collaborator

Can you please add the current timestep in the PR description

Comment thread src/maxdiffusion/configs/base_wan_27b.yml Outdated
Comment thread src/maxdiffusion/trainers/wan_trainer_2_2.py Outdated
Comment thread setup.sh
@prishajain1

Copy link
Copy Markdown
Collaborator

Please add relevant unittests

Comment thread src/maxdiffusion/pyconfig.py Outdated
Comment thread src/maxdiffusion/models/wan/wan_utils.py Outdated
Comment thread src/maxdiffusion/trainers/wan_trainer_2_2.py Outdated
@Toshi-31

Toshi-31 commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

Can you please add the current timestep in the PR description

Done!

@Toshi-31

Toshi-31 commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

Please add relevant unittests

Done!

@Toshi-31
Toshi-31 force-pushed the wan-2.2-training branch 3 times, most recently from ff2a971 to 4ffe7dc Compare September 2, 2026 11:23
Comment thread src/maxdiffusion/configs/training_wan_27b.yml Outdated
@Toshi-31
Toshi-31 force-pushed the wan-2.2-training branch 5 times, most recently from 8edcb2d to 1a6b32e Compare September 3, 2026 09:33
@Toshi-31
Toshi-31 requested a review from Perseus14 September 3, 2026 09:39
@Toshi-31
Toshi-31 force-pushed the wan-2.2-training branch 2 times, most recently from a1f349a to 385c2d9 Compare September 4, 2026 10:07
@Toshi-31
Toshi-31 force-pushed the wan-2.2-training branch 5 times, most recently from d674d8a to 4ddaa08 Compare September 4, 2026 11:15
Comment thread src/maxdiffusion/configs/base_wan_27b.yml
…expert routing

- Implement WanTrainer2_2 dual-expert joint training pipeline with probabilistic batch routing based on boundary_ratio
- Use unbiased uniform integer timestep sampling [boundary, num_train_timesteps) for high-noise expert and [0, boundary) for low-noise expert
- Pass (state_high, state_low) functionally as operands to jax.lax.cond without outer closures
- Isolate buffer updates to avoid buffer donation hazards on untouched states
- Implement batched conditional evaluation in eval_step_2_2 with exact per-sample routing and constant HLO graph complexity
- Strip redundant process_allgather on replicated evaluation metrics in eval_2_2
- Save and restore both low_noise_transformer and high_noise_transformer configurations and states in WanCheckpointer2_2
- Track active expert step counts on host to log active learning rates and eliminate device-to-host sync stalls
- Harmonize TensorBoard writer metric_step tracking for per-expert steps in train_utils.py
- Document checkpoint_save_location as local staging cache with disk capacity considerations in base_wan_27b.yml
- Add comprehensive test suite covering training steps, eval steps, checkpointing, and resume equivalence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants