Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/maxdiffusion/pipelines/wan/wan_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ def create_model(rngs: nnx.Rngs, config: HyperParameters):
# This helps with loading sharded weights directly into the accelerators without fist copying them
# all to one device and then distributing them, thus using low HBM memory.
params = load_wan_vae(config.pretrained_model_name_or_path, params, "cpu")
params = jax.tree_util.tree_map(lambda x: x.astype(config.weights_dtype), params)
params = jax.tree_util.tree_map(lambda x: x.astype(config.vae_weights_dtype), params)
for path, val in flax.traverse_util.flatten_dict(params).items():
sharding = logical_state_sharding[path].value
if config.replicate_vae:
Expand Down
21 changes: 21 additions & 0 deletions src/maxdiffusion/tests/wan/wan_vae_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
AutoencoderKLWanCache,
)
from maxdiffusion.models.wan.wan_utils import load_wan_vae
from maxdiffusion.pipelines.wan.wan_pipeline import WanPipeline
from maxdiffusion.utils import load_video
from maxdiffusion.video_processor import VideoProcessor
import flax
Expand Down Expand Up @@ -578,6 +579,26 @@ def vae_encode(video, wan_vae, vae_cache, key):
ssim_compare = ssim(video[0], original_video[0], multichannel=True, channel_axis=-1, data_range=255)
assert ssim_compare >= 0.9999

def test_load_vae_uses_vae_weights_dtype(self):
for weights_dtype, vae_weights_dtype in (("bfloat16", "float32"), ("float32", "bfloat16")):
with self.subTest(weights_dtype=weights_dtype, vae_weights_dtype=vae_weights_dtype):
pyconfig.initialize(
[
None,
os.path.join(THIS_DIR, "..", "..", "configs", "base_wan_14b.yml"),
f"weights_dtype={weights_dtype}",
f"vae_weights_dtype={vae_weights_dtype}",
],
unittest=True,
)
config = pyconfig.config
components = WanPipeline._create_common_components( # pylint: disable=protected-access
config, load_vae=True, load_text_encoder=False, load_scheduler=False
)
params = nnx.state(components["vae"], nnx.Param)
param_dtypes = {x.dtype for x in jax.tree_util.tree_leaves(params)}
self.assertEqual(param_dtypes, {jnp.dtype(vae_weights_dtype)})


if __name__ == "__main__":
absltest.main()
Loading