π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): preserve Planckian Jitter dtype (#4578)
NiteeshGowda72755082f1c11a
diff --git a/changelog.d/4578.breaking.md b/changelog.d/4578.breaking.mdnew file mode 100644index 000000000..67d389be1--- /dev/null+++ b/changelog.d/4578.breaking.md@@ -0,0 +1,5 @@+Casting `RandomPlanckianJitter` to a dtype no longer changes the dtype of its output. The illuminant+table now follows the input rather than the module, so `K.RandomPlanckianJitter(p=1.0).to(torch.float64)`+applied to a `float32` input returns `float32` where it previously returned `float64`, and a module cast+to the other half dtype returns the input's dtype instead of `float32`. Casting the module is no longer+the way to keep a half-precision input's dtype; it is preserved by default. Refs #4574.diff --git a/changelog.d/4578.fixed.md b/changelog.d/4578.fixed.mdnew file mode 100644index 000000000..5a896b4b6--- /dev/null+++ b/changelog.d/4578.fixed.md@@ -0,0 +1,4 @@+`RandomPlanckianJitter` now preserves the input dtype. The illuminant table was moved to the input's+device but left at `float32`, so multiplying the red and blue channels by it promoted a `float16` or+`bfloat16` input to a `float32` output; the table now follows the input's dtype as well. `float32` and+`float64` outputs are bit-identical. Fixes #4574.diff --git a/kornia/augmentation/_2d/intensity/planckian_jitter.py b/kornia/augmentation/_2d/intensity/planckian_jitter.pyindex 983692f3d..c6509f98e 100644--- a/kornia/augmentation/_2d/intensity/planckian_jitter.py+++ b/kornia/augmentation/_2d/intensity/planckian_jitter.py@@ -130,12 +130,6 @@ class RandomPlanckianJitter(IntensityAugmentationBase2D):does not load into an instance built with the other. Tracked in`#4428 <https://github.com/kornia/kornia/issues/4428>`_.- .. warning::- The table is not cast to the input's dtype, so the output takes the promoted dtype of the input and- the table. With the table left at ``float32``, a ``float16`` or ``bfloat16`` input comes back as- ``float32``; casting the module to the input's own dtype keeps it. Tracked in- `#4574 <https://github.com/kornia/kornia/issues/4574>`_.-.. note::Input torch.Tensor must be float and normalized into [0, 1].@@ -215,9 +209,9 @@ class RandomPlanckianJitter(IntensityAugmentationBase2D):transform: Optional[torch.Tensor] = None,) -> torch.Tensor:KORNIA_CHECK_SHAPE(input, ["*", "3", "H", "W"])- # Index with the tensor itself: `.tolist()` reads the data, which graph capture cannot do. The buffer- # follows the module's device, so it is not re-assigned here.- coeffs = self.pl.to(device=input.device)[params["idx"].long()]+ # Index with the tensor itself: `.tolist()` reads the data, which graph capture cannot do. Cast the+ # buffer to the input so both device and dtype follow the input for the channel-wise multiplication.+ coeffs = self.pl.to(input)[params["idx"].long()]r_w = coeffs[:, 0][..., None, None]b_w = coeffs[:, 1][..., None, None]diff --git a/testing/half_precision_xfails/cpu_bfloat16.txt b/testing/half_precision_xfails/cpu_bfloat16.txtindex 4d8004fda..1fd593715 100644--- a/testing/half_precision_xfails/cpu_bfloat16.txt+++ b/testing/half_precision_xfails/cpu_bfloat16.txt@@ -15,10 +15,6 @@ call builtins.AssertionError tests/augmentation/_3d/test_random_rotation_3d.py::call builtins.AssertionError tests/augmentation/_3d/test_random_rotation_3d.py::TestRandomRotation3D::test_random_rotation[cpu-bfloat16]call builtins.AssertionError tests/augmentation/container/test_augmentation_sequential.py::TestAugmentationSequential::test_random_crops[cpu-bfloat16]call builtins.AssertionError tests/augmentation/container/test_augmentation_sequential.py::TestAugmentationSequential::test_random_erasing[cpu-bfloat16]-call builtins.AssertionError tests/augmentation/test_augmentation.py::TestPlanckianJitter::test_planckian_jitter_batch[cpu-bfloat16]-call builtins.AssertionError tests/augmentation/test_augmentation.py::TestPlanckianJitter::test_planckian_jitter_blackbody[cpu-bfloat16]-call builtins.AssertionError tests/augmentation/test_augmentation.py::TestPlanckianJitter::test_planckian_jitter_cied[cpu-bfloat16]-call builtins.AssertionError tests/augmentation/test_augmentation.py::TestPlanckianJitter::test_planckian_jitter_same_on_batch[cpu-bfloat16]call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomClahe::test_smoke[cpu-bfloat16]call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomCrop::test_pad_if_needed_both[cpu-bfloat16]call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomGaussianIllumination::test_smoke[cpu-bfloat16]diff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txtindex 0b9d2f5d5..318f421e9 100644--- a/testing/half_precision_xfails/cpu_float16.txt+++ b/testing/half_precision_xfails/cpu_float16.txt@@ -56,10 +56,6 @@ call builtins.AssertionError tests/augmentation/container/test_video_sequential.call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply2-BCTHW-augmentations1]call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply2-BTCHW-augmentations0]call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply2-BTCHW-augmentations1]-call builtins.AssertionError tests/augmentation/test_augmentation.py::TestPlanckianJitter::test_planckian_jitter_batch[cpu-float16]-call builtins.AssertionError tests/augmentation/test_augmentation.py::TestPlanckianJitter::test_planckian_jitter_blackbody[cpu-float16]-call builtins.AssertionError tests/augmentation/test_augmentation.py::TestPlanckianJitter::test_planckian_jitter_cied[cpu-float16]-call builtins.AssertionError tests/augmentation/test_augmentation.py::TestPlanckianJitter::test_planckian_jitter_same_on_batch[cpu-float16]call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomGaussianIllumination::test_smoke[cpu-float16]call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomRGBShift::test_random_rgb_shift[cpu-float16]call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomRGBShift::test_random_rgb_shift_same_batch[cpu-float16]diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.pyindex b300db3d1..316d6a15e 100644--- a/tests/augmentation/test_augmentation.py+++ b/tests/augmentation/test_augmentation.py@@ -5448,6 +5448,25 @@ class TestPlanckianJitter(BaseTester):expected = self._get_expected_output_same_on_batch(device, dtype)self.assert_close(f(input), expected, low_tolerance=True)+ def test_planckian_jitter_preserves_dtype_4574(self, device, dtype):+ input = torch.rand(2, 3, 4, 4, device=device, dtype=dtype)+ output = RandomPlanckianJitter(p=1.0)(input)++ assert output.dtype == input.dtype+ assert output.device == input.device++ # The half dtypes are named rather than taken from the fixture so the preservation is+ # exercised on the default float32 leg too, not only on the two half-precision jobs.+ @pytest.mark.parametrize("half_dtype", [torch.float16, torch.bfloat16])+ def test_planckian_jitter_preserves_half_dtype_on_any_leg_4574(self, device, half_dtype):+ if device.type == "mps" and half_dtype is torch.bfloat16:+ pytest.skip("bfloat16 support on MPS is incomplete")+ input = torch.rand(2, 3, 4, 4, device=device, dtype=half_dtype)+ output = RandomPlanckianJitter(p=1.0)(input)++ assert output.dtype == half_dtype+ assert output.device == input.device+class TestRandomRGBShift(BaseTester):def test_smoke(self, device, dtype):diff --git a/tests/augmentation/test_conventions_intensity_values.py b/tests/augmentation/test_conventions_intensity_values.pyindex 175351554..3c7de2256 100644--- a/tests/augmentation/test_conventions_intensity_values.py+++ b/tests/augmentation/test_conventions_intensity_values.py@@ -1430,32 +1430,55 @@ class TestIntensityColourConventions(BaseTester):out = aug(image)red_gain, blue_gain = (float(value) for value in aug.pl[0])scaled = torch.stack([image[:, 0] * red_gain, image[:, 1], image[:, 2] * blue_gain], dim=1)- # Compared in the input's dtype: the output is float32 for a half input (#4574, pinned below).- self.assert_close(out.to(dtype), scaled.clamp(max=1.0))+ # The coefficient table follows the input dtype, so the output is already in it and the+ # comparison needs no cast.+ self.assert_close(out, scaled.clamp(max=1.0))assert float(out[0, 1, 0, 0]) == 1.0 and float(out[0, 2, 0, 0]) < 0.01assert bool((out[..., 1] < 0).all())- # Issue #4574: the `pl` table is float32 unless the module is cast, and apply_transform multiplies the- # red and blue channels by it without casting, so a half-precision input comes back float32; a float64- # input keeps its dtype, and so does a half input once the module is cast with `.half()`.+ # Issue #4574: the coefficient table follows the input dtype, so half-precision inputs are no+ # longer promoted to float32, and a module cast does not decide the output dtype either.# Snippet used to generate expected:- # for dt in (torch.float16, torch.bfloat16, torch.float64):- # torch.manual_seed(0); print(dt, K.RandomPlanckianJitter(p=1.0)(torch.rand(2, 3, 4, 4).to(dt)).dtype)- # executed 2026-09-15 (torch 2.14.0, cpu) -> `float32`, `float32`, `float64`.- def test_wart_random_planckian_jitter_half_input_comes_back_float32_4574(self, device, dtype):+ # for dt in (torch.float16, torch.bfloat16):+ # other = torch.bfloat16 if dt == torch.float16 else torch.float16+ # torch.manual_seed(0); x = torch.rand(2, 3, 4, 4).to(dt)+ # torch.manual_seed(0); a = K.RandomPlanckianJitter(p=1.0)(x).dtype+ # torch.manual_seed(0); b = K.RandomPlanckianJitter(p=1.0).to(dtype=other)(x).dtype+ # print(dt, a, b)+ # executed 2026-09-17 (torch 2.14.0, cpu) -> `float16 float16 float16` and `bfloat16 bfloat16+ # bfloat16`; on `30dfbf711`, this PR's base, both columns were `float32` for both inputs.+ def test_convention_random_planckian_jitter_preserves_dtype_4574(self, device, dtype):torch.manual_seed(_FIXTURE_SEED)image = torch.rand(2, 3, 4, 4).to(device=device, dtype=dtype)torch.manual_seed(_FORWARD_SEED)out = K.RandomPlanckianJitter(p=1.0)(image)- assert out.dtype == (torch.float32 if dtype in _HALF else dtype)- # The output is the promoted dtype of input and table, so casting the module to the input's own- # dtype restores it, and a wider cast widens the output.- torch.manual_seed(_FORWARD_SEED)- assert K.RandomPlanckianJitter(p=1.0).to(device=device, dtype=dtype)(image).dtype == dtype++ assert out.dtype == dtype+ assert out.device == image.device+if dtype in _HALF:+ # A module cast to the *other* half dtype returned float32 before the fix, because the+ # table's dtype decided the promotion; it now follows the input like every other cast.other = torch.bfloat16 if dtype == torch.float16 else torch.float16torch.manual_seed(_FORWARD_SEED)- assert K.RandomPlanckianJitter(p=1.0).to(device=device, dtype=other)(image).dtype == torch.float32+ assert K.RandomPlanckianJitter(p=1.0).to(device=device, dtype=other)(image).dtype == dtype++ # Issue #4574, the wider direction: a module cast wider than the input does not widen the output.+ # Snippet used to generate expected:+ # torch.manual_seed(0); x = torch.rand(2, 3, 4, 4)+ # torch.manual_seed(0); print(K.RandomPlanckianJitter(p=1.0).to(dtype=torch.float64)(x).dtype)+ # executed 2026-09-17 (torch 2.14.0, cpu) -> `float32`; on `30dfbf711` it was `float64`.+ def test_convention_random_planckian_jitter_wider_module_dtype_4574(self, device):+ if device.type == "mps":+ pytest.skip("MPS does not support float64")++ torch.manual_seed(_FIXTURE_SEED)+ image = torch.rand(2, 3, 4, 4, device=device, dtype=torch.float32)+ torch.manual_seed(_FORWARD_SEED)+ out = K.RandomPlanckianJitter(p=1.0).to(device=device, dtype=torch.float64)(image)++ assert out.dtype == torch.float32+ assert out.device == image.device# Row 6c-32/6c-33: the illuminant table is an RGB ratio, so a non-RGB input is rejected rather# than broadcast. Snippet used to generate expected: