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: make ALL strided surfaces of the conv3d decompositions contiguous
Lincoln Stein11a8f3ceb2c9
diff --git a/invokeai/backend/minimax_h3/rocm_causal_conv3d.py b/invokeai/backend/minimax_h3/rocm_causal_conv3d.pyindex 84fd82868b..a22080b32d 100644--- a/invokeai/backend/minimax_h3/rocm_causal_conv3d.py+++ b/invokeai/backend/minimax_h3/rocm_causal_conv3d.py@@ -35,23 +35,29 @@ 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 this decomposition hands to a kernel (or returns to the caller) MUST be+ # standard-contiguous. The naive expressions produce three legal-but-exotic strided views+ # that old MIOpen tolerated (it copied internally) but the MIOpen in torch's rocm7.2 wheels+ # consumes directly and mis-addresses, corrupting rows whenever H != W (observed as+ # horizontal line/tearing artifacts via the Wan VAE's identical decomposition; this encoder+ # shares the bug for non-square canvases):+ # 1. the tap input — at B=1 (every real encode) the reshape is satisfiable as a zero-copy+ # view whose channel stride (T*H*W) exceeds its batch stride (H*W);+ # 2. the tap weight — weight[:, :, k] is a strided view over the temporal dim for every+ # kT > 1 conv;+ # 3. the returned activation — reshape+transpose yields a transposed view consumed by+ # downstream norm/resample kernels.+ # The explicit copies cost a small fraction of the conv itself.out = Nonefor k in range(k_t):- # The conv2d input MUST be standard-NCHW contiguous. At B=1 (every real encode) the- # reshape below is satisfiable as a zero-copy VIEW whose channel stride (T*H*W) exceeds- # its batch stride (H*W) — a legal but exotic layout. Old MIOpen copied such inputs- # internally; the MIOpen shipped with torch's rocm7.2 wheels consumes the strides- # directly and mis-addresses rows whenever H != W (observed as horizontal line/tearing- # artifacts in the Wan VAE's identical decomposition; this encoder shares the bug for- # non-square canvases). The explicit copy costs a small fraction of the conv itself.xs = x[:, :, k : k + t_out].transpose(1, 2).reshape(b * t_out, c, h, w).contiguous()- o = F.conv2d(xs, module.weight[:, :, k], None)+ o = F.conv2d(xs, module.weight[:, :, k].contiguous(), None)out = o if out is None else out + oassert 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/invokeai/backend/wan/rocm_causal_conv3d.py b/invokeai/backend/wan/rocm_causal_conv3d.pyindex ea5a3833d6..75f9b128cd 100644--- a/invokeai/backend/wan/rocm_causal_conv3d.py+++ b/invokeai/backend/wan/rocm_causal_conv3d.py@@ -32,24 +32,28 @@ 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 this decomposition hands to a kernel (or returns to the caller) MUST be+ # standard-contiguous. The naive expressions produce three legal-but-exotic strided views+ # that old MIOpen tolerated (it copied internally) but the MIOpen in torch's rocm7.2 wheels+ # consumes directly and mis-addresses, corrupting rows whenever H != W (horizontal+ # line/tearing artifacts in decoded images):+ # 1. the tap input — at B=1 (every real decode) the reshape is satisfiable as a zero-copy+ # view whose channel stride (T*H*W) exceeds its batch stride (H*W);+ # 2. the tap weight — weight[:, :, k] is a strided view over the temporal dim for every+ # kT > 1 conv;+ # 3. the returned activation — reshape+transpose yields a transposed view consumed by+ # downstream norm/upsample kernels.+ # The explicit copies cost a small fraction of the conv itself.out = Nonefor k in range(k_t):- # The conv2d input MUST be standard-NCHW contiguous. At B=1 (every real decode) the- # reshape below is satisfiable as a zero-copy VIEW whose channel stride (T*H*W) exceeds- # its batch stride (H*W) — a legal but exotic layout. Old MIOpen copied such inputs- # internally; the MIOpen shipped with torch's rocm7.2 wheels consumes the strides- # directly and mis-addresses rows whenever H != W, producing horizontal line/tearing- # artifacts in decoded images. (At B > 1 the reshape is forced to copy, which is why- # the corruption never appeared in batched paths.) The explicit copy costs a small- # fraction of the conv itself.xs = x[:, :, k : k + t_out].transpose(1, 2).reshape(b * t_out, c, h, w).contiguous()- o = F.conv2d(xs, module.weight[:, :, k], None)+ o = F.conv2d(xs, module.weight[:, :, k].contiguous(), None)out = o if out is None else out + oassert 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, x: torch.Tensor, cache_x: torch.Tensor | None = None) -> torch.Tensor:diff --git a/tests/backend/minimax_h3/test_rocm_causal_conv3d.py b/tests/backend/minimax_h3/test_rocm_causal_conv3d.pyindex 159ad4f94d..fcec15166e 100644--- a/tests/backend/minimax_h3/test_rocm_causal_conv3d.py+++ b/tests/backend/minimax_h3/test_rocm_causal_conv3d.py@@ -92,13 +92,14 @@ def test_decomposed_conv3d_feeds_contiguous_conv2d_inputs(monkeypatch) -> None:seen: list[bool] = []original_conv2d = mod.F.conv2d- def checked_conv2d(x: torch.Tensor, *args, **kwargs) -> torch.Tensor:- seen.append(x.is_contiguous())- return original_conv2d(x, *args, **kwargs)+ def checked_conv2d(x: torch.Tensor, weight: torch.Tensor, *args, **kwargs) -> torch.Tensor:+ seen.append(x.is_contiguous() and weight.is_contiguous())+ return original_conv2d(x, weight, *args, **kwargs)monkeypatch.setattr(mod.F, "conv2d", checked_conv2d)conv = torch.nn.Conv3d(6, 10, kernel_size=(3, 3, 3))x = torch.randn(1, 6, 5, 12, 16) # B=1 and H != W: the exotic-view case- _decomposed_conv3d(conv, x)+ result = _decomposed_conv3d(conv, x)assert seen, "decomposition never called conv2d"- assert all(seen), "conv2d received a non-contiguous input"+ assert all(seen), "conv2d received a non-contiguous input or weight"+ assert result.is_contiguous(), "decomposition returned a non-contiguous activation"diff --git a/tests/backend/wan/test_rocm_causal_conv3d.py b/tests/backend/wan/test_rocm_causal_conv3d.pyindex cae7673cbd..011ecdce57 100644--- a/tests/backend/wan/test_rocm_causal_conv3d.py+++ b/tests/backend/wan/test_rocm_causal_conv3d.py@@ -99,13 +99,14 @@ def test_decomposed_conv3d_feeds_contiguous_conv2d_inputs(monkeypatch: pytest.Moseen: list[bool] = []original_conv2d = mod.F.conv2d- def checked_conv2d(x: torch.Tensor, *args, **kwargs) -> torch.Tensor:- seen.append(x.is_contiguous())- return original_conv2d(x, *args, **kwargs)+ def checked_conv2d(x: torch.Tensor, weight: torch.Tensor, *args, **kwargs) -> torch.Tensor:+ seen.append(x.is_contiguous() and weight.is_contiguous())+ return original_conv2d(x, weight, *args, **kwargs)monkeypatch.setattr(mod.F, "conv2d", checked_conv2d)conv = torch.nn.Conv3d(6, 10, kernel_size=(3, 3, 3))x = torch.randn(1, 6, 5, 12, 16) # B=1 and H != W: the exotic-view case- _decomposed_conv3d(conv, x)+ result = _decomposed_conv3d(conv, x)assert seen, "decomposition never called conv2d"- assert all(seen), "conv2d received a non-contiguous input"+ assert all(seen), "conv2d received a non-contiguous input or weight"+ assert result.is_contiguous(), "decomposition returned a non-contiguous activation"