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(wan): calibrate the VAE under the app's ROCm runtime

Alexander Eichhorn9c5f14cf94ec
diff --git a/invokeai/backend/util/vae_working_memory.py b/invokeai/backend/util/vae_working_memory.py
index c43b13785f..4c92894f1e 100644
--- a/invokeai/backend/util/vae_working_memory.py
+++ b/invokeai/backend/util/vae_working_memory.py
@@ -79,7 +79,7 @@ def should_pretile_vae_decode(
# its score matrix is the dominant term for a large untiled image, exactly as it is for FLUX.2;
# `_vae_mid_block_score_matrix_bytes` prices it for those estimators the same way. The video VAEs
# (Wan, Qwen-Image) need no such term: the Qwen-Image ROCm constants below were measured with math
-# attention. The Wan video-decode constant was measured on CUDA only; ROCm is unmeasured there.
+# attention. The Wan video-decode constant was measured on CUDA and on an RX 9060 XT (see there).
_CLASSIC_VAE_MID_BLOCK_HEADS = 1
_CLASSIC_VAE_MID_BLOCK_HEAD_DIM = 512
@@ -727,6 +727,9 @@ def estimate_vae_working_memory_wan(
# and at 121 frames), so the clip bytes are budget on top of it rather than part of it. Both
# VAEs reserved exactly the same 6666 MiB at 832x480x49 standalone, so one constant covers
# both: the highest measurement, the server's 480p full decode, plus ~5%.
+ # ROCm (RX 9060 XT, gfx1200, torch 2.13+rocm10, with the app's conv3d decomposition, SDPA guard
+ # and expandable segments): 7973-8813 standalone across both VAEs, full and streaming up to
+ # 832x480x81, and 7719-8297 for TI2V 832x480x49 in a server -- covered as well.
#
# The same estimate decides auto-tiling (`should_pretile_vae_decode`): with it, 832x480 clips
# tile on 8 GB cards and 720p clips on 16 GB cards, where their untiled decodes reserve about as
diff --git a/scripts/calibrate_wan_vae_working_memory.py b/scripts/calibrate_wan_vae_working_memory.py
index 0c2c18bde8..6e7993a18f 100644
--- a/scripts/calibrate_wan_vae_working_memory.py
+++ b/scripts/calibrate_wan_vae_working_memory.py
@@ -20,20 +20,31 @@ the constant sits above what this script reports.
from __future__ import annotations
import argparse
+import logging
+import os
import sys
from pathlib import Path
-import torch
-from diffusers.models.autoencoders import AutoencoderKLWan
-
# Direct script execution puts ``scripts/`` on sys.path, not the repository root.
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+from invokeai.app.util.torch_cuda_allocator import apply_rocm_windows_allocator_default # noqa: E402
+
+# The server's ROCm runtime, so a ROCm measurement is what Invoke would see: both must be set before torch is
+# imported (see `invokeai.app.run_app` and `apply_rocm_windows_allocator_default`). Inert on CUDA.
+os.environ.setdefault("MIOPEN_FIND_MODE", "FAST")
+apply_rocm_windows_allocator_default(logging.getLogger(__name__))
+
+import torch # noqa: E402
+from diffusers.models.autoencoders import AutoencoderKLWan # noqa: E402
+
from invokeai.backend.model_manager.load.model_loaders.vae import _wan_vae_init_kwargs_for # noqa: E402
+from invokeai.backend.util.attention import install_rocm_sdpa_guard # noqa: E402
from invokeai.backend.util.vae_working_memory import ( # noqa: E402
estimate_vae_working_memory_wan,
wan_vae_clip_bytes,
)
+from invokeai.backend.wan.rocm_causal_conv3d import patch_wan_causal_conv3d_for_rocm # noqa: E402
from invokeai.backend.wan.vae_decode import iter_wan_vae_decode_chunks # noqa: E402
DTYPES = {"float16": torch.float16, "bfloat16": torch.bfloat16, "float32": torch.float32}
@@ -205,6 +216,9 @@ def main() -> None:
if not torch.cuda.is_available():
raise SystemExit("CUDA or ROCm device required")
+ # What the Wan VAE loader and the app's startup apply on ROCm; no-ops on CUDA.
+ patch_wan_causal_conv3d_for_rocm()
+ install_rocm_sdpa_guard()
vae = _load_vae(args.vae, DTYPES[args.dtype])
try:
result = _measure(