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
test(rocm): start the conv3d patch idempotence tests from an unpatched class
Lincoln Steincb83bbbb42b6
diff --git a/invokeai/backend/minimax_h3/rocm_causal_conv3d.py b/invokeai/backend/minimax_h3/rocm_causal_conv3d.pyindex b704968d88..908ba939e6 100644--- a/invokeai/backend/minimax_h3/rocm_causal_conv3d.py+++ b/invokeai/backend/minimax_h3/rocm_causal_conv3d.py@@ -40,6 +40,9 @@ import torchimport torch.nn.functional as F_SENTINEL = "_invokeai_rocm_conv2d_decomposition"++_STOCK_FORWARD = None+"""The forward the patch replaced, kept so a test can restore the class to its unpatched state."""_MODE = os.environ.get("INVOKEAI_ROCM_CONV3D", "decomposed").strip().lower()@@ -85,8 +88,11 @@ def _patch_minimax_h3_causal_conv3d() -> None:"""Rebind MiniMaxH3VideoCausalConv3d.forward to the conv2d decomposition (idempotent)."""from invokeai.backend.minimax_h3.autoencoder_kl_minimax_h3 import MiniMaxH3VideoCausalConv3d+ global _STOCK_FORWARD+if getattr(MiniMaxH3VideoCausalConv3d, _SENTINEL, False):return+ _STOCK_FORWARD = MiniMaxH3VideoCausalConv3d.forwardMiniMaxH3VideoCausalConv3d.forward = _decomposed_forwardsetattr(MiniMaxH3VideoCausalConv3d, _SENTINEL, True)diff --git a/tests/backend/minimax_h3/test_rocm_causal_conv3d.py b/tests/backend/minimax_h3/test_rocm_causal_conv3d.pyindex 09ff73e98d..ae39acb90c 100644--- a/tests/backend/minimax_h3/test_rocm_causal_conv3d.py+++ b/tests/backend/minimax_h3/test_rocm_causal_conv3d.py@@ -13,6 +13,7 @@ import randomimport pytestimport torch+import invokeai.backend.minimax_h3.rocm_causal_conv3d as modfrom invokeai.backend.minimax_h3.autoencoder_kl_minimax_h3 import MiniMaxH3VideoCausalConv3dfrom invokeai.backend.minimax_h3.rocm_causal_conv3d import (_decomposed_conv3d,@@ -21,6 +22,19 @@ from invokeai.backend.minimax_h3.rocm_causal_conv3d import ()+def _restore_stock_forward() -> None:+ """Put the class back to its unpatched state.++ The patch is process-wide and idempotent, and on a ROCm build it is real: any earlier test on the+ same xdist worker that loads the MiniMax H3 video VAE for real leaves the class patched. A test that then records the patched forward as "stock" cannot tell whether patching+ did anything, and fails only on ROCm rigs, only for some worker assignments.+ """+ if getattr(MiniMaxH3VideoCausalConv3d, mod._SENTINEL, False):+ assert mod._STOCK_FORWARD is not None+ MiniMaxH3VideoCausalConv3d.forward = mod._STOCK_FORWARD+ delattr(MiniMaxH3VideoCausalConv3d, mod._SENTINEL)++def test_decomposed_conv3d_matches_f_conv3d() -> None:torch.manual_seed(0)for kernel_size in [(3, 3, 3), (1, 1, 1), (3, 1, 1), (1, 3, 3)]:@@ -66,6 +80,7 @@ def test_decomposed_forward_falls_back_to_conv3d_for_strided_convs() -> None:def test_class_patch_is_idempotent_and_preserves_behavior() -> None:torch.manual_seed(4)+ _restore_stock_forward()stock_forward = MiniMaxH3VideoCausalConv3d.forwardtry:conv = MiniMaxH3VideoCausalConv3d(4, 8, kernel_size=3, spatial_padding=1, temporal_padding=2)@@ -80,9 +95,7 @@ def test_class_patch_is_idempotent_and_preserves_behavior() -> None:assert torch.allclose(conv(x), ref, atol=1e-5)finally:- MiniMaxH3VideoCausalConv3d.forward = stock_forward- if hasattr(MiniMaxH3VideoCausalConv3d, "_invokeai_rocm_conv2d_decomposition"):- delattr(MiniMaxH3VideoCausalConv3d, "_invokeai_rocm_conv2d_decomposition")+ _restore_stock_forward()def test_patch_applies_on_every_hip_version(monkeypatch) -> None:diff --git a/tests/backend/wan/test_rocm_causal_conv3d.py b/tests/backend/wan/test_rocm_causal_conv3d.pyindex 300322b20e..dd52ef1718 100644--- a/tests/backend/wan/test_rocm_causal_conv3d.py+++ b/tests/backend/wan/test_rocm_causal_conv3d.py@@ -24,6 +24,20 @@ diffusers = pytest.importorskip("diffusers")from diffusers.models.autoencoders.autoencoder_kl_wan import WanCausalConv3d # noqa: E402+def _restore_stock_forward() -> None:+ """Put the class back to its unpatched state.++ The patch is process-wide and idempotent, and on a ROCm build it is real: any earlier test on the+ same xdist worker that loads a Wan-family VAE for real (the loader matrix does) leaves the class+ patched. A test that then records the patched forward as "stock" cannot tell whether patching+ did anything, and fails only on ROCm rigs, only for some worker assignments.+ """+ if getattr(WanCausalConv3d, mod._SENTINEL, False):+ assert mod._STOCK_FORWARD is not None+ WanCausalConv3d.forward = mod._STOCK_FORWARD+ delattr(WanCausalConv3d, mod._SENTINEL)++@pytest.fixture(autouse=True)def _decomposed_mode(monkeypatch: pytest.MonkeyPatch) -> None:"""A developer's INVOKEAI_ROCM_CONV3D=verify shell must not reroute the parity tests."""@@ -80,6 +94,7 @@ def test_decomposed_forward_falls_back_to_conv3d_for_strided_convs() -> None:def test_class_patch_is_idempotent_and_preserves_behavior() -> None:torch.manual_seed(4)+ _restore_stock_forward()stock_forward = WanCausalConv3d.forwardtry:conv = WanCausalConv3d(4, 8, kernel_size=3, padding=1)@@ -94,9 +109,7 @@ def test_class_patch_is_idempotent_and_preserves_behavior() -> None:assert torch.allclose(conv(x), ref, atol=1e-5)finally:- WanCausalConv3d.forward = stock_forward- if hasattr(WanCausalConv3d, "_invokeai_rocm_conv2d_decomposition"):- delattr(WanCausalConv3d, "_invokeai_rocm_conv2d_decomposition")+ _restore_stock_forward()def test_patch_applies_on_every_hip_version(monkeypatch: pytest.MonkeyPatch) -> None: