π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(filters): build the Hanning ramp in a floating dtype so the kernels export on torch 2.14 (#6129)
Dmytro Mishkin2c3c3a7fdfc3
diff --git a/changelog.d/6129.fixed.md b/changelog.d/6129.fixed.mdnew file mode 100644index 000000000..1c4e067ca--- /dev/null+++ b/changelog.d/6129.fixed.md@@ -0,0 +1 @@+`get_hanning_kernel1d` and `get_hanning_kernel2d` export with torch 2.14's default (dynamo) ONNX exporter again. Built without a `dtype`, the window multiplied an int64 ramp by a float, an `aten.mul.Scalar` that the exporter has no translation for with onnxscript 0.7.1 (`ConversionError`). The ramp is now converted to the default dtype first, the conversion the multiplication made implicitly on the CPU. Eager outputs are unchanged bit for bit with an explicit `dtype`, with a float32 or float64 default dtype, and on the CPU with any default dtype. With a float16 or bfloat16 default dtype on MPS, windows from 2057 (float16) or 259 (bfloat16) taps now round the ramp to that dtype as the CPU does, and differ from before by up to about 16 units in the last place.diff --git a/kornia/filters/kernels.py b/kornia/filters/kernels.pyindex 08f3a50af..ccdd705f5 100644--- a/kornia/filters/kernels.py+++ b/kornia/filters/kernels.py@@ -1320,6 +1320,11 @@ def get_hanning_kernel1d(_check_kernel_size(kernel_size, 2, allow_even=True)x = torch.arange(kernel_size, device=device, dtype=dtype)+ if dtype is None:+ # The int64 ramp is converted to the default dtype explicitly, as the multiplication below promoted it on the+ # CPU, without the int64-tensor-times-float ``aten.mul.Scalar`` that the torch 2.14 dynamo ONNX exporter cannot+ # translate with onnxscript 0.7.1.+ x = x.to(torch.get_default_dtype())return 0.5 - 0.5 * torch.cos(2.0 * math.pi * x / float(kernel_size - 1))diff --git a/tests/filters/test_hanning.py b/tests/filters/test_hanning.pyindex c5cb0e666..51d2c8eba 100644--- a/tests/filters/test_hanning.py+++ b/tests/filters/test_hanning.py@@ -56,6 +56,22 @@ def test_get_hanning_kernel2d_3x4(device, dtype):assert_close(kernel, expected)[email protected]("default_dtype", [torch.float64, torch.float16, torch.bfloat16])+def test_get_hanning_kernel1d_without_dtype_follows_the_default_dtype(default_dtype):+ # dtype=None: the default dtype, with the values of the int64 ramp promoted by the multiplication (#6123). CPU+ # only: MPS computes that promotion without rounding the ramp to a half default dtype. 4097 taps put ramp values+ # above what float16 and bfloat16 hold exactly, where building the ramp in the default dtype rounds differently.+ previous = torch.get_default_dtype()+ torch.set_default_dtype(default_dtype)+ try:+ kernel = get_hanning_kernel1d(4097)+ expected = 0.5 - 0.5 * torch.cos(2.0 * math.pi * torch.arange(4097) / 4096.0)+ finally:+ torch.set_default_dtype(previous)+ assert kernel.dtype == default_dtype+ assert torch.equal(kernel, expected)++class TestConventionsHanningKernels(BaseTester):@pytest.mark.parametrize("window_size", [4, 5, 6])def test_convention_hanning_kernel1d_is_the_unnormalised_symmetric_window(self, window_size, device, dtype):diff --git a/tests/onnx/test_export_coverage.py b/tests/onnx/test_export_coverage.pyindex d484fe0ad..bf86731a3 100644--- a/tests/onnx/test_export_coverage.py+++ b/tests/onnx/test_export_coverage.py@@ -108,6 +108,12 @@ def _cases():id="warp_perspective",),pytest.param(kornia.filters.MedianBlur((3, 3)), (img,), id="MedianBlur"),+ # A kernel factory has no tensor input: add it to one. The ramp was an int64 arange times a float, an+ # ``aten.mul.Scalar`` the 2.14 dynamo exporter has no translation for with onnxscript 0.7.1.+ pytest.param(_Fn(lambda x: kornia.filters.get_hanning_kernel1d(9) + x), (torch.zeros(9),), id="hanning1d"),+ pytest.param(+ _Fn(lambda x: kornia.filters.get_hanning_kernel2d((5, 7)) + x), (torch.zeros(5, 7),), id="hanning2d"+ ),pytest.param(_Fn(kornia.contrib.distance_transform), ((gray > 0.7).float(),), id="distance_transform"),pytest.param(kornia.losses.HausdorffERLoss(),