π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(morphology): run the convolution engine where isposinf has no kernel (#5472)
niukanen1eda494155d12
diff --git a/changelog.d/5472.fixed.md b/changelog.d/5472.fixed.mdnew file mode 100644index 000000000..e20572f36--- /dev/null+++ b/changelog.d/5472.fixed.md@@ -0,0 +1 @@+The convolution engine of the morphology operators no longer raises `NotImplementedError` on MPS with torch 2.5.1: the non-finite codes are now matched by scalar comparison instead of `torch.isposinf`/`torch.isneginf`, which lack an MPS kernel therediff --git a/kornia/morphology/morphology.py b/kornia/morphology/morphology.pyindex b4dc29de5..1236012a0 100644--- a/kornia/morphology/morphology.py+++ b/kornia/morphology/morphology.py@@ -392,10 +392,12 @@ def dilation(# ``conv2d`` multiplies every window cell by the one-hot weight, and ``inf * 0`` or ``nan * 0`` would# spread to the whole window. Convolve zeros in their place, then route a code for each non-finite# value (1: +inf, 2: -inf, 3: nan) through the same one-hot weight, which reproduces it exactly.+ # The non-finite codes are matched by scalar comparison because ``torch.isposinf``/``torch.isneginf``+ # have no MPS kernel on torch 2.5.1 (#5470).special = torch.zeros_like(output)if output.is_floating_point():- special = special.masked_fill(torch.isposinf(output), 1.0)- special = special.masked_fill(torch.isneginf(output), 2.0)+ special = special.masked_fill(output == float("inf"), 1.0)+ special = special.masked_fill(output == -float("inf"), 2.0)special = special.masked_fill(torch.isnan(output), 3.0)output = output.masked_fill(special != 0, 0.0)special = F.conv2d(special.view(B * C, 1, h_pad, w_pad), reshape_kernel, padding=0)@@ -574,10 +576,12 @@ def erosion(# ``conv2d`` multiplies every window cell by the one-hot weight, and ``inf * 0`` or ``nan * 0`` would# spread to the whole window. Convolve zeros in their place, then route a code for each non-finite# value (1: +inf, 2: -inf, 3: nan) through the same one-hot weight, which reproduces it exactly.+ # The non-finite codes are matched by scalar comparison because ``torch.isposinf``/``torch.isneginf``+ # have no MPS kernel on torch 2.5.1 (#5470).special = torch.zeros_like(output)if output.is_floating_point():- special = special.masked_fill(torch.isposinf(output), 1.0)- special = special.masked_fill(torch.isneginf(output), 2.0)+ special = special.masked_fill(output == float("inf"), 1.0)+ special = special.masked_fill(output == -float("inf"), 2.0)special = special.masked_fill(torch.isnan(output), 3.0)output = output.masked_fill(special != 0, 0.0)special = F.conv2d(special.view(B * C, 1, Hpad, Wpad), reshape_kernel, padding=0)diff --git a/tests/morphology/test_dilation.py b/tests/morphology/test_dilation.pyindex 285749b37..a3c983b16 100644--- a/tests/morphology/test_dilation.py+++ b/tests/morphology/test_dilation.py@@ -860,6 +860,32 @@ class TestDilate(BaseTester):expected_tensor = torch.tensor(expected, device=device, dtype=actual.dtype)torch.testing.assert_close(actual, expected_tensor, rtol=0.0, atol=0.0, equal_nan=True)+ def test_convention_convolution_engine_runs_where_isposinf_is_unimplemented_5470(self, device, dtype, monkeypatch):+ # torch 2.5.1 has no MPS kernel for `torch.isposinf`/`torch.isneginf`, so every+ # engine="convolution" call raised `NotImplementedError` there, finite input included. The+ # non-finite codes are now matched by scalar comparison. Simulate such a backend wherever+ # this suite runs and check the result stays exact, non-finite pixels included.+ def unimplemented(_tensor):+ raise NotImplementedError("aten::isposinf.out is not implemented for this device")++ nan, inf = float("nan"), float("inf")+ tensor = torch.tensor([[[[0.25, nan, 0.75, inf, 0.5, -inf, 0.75]]]], device=device, dtype=dtype)+ side_kernel = torch.tensor([[1.0, 0.0, 0.0]], device=device, dtype=dtype)+ cases = [+ (dilation, torch.ones(1, 3, device=device, dtype=dtype), [nan, nan, nan, inf, inf, 0.75, 0.75]),+ (erosion, torch.ones(1, 3, device=device, dtype=dtype), [nan, nan, nan, 0.5, -inf, -inf, -inf]),+ (dilation, side_kernel, [nan, 0.75, inf, 0.5, -inf, 0.75, -inf]),+ (erosion, side_kernel, [inf, 0.25, nan, 0.75, inf, 0.5, -inf]),+ ]++ with monkeypatch.context() as patch:+ patch.setattr(torch, "isposinf", unimplemented)+ patch.setattr(torch, "isneginf", unimplemented)+ for op, kernel, expected in cases:+ actual = op(tensor, kernel, engine="convolution").flatten()+ expected_tensor = torch.tensor(expected, device=device, dtype=actual.dtype)+ torch.testing.assert_close(actual, expected_tensor, rtol=0.0, atol=0.0, equal_nan=True)+@pytest.mark.parametrize("op", [dilation, erosion, opening, closing, gradient, top_hat, bottom_hat])@pytest.mark.parametrize("requires_grad", [False, True])def test_convention_shift_tracks_nan_when_maximum_ignores_it_4997(