Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test(filters): build the non-finite autograd reference for median_blur without a convolution (#5532)

εŒΊζ’“ηe7fef1edd233
diff --git a/tests/filters/test_median.py b/tests/filters/test_median.py
index 563aa34d5..d1652c2a7 100644
--- a/tests/filters/test_median.py
+++ b/tests/filters/test_median.py
@@ -247,14 +247,18 @@ class TestMedianBlur(BaseTester):
inp = torch.ones(1, 1, 7, 9, device=device, dtype=dtype)
inp[..., 3, 4] = invalid
inp.requires_grad_()
+ # The same window-local expectation as test_selection_nonfinite, built without a convolution: a bf16
+ # convolution reference spreads the NaN outside its windows on the x86 CI runners, which is what the
+ # autograd path stopped following.
radius = kernel_size // 2
- padded = torch.nn.functional.pad(
- inp,
- (radius, radius, radius, radius),
- mode=border_type,
- )
- weights = get_binary_kernel2d(kernel_size, device=device, dtype=dtype)
- expected = torch.nn.functional.conv2d(padded, weights, padding=0).median(1).values[:, None]
+ expected = torch.ones_like(inp)
+ if border_type == "constant":
+ for y in range(7):
+ for x in range(9):
+ rows = min(7, y + radius + 1) - max(0, y - radius)
+ cols = min(9, x + radius + 1) - max(0, x - radius)
+ expected[..., y, x] = float(rows * cols > kernel_size**2 // 2)
+ expected[..., 3 - radius : 4 + radius, 4 - radius : 5 + radius] = float("nan")
actual = median_blur(inp, kernel_size, border_type)
self.assert_close(actual.isnan(), expected.isnan())
self.assert_close(actual.nan_to_num(), expected.nan_to_num())