Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(enhance): make solarize torch.compile fullgraph-safe (#3805)

Edgar Riba0c6854a9f5d2
diff --git a/kornia/enhance/adjust.py b/kornia/enhance/adjust.py
index ae89aa890..5078b5bec 100644
--- a/kornia/enhance/adjust.py
+++ b/kornia/enhance/adjust.py
@@ -660,9 +660,10 @@ def _solarize(input: torch.Tensor, thresholds: Union[float, torch.Tensor] = 0.5)
if isinstance(thresholds, torch.Tensor) and len(thresholds.shape) != 0:
if not (input.size(0) == len(thresholds) and len(thresholds.shape) == 1):
raise AssertionError(f"thresholds must be a 1-d vector of shape ({input.size(0)},). Got {thresholds}")
- # TODO: I am not happy about this line, but no easy to do batch-wise operation
+ # Reshape the per-sample threshold to broadcast over (C, H, W) rather than iterating the
+ # tensor (`for x in thresholds` breaks torch.compile); the `where` below broadcasts identically.
thresholds = thresholds.to(input.device).to(input.dtype)
- thresholds = torch.stack([x.expand(*input.shape[-3:]) for x in thresholds])
+ thresholds = thresholds.view([-1] + [1] * (input.ndim - 1))
return torch.where(input < thresholds, input, 1.0 - input)
@@ -721,15 +722,20 @@ def solarize(
if isinstance(additions, float):
additions = torch.as_tensor(additions)
- if not torch.all((additions < 0.5) * (additions > -0.5)):
- raise AssertionError(f"The value of 'addition' is between -0.5 and 0.5. Got {additions}.")
+ # `torch._assert_async` keeps this a single traceable path (no Python-level branch on a
+ # tensor value), so `solarize` stays torch.compile fullgraph-safe while still validating.
+ torch._assert_async(
+ ((additions < 0.5) & (additions > -0.5)).all(),
+ "The value of 'addition' is between -0.5 and 0.5.",
+ )
if isinstance(additions, torch.Tensor) and len(additions.shape) != 0:
if not (input.size(0) == len(additions) and len(additions.shape) == 1):
raise AssertionError(f"additions must be a 1-d vector of shape ({input.size(0)},). Got {additions}")
- # TODO: I am not happy about this line, but no easy to do batch-wise operation
+ # Broadcast the per-sample scalar over (C, H, W) by reshaping instead of iterating the
+ # tensor (`for x in additions` breaks the graph); numerically identical.
additions = additions.to(input.device).to(input.dtype)
- additions = torch.stack([x.expand(*input.shape[-3:]) for x in additions])
+ additions = additions.view([-1] + [1] * (input.ndim - 1))
input = input + additions
input = input.clamp(0.0, 1.0)
diff --git a/tests/enhance/test_adjust.py b/tests/enhance/test_adjust.py
index 5956020c1..aa8c22d6e 100644
--- a/tests/enhance/test_adjust.py
+++ b/tests/enhance/test_adjust.py
@@ -1061,6 +1061,15 @@ class TestSharpness(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))
+
@pytest.mark.skipif(kornia.core.utils.xla_is_available(), reason="issues with xla device")
class TestSolarize(BaseTester):
@@ -1145,12 +1154,23 @@ class TestSolarize(BaseTester):
def test_dynamo(self, device, dtype, torch_optimizer):
img = torch.rand(3, 3, 5, 5, device=device, dtype=dtype)
- op = kornia.enhance.sharpness
+ op = kornia.enhance.solarize
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))
+ # scalar threshold/addition (whole batch)
+ self.assert_close(op(img, 0.5, 0.1), op_optimized(img, 0.5, 0.1))
+ # per-sample 1-d threshold/addition β€” the batch-wise path that must stay fullgraph-safe
+ thresholds = torch.tensor([0.3, 0.6, 0.8], device=device, dtype=dtype)
+ additions = torch.tensor([-0.2, 0.0, 0.2], device=device, dtype=dtype)
+ self.assert_close(op(img, thresholds, additions), op_optimized(img, thresholds, additions))
+
+ def test_dynamo_fullgraph(self, device, dtype):
+ img = torch.rand(3, 3, 5, 5, device=device, dtype=dtype)
+ thresholds = torch.tensor([0.3, 0.6, 0.8], device=device, dtype=dtype)
+ additions = torch.tensor([-0.2, 0.0, 0.2], device=device, dtype=dtype)
+ expected = kornia.enhance.solarize(img, thresholds, additions)
+ torch._dynamo.reset()
+ compiled = torch.compile(kornia.enhance.solarize, fullgraph=True)
+ self.assert_close(compiled(img, thresholds, additions), expected)
class TestPosterize(BaseTester):