Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

feat(augmentation): deterministic order for ColorJitter (fullgraph-safe) (#3799)

Edgar Ribae21b288b2b22
diff --git a/kornia/augmentation/_2d/intensity/color_jitter.py b/kornia/augmentation/_2d/intensity/color_jitter.py
index bae5b22dd..a50530231 100644
--- a/kornia/augmentation/_2d/intensity/color_jitter.py
+++ b/kornia/augmentation/_2d/intensity/color_jitter.py
@@ -15,6 +15,7 @@
# limitations under the License.
#
+from collections.abc import Sequence
from typing import Any, Dict, List, Optional, Tuple, Union
import torch
@@ -94,6 +95,7 @@ class ColorJitter(IntensityAugmentationBase2D):
same_on_batch: bool = False,
p: float = 1.0,
keepdim: bool = False,
+ order: Optional[Sequence[int]] = None,
) -> None:
super().__init__(p=p, same_on_batch=same_on_batch, keepdim=keepdim)
@@ -103,6 +105,18 @@ class ColorJitter(IntensityAugmentationBase2D):
self.hue = hue
self._param_generator = rg.ColorJitterGenerator(brightness, contrast, saturation, hue)
+ # A fixed application order (a permutation/subset of 0..3 for brightness, contrast,
+ # saturation, hue) makes apply_transform a static Python loop instead of iterating the
+ # random `order` tensor, so it becomes torch.compile fullgraph-safe. Default (None)
+ # keeps the original random per-call order.
+ if order is not None:
+ order = tuple(int(i) for i in order)
+ if not set(order) <= {0, 1, 2, 3}:
+ raise ValueError(
+ f"`order` entries must be in 0..3 (brightness, contrast, saturation, hue). Got {order}"
+ )
+ self._fixed_order: Optional[Tuple[int, ...]] = order
+
# native functions
self._brightness_fn = adjust_brightness_accumulative
self._contrast_fn = adjust_contrast_with_mean_subtraction
@@ -116,27 +130,31 @@ class ColorJitter(IntensityAugmentationBase2D):
flags: Dict[str, Any],
transform: Optional[torch.Tensor] = None,
) -> torch.Tensor:
+ # torch.where (compute-then-select) rather than a Python `if ... .any() else img`:
+ # numerically identical (the op runs on the whole batch or not at all, exactly as the
+ # guard decided) but without a data-dependent branch, so it stays fullgraph-compilable.
transforms = [
- lambda img: (
- self._brightness_fn(img, params["brightness_factor"])
- if (params["brightness_factor"] != 0).any()
- else img
+ lambda img: torch.where(
+ (params["brightness_factor"] != 0).any(), self._brightness_fn(img, params["brightness_factor"]), img
+ ),
+ lambda img: torch.where(
+ (params["contrast_factor"] != 1).any(), self._contrast_fn(img, params["contrast_factor"]), img
),
- lambda img: (
- self._contrast_fn(img, params["contrast_factor"]) if (params["contrast_factor"] != 1).any() else img
+ lambda img: torch.where(
+ (params["saturation_factor"] != 1).any(), self._saturation_fn(img, params["saturation_factor"]), img
),
- lambda img: (
- self._saturation_fn(img, params["saturation_factor"])
- if (params["saturation_factor"] != 1).any()
- else img
+ lambda img: torch.where(
+ (params["hue_factor"] != 0).any(), self._hue_fn(img, params["hue_factor"] * 2 * pi), img
),
- lambda img: self._hue_fn(img, params["hue_factor"] * 2 * pi) if (params["hue_factor"] != 0).any() else img,
]
+ # A fixed order is a static Python sequence (fullgraph); otherwise iterate the random
+ # per-call `order` tensor (inherently data-dependent, not fullgraph-compilable).
+ order: Sequence[int] = self._fixed_order if self._fixed_order is not None else params["order"]
+
jittered = input
- for idx in params["order"]:
- t = transforms[idx]
- jittered = t(jittered)
+ for idx in order:
+ jittered = transforms[idx](jittered)
return jittered
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index fa38b5e51..287cab955 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -1646,6 +1646,23 @@ class TestColorJitter(BaseTester):
)
assert str(f) == repr
+ def test_fixed_order(self, device, dtype):
+ # A fixed `order` applies the same adjustments deterministically; invalid entries raise.
+ img = torch.rand(2, 3, 8, 8, device=device, dtype=dtype)
+ out = ColorJitter(0.2, 0.2, 0.2, 0.1, p=1.0, order=(0, 1, 2, 3))(img)
+ assert out.shape == img.shape
+ with pytest.raises(ValueError):
+ ColorJitter(0.2, 0.2, 0.2, 0.1, order=(0, 1, 9))
+
+ def test_dynamo_fixed_order(self, device, dtype, torch_optimizer):
+ # A fixed `order` avoids iterating the random order tensor, so it is fullgraph-safe.
+ # Replay the sampled params so the (random) eager and compiled runs are comparable.
+ img = torch.rand(2, 3, 8, 8, device=device, dtype=dtype)
+ op = ColorJitter(0.2, 0.2, 0.2, 0.1, p=1.0, order=(0, 1, 2, 3))
+ eager = op(img)
+ op_optimized = torch_optimizer(op)
+ self.assert_close(eager, op_optimized(img, params=op._params))
+
def test_color_jitter(self, device, dtype):
if dtype == torch.float16:
pytest.skip("not work for half-precision")