Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(morphology): cast conv weight/bias to input dtype in engine=convolution (#4544)

Aryan Kumar5967bc411692
diff --git a/changelog.d/4544.fixed.md b/changelog.d/4544.fixed.md
new file mode 100644
index 000000000..abdf62e14
--- /dev/null
+++ b/changelog.d/4544.fixed.md
@@ -0,0 +1 @@
+`kornia.morphology.dilation`/`erosion` (and `opening`, `closing`, `gradient`, `top_hat`, `bottom_hat`) no longer crash with `engine="convolution"` when the input tensor's dtype differs from the kernel's dtype; the result now preserves the input tensor's dtype.
diff --git a/kornia/morphology/morphology.py b/kornia/morphology/morphology.py
index 694576911..5c101ffea 100644
--- a/kornia/morphology/morphology.py
+++ b/kornia/morphology/morphology.py
@@ -112,9 +112,12 @@ def dilation(
elif engine == "convolution":
B, C, H, W = tensor.size()
h_pad, w_pad = output.shape[-2:]
- reshape_kernel = _neight2channels_like_kernel(kernel)
+ reshape_kernel = _neight2channels_like_kernel(kernel).to(dtype=output.dtype)
output, _ = F.conv2d(
- output.view(B * C, 1, h_pad, w_pad), reshape_kernel, padding=0, bias=neighborhood.view(-1).flip(0)
+ output.view(B * C, 1, h_pad, w_pad),
+ reshape_kernel,
+ padding=0,
+ bias=neighborhood.view(-1).flip(0).to(dtype=output.dtype),
).max(dim=1)
output = output.view(B, C, H, W)
else:
@@ -205,9 +208,12 @@ def erosion(
elif engine == "convolution":
B, C, H, W = tensor.size()
Hpad, Wpad = output.shape[-2:]
- reshape_kernel = _neight2channels_like_kernel(kernel)
+ reshape_kernel = _neight2channels_like_kernel(kernel).to(dtype=output.dtype)
output, _ = F.conv2d(
- output.view(B * C, 1, Hpad, Wpad), reshape_kernel, padding=0, bias=-neighborhood.view(-1)
+ output.view(B * C, 1, Hpad, Wpad),
+ reshape_kernel,
+ padding=0,
+ bias=-neighborhood.view(-1).to(dtype=output.dtype),
).min(dim=1)
output = output.view(B, C, H, W)
else:
diff --git a/tests/morphology/test_dilation.py b/tests/morphology/test_dilation.py
index 06920e0da..56aafedcf 100644
--- a/tests/morphology/test_dilation.py
+++ b/tests/morphology/test_dilation.py
@@ -167,3 +167,18 @@ class TestDilate(BaseTester):
expected = op(tensor, kernel)
assert_close(actual, expected)
+
+ def test_convolution_engine_dtype_mismatch(self, device, dtype):
+ # engine="convolution" used to crash when tensor.dtype != kernel.dtype, because the
+ # conv weight/bias were built from kernel.dtype instead of the input's dtype. See #4541.
+ # Passing a mismatched kernel must match casting the kernel to the input dtype up front;
+ # that is the same computation, so the results are bitwise equal (no tolerance needed).
+ other_dtype = torch.float16 if dtype == torch.float32 else torch.float32
+
+ tensor = torch.rand(1, 2, 5, 5, device=device, dtype=dtype)
+ kernel = torch.ones(3, 3, device=device, dtype=other_dtype)
+
+ result = dilation(tensor, kernel, engine="convolution")
+
+ assert result.dtype == dtype
+ self.assert_close(result, dilation(tensor, kernel.to(dtype), engine="convolution"))
diff --git a/tests/morphology/test_erosion.py b/tests/morphology/test_erosion.py
index 959f2d25f..d0013c09d 100644
--- a/tests/morphology/test_erosion.py
+++ b/tests/morphology/test_erosion.py
@@ -154,3 +154,18 @@ class TestErode(BaseTester):
expected = op(tensor, kernel)
assert_close(actual, expected)
+
+ def test_convolution_engine_dtype_mismatch(self, device, dtype):
+ # engine="convolution" used to crash when tensor.dtype != kernel.dtype, because the
+ # conv weight/bias were built from kernel.dtype instead of the input's dtype. See #4541.
+ # Passing a mismatched kernel must match casting the kernel to the input dtype up front;
+ # that is the same computation, so the results are bitwise equal (no tolerance needed).
+ other_dtype = torch.float16 if dtype == torch.float32 else torch.float32
+
+ tensor = torch.rand(1, 2, 5, 5, device=device, dtype=dtype)
+ kernel = torch.ones(3, 3, device=device, dtype=other_dtype)
+
+ result = erosion(tensor, kernel, engine="convolution")
+
+ assert result.dtype == dtype
+ self.assert_close(result, erosion(tensor, kernel.to(dtype), engine="convolution"))