Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test: fix CUDA-only test contracts (host-sync registry, half tolerances, fuser, complex32 optimizer) (#6566)

Dmytro Mishkin83f3f8452d46
diff --git a/kornia/geometry/liegroup/so2.py b/kornia/geometry/liegroup/so2.py
index 7ae29bfbf..dcf18dcd6 100644
--- a/kornia/geometry/liegroup/so2.py
+++ b/kornia/geometry/liegroup/so2.py
@@ -77,8 +77,10 @@ class So2(nn.Module):
``.to(torch.float32)`` use complex64 state, while ``.double()`` and ``.to(torch.float64)`` use complex128.
Device-only conversions preserve the complex precision, and explicit complex dtypes are also accepted.
The complex checkpoint key ``_z`` and its shape are unchanged. ``.half()`` uses PyTorch's experimental
- complex32 dtype. Since PyTorch has no complex bfloat16 dtype, ``.bfloat16()`` and ``.to(torch.bfloat16)``
- keep the rotation's existing complex precision while following any requested device move.
+ complex32 dtype, which torch's CUDA ``foreach`` SGD step does not support: build a ``torch.optim.SGD``
+ of a CUDA complex32 rotation with ``foreach=False``. Since PyTorch has no complex bfloat16 dtype,
+ ``.bfloat16()`` and ``.to(torch.bfloat16)`` keep the rotation's existing complex precision while
+ following any requested device move.
See Also:
- :class:`So3`: the 3D rotation group, stored as a quaternion.
diff --git a/testing/augmentation/sync_registry.py b/testing/augmentation/sync_registry.py
index c61876656..59cc24b94 100644
--- a/testing/augmentation/sync_registry.py
+++ b/testing/augmentation/sync_registry.py
@@ -219,7 +219,9 @@ def _geometric_cases() -> list[AugCase]:
_aug(K.PadTo, (40, 40), cases=("forward", "inverse", "seq_annotations", "seq_inverse")),
_aug(K.RandomAffine, 15.0, translate=(0.1, 0.1), scale=(0.9, 1.1), shear=10.0, p=1.0, cases=GEOMETRIC_2D),
_aug(K.RandomCrop, (16, 16), p=1.0, cases=GEOMETRIC_2D),
- _aug(K.RandomElasticTransform, p=1.0),
+ # A sequential call passes keypoints and boxes, which the non-rigid warps refuse (#4420), so they run
+ # the forward cases only.
+ _aug(K.RandomElasticTransform, p=1.0, cases=FORWARD_P),
_aug(K.RandomFisheye, **fisheye, p=1.0, cases=FORWARD_P),
_aug(K.RandomHorizontalFlip, p=1.0, cases=GEOMETRIC_2D),
_aug(K.RandomPerspective, 0.5, p=1.0, cases=GEOMETRIC_2D),
diff --git a/tests/geometry/liegroup/test_so2.py b/tests/geometry/liegroup/test_so2.py
index b9c855009..ce9af548d 100644
--- a/tests/geometry/liegroup/test_so2.py
+++ b/tests/geometry/liegroup/test_so2.py
@@ -505,7 +505,9 @@ class TestSo2(BaseTester):
assert s[1].z.shape == () # indexing reads the (B,) view, as for a (B,) z
before = torch.view_as_real(param.detach()).clone() # torch.equal has no complex float16 kernel
s.log().sum().backward()
- torch.optim.SGD(s.parameters(), lr=0.1).step()
+ # torch's CUDA foreach SGD step has no complex32 (float16) implementation.
+ foreach = False if device.type == "cuda" and dtype == torch.float16 else None
+ torch.optim.SGD(s.parameters(), lr=0.1, foreach=foreach).step()
assert not torch.equal(torch.view_as_real(param.detach()), before)
@pytest.mark.parametrize("method", ["to", "bfloat16"])
@@ -715,7 +717,9 @@ class TestSo2DtypeMigration(BaseTester):
assert rotation._z.grad.real.dtype == dtype
self.assert_close(rotation._z.grad.real, torch.ones(2, device=device, dtype=dtype))
self.assert_close(rotation._z.grad.imag, torch.full((2,), 2.0, device=device, dtype=dtype))
- torch.optim.SGD(rotation.parameters(), lr=0.125).step()
+ # torch's CUDA foreach SGD step has no complex32 (float16) implementation.
+ foreach = False if device.type == "cuda" and dtype == torch.float16 else None
+ torch.optim.SGD(rotation.parameters(), lr=0.125, foreach=foreach).step()
self.assert_close(rotation.z.real, before.real.to(dtype) - 0.125)
self.assert_close(rotation.z.imag, before.imag.to(dtype) - 0.25)
diff --git a/tests/geometry/subpix/test_spatial_softargmax.py b/tests/geometry/subpix/test_spatial_softargmax.py
index 3a862b687..eae8aea26 100644
--- a/tests/geometry/subpix/test_spatial_softargmax.py
+++ b/tests/geometry/subpix/test_spatial_softargmax.py
@@ -1135,9 +1135,13 @@ class TestConvQuadInterp3d(BaseTester):
converged = n_iters == 2
expected = torch.tensor(peak if converged else candidate, device=device, dtype=dtype)
self.assert_close(coord[0, 0, :, 2, 2, 5], expected, atol=1e-4, rtol=1e-4)
- # a rejected point keeps the input value at its NMS voxel; the converged one reads the peak value 0
+ # a rejected point keeps the input value at its NMS voxel; the converged one reads the peak value 0. That 0
+ # is the sum of terms of about 0.42 = 0.65**2, so a half dtype gets it to one rounding of those terms: CUDA
+ # returns 0.0015 in bfloat16 and -0.00024 in float16, where the CPU and MPS on torch 2.14 happen to cancel
+ # exactly. The rejected point's value is read, not summed, so it keeps the tight tolerance.
expected_val = torch.zeros((), device=device, dtype=dtype) if converged else sample[0, 0, 2, 2, 5]
- self.assert_close(val[0, 0, 2, 2, 5], expected_val, atol=1e-4, rtol=1e-4)
+ loose = converged and dtype in (torch.float16, torch.bfloat16)
+ self.assert_close(val[0, 0, 2, 2, 5], expected_val, **({} if loose else {"atol": 1e-4, "rtol": 1e-4}))
@pytest.mark.parametrize("op", ["conv_quad_interp3d", "iterative_quad_interp3d"])
@pytest.mark.parametrize("amplitude", [1.0, 1e-2, 1e-3])
diff --git a/tests/geometry/test_conversions.py b/tests/geometry/test_conversions.py
index b286aa3f0..8833b1053 100644
--- a/tests/geometry/test_conversions.py
+++ b/tests/geometry/test_conversions.py
@@ -3243,7 +3243,10 @@ class TestNormalizePixelCoordinates(BaseTester):
runtime = torch.zeros(1, 1, runtime_height, 4, device=device, dtype=dtype)
convert = Convert()
traced = torch.jit.trace(convert, (example, coords))
- actual = traced(runtime, coords)
+ # TorchScript's CUDA fuser keeps a half chain in float32 and rounds once, one ulp from eager; the
+ # comparison is about the traced shape, so it runs unfused.
+ with torch.jit.fuser("none"):
+ actual = traced(runtime, coords)
expected = convert(runtime, coords)
self.assert_close(actual[0], expected[0], atol=0.0, rtol=0.0)
self.assert_close(actual[1], expected[1], atol=0.0, rtol=0.0)
@@ -3262,7 +3265,8 @@ class TestNormalizePixelCoordinates(BaseTester):
runtime = torch.zeros(1, 1, runtime_depth, 3, 4, device=device, dtype=dtype)
convert = Convert3d()
traced = torch.jit.trace(convert, (example, coords3d))
- actual = traced(runtime, coords3d)
+ with torch.jit.fuser("none"):
+ actual = traced(runtime, coords3d)
expected = convert(runtime, coords3d)
self.assert_close(actual[0], expected[0], atol=0.0, rtol=0.0)
self.assert_close(actual[1], expected[1], atol=0.0, rtol=0.0)
diff --git a/tests/geometry/transform/test_homography_warper.py b/tests/geometry/transform/test_homography_warper.py
index b91f91c73..098c27262 100644
--- a/tests/geometry/transform/test_homography_warper.py
+++ b/tests/geometry/transform/test_homography_warper.py
@@ -620,7 +620,10 @@ class TestHomographyWarper(BaseTester):
image, src_homo_dst, (2, width), normalized_coordinates=False, align_corners=align_corners
)
- with torch.autocast(device.type, dtype=autocast_dtype):
+ # The parametrizations share one scripted function. Once TorchScript has profiled it, its CUDA fuser fuses the
+ # float32 grid arithmetic and rounds it a few ulps from eager; the comparison is about autocast, so it runs
+ # unfused.
+ with torch.jit.fuser("none"), torch.autocast(device.type, dtype=autocast_dtype):
actual = scripted(
image, src_homo_dst, (2, width), normalized_coordinates=False, align_corners=align_corners
)