π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
perf(filters): tap-loop bilateral filter for large forward-only CPU inputs (#6079)
Dmytro Mishkinb2ed2b8516b7
diff --git a/changelog.d/6079.fixed.md b/changelog.d/6079.fixed.mdnew file mode 100644index 000000000..b78fc8ddd--- /dev/null+++ b/changelog.d/6079.fixed.md@@ -0,0 +1,10 @@+`bilateral_blur` and `joint_bilateral_blur` run faster on CPU float32 and float64 inputs with at least 65,536 values+whose gradient is not recorded: the filter is accumulated tap by tap from shifted slices of the padded input instead of+from its unfolded windows, whose eight temporaries of `kernel_size` times the image were fresh memory faulted in on+every call. On batches of 256 x 256 images with a 5 x 5 kernel, measured on an i7-14700K under WSL2 (where page faults+cost more than on native Linux), the RGB filter is 3.0-3.2x faster at batch 8 and 2.8-3.0x at batch 32 in eager mode,+and the grayscale one 17-20x and 13-17x; with the faults removed from both paths by `GLIBC_TUNABLES`, the speed-ups are+1.8x and 1.75x (RGB) and 8.7x and 8.0x (grayscale). Under `torch.compile` the RGB filter is 1.06x and the grayscale one+2.9x faster; kernels of more than 25 taps keep the unfold path there, because the compiled tap loop takes longer to+compile with every tap. The values agree with the unfold path to rounding. Exported and traced programs, reverse-mode+gradients, CUDA, MPS and half precision keep the unfold path.diff --git a/kornia/filters/bilateral.py b/kornia/filters/bilateral.pyindex 01adf4ca5..b20cc8eff 100644--- a/kornia/filters/bilateral.py+++ b/kornia/filters/bilateral.py@@ -24,7 +24,7 @@ import torch.nn.functional as Ffrom torch import nnfrom kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SAME_SIZE, KORNIA_CHECK_SHAPE-from kornia.core.utils import is_compiling+from kornia.core.utils import is_compiling, is_exportingfrom .filter import _to_floatingfrom .kernels import _check_kernel_size, _unpack_2d_ks, get_gaussian_kernel2d@@ -36,6 +36,178 @@ def _check_sigma_batch(name: str, sigma: torch.Tensor, input: torch.Tensor) -> NKORNIA_CHECK_SAME_SIZE(sigma, input, allow_one=True, msg=f"{name} must have a batch of 1 or the input batch.")+# Measured on one image, 3 x 3 to 9 x 9 kernels, float32 and float64, with no page faults on either side: the tap loop+# overtakes the unfold path between 20k and 36k values (earlier for 3 x 3 kernels), so 65,536 values leaves a margin.+_BILATERAL_CPU_MIN_NUMEL = 65536+# The eager tap loop works through the batch in chunks of about this many input bytes, so that every tap's temporaries+# stay in cache: on 256 x 256 images, 2 MB chunks took 0.3x to 0.7x the time of one pass over a batch of 8 or 32.+_BILATERAL_CPU_CHUNK_BYTES = 2 * 1024 * 1024+# torch.compile unrolls the tap loop into one graph with a block of operations per tap, so its compile time grows with+# the tap count: a 9 x 9 kernel took 56 s to compile against 2.3 s for the unfold path. Larger kernels keep the unfold+# path under compile.+_BILATERAL_CPU_MAX_COMPILED_TAPS = 25+++def _records_gradient(+ input: torch.Tensor,+ guidance: Optional[torch.Tensor],+ sigma_color: float | torch.Tensor,+ sigma_space: tuple[float, float] | torch.Tensor,+) -> bool:+ """Whether autograd records a reverse-mode gradient through the filter."""+ return torch.is_grad_enabled() and (+ input.requires_grad+ or (guidance is not None and guidance.requires_grad)+ or (isinstance(sigma_color, torch.Tensor) and sigma_color.requires_grad)+ or (isinstance(sigma_space, torch.Tensor) and sigma_space.requires_grad)+ )+++def _bilateral_blur_cpu_eligible(+ input: torch.Tensor,+ guidance: Optional[torch.Tensor],+ sigma_color: float | torch.Tensor,+ sigma_space: tuple[float, float] | torch.Tensor,+) -> bool:+ """Select large forward-only native-precision CPU inputs, where a tap loop beats unfolding every window."""+ # Keep the unfold path where it is what the caller captured or differentiates: the exporters and the legacy tracer+ # export it, and its reverse mode is one unfold backward instead of a chain of per-tap backwards.+ return (+ input.device.type == "cpu"+ and input.dtype in (torch.float32, torch.float64)+ and (guidance is None or guidance.device.type == "cpu")+ # Before the size check: torch.export would turn it into a guard of the exported program, which then fails at+ # run time for an image on the other side of the threshold although its dynamic shapes allow it.+ and not is_exporting()+ and input.numel() >= _BILATERAL_CPU_MIN_NUMEL+ and not torch.jit.is_tracing()+ and not _records_gradient(input, guidance, sigma_color, sigma_space)+ )+++def _bilateral_blur_taps(+ image: torch.Tensor,+ guide: Optional[torch.Tensor],+ kernel_size: tuple[int, int],+ color_scale: float | torch.Tensor,+ space_kernel: torch.Tensor,+ border_type: str,+ color_distance_type: str,+ in_place: bool,+ out: Optional[torch.Tensor],+) -> torch.Tensor:+ """Filter one batch chunk tap by tap, reusing its own temporaries ``in_place``, into ``out`` if given."""+ ky, kx = kernel_size+ height, width = image.shape[-2], image.shape[-1]+ pad_y, pad_x = (ky - 1) // 2, (kx - 1) // 2+ # The unfold path returned a contiguous result for any input layout.+ image = image.contiguous()+ padded_image = F.pad(image, (pad_x, pad_x, pad_y, pad_y), mode=border_type)+ if guide is None:+ guide, padded_guide = image, padded_image+ else:+ guide = guide.contiguous()+ padded_guide = F.pad(guide, (pad_x, pad_x, pad_y, pad_y), mode=border_type)+ if color_distance_type not in ("l1", "l2"):+ raise ValueError("color_distance_type only accepts l1 or l2")++ def tap_weight(tap: int) -> tuple[torch.Tensor, torch.Tensor]:+ # The unfold path's kernel entry for this tap, computed with the same operations in the same order.+ dy, dx = tap // kx, tap % kx+ diff = padded_guide[..., dy : dy + height, dx : dx + width] - guide+ if diff.shape[1] == 1:+ # One channel's sum is the value itself, and |d|^2 == d^2: both distances are the squared difference.+ color_distance_sq = diff.square_() if in_place else diff.square()+ elif color_distance_type == "l1":+ color_distance_sq = (diff.abs_() if in_place else diff.abs()).sum(1, keepdim=True)+ color_distance_sq = color_distance_sq.square_() if in_place else color_distance_sq.square()+ else:+ color_distance_sq = (diff.square_() if in_place else diff.square()).sum(1, keepdim=True)+ space_weight = space_kernel[:, tap : tap + 1]+ if in_place:+ weight = color_distance_sq.mul_(color_scale).exp_().mul_(space_weight)+ else:+ weight = space_weight * (color_scale * color_distance_sq).exp()+ return padded_image[..., dy : dy + height, dx : dx + width], weight++ values, weight = tap_weight(0)+ weighted = values * weight if out is None else out.copy_(values).mul_(weight)+ normalizer = weight+ for tap in range(1, ky * kx):+ values, weight = tap_weight(tap)+ if in_place:+ weighted.addcmul_(values, weight)+ normalizer.add_(weight)+ else:+ weighted = weighted.addcmul(values, weight)+ normalizer = normalizer + weight+ return weighted.div_(normalizer) if in_place else weighted / normalizer+++def _bilateral_blur_cpu(+ input: torch.Tensor,+ guidance: Optional[torch.Tensor],+ kernel_size: tuple[int, int] | int,+ sigma_color: float | torch.Tensor,+ sigma_space: tuple[float, float] | torch.Tensor,+ border_type: str,+ color_distance_type: str,+) -> torch.Tensor:+ """Bilateral filter as a loop over the kernel taps, without unfolding every window.++ The unfold path materialises several :math:`(B, C, H, W, k_y, k_x)` tensors, :math:`k_y k_x` times the image; on a+ large CPU batch they are fresh memory, faulted in on every call. Here every tap takes shifted slices of the padded+ guidance and input, computes its weights with the unfold path's expression, so they are the same numbers, and adds+ them to the weighted sum and to the normalizer; only the order of the :math:`k_y k_x` terms of those two sums+ differs. Tensor sigmas have the input's dtype and device and a batch of 1 or the input's.+ """+ ky, kx = _unpack_2d_ks(kernel_size)+ space_kernel = get_gaussian_kernel2d(kernel_size, sigma_space, device=input.device, dtype=input.dtype)+ space_kernel = space_kernel.view(-1, ky * kx, 1, 1)+ color_scale = -0.5 / sigma_color**2+ if isinstance(color_scale, torch.Tensor):+ color_scale = color_scale.view(-1, 1, 1, 1)+ # In place and chunk by chunk only where nothing records the arithmetic: autograd needs the temporaries that the+ # in-place operations overwrite, vmap has no batching rule for addcmul_, and compilers fuse the functional loop over+ # the whole batch without these allocations. Every value is computed the same way in both forms.+ in_place = (+ not is_compiling()+ and not torch._C._are_functorch_transforms_active()+ and not _records_gradient(input, guidance, sigma_color, sigma_space)+ )+ if not in_place:+ return _bilateral_blur_taps(+ input, guidance, (ky, kx), color_scale, space_kernel, border_type, color_distance_type, False, None+ )+ batch = input.shape[0]+ channels = input.shape[1] if guidance is None else max(input.shape[1], guidance.shape[1])+ chunk = max(1, _BILATERAL_CPU_CHUNK_BYTES // (channels * input.shape[-2] * input.shape[-1] * input.element_size()))+ if chunk >= batch:+ return _bilateral_blur_taps(+ input, guidance, (ky, kx), color_scale, space_kernel, border_type, color_distance_type, True, None+ )+ output = torch.empty(input.shape, device=input.device, dtype=input.dtype)+ for start in range(0, batch, chunk):+ stop = min(start + chunk, batch)+ scale = color_scale+ if isinstance(scale, torch.Tensor) and scale.shape[0] != 1:+ scale = scale[start:stop]+ space = space_kernel if space_kernel.shape[0] == 1 else space_kernel[start:stop]+ guide = None if guidance is None else guidance[start:stop]+ _bilateral_blur_taps(+ input[start:stop],+ guide,+ (ky, kx),+ scale,+ space,+ border_type,+ color_distance_type,+ True,+ output[start:stop],+ )+ return output++def _bilateral_blur(input: torch.Tensor,guidance: Optional[torch.Tensor],@@ -68,7 +240,7 @@ def _bilateral_blur(# `bool()` on a tensor is untraceable by dynamo; skip the data-dependent check under compile.if not is_compiling() and not bool((sigma_color > 0).all()):KORNIA_CHECK(False, f"sigma_color must be positive. Got {sigma_color}")- sigma_color = sigma_color.to(device=input.device, dtype=input.dtype).view(-1, 1, 1, 1, 1, 1)+ sigma_color = sigma_color.to(device=input.device, dtype=input.dtype)elif not sigma_color > 0:KORNIA_CHECK(False, f"sigma_color must be positive. Got {sigma_color}")@@ -81,6 +253,20 @@ def _bilateral_blur(ky, kx = _unpack_2d_ks(kernel_size)_check_kernel_size((ky, kx))++ # the colour kernel takes the guidance's dtype, so a wider guidance would promote the output+ if guidance is not None and input.is_floating_point() and guidance.is_floating_point():+ guidance = guidance.to(input.dtype)++ if _bilateral_blur_cpu_eligible(input, guidance, sigma_color, sigma_space) and (+ not is_compiling() or ky * kx <= _BILATERAL_CPU_MAX_COMPILED_TAPS+ ):+ return _bilateral_blur_cpu(+ input, guidance, kernel_size, sigma_color, sigma_space, border_type, color_distance_type+ )++ if isinstance(sigma_color, torch.Tensor):+ sigma_color = sigma_color.view(-1, 1, 1, 1, 1, 1)pad_y, pad_x = _compute_zero_padding(kernel_size)# Keep both patch axes: flattening them would copy every overlapping window.@@ -91,9 +277,6 @@ def _bilateral_blur(guidance = inputunfolded_guidance = unfolded_inputelse:- # the colour kernel takes the guidance's dtype, so a wider guidance would promote the output- if input.is_floating_point() and guidance.is_floating_point():- guidance = guidance.to(input.dtype)padded_guidance = F.pad(guidance, (pad_x, pad_x, pad_y, pad_y), mode=border_type)unfolded_guidance = padded_guidance.unfold(2, ky, 1).unfold(3, kx, 1) # (B, C, H, W, Ky, Kx)@@ -142,6 +325,13 @@ def bilateral_blur(its device.- An integer or bool input is differenced and averaged in float32 and the output is float32, as in:func:`~kornia.filters.filter2d`, so uint8 differences do not wrap.+ - On the CPU, a float32 or float64 input (after that conversion) with at least 65,536 values is filtered tap+ by tap from shifted slices of the padded input instead of from its unfolded windows, which avoids+ temporaries of ``kernel_size`` times the image, unless a reverse-mode gradient is recorded through any+ argument, or ``torch.export`` or ``torch.jit.trace`` captures the call, so an exported program keeps the+ windows for every input size. Under ``torch.compile``, only kernels of up to 25 taps are filtered tap by+ tap, since the compiled loop has a block of operations per tap. Both forms compute the same weights and add+ up the weighted values in a different order, so they agree to rounding.Arguments:input: the input torch.Tensor with shape :math:`(B,C,H,W)`.diff --git a/tests/filters/test_bilateral_cpu.py b/tests/filters/test_bilateral_cpu.pynew file mode 100644index 000000000..69dd498dc--- /dev/null+++ b/tests/filters/test_bilateral_cpu.py@@ -0,0 +1,578 @@+# LICENSE HEADER MANAGED BY add-license-header+#+# Copyright 2018 Kornia Team+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++import importlib+import warnings+from typing import Optional, Union++import pytest+import torch+import torch.nn.functional as F++from kornia.filters import bilateral_blur, joint_bilateral_blur+from kornia.filters.bilateral import _bilateral_blur_cpu, _bilateral_blur_cpu_eligible+from kornia.filters.kernels import get_gaussian_kernel2d++from testing.base import DYNAMO_UNAVAILABLE_REASON, BaseTester, dynamo_is_available++bilateral_module = importlib.import_module("kornia.filters.bilateral")++# One image with 3 x 148 x 148 = 65,712 values: just above the tap loop's threshold.+_ELIGIBLE = (1, 3, 148, 148)+++def _require_native_cpu(device: torch.device, dtype: torch.dtype) -> None:+ if device.type != "cpu" or dtype not in (torch.float32, torch.float64):+ pytest.skip("the CPU tap-loop bilateral path supports native CPU float32 and float64 only")+++def _cpu_tolerance(dtype: torch.dtype) -> float:+ # The per-tap weights are the unfold path's; only the order of the kH * kW-term sums differs.+ return 1e-6 if dtype == torch.float32 else 1e-13+++def _unfold_reference(+ input: torch.Tensor,+ guidance: Optional[torch.Tensor],+ kernel_size: Union[tuple[int, int], int],+ sigma_color: Union[float, torch.Tensor],+ sigma_space: Union[tuple[float, float], torch.Tensor],+ border_type: str = "reflect",+ color_distance_type: str = "l1",+) -> torch.Tensor:+ """The unfold-based bilateral filter that the tap loop replaces on large CPU inputs, for floating inputs."""+ ky, kx = (kernel_size, kernel_size) if isinstance(kernel_size, int) else kernel_size+ pad_y, pad_x = (ky - 1) // 2, (kx - 1) // 2+ if isinstance(sigma_color, torch.Tensor):+ sigma_color = sigma_color.to(device=input.device, dtype=input.dtype).view(-1, 1, 1, 1, 1, 1)+ if isinstance(sigma_space, torch.Tensor):+ sigma_space = sigma_space.to(device=input.device, dtype=input.dtype)+ padded_input = F.pad(input, (pad_x, pad_x, pad_y, pad_y), mode=border_type)+ unfolded_input = padded_input.unfold(2, ky, 1).unfold(3, kx, 1)+ if guidance is None:+ guidance = input+ unfolded_guidance = unfolded_input+ else:+ guidance = guidance.to(input.dtype)+ padded_guidance = F.pad(guidance, (pad_x, pad_x, pad_y, pad_y), mode=border_type)+ unfolded_guidance = padded_guidance.unfold(2, ky, 1).unfold(3, kx, 1)+ diff = unfolded_guidance - guidance[..., None, None]+ if color_distance_type == "l1":+ color_distance_sq = diff.abs().sum(1, keepdim=True).square()+ elif color_distance_type == "l2":+ color_distance_sq = diff.square().sum(1, keepdim=True)+ else:+ raise ValueError("color_distance_type only accepts l1 or l2")+ color_kernel = (-0.5 / sigma_color**2 * color_distance_sq).exp()+ space_kernel = get_gaussian_kernel2d(kernel_size, sigma_space, device=input.device, dtype=input.dtype)+ kernel = space_kernel.view(-1, 1, 1, 1, ky, kx) * color_kernel+ return (unfolded_input * kernel).sum((-2, -1)) / kernel.sum((-2, -1))+++def _counting(monkeypatch: pytest.MonkeyPatch) -> list:+ calls = []++ def counting(*args, **kwargs):+ calls.append(args[0].shape)+ return _bilateral_blur_cpu(*args, **kwargs)++ monkeypatch.setattr(bilateral_module, "_bilateral_blur_cpu", counting)+ return calls+++def _forbid_tap_loop(monkeypatch: pytest.MonkeyPatch) -> None:+ def fail(*args, **kwargs):+ raise AssertionError("the tap loop must not run here")++ monkeypatch.setattr(bilateral_module, "_bilateral_blur_cpu", fail)+++def _assert_same_nonfinite(actual: torch.Tensor, expected: torch.Tensor) -> None:+ assert torch.equal(actual.isnan(), expected.isnan())+ assert torch.equal(actual == float("inf"), expected == float("inf"))+ assert torch.equal(actual == -float("inf"), expected == -float("inf"))+++class TestBilateralBlurCpuTapLoop(BaseTester):+ @pytest.mark.parametrize("border_type", ["constant", "reflect", "replicate", "circular"])+ @pytest.mark.parametrize("color_distance_type", ["l1", "l2"])+ @pytest.mark.parametrize("channels", [1, 2, 3])+ def test_matches_unfold_path(self, border_type, color_distance_type, channels, monkeypatch, device, dtype):+ _require_native_cpu(device, dtype)+ calls = _counting(monkeypatch)+ image = torch.rand(2, channels, 181, 183, device=device, dtype=dtype)++ actual = bilateral_blur(image, (5, 7), 0.1, (1.5, 2.0), border_type, color_distance_type)+ expected = _unfold_reference(image, None, (5, 7), 0.1, (1.5, 2.0), border_type, color_distance_type)++ assert len(calls) == 1+ tolerance = _cpu_tolerance(dtype)+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ @pytest.mark.parametrize("kernel_size", [1, 3, (3, 5), (7, 3), 9])+ def test_kernel_sizes(self, kernel_size, device, dtype):+ _require_native_cpu(device, dtype)+ image = torch.rand(*_ELIGIBLE, device=device, dtype=dtype)++ actual = bilateral_blur(image, kernel_size, 0.3, (1.0, 2.5))+ expected = _unfold_reference(image, None, kernel_size, 0.3, (1.0, 2.5))++ tolerance = _cpu_tolerance(dtype)+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ @pytest.mark.parametrize("channels, guidance_channels", [(3, 1), (1, 3), (2, 3), (3, 3)])+ @pytest.mark.parametrize("color_distance_type", ["l1", "l2"])+ def test_joint_matches_unfold_path(+ self, channels, guidance_channels, color_distance_type, monkeypatch, device, dtype+ ):+ _require_native_cpu(device, dtype)+ calls = _counting(monkeypatch)+ image = torch.rand(2, channels, 131, 257, device=device, dtype=dtype)+ guidance = torch.rand(2, guidance_channels, 131, 257, device=device, dtype=dtype)++ actual = joint_bilateral_blur(image, guidance, 5, 0.2, (1.5, 1.5), "reflect", color_distance_type)+ expected = _unfold_reference(image, guidance, 5, 0.2, (1.5, 1.5), "reflect", color_distance_type)++ assert len(calls) == 1+ tolerance = _cpu_tolerance(dtype)+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ @pytest.mark.parametrize("guidance_dtype", [torch.float16, torch.float32, torch.float64, torch.uint8])+ def test_joint_guidance_takes_the_input_dtype(self, guidance_dtype, monkeypatch, device, dtype):+ _require_native_cpu(device, dtype)+ calls = _counting(monkeypatch)+ image = torch.rand(2, 3, 131, 257, device=device, dtype=dtype)+ guidance = (torch.rand(2, 1, 131, 257, device=device) * 255).to(guidance_dtype)++ actual = joint_bilateral_blur(image, guidance, 5, 30.0, (1.5, 1.5))++ assert len(calls) == 1+ assert actual.dtype == dtype+ tolerance = _cpu_tolerance(dtype)+ expected = _unfold_reference(image, guidance.to(dtype), 5, 30.0, (1.5, 1.5))+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ @pytest.mark.parametrize("chunk_images", [1, 2, 3])+ @pytest.mark.parametrize("per_sample", [False, True], ids=["shared_sigma", "per_sample_sigma"])+ def test_batch_chunks(self, chunk_images, per_sample, monkeypatch, device, dtype):+ # Eager calls work through the batch in chunks; per-sample sigmas must follow their samples across chunks,+ # including a last chunk that is shorter than the others.+ _require_native_cpu(device, dtype)+ image = torch.rand(5, 3, 64, 96, device=device, dtype=dtype)+ guidance = torch.rand(5, 2, 64, 96, device=device, dtype=dtype)+ rows = 5 if per_sample else 1+ sigma_color = torch.rand(rows, device=device, dtype=dtype) + 0.05+ sigma_space = torch.rand(rows, 2, device=device, dtype=dtype) + 0.5+ monkeypatch.setattr(bilateral_module, "_BILATERAL_CPU_CHUNK_BYTES", chunk_images * image[0].nbytes)++ tolerance = _cpu_tolerance(dtype)+ for guide in (None, guidance):+ actual = _bilateral_blur_cpu(image, guide, (3, 5), sigma_color, sigma_space, "reflect", "l1")+ expected = _unfold_reference(image, guide, (3, 5), sigma_color, sigma_space, "reflect", "l1")+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ def test_chunks_do_not_change_values(self, monkeypatch, device, dtype):+ # Chunking only changes which samples each operation sees; every value is computed the same way.+ _require_native_cpu(device, dtype)+ image = torch.rand(6, 3, 61, 67, device=device, dtype=dtype)+ sigma_color = torch.rand(6, device=device, dtype=dtype) + 0.05++ whole = _bilateral_blur_cpu(image, None, 5, sigma_color, (1.5, 1.5), "reflect", "l1")+ monkeypatch.setattr(bilateral_module, "_BILATERAL_CPU_CHUNK_BYTES", 2 * image[0].nbytes)+ chunked = _bilateral_blur_cpu(image, None, 5, sigma_color, (1.5, 1.5), "reflect", "l1")++ self.assert_close(chunked, whole, rtol=0.0, atol=0.0)++ @pytest.mark.parametrize("color_distance_type", ["l1", "l2"])+ def test_functional_form_computes_the_same_values(self, color_distance_type, monkeypatch, device, dtype):+ # Compiled calls run the functional form of the loop over the whole batch; its operations are the in-place+ # form's, so the values are identical.+ _require_native_cpu(device, dtype)+ image = torch.rand(4, 3, 61, 67, device=device, dtype=dtype)+ guidance = torch.rand(4, 2, 61, 67, device=device, dtype=dtype)+ sigma_color = torch.rand(4, device=device, dtype=dtype) + 0.05+ monkeypatch.setattr(bilateral_module, "_BILATERAL_CPU_CHUNK_BYTES", image[0].nbytes)+ args = (5, sigma_color, (1.5, 1.5), "reflect", color_distance_type)++ in_place = [_bilateral_blur_cpu(image, guide, *args) for guide in (None, guidance)]+ monkeypatch.setattr(bilateral_module, "is_compiling", lambda: True)+ functional = [_bilateral_blur_cpu(image, guide, *args) for guide in (None, guidance)]++ for actual, expected in zip(functional, in_place):+ self.assert_close(actual, expected, rtol=0.0, atol=0.0)++ @pytest.mark.parametrize("joint", [False, True])+ def test_preserves_nonfinite_pattern(self, joint, device, dtype):+ _require_native_cpu(device, dtype)+ generator = torch.Generator().manual_seed(0)+ image = torch.rand(2, 3, 128, 160, generator=generator, dtype=dtype)+ flat = image.view(-1)+ positions = torch.randperm(flat.numel(), generator=generator)[:60]+ flat[positions[:20]] = float("nan")+ flat[positions[20:40]] = float("inf")+ flat[positions[40:]] = -float("inf")+ image = image.to(device)+ # A finite guidance gives the non-finite input values positive weights, so infinities reach the output.+ guidance = torch.rand(2, 1, 128, 160, generator=generator, dtype=dtype).to(device) if joint else None++ for sigma_color in (0.1, 10.0):+ actual = _bilateral_blur_cpu(image, guidance, 5, sigma_color, (1.5, 1.5), "reflect", "l1")+ expected = _unfold_reference(image, guidance, 5, sigma_color, (1.5, 1.5), "reflect", "l1")++ _assert_same_nonfinite(actual, expected)+ tolerance = _cpu_tolerance(dtype)+ self.assert_close(actual.nan_to_num(), expected.nan_to_num(), rtol=tolerance, atol=tolerance)+ if joint:+ assert actual.isinf().any()++ def test_large_finite_values_stay_finite(self, device, dtype):+ # The weights of a pixel sum to one up to rounding, so no partial weighted sum of values within half the+ # dtype's range can overflow, in either summation order.+ _require_native_cpu(device, dtype)+ huge = torch.finfo(dtype).max / 2+ image = torch.zeros(1, 1, 256, 256, device=device, dtype=dtype)+ image[..., 100:, :] = huge+ image[..., :, 200:] = -huge+ guidance = torch.zeros(1, 1, 256, 256, device=device, dtype=dtype)++ actual = _bilateral_blur_cpu(image, guidance, 5, 1.0, (1.5, 1.5), "reflect", "l1")+ expected = _unfold_reference(image, guidance, 5, 1.0, (1.5, 1.5), "reflect", "l1")++ assert torch.isfinite(expected).all()+ assert torch.isfinite(actual).all()+ self.assert_close(actual / huge, expected / huge, rtol=1e-6, atol=1e-6)++ @pytest.mark.parametrize(+ "shape, eligible",+ [+ ((1, 1, 256, 255), False),+ ((1, 1, 256, 256), True),+ ((1, 3, 147, 148), False),+ ((1, 3, 148, 148), True),+ ((4, 1, 128, 128), True),+ ((1, 2, 3, 20000), True),+ ],+ )+ def test_threshold_and_odd_shapes(self, shape, eligible, device, dtype):+ _require_native_cpu(device, dtype)+ image = torch.rand(*shape, device=device, dtype=dtype)+ assert _bilateral_blur_cpu_eligible(image, None, 0.1, (1.5, 1.5)) == eligible++ tolerance = _cpu_tolerance(dtype)+ self.assert_close(+ bilateral_blur(image, 3, 0.1, (1.5, 1.5), "replicate"),+ _unfold_reference(image, None, 3, 0.1, (1.5, 1.5), "replicate"),+ rtol=tolerance,+ atol=tolerance,+ )++ def test_small_input_uses_unfold(self, monkeypatch, device, dtype):+ _require_native_cpu(device, dtype)+ _forbid_tap_loop(monkeypatch)+ image = torch.rand(1, 3, 32, 32, device=device, dtype=dtype)++ self.assert_close(bilateral_blur(image, 5, 0.1, (1.5, 1.5)), _unfold_reference(image, None, 5, 0.1, (1.5, 1.5)))++ def test_other_devices_and_dtypes_use_unfold(self, monkeypatch, device, dtype):+ if device.type == "cpu" and dtype in (torch.float32, torch.float64):+ pytest.skip("covered by the tap-loop tests")+ _forbid_tap_loop(monkeypatch)+ image = torch.rand(1, 3, 160, 160, device=device, dtype=dtype)++ self.assert_close(bilateral_blur(image, 5, 0.1, (1.5, 1.5)), _unfold_reference(image, None, 5, 0.1, (1.5, 1.5)))++ @pytest.mark.parametrize("which", ["input", "guidance", "sigma_color", "sigma_space"])+ def test_reverse_mode_gradients_use_unfold(self, which, monkeypatch, device, dtype):+ _require_native_cpu(device, dtype)+ _forbid_tap_loop(monkeypatch)+ tensors = {+ "input": torch.rand(*_ELIGIBLE, device=device, dtype=dtype),+ "guidance": torch.rand(1, 1, 148, 148, device=device, dtype=dtype),+ "sigma_color": torch.tensor([0.2], device=device, dtype=dtype),+ "sigma_space": torch.tensor([[1.5, 2.0]], device=device, dtype=dtype),+ }+ if which != "guidance":+ tensors["guidance"] = None+ tensors[which].requires_grad_()+ reference = {+ name: None if tensor is None else tensor.detach().clone().requires_grad_(name == which)+ for name, tensor in tensors.items()+ }+ weights = torch.rand(*_ELIGIBLE, device=device, dtype=dtype)++ def run(op, args):+ return (op(args["input"], args["guidance"], 5, args["sigma_color"], args["sigma_space"]) * weights).sum()++ if tensors["guidance"] is None:+ actual = (+ bilateral_blur(tensors["input"], 5, tensors["sigma_color"], tensors["sigma_space"]) * weights+ ).sum()+ else:+ actual = run(joint_bilateral_blur, tensors)+ actual.backward()+ run(_unfold_reference, reference).backward()++ self.assert_close(tensors[which].grad, reference[which].grad)++ @pytest.mark.parametrize("mode", ["no_grad", "inference_mode"])+ def test_requires_grad_input_without_recorded_gradients_takes_the_tap_loop(self, mode, monkeypatch, device, dtype):+ _require_native_cpu(device, dtype)+ calls = _counting(monkeypatch)+ image = torch.rand(*_ELIGIBLE, device=device, dtype=dtype, requires_grad=True)+ sigma_color = torch.tensor([0.2], device=device, dtype=dtype, requires_grad=True)++ context = torch.no_grad() if mode == "no_grad" else torch.inference_mode()+ with context:+ actual = bilateral_blur(image, 5, sigma_color, (1.5, 1.5))++ assert len(calls) == 1+ tolerance = _cpu_tolerance(dtype)+ expected = _unfold_reference(image.detach(), None, 5, sigma_color.detach(), (1.5, 1.5))+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ def test_jit_trace_uses_unfold(self, monkeypatch, device, dtype):+ _require_native_cpu(device, dtype)+ image = torch.rand(*_ELIGIBLE, device=device, dtype=dtype)+ _forbid_tap_loop(monkeypatch)++ with warnings.catch_warnings():+ warnings.simplefilter("ignore")+ # check_trace would rerun the function eagerly, where the tap loop is the right path+ traced = torch.jit.trace(lambda x: bilateral_blur(x, 5, 0.1, (1.5, 1.5)), (image,), check_trace=False)+ self.assert_close(traced(image), _unfold_reference(image, None, 5, 0.1, (1.5, 1.5)))++ def test_layouts_give_contiguous_outputs(self, device, dtype):+ # The unfold path returned contiguous outputs for any input layout; callers may .view() them.+ _require_native_cpu(device, dtype)+ image = torch.rand(2, 3, 148, 150, device=device, dtype=dtype)+ transposed = image.transpose(-1, -2).contiguous().transpose(-1, -2)+ channels_last = image.contiguous(memory_format=torch.channels_last)+ guidance = torch.rand(2, 1, 148, 150, device=device, dtype=dtype).contiguous(memory_format=torch.channels_last)++ tolerance = _cpu_tolerance(dtype)+ for variant in (transposed, channels_last):+ assert _bilateral_blur_cpu_eligible(variant, None, 0.1, (1.5, 1.5))+ for actual, guide in (+ (bilateral_blur(variant, 5, 0.1, (1.5, 1.5)), None),+ (joint_bilateral_blur(variant, guidance, 5, 0.1, (1.5, 1.5)), guidance),+ ):+ assert actual.is_contiguous()+ assert actual.dtype == dtype+ expected = _unfold_reference(image, guide, 5, 0.1, (1.5, 1.5))+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)+ # a chunked batch is assembled in a contiguous buffer as well+ batch = torch.rand(8, 3, 160, 160, device=device, dtype=dtype).contiguous(memory_format=torch.channels_last)+ assert bilateral_blur(batch, 3, 0.1, (1.5, 1.5)).is_contiguous()++ def test_integer_input_is_filtered_in_float32(self, monkeypatch, device, dtype):+ _require_native_cpu(device, dtype)+ calls = _counting(monkeypatch)+ image = torch.randint(0, 256, _ELIGIBLE, device=device, dtype=torch.uint8)++ actual = bilateral_blur(image, 5, 25.0, (1.5, 1.5))++ assert len(calls) == 1+ assert actual.dtype == torch.float32+ expected = _unfold_reference(image.float(), None, 5, 25.0, (1.5, 1.5))+ self.assert_close(actual, expected, rtol=1e-6, atol=1e-4)++ @pytest.mark.parametrize("channels", [1, 3])+ def test_invalid_color_distance_type_raises(self, channels, device, dtype):+ _require_native_cpu(device, dtype)+ image = torch.rand(1, channels, 256, 256, device=device, dtype=dtype)++ with pytest.raises(ValueError, match="color_distance_type only accepts l1 or l2"):+ bilateral_blur(image, 5, 0.1, (1.5, 1.5), color_distance_type="l3")++ def test_reflect_padding_larger_than_the_image_raises_as_before(self, device, dtype):+ _require_native_cpu(device, dtype)+ image = torch.rand(1, 1, 2, 40000, device=device, dtype=dtype)++ with pytest.raises(RuntimeError) as expected:+ _unfold_reference(image, None, 5, 0.1, (1.5, 1.5), color_distance_type="l3")+ with pytest.raises(RuntimeError) as actual:+ bilateral_blur(image, 5, 0.1, (1.5, 1.5), color_distance_type="l3")+ assert str(actual.value) == str(expected.value)++ def test_cpu_autocast_does_not_change_the_result(self, device, dtype):+ # No operation of either path is on an autocast list except the shared padding, so autocast needs no clause.+ _require_native_cpu(device, dtype)+ image = torch.rand(*_ELIGIBLE, device=device, dtype=dtype)++ with torch.autocast("cpu", dtype=torch.bfloat16):+ actual = bilateral_blur(image, 5, 0.1, (1.5, 1.5))+ expected = _unfold_reference(image, None, 5, 0.1, (1.5, 1.5))++ assert actual.dtype == dtype+ tolerance = _cpu_tolerance(dtype)+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ def test_public_vmap_has_no_user_warning(self, device, dtype):+ _require_native_cpu(device, dtype)+ images = torch.rand(2, 1, 3, 148, 148, device=device, dtype=dtype)+ guidance = torch.rand(2, 1, 1, 148, 148, device=device, dtype=dtype)+ tolerance = _cpu_tolerance(dtype)++ with warnings.catch_warnings(record=True) as recorded:+ warnings.simplefilter("always")+ actual = torch.vmap(lambda x: bilateral_blur(x, 5, 0.1, (1.5, 1.5)))(images)+ joint = torch.vmap(lambda x, g: joint_bilateral_blur(x, g, 5, 0.1, (1.5, 1.5)))(images, guidance)+ assert not any(issubclass(warning.category, UserWarning) for warning in recorded)+ expected = torch.stack([_unfold_reference(image, None, 5, 0.1, (1.5, 1.5)) for image in images])+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)+ expected = torch.stack(+ [_unfold_reference(image, guide, 5, 0.1, (1.5, 1.5)) for image, guide in zip(images, guidance)]+ )+ self.assert_close(joint, expected, rtol=tolerance, atol=tolerance)++ def test_forward_mode_gradients_match_unfold(self, monkeypatch, device, dtype):+ # The tap loop runs for dual inputs, in place on its own temporaries and in chunks, and must carry their+ # tangents as the unfold path does.+ _require_native_cpu(device, dtype)+ import torch.autograd.forward_ad as fwAD++ calls = _counting(monkeypatch)+ monkeypatch.setattr(bilateral_module, "_BILATERAL_CPU_CHUNK_BYTES", 2 * 3 * 148 * 148 * image_bytes(dtype))+ image = torch.rand(3, 3, 148, 148, device=device, dtype=dtype)+ image_tangent = torch.rand_like(image)+ sigma_color = torch.tensor([0.2, 0.3, 0.4], device=device, dtype=dtype)+ sigma_tangent = torch.rand_like(sigma_color)++ with fwAD.dual_level():+ output = bilateral_blur(+ fwAD.make_dual(image, image_tangent), 3, fwAD.make_dual(sigma_color, sigma_tangent), (1.5, 1.5)+ )+ actual = fwAD.unpack_dual(output).tangent+ assert len(calls) == 1+ _, expected = torch.func.jvp(+ lambda x, s: _unfold_reference(x, None, 3, s, (1.5, 1.5)),+ (image, sigma_color),+ (image_tangent, sigma_tangent),+ )+ tolerance = 1e-5 if dtype == torch.float32 else 1e-12+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ _, actual = torch.func.jvp(+ lambda x, s: bilateral_blur(x, 3, s, (1.5, 1.5)), (image, sigma_color), (image_tangent, sigma_tangent)+ )+ self.assert_close(actual, expected, rtol=tolerance, atol=tolerance)++ def test_gradcheck_tap_loop(self, device):+ if device.type != "cpu":+ pytest.skip("the CPU tap-loop bilateral path runs on the CPU only")+ image = torch.rand(2, 2, 6, 7, device=device, dtype=torch.float64)+ guidance = torch.rand(2, 1, 6, 7, device=device, dtype=torch.float64)+ sigma_color = torch.rand(2, device=device, dtype=torch.float64) + 0.5+ sigma_space = torch.rand(2, 2, device=device, dtype=torch.float64) + 0.5+ self.gradcheck(_bilateral_blur_cpu, (image, guidance, 3, sigma_color, sigma_space, "reflect", "l1"))+ self.gradcheck(_bilateral_blur_cpu, (image, None, (3, 5), 0.7, sigma_space, "circular", "l2"))++ def test_dynamo_on_an_eligible_input(self, device, dtype, torch_optimizer, optimizer_backend):+ if optimizer_backend == "jit":+ pytest.skip("passes torch.compile options, which torch.jit.script does not take")+ _require_native_cpu(device, dtype)+ image = torch.rand(2, 3, 148, 150, device=device, dtype=dtype)+ guidance = torch.rand(2, 1, 148, 150, device=device, dtype=dtype)+ sigma_color = torch.tensor([0.1, 0.3], device=device, dtype=dtype)+ assert _bilateral_blur_cpu_eligible(image, guidance, sigma_color, (1.5, 1.5))++ def op(x, g, s):+ first = bilateral_blur(x, 5, s, (1.5, 1.5))+ return first + joint_bilateral_blur(x, g, (3, 5), 0.2, (1.0, 2.0), "reflect", "l2")++ tolerance = 1e-5 if dtype == torch.float32 else 1e-12+ compiled = torch_optimizer(op, fullgraph=True)+ expected = op(image, guidance, sigma_color)+ self.assert_close(compiled(image, guidance, sigma_color), expected, rtol=tolerance, atol=tolerance)++ @pytest.mark.parametrize("kernel_size", [(7, 7), (5, 7), 9])+ def test_dynamo_keeps_unfold_for_kernels_above_25_taps(+ self, kernel_size, monkeypatch, device, dtype, torch_optimizer, optimizer_backend+ ):+ # Compiling the tap loop of a 9 x 9 kernel took 56 s against 2.3 s for the unfold path.+ if optimizer_backend == "jit":+ pytest.skip("passes torch.compile options, which torch.jit.script does not take")+ _require_native_cpu(device, dtype)+ _forbid_tap_loop(monkeypatch)+ image = torch.rand(*_ELIGIBLE, device=device, dtype=dtype)++ compiled = torch_optimizer(lambda x: bilateral_blur(x, kernel_size, 0.1, (1.5, 1.5)), fullgraph=True)++ tolerance = 1e-5 if dtype == torch.float32 else 1e-12+ expected = _unfold_reference(image, None, kernel_size, 0.1, (1.5, 1.5))+ self.assert_close(compiled(image), expected, rtol=tolerance, atol=tolerance)++ def test_dynamo_dynamic_batch_on_eligible_inputs(self, device, dtype, torch_optimizer, optimizer_backend):+ # The compiled tap loop runs over the whole batch, so one dynamic graph serves every eligible batch.+ if optimizer_backend == "jit":+ pytest.skip("passes torch.compile options, which torch.jit.script does not take")+ _require_native_cpu(device, dtype)+ from torch._dynamo.testing import CompileCounter++ def op(x, sigma_color, sigma_space):+ return bilateral_blur(x, 3, sigma_color, sigma_space, "reflect")++ counter = CompileCounter()+ compiled = torch_optimizer(op, backend=counter, fullgraph=True, dynamic=True)+ for batch in (4, 5, 6):+ image = torch.rand(batch, 3, 129, 131, device=device, dtype=dtype)+ sigma_color = torch.rand(batch, device=device, dtype=dtype) + 0.5+ sigma_space = torch.rand(batch, 2, device=device, dtype=dtype) + 0.5+ assert _bilateral_blur_cpu_eligible(image, None, sigma_color, sigma_space)+ tolerance = _cpu_tolerance(dtype)+ expected = op(image, sigma_color, sigma_space)+ self.assert_close(compiled(image, sigma_color, sigma_space), expected, rtol=tolerance, atol=tolerance)+ assert counter.frame_count == 1+++def image_bytes(dtype: torch.dtype) -> int:+ return torch.empty((), dtype=dtype).element_size()+++class _Exportable(torch.nn.Module):+ def forward(self, image: torch.Tensor) -> torch.Tensor:+ return bilateral_blur(image, 5, 0.1, (1.5, 1.5))++[email protected](not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON)+class TestBilateralBlurCpuExport(BaseTester):+ # torch.export keeps the unfold path: the tap loop's size check would otherwise become a guard of the exported+ # program, which then fails at run time for an image on the other side of the threshold from the example.+ @pytest.mark.parametrize("example, other", [((2, 3, 256, 256), (3, 3, 32, 48)), ((2, 3, 32, 32), (3, 3, 300, 310))])+ def test_dynamic_sizes_run_on_both_sides_of_the_threshold(self, example, other, device, dtype):+ _require_native_cpu(device, dtype)+ dim = torch.export.Dim+ dynamic = {"image": {0: dim("B", min=1, max=64), 2: dim("H", min=8, max=4096), 3: dim("W", min=8, max=4096)}}+ program = torch.export.export(+ _Exportable(), (torch.rand(*example, device=device, dtype=dtype),), dynamic_shapes=dynamic+ )+ assert any(node.target == torch.ops.aten.unfold.default for node in program.graph.nodes)++ exported = program.module()+ for shape in (example, other):+ image = torch.rand(*shape, device=device, dtype=dtype)+ self.assert_close(exported(image), _unfold_reference(image, None, 5, 0.1, (1.5, 1.5)))++ def test_dynamic_batch_of_small_grayscale_images(self, device, dtype):+ # (B, 1, 128, 128) reaches the threshold from B = 4 on, so a size check would constrain the dynamic batch.+ _require_native_cpu(device, dtype)+ dynamic = {"image": {0: torch.export.Dim("B", min=1, max=64)}}+ program = torch.export.export(+ _Exportable(), (torch.rand(8, 1, 128, 128, device=device, dtype=dtype),), dynamic_shapes=dynamic+ )+ image = torch.rand(2, 1, 128, 128, device=device, dtype=dtype)+ self.assert_close(program.module()(image), _unfold_reference(image, None, 5, 0.1, (1.5, 1.5)))