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
fix(minimax-h3): hand MIOpen contiguous tensors in the conv3d decomposition
Lincoln Stein74a7d2d2f873
diff --git a/invokeai/backend/minimax_h3/rocm_causal_conv3d.py b/invokeai/backend/minimax_h3/rocm_causal_conv3d.pyindex 0fe512f443..b704968d88 100644--- a/invokeai/backend/minimax_h3/rocm_causal_conv3d.py+++ b/invokeai/backend/minimax_h3/rocm_causal_conv3d.py@@ -49,16 +49,21 @@ def _decomposed_conv3d(module: torch.nn.Conv3d, x: torch.Tensor) -> torch.Tensorb, c, t, h, w = x.shapek_t = module.weight.shape[2]t_out = t - k_t + 1+ # Every tensor handed to MIOpen, and the activation handed back, is made standard-contiguous:+ # the Wan twin's identical strided views made MIOpen in torch 2.13.0+rocm7.2 fault+ # asynchronously part-way through long clips (see `invokeai.backend.wan.rocm_causal_conv3d`).+ # The copies cost a small fraction of the conv itself.out = Nonefor k in range(k_t):- xs = x[:, :, k : k + t_out].transpose(1, 2).reshape(b * t_out, c, h, w)- o = F.conv2d(xs, module.weight[:, :, k], None)- out = o if out is None else out + o+ xs = x[:, :, k : k + t_out].transpose(1, 2).reshape(b * t_out, c, h, w).contiguous()+ o = F.conv2d(xs, module.weight[:, :, k].contiguous(), None)+ del xs # keep at most one tap input live alongside the accumulator+ out = o if out is None else out.add_(o)assert out is not Noneif module.bias is not None:out = out + module.bias.view(1, -1, 1, 1)oh, ow = out.shape[-2:]- return out.reshape(b, t_out, -1, oh, ow).transpose(1, 2)+ return out.reshape(b, t_out, -1, oh, ow).transpose(1, 2).contiguous()def _decomposed_forward(self, hidden_states: torch.Tensor) -> torch.Tensor:diff --git a/tests/backend/minimax_h3/test_rocm_causal_conv3d.py b/tests/backend/minimax_h3/test_rocm_causal_conv3d.pyindex 782bf7462a..09ff73e98d 100644--- a/tests/backend/minimax_h3/test_rocm_causal_conv3d.py+++ b/tests/backend/minimax_h3/test_rocm_causal_conv3d.py@@ -8,6 +8,9 @@ causal temporal padding — so the ROCm-gated class patch can never changeresults, only speed."""+import random++import pytestimport torchfrom invokeai.backend.minimax_h3.autoencoder_kl_minimax_h3 import MiniMaxH3VideoCausalConv3d@@ -107,3 +110,36 @@ def test_patch_applies_on_every_hip_version(monkeypatch) -> None:monkeypatch.setattr(mod, "_MODE", "native")mod.patch_minimax_h3_causal_conv3d_for_rocm()assert calls == [True, True], "INVOKEAI_ROCM_CONV3D=native leaves the stock forward in place"+++needs_rocm = pytest.mark.skipif(torch.version.hip is None or not torch.cuda.is_available(), reason="needs a ROCm GPU")+++@needs_rocm+def test_decomposition_matches_native_on_device_under_allocation_churn() -> None:+ """Both device paths stay within bf16 noise of a CPU fp32 reference on a 17-frame encoder chunk+ at a non-square size while junk allocations come and go between calls. `slow`: a dev-machine+ lane, not CI."""+ torch.cuda.empty_cache()+ if torch.cuda.mem_get_info()[0] < 8 * 2**30:+ pytest.skip("needs ~8 GiB free")+ torch.manual_seed(0)+ conv = MiniMaxH3VideoCausalConv3d(64, 64, kernel_size=3, spatial_padding=1, temporal_padding=2)+ x = torch.randn((1, 64, 17, 224, 384))+ with torch.inference_mode():+ reference = MiniMaxH3VideoCausalConv3d.forward(conv, x)+ conv = conv.to("cuda", torch.bfloat16)+ x = x.to("cuda", torch.bfloat16)+ rnd = random.Random(0)+ for _ in range(20):+ junk = [+ torch.empty(rnd.randint(1, 400) * 2**20, device="cuda", dtype=torch.uint8)+ for _ in range(rnd.randint(1, 4))+ ]+ for fn in (MiniMaxH3VideoCausalConv3d.forward, _decomposed_forward):+ err = (fn(conv, x).float().cpu() - reference).abs().max().item()+ assert err < 0.1, f"{fn.__name__}: max abs error {err:.4f} vs CPU fp32"+ del junk+ if rnd.random() < 0.5:+ torch.cuda.empty_cache()