From c012b9637799a55e22c71d3acbb3312fc9eb75c1 Mon Sep 17 00:00:00 2001 From: Josh Berns <98094229+boshjerns@users.noreply.github.com> Date: Thu, 22 Oct 2026 08:24:52 +0200 Subject: [PATCH] Cast the Wan VAE weights to vae_weights_dtype instead of weights_dtype --- .../pipelines/wan/wan_pipeline.py | 2 +- src/maxdiffusion/tests/wan/wan_vae_test.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/maxdiffusion/pipelines/wan/wan_pipeline.py b/src/maxdiffusion/pipelines/wan/wan_pipeline.py index 4006e2fd1..5271cbd80 100644 --- a/src/maxdiffusion/pipelines/wan/wan_pipeline.py +++ b/src/maxdiffusion/pipelines/wan/wan_pipeline.py @@ -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: diff --git a/src/maxdiffusion/tests/wan/wan_vae_test.py b/src/maxdiffusion/tests/wan/wan_vae_test.py index c1bbd26a8..c89f0554d 100644 --- a/src/maxdiffusion/tests/wan/wan_vae_test.py +++ b/src/maxdiffusion/tests/wan/wan_vae_test.py @@ -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 @@ -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()