π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): make adjust_gamma torch.compile-compatible (#3786)
Edgar Ribabcdaccf235cf
diff --git a/kornia/enhance/adjust.py b/kornia/enhance/adjust.pyindex a171bb79f..8623f764a 100644--- a/kornia/enhance/adjust.py+++ b/kornia/enhance/adjust.py@@ -292,11 +292,13 @@ def adjust_gamma(gamma = gamma.to(input.device).to(input.dtype)gain = gain.to(input.device).to(input.dtype)- if (gamma < 0.0).any():- raise ValueError(f"Gamma must be non-negative. Got {gamma}")+ # `bool(tensor)` is untraceable by dynamo; skip the data-dependent validation under compile.+ if not torch.compiler.is_compiling():+ if (gamma < 0.0).any():+ raise ValueError(f"Gamma must be non-negative. Got {gamma}")- if (gain < 0.0).any():- raise ValueError(f"Gain must be non-negative. Got {gain}")+ if (gain < 0.0).any():+ raise ValueError(f"Gain must be non-negative. Got {gain}")for _ in range(len(input.shape) - len(gamma.shape)):gamma = torch.unsqueeze(gamma, dim=-1)diff --git a/tests/enhance/test_adjust.py b/tests/enhance/test_adjust.pyindex c10aa8d21..5c3990e36 100644--- a/tests/enhance/test_adjust.py+++ b/tests/enhance/test_adjust.py@@ -291,6 +291,13 @@ class TestAdjustGamma(BaseTester):img = torch.ones(batch_size, channels, height, width, device=device, dtype=torch.float)self.gradcheck(kornia.enhance.adjust_gamma, (img, 1.0, 2.0))+ def test_dynamo(self, device, dtype, torch_optimizer):+ B, C, H, W = 2, 3, 4, 4+ img = torch.ones(B, C, H, W, device=device, dtype=dtype)+ op = kornia.enhance.adjust_gamma+ op_optimized = torch_optimizer(op)+ self.assert_close(op(img, 1.5), op_optimized(img, 1.5))+class TestAdjustContrast(BaseTester):@pytest.mark.parametrize("shape", [(3, 4, 4), (2, 3, 3, 3), (4, 3, 3, 1, 1)])