π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): make sharpness torch.compile fullgraph-safe (#3790)
Edgar Riba4376ab7b6766
diff --git a/kornia/enhance/adjust.py b/kornia/enhance/adjust.pyindex 8623f764a..2b0f1acfd 100644--- a/kornia/enhance/adjust.py+++ b/kornia/enhance/adjust.py@@ -881,39 +881,14 @@ def sharpness(input: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.padded_degenerate = F.pad(degenerate, [1, 1, 1, 1])result = torch.where(padded_mask == 1, padded_degenerate, input)- if len(factor.size()) == 0:- return _blend_one(result, input, factor)- return torch.stack([_blend_one(result[i], input[i], factor[i]) for i in range(len(factor))])---def _blend_one(input1: torch.Tensor, input2: torch.Tensor, factor: torch.Tensor) -> torch.Tensor:- r"""Blend two images into one.-- Args:- input1: image torch.Tensor with shapes like :math:`(H, W)` or :math:`(D, H, W)`.- input2: image torch.Tensor with shapes like :math:`(H, W)` or :math:`(D, H, W)`.- factor: factor 0-dim torch.Tensor.-- Returns:- : image torch.Tensor with the batch in the zero position.-- """- if not isinstance(input1, torch.Tensor):- raise AssertionError(f"`input1` must be a torch.Tensor. Got {input1}.")- if not isinstance(input2, torch.Tensor):- raise AssertionError(f"`input1` must be a torch.Tensor. Got {input2}.")-- if isinstance(factor, torch.Tensor) and len(factor.size()) != 0:- raise AssertionError(f"Factor shall be a float or single element torch.Tensor. Got {factor}.")- if factor == 0.0:- return input1- if factor == 1.0:- return input2- diff = (input2 - input1) * factor- res = input1 + diff- if factor > 0.0 and factor < 1.0:- return res- return torch.clamp(res, 0, 1)+ # Blend the sharpened result with the input. factor may be 0-dim (whole batch) or+ # 1-d (per-sample); reshape to broadcast, then blend branchlessly so the op stays+ # torch.compile fullgraph-safe (no per-sample Python loop, no factor==0/1 branches).+ # Equivalent to the old _blend_one: clamp is a no-op for factor in [0, 1] and only+ # bites for extrapolation, matching the previous behavior.+ if len(factor.size()) != 0:+ factor = factor.view(-1, *([1] * (result.dim() - 1)))+ return torch.clamp(result + (input - result) * factor, 0.0, 1.0)def _build_lut(histo: torch.Tensor, step: torch.Tensor) -> torch.Tensor:diff --git a/tests/enhance/test_adjust.py b/tests/enhance/test_adjust.pyindex 5c3990e36..5956020c1 100644--- a/tests/enhance/test_adjust.py+++ b/tests/enhance/test_adjust.py@@ -1143,6 +1143,15 @@ class TestSolarize(BaseTester):actual = op_script(img, 0.8)self.assert_close(actual, expected)+ def test_dynamo(self, device, dtype, torch_optimizer):+ img = torch.rand(3, 3, 5, 5, device=device, dtype=dtype)+ op = kornia.enhance.sharpness+ op_optimized = torch_optimizer(op)+ # scalar factor (whole batch) and per-sample factor+ self.assert_close(op(img, 0.5), op_optimized(img, 0.5))+ factor = torch.tensor([0.3, 0.7, 1.5], device=device, dtype=dtype)+ self.assert_close(op(img, factor), op_optimized(img, factor))+class TestPosterize(BaseTester):f = kornia.enhance.posterize