Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): keep the PlanckianJitter index sampler in float32 (#4839)

Tejas Anil Nagmotea1d339e96750
diff --git a/changelog.d/4839.fixed.md b/changelog.d/4839.fixed.md
new file mode 100644
index 000000000..990e94108
--- /dev/null
+++ b/changelog.d/4839.fixed.md
@@ -0,0 +1 @@
+PlanckianJitterGenerator keeps its index sampler in float32 when the augmentation is moved to float16 or bfloat16, so the sampled Planckian table index cannot go one past the end. On MPS, half-precision `torch.rand` returns exactly 1.0 for about 1 in 4096 (float16) or 1 in 512 (bfloat16) draws, which made `RandomPlanckianJitter` raise an out-of-bounds index error. float32 and float64 draws are unchanged; for the same seed, float16 and bfloat16 pipelines now draw some different indices than before. (#4553)
diff --git a/kornia/augmentation/random_generator/_2d/planckian_jitter.py b/kornia/augmentation/random_generator/_2d/planckian_jitter.py
index b3b41beb2..96f4f2e60 100644
--- a/kornia/augmentation/random_generator/_2d/planckian_jitter.py
+++ b/kornia/augmentation/random_generator/_2d/planckian_jitter.py
@@ -33,6 +33,11 @@ class PlanckianJitterGenerator(RandomGeneratorBase):
self.domain = domain
def make_samplers(self, device: torch.device, dtype: torch.dtype) -> None:
+ # The draw is truncated to an index into the Planckian table, so keep it in
+ # float32: half-precision torch.rand on MPS can return exactly 1.0, which
+ # would give an index one past the end (#4553).
+ if dtype in (torch.float16, torch.bfloat16):
+ dtype = torch.float32
idx_range = _range_bound(self.domain, "idx_range", device=device, dtype=dtype)
_joint_range_check(idx_range, "idx_range", (0, self.domain[1]))
diff --git a/tests/augmentation/test_random_generator.py b/tests/augmentation/test_random_generator.py
index e95e623e3..c2474e03c 100644
--- a/tests/augmentation/test_random_generator.py
+++ b/tests/augmentation/test_random_generator.py
@@ -33,6 +33,7 @@ from kornia.augmentation.random_generator import (
MotionBlurGenerator,
PerspectiveGenerator,
PlainUniformGenerator,
+ PlanckianJitterGenerator,
PosterizeGenerator,
ProbabilityGenerator,
RandomGaussianBlurGenerator,
@@ -1865,3 +1866,36 @@ class TestGaussianBlurGenBufferHygiene:
gen = RandomGaussianBlurGenerator(sigma=(0.1, 2.0))
assert "sigma" not in dict(gen.named_buffers())
assert gen.sigma == (0.1, 2.0)
+
+
+class TestPlanckianJitterGenerator:
+ # The half dtypes are named rather than taken from the fixture so the pins run on the default float32
+ # legs too; the device comes from the fixture so the MPS leg draws on MPS.
+ @pytest.mark.parametrize("half_dtype", [torch.float16, torch.bfloat16])
+ def test_index_sampler_stays_float32_for_half_precision(self, device, half_dtype):
+ # The draw is truncated to a table index. Half-precision torch.rand on MPS
+ # can return exactly 1.0, which gave an index one past the end (#4553).
+ generator = PlanckianJitterGenerator([0, 25])
+ generator.set_rng_device_and_dtype(device, half_dtype)
+ assert generator.pl_idx_dist.low.dtype == torch.float32
+ assert generator.pl_idx_dist.high.dtype == torch.float32
+ assert generator.pl_idx_dist.low.device.type == device.type
+
+ @pytest.mark.parametrize("dtype", [torch.float32, torch.float64])
+ def test_index_sampler_follows_full_precision_dtype(self, dtype):
+ generator = PlanckianJitterGenerator([0, 25])
+ generator.set_rng_device_and_dtype(torch.device("cpu"), dtype)
+ assert generator.pl_idx_dist.low.dtype == dtype
+
+ # CPU half torch.rand never returns 1.0, so this only discriminates on the MPS leg: there, with a
+ # half-precision sampler, 2**16 draws give about 16 (float16) and 128 (bfloat16) indices equal to 25.
+ @pytest.mark.parametrize("half_dtype", [torch.float16, torch.bfloat16])
+ def test_half_precision_indices_in_range(self, device, half_dtype):
+ generator = PlanckianJitterGenerator([0, 25])
+ generator.set_rng_device_and_dtype(device, half_dtype)
+ torch.manual_seed(0)
+ idx = generator(torch.Size([1 << 16, 3, 4, 4]))["idx"]
+ assert idx.dtype == torch.long
+ assert idx.device.type == device.type
+ assert int(idx.min()) >= 0
+ assert int(idx.max()) < 25