π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): auto-augment ops preserve float16 / bfloat16 input dtype (#4210)
tritsystem3bdbaad1108a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2a2239468..8a375db6f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -292,6 +292,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Bug fixes+* `RandAugment`, `AutoAugment`, `TrivialAugment` and `AugMix` no longer silently upcast half-precision+ batches to `float32`. `OperationBase.forward` β the shared gate every auto-augment op routes through β+ moved its `batch_prob` mask to `input.device` but not `input.dtype`, and since `batch_prob` is `float32`+ the expression `batch_prob * op(x) + (1 - batch_prob) * x` promoted a `float16` / `bfloat16` input up to+ `float32` (the op output and input are both low precision, only the gate was not). `float32` / `float64`+ inputs are unaffected and their numeric output is byte-identical. The mask is now cast to `input.dtype`+ as well. Partially addresses #4153: this clears the `RuntimeError` behind nineteen of that issue's Linux+ CPU half-precision xfail lines for `tests/augmentation/test_auto_operation.py` (all ten `cpu_bfloat16`+ entries, nine of the ten `cpu_float16`), which are removed from `testing/half_precision_xfails/`. The+ tenth `float16` line is kept: `TestAutoAugment::test_reproduce[cpu-float16]` still fails, now on an+ `AssertionError` rather than a `RuntimeError` β a smaller residual, not a fix. (#4210)* Reduce local-feature extraction overhead by accumulating orientation histograms directly,batching built-in DoG extrema refinement, selecting coordinates before combining response signs,and using faster activation layouts in KeyNet and CPU HardNet. Detector settings and pretraineddiff --git a/kornia/augmentation/auto/operations/base.py b/kornia/augmentation/auto/operations/base.pyindex 3edcb9b64..844fee4a5 100644--- a/kornia/augmentation/auto/operations/base.py+++ b/kornia/augmentation/auto/operations/base.py@@ -192,7 +192,9 @@ class OperationBase(nn.Module):if params is None:params = self.forward_parameters(input.shape)- batch_prob = params["batch_prob"][(...,) + ((None,) * (len(input.shape) - 1))].to(device=input.device)+ batch_prob = params["batch_prob"][(...,) + ((None,) * (len(input.shape) - 1))].to(+ device=input.device, dtype=input.dtype+ )return batch_prob * self.op(input, params=params) + (1 - batch_prob) * inputdiff --git a/testing/half_precision_xfails/cpu_bfloat16.txt b/testing/half_precision_xfails/cpu_bfloat16.txtindex 987c559d4..1e2026d30 100644--- a/testing/half_precision_xfails/cpu_bfloat16.txt+++ b/testing/half_precision_xfails/cpu_bfloat16.txt@@ -53,16 +53,6 @@ call builtins.KeyError tests/augmentation/test_augmentation_mix.py::TestRandomMocall builtins.KeyError tests/augmentation/test_augmentation_mix.py::TestRandomMosaic::test_p[cpu-bfloat16-0.0]call builtins.KeyError tests/augmentation/test_augmentation_mix.py::TestRandomMosaic::test_p[cpu-bfloat16-0.5]call builtins.KeyError tests/augmentation/test_augmentation_mix.py::TestRandomMosaic::test_p[cpu-bfloat16-1.0]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_reproduce[cpu-bfloat16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_sequential[cpu-bfloat16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_smoke[cpu-bfloat16-cifar10]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_smoke[cpu-bfloat16-imagenet]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_smoke[cpu-bfloat16-policy3]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_smoke[cpu-bfloat16-svhn]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_transform_mat[cpu-bfloat16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestRandAugment::test_sequential[cpu-bfloat16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestRandAugment::test_transform_mat[cpu-bfloat16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestRandAugment::test_transform_mat_repeated_policy[cpu-bfloat16]call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-bfloat16-False-bilinear-circular-direction0-angle1]call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-bfloat16-False-bilinear-circular-direction0-angle2]call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-bfloat16-False-bilinear-circular-direction1-angle1]diff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txtindex d53282d38..fb83ed3d3 100644--- a/testing/half_precision_xfails/cpu_float16.txt+++ b/testing/half_precision_xfails/cpu_float16.txt@@ -68,15 +68,7 @@ call builtins.AssertionError tests/augmentation/test_augmentation_mix.py::TestRacall builtins.AssertionError tests/augmentation/test_augmentation_mix.py::TestRandomMixUpV2::test_random_mixup_p1[cpu-float16]call builtins.AssertionError tests/augmentation/test_augmentation_mix.py::TestRandomMixUpV2::test_random_mixup_same_on_batch[cpu-float16]call builtins.AssertionError tests/augmentation/test_augmentation_mix.py::TestRandomMosaic::test_numerical[cpu-float16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_reproduce[cpu-float16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_sequential[cpu-float16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_smoke[cpu-float16-cifar10]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_smoke[cpu-float16-imagenet]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_smoke[cpu-float16-policy3]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_smoke[cpu-float16-svhn]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_transform_mat[cpu-float16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestRandAugment::test_transform_mat[cpu-float16]-call builtins.RuntimeError tests/augmentation/test_auto_operation.py::TestRandAugment::test_transform_mat_repeated_policy[cpu-float16]+call builtins.AssertionError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_reproduce[cpu-float16]call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-float16-False-bilinear-circular-direction0-angle1]call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-float16-False-bilinear-circular-direction0-angle2]call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-float16-False-bilinear-circular-direction1-angle1]diff --git a/tests/augmentation/test_auto_operation.py b/tests/augmentation/test_auto_operation.pyindex 72f36f63d..4946c9dbb 100644--- a/tests/augmentation/test_auto_operation.py+++ b/tests/augmentation/test_auto_operation.py@@ -187,3 +187,31 @@ class TestTrivialAugment(BaseTester):def test_sequential(augment_method, device, dtype):_test_sequential(TrivialAugment(), device=device, dtype=dtype)++[email protected]("low_dtype", [torch.float16, torch.bfloat16])+def test_operation_preserves_input_dtype(device, low_dtype):+ # OperationBase.forward gated the op output with a float32 ``batch_prob``+ # mask, so ``mask * op(x) + (1 - mask) * x`` promoted a float16 / bfloat16+ # input to float32. float32 / float64 inputs are unaffected (promotion goes+ # up), which is why this only shows below float32. Covers RandAugment /+ # AutoAugment / TrivialAugment / AugMix, which all route through this method.+ # dtype preservation is device-independent; check it on CPU (the auto-augment+ # RNG state of a freshly built ``_find_all_ops()`` op does not fully follow a+ # post-hoc ``.to(non-cpu)``).+ if torch.device(device).type != "cpu":+ pytest.skip("dtype-preservation check runs on CPU")+ checked = 0+ for op in _find_all_ops():+ x = torch.rand(2, 3, 16, 16, dtype=low_dtype)+ try:+ out = op(x)+ except RuntimeError as e:+ # some kernels (e.g. grid_sample on older torch) have no half/bfloat16+ # CPU path -- that is a torch capability gap, not a kornia contract.+ if "not implemented for" in str(e):+ continue+ raise+ assert out.dtype == low_dtype, f"{type(op).__name__}: {out.dtype} != {low_dtype}"+ checked += 1+ assert checked, f"no op could run in {low_dtype} on this build"