-
Notifications
You must be signed in to change notification settings - Fork 64
Refactor Wan Model Training & Add Wan-VACE Training Support #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ninatu
wants to merge
10
commits into
main
Choose a base branch
from
ninatu/wan_training
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,133
−395
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fb25b23
Update wan configs for training
ninatu e205aa1
Wan training: Resolve training mode bug with dropout and layer_forward
ninatu 1fe4ce0
Wan training: use learning rate from config
ninatu 5c6f65f
Fix: Ensure prepare_sample_fn is used for 'tfrecord' dataset type
ninatu 6101386
Wan training: Set default dropout to 0.0 in Wan configs
ninatu efbc91d
Wan 2.1 training: Resolve checkpoint loading issues with larger TPU s…
ninatu f30daac
Wan training: Fix WAN training timestep sampling with continuous samp…
ninatu 28fbabb
Abstract common WAN training components into BaseWanTrainer
ninatu 7d8fdfb
Add WAN-VACE training functionality
ninatu dec1690
Fix: remove unnecessary tensorboard_dir from wan configs
ninatu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,14 +15,15 @@ | |
| """ | ||
|
|
||
| import json | ||
| import jax | ||
| import numpy as np | ||
| from typing import Optional, Tuple | ||
| from ..pipelines.wan.wan_pipeline_2_1 import WanPipeline2_1 | ||
| from .. import max_logging | ||
| import orbax.checkpoint as ocp | ||
| from etils import epath | ||
| import jax | ||
| from jax.sharding import Mesh, NamedSharding, PartitionSpec as P | ||
| from maxdiffusion.checkpointing.wan_checkpointer import WanCheckpointer | ||
| import numpy as np | ||
| import orbax.checkpoint as ocp | ||
| from .. import max_logging | ||
| from ..pipelines.wan.wan_pipeline_2_1 import WanPipeline2_1 | ||
|
|
||
|
|
||
| class WanCheckpointer2_1(WanCheckpointer): | ||
|
|
@@ -35,13 +36,32 @@ def load_wan_configs_from_orbax(self, step: Optional[int]) -> Tuple[Optional[dic | |
| max_logging.log("No WAN checkpoint found.") | ||
| return None, None | ||
| max_logging.log(f"Loading WAN checkpoint from step {step}") | ||
|
|
||
| cpu_devices = np.array(jax.devices(backend="cpu")) | ||
| mesh = Mesh(cpu_devices, axis_names=("data",)) | ||
| replicated_sharding = NamedSharding(mesh, P()) | ||
|
|
||
| metadatas = self.checkpoint_manager.item_metadata(step) | ||
| transformer_metadata = metadatas.wan_state | ||
| abstract_tree_structure_params = jax.tree_util.tree_map(ocp.utils.to_shape_dtype_struct, transformer_metadata) | ||
| state = metadatas.wan_state | ||
|
|
||
| def add_sharding_to_struct(leaf_struct, sharding): | ||
| return jax.ShapeDtypeStruct( | ||
| shape=leaf_struct.shape, dtype=leaf_struct.dtype, sharding=sharding | ||
| ) | ||
|
|
||
| target_shardings = jax.tree_util.tree_map( | ||
| lambda x: replicated_sharding, state | ||
| ) | ||
|
|
||
| with mesh: | ||
| abstract_train_state_with_sharding = jax.tree_util.tree_map( | ||
| add_sharding_to_struct, state, target_shardings | ||
| ) | ||
|
|
||
| params_restore = ocp.args.PyTreeRestore( | ||
| restore_args=jax.tree.map( | ||
| lambda _: ocp.RestoreArgs(restore_type=np.ndarray), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing restore_type = np.ndarray causes the JAX sharding applied above to be redundant. (JAX sharding cannot work on np.ndarrays). Suggest to make it jax.Array to ensure checkpoint is loaded on host in sharded manner if that's intended |
||
| abstract_tree_structure_params, | ||
| abstract_train_state_with_sharding, | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/maxdiffusion/checkpointing/wan_vace_checkpointer_2_1.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| """Copyright 2025 Google LLC | ||
|
|
||
| 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 | ||
|
|
||
| https://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. | ||
| """ | ||
|
|
||
| import json | ||
| from typing import Optional, Tuple | ||
| import jax | ||
| from jax.sharding import Mesh, NamedSharding, PartitionSpec as P | ||
| from maxdiffusion.checkpointing.wan_checkpointer import WanCheckpointer | ||
| import numpy as np | ||
| import orbax.checkpoint as ocp | ||
| from .. import max_logging | ||
| from ..pipelines.wan.wan_vace_pipeline_2_1 import VaceWanPipeline2_1 | ||
|
|
||
|
|
||
| class WanVaceCheckpointer2_1(WanCheckpointer): | ||
|
|
||
| def load_wan_configs_from_orbax(self, step: Optional[int]) -> Tuple[Optional[dict], Optional[int]]: | ||
| if step is None: | ||
| step = self.checkpoint_manager.latest_step() | ||
| max_logging.log(f"Latest WAN checkpoint step: {step}") | ||
| if step is None: | ||
| max_logging.log("No WAN checkpoint found.") | ||
| return None, None | ||
| max_logging.log(f"Loading WAN checkpoint from step {step}") | ||
|
|
||
| cpu_devices = np.array(jax.devices(backend="cpu")) | ||
| mesh = Mesh(cpu_devices, axis_names=("data",)) | ||
| replicated_sharding = NamedSharding(mesh, P()) | ||
|
|
||
| metadatas = self.checkpoint_manager.item_metadata(step) | ||
| state = metadatas.wan_state | ||
|
|
||
| def add_sharding_to_struct(leaf_struct, sharding): | ||
| return jax.ShapeDtypeStruct( | ||
| shape=leaf_struct.shape, dtype=leaf_struct.dtype, sharding=sharding | ||
| ) | ||
|
|
||
| target_shardings = jax.tree_util.tree_map( | ||
| lambda x: replicated_sharding, state | ||
| ) | ||
|
|
||
| with mesh: | ||
| abstract_train_state_with_sharding = jax.tree_util.tree_map( | ||
| add_sharding_to_struct, state, target_shardings | ||
| ) | ||
|
|
||
| max_logging.log("Restoring WAN checkpoint") | ||
| restored_checkpoint = self.checkpoint_manager.restore( | ||
| step=step, | ||
| args=ocp.args.Composite( | ||
| wan_config=ocp.args.JsonRestore(), | ||
| wan_state=ocp.args.StandardRestore( | ||
| abstract_train_state_with_sharding | ||
| ), | ||
| ), | ||
| ) | ||
| max_logging.log(f"restored checkpoint {restored_checkpoint.keys()}") | ||
| max_logging.log(f"restored checkpoint wan_state {restored_checkpoint.wan_state.keys()}") | ||
| max_logging.log(f"optimizer found in checkpoint {'opt_state' in restored_checkpoint.wan_state.keys()}") | ||
| max_logging.log(f"optimizer state saved in attribute self.opt_state {self.opt_state}") | ||
| return restored_checkpoint, step | ||
|
|
||
| def load_diffusers_checkpoint(self): | ||
| pipeline = VaceWanPipeline2_1.from_pretrained(self.config) | ||
| return pipeline | ||
|
|
||
| def load_checkpoint(self, step=None) -> Tuple[VaceWanPipeline2_1, Optional[dict], Optional[int]]: | ||
| restored_checkpoint, step = self.load_wan_configs_from_orbax(step) | ||
| opt_state = None | ||
| if restored_checkpoint: | ||
| max_logging.log("Loading WAN pipeline from checkpoint") | ||
| pipeline = VaceWanPipeline2_1.from_checkpoint(self.config, restored_checkpoint) | ||
| if "opt_state" in restored_checkpoint.wan_state.keys(): | ||
| opt_state = restored_checkpoint.wan_state["opt_state"] | ||
| else: | ||
| max_logging.log("No checkpoint found, loading default pipeline.") | ||
| pipeline = self.load_diffusers_checkpoint() | ||
|
|
||
| return pipeline, opt_state, step | ||
|
|
||
| def save_checkpoint( | ||
| self, train_step, pipeline: VaceWanPipeline2_1, train_states: dict | ||
| ): | ||
| """Saves the training state and model configurations.""" | ||
|
|
||
| def config_to_json(model_or_config): | ||
| return json.loads(model_or_config.to_json_string()) | ||
|
|
||
| max_logging.log(f"Saving checkpoint for step {train_step}") | ||
|
|
||
| # Save the checkpoint | ||
| self.checkpoint_manager.save( | ||
| train_step, | ||
| args=ocp.args.Composite( | ||
| wan_config=ocp.args.JsonSave(config_to_json(pipeline.transformer)), | ||
| wan_state=ocp.args.StandardSave(train_states), | ||
| ), | ||
| ) | ||
|
|
||
| max_logging.log(f"Checkpoint for step {train_step} is saved.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A safer way to do this to prevent unexpected crashes (for any elements not having shape/dtype attributes):