Skip to content

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.

fix(vae): scope the Wan video decode's tiling state

Alexander Eichhorn444ad8a6788a
diff --git a/invokeai/app/invocations/vae/wan_latents_to_video.py b/invokeai/app/invocations/vae/wan_latents_to_video.py
index 25d2e63e92..ae1b0754a2 100644
--- a/invokeai/app/invocations/vae/wan_latents_to_video.py
+++ b/invokeai/app/invocations/vae/wan_latents_to_video.py
@@ -35,6 +35,10 @@ from invokeai.app.services.shared.invocation_context import InvocationContext
from invokeai.app.util.video_encoding import make_mp4_writer
from invokeai.backend.model_manager.load.model_cache.utils import get_effective_device
from invokeai.backend.util.devices import TorchDevice
+from invokeai.backend.util.qwen_image_vae import (
+ patch_qwen_image_vae_tiling,
+ resolve_qwen_image_vae_tile_size,
+)
from invokeai.backend.util.vae_working_memory import estimate_vae_working_memory_wan
from invokeai.backend.wan.vae_decode import iter_wan_vae_decode_chunks
@@ -133,6 +137,7 @@ class WanLatentsToVideoInvocation(BaseInvocation, WithMetadata, WithBoard):
# and budget for the tiled working set instead. (A cpu_only VAE runs in system
# RAM, where the working set is not the constraint.)
use_tiling = False
+ tile_size: int | None = None
if not getattr(vae_info.config, "cpu_only", None):
exec_device = TorchDevice.choose_torch_device()
total_vram: int | None = None
@@ -142,7 +147,11 @@ class WanLatentsToVideoInvocation(BaseInvocation, WithMetadata, WithBoard):
total_vram = torch.xpu.get_device_properties(exec_device).total_memory
if total_vram is not None and estimated_working_memory > 0.9 * total_vram:
use_tiling = True
- tile_size = int(getattr(vae_info.model, "tile_sample_min_height", 256))
+ # Resolved against the module constant, not read off `vae_info.model`: that is the
+ # cache's shared instance, so its current geometry is whatever the last invocation
+ # left rather than what this decode is about to ask for. The sentinel resolves to
+ # 256/192, which is also this VAE class's own default.
+ tile_size = resolve_qwen_image_vae_tile_size(0)
estimated_working_memory = estimate_vae_working_memory_wan(
operation="decode",
vae=vae_info.model,
@@ -176,13 +185,12 @@ class WanLatentsToVideoInvocation(BaseInvocation, WithMetadata, WithBoard):
latents = latents.to(device=get_effective_device(vae), dtype=vae_dtype)
TorchDevice.empty_cache()
- if use_tiling:
- vae.enable_tiling()
- else:
- # AutoencoderKLWan is cached and shared with Anima. Clear any
- # tiling state left by a prior image decode before streaming.
- vae.disable_tiling()
- try:
+ # `AutoencoderKLWan` is the cache's instance, shared with Anima and with the
+ # image decode. A bare `enable_tiling()` inherits whatever min/stride pair the
+ # module happens to carry, and an inconsistent pair drops whole bands of the frame;
+ # `disable_tiling()` clears the flag without restoring the geometry. The scope sets
+ # all four explicitly and puts back what was there.
+ with patch_qwen_image_vae_tiling(vae, tile_size):
with torch.inference_mode():
# Denormalise from denoiser space back to VAE space.
latents_mean = torch.tensor(vae.config.latents_mean).view(1, -1, 1, 1, 1).to(latents)
@@ -209,10 +217,6 @@ class WanLatentsToVideoInvocation(BaseInvocation, WithMetadata, WithBoard):
decoded = vae.decode(latents, return_dict=False)[0][0].cpu()
num_frames = decoded.shape[1]
del latents, latents_mean, latents_std
- finally:
- # The VAE instance is cached and shared; don't leak tiling into other nodes.
- if use_tiling:
- vae.disable_tiling()
TorchDevice.empty_cache()
diff --git a/tests/app/invocations/test_wan_working_memory.py b/tests/app/invocations/test_wan_working_memory.py
index 967bbefb78..8732f2aada 100644
--- a/tests/app/invocations/test_wan_working_memory.py
+++ b/tests/app/invocations/test_wan_working_memory.py
@@ -27,7 +27,13 @@ def _mock_wan_vae(
vae.config.scale_factor_temporal = temporal_scale
vae.config.latents_mean = [0.0] * z_dim
vae.config.latents_std = [1.0] * z_dim
+ # `patch_qwen_image_vae_tiling` snapshots and restores all five, and `enable_tiling` writes
+ # all four geometry values rather than inheriting any.
+ vae.use_tiling = False
vae.tile_sample_min_height = 256
+ vae.tile_sample_min_width = 256
+ vae.tile_sample_stride_height = 192
+ vae.tile_sample_stride_width = 192
return vae
@@ -298,6 +304,9 @@ class TestWanInvocationsRequestWorkingMemory:
def test_latents_to_video_falls_back_to_tiling_when_estimate_exceeds_vram(self):
vae = _mock_wan_vae()
+ # Not 256: the point of the change is that the tile comes from a module constant rather than
+ # from the shared cache instance, and with 256 on the instance both implementations agree.
+ vae.tile_sample_min_height = 999
vae_info = _mock_vae_info(vae)
mock_context = self._video_context(vae_info)
@@ -320,8 +329,15 @@ class TestWanInvocationsRequestWorkingMemory:
assert mock_estimate.call_count == 2
assert mock_estimate.call_args_list[1].kwargs["tile_size"] == 256
- vae.enable_tiling.assert_called_once()
- vae.disable_tiling.assert_called_once()
+ # All four geometry values are set explicitly rather than inherited from the shared module:
+ # `enable_tiling` falls back to whatever the instance carries for any argument left out, and
+ # a min below the inherited stride drops whole bands of the frame.
+ vae.enable_tiling.assert_called_once_with(
+ tile_sample_min_height=256,
+ tile_sample_min_width=256,
+ tile_sample_stride_height=192,
+ tile_sample_stride_width=192,
+ )
vae_info.model_on_device.assert_called_once_with(working_mem_bytes=4 * 2**30)
def test_latents_to_video_skips_tiling_for_cpu_only_vae(self):