Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
"""Tests for the Wan VAE single-file loader helper.
Covers the bug where ``AutoencoderKLWan`` was always instantiated with the A14B
defaults (base_dim=96, out_channels=3, no patchify), causing the TI2V-5B VAE
checkpoint to fail state_dict loading with shape mismatches throughout the
encoder + decoder. The fix routes z_dim=48 to the TI2V-5B-specific
constructor kwargs.
"""
from unittest.mock import MagicMock, patch
import accelerate
import torch
from diffusers.models.autoencoders.autoencoder_kl_wan import AutoencoderKLWan
from invokeai.backend.model_manager.load.model_loaders.vae import VAELoader, _wan_vae_init_kwargs_for
def test_a14b_returns_default_z_dim_only() -> None:
# The A14B path should still be the trivial case — only z_dim is overridden,
# leaving diffusers' defaults (base_dim=96, out_channels=3, etc.) intact.
assert _wan_vae_init_kwargs_for(16) == {"z_dim": 16}
def test_ti2v_5b_returns_full_architectural_override() -> None:
kw = _wan_vae_init_kwargs_for(48)
assert kw["z_dim"] == 48
assert kw["base_dim"] == 160
assert kw["decoder_base_dim"] == 256
assert kw["in_channels"] == 12
assert kw["out_channels"] == 12
assert kw["patch_size"] == 2
assert kw["scale_factor_spatial"] == 16
assert kw["is_residual"] is True
# latents_mean/std need to be 48-vectors so the model can construct.
assert len(kw["latents_mean"]) == 48
assert len(kw["latents_std"]) == 48
def test_ti2v_5b_kwargs_instantiate_with_expected_shapes() -> None:
# End-to-end check: the kwargs let AutoencoderKLWan build cleanly and the
# resulting model carries the TI2V-5B-shaped layers (z_dim=48, decoder
# outputs 12 channels — this is what failed before the fix).
with accelerate.init_empty_weights():
model = AutoencoderKLWan(**_wan_vae_init_kwargs_for(48))
assert model.z_dim == 48
assert model.config.base_dim == 160
assert model.config.decoder_base_dim == 256
assert model.config.out_channels == 12
assert model.config.patch_size == 2
# decoder.conv_out emits the patchified 12-channel output (3 RGB x 2x2 patch).
assert model.decoder.conv_out.weight.shape[0] == 12
def test_a14b_kwargs_instantiate_with_expected_shapes() -> None:
with accelerate.init_empty_weights():
model = AutoencoderKLWan(**_wan_vae_init_kwargs_for(16))
assert model.z_dim == 16
assert model.config.base_dim == 96
assert model.config.out_channels == 3
assert model.config.patch_size is None
def test_wan_vae_checkpoint_loads_bfloat16_even_when_loader_dtype_is_fp16() -> None:
"""fp16 is unstable on the Wan VAE (NaN/garbled frames), so the checkpoint path raises a
float16 request to bfloat16 like ``_load_wan_vae_diffusers`` does — ``precision: auto``
resolves the loader's default dtype to float16 on CUDA."""
loader = VAELoader.__new__(VAELoader)
loader._ram_cache = MagicMock()
loader._torch_dtype = torch.float16 # what `precision: auto` yields on CUDA
sd = {
"decoder.weight": torch.zeros(2, 2, dtype=torch.float32),
"step_counter": torch.zeros(1, dtype=torch.int64),
}
config = MagicMock()
# The loader picks its reader from the suffix, so the (mocked) safetensors reader is the one used.
config.path = "wan-vae.safetensors"
config.latent_channels = 16
fake_model = MagicMock()
with (
patch("safetensors.torch.load_file", return_value=sd),
patch("diffusers.models.autoencoders.autoencoder_kl_wan.AutoencoderKLWan", return_value=fake_model),
# The real ROCm patch mutates WanCausalConv3d.forward process-wide, which breaks
# the conv3d idempotency test when it runs later in the same session.
patch("invokeai.backend.wan.rocm_causal_conv3d.patch_wan_causal_conv3d_for_rocm"),
):
result = loader._load_wan_vae(config)
assert result is fake_model
loaded_sd = fake_model.load_state_dict.call_args.args[0]
assert loaded_sd["decoder.weight"].dtype == torch.bfloat16
assert loaded_sd["step_counter"].dtype == torch.int64 # non-float buffers untouched