Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test: give CUDA half batch-vs-single comparisons a half tolerance (LoFTR, SmallSR, LightGlue) (#6584)

Dmytro Mishkin2620e13e0f38
diff --git a/tests/feature/test_lightglue.py b/tests/feature/test_lightglue.py
index 43d3abf9b..668996472 100644
--- a/tests/feature/test_lightglue.py
+++ b/tests/feature/test_lightglue.py
@@ -1362,9 +1362,15 @@ class TestLightGlueBatchedPairs(BaseTester):
out = self._forward(model, self._stack([data for data, _ in pairs]), pruning_threshold=1000)
assert out["log_assignment"].shape == (2, self._M + 1, self._M + 1)
assert out["stop"] == 2
+ # CUDA half matrix products accumulate in an order that depends on the batch size. The matches stay exact,
+ # but in bfloat16 a log-assignment entry of about -90 moves by up to two ulps (1.4 eps relative), about as
+ # far as bfloat16 log assignments are from float32 ones (1.2 eps); on CPU the half values are bitwise equal.
+ low_tolerance = device.type == "cuda" and dtype in (torch.float16, torch.bfloat16)
for k in range(2):
self._assert_pair(out, k, singles[k], exact=False)
- self.assert_close(out["log_assignment"][k : k + 1], singles[k]["log_assignment"])
+ self.assert_close(
+ out["log_assignment"][k : k + 1], singles[k]["log_assignment"], low_tolerance=low_tolerance
+ )
self.assert_close(out["prune0"], torch.ones(2, self._M, device=device, dtype=torch.long))
def test_convention_lightglue_batch_stops_early_when_every_pair_meets_its_criterion_5898(self, device, dtype):
diff --git a/tests/feature/test_loftr.py b/tests/feature/test_loftr.py
index a7d57fa7c..e00617972 100644
--- a/tests/feature/test_loftr.py
+++ b/tests/feature/test_loftr.py
@@ -266,9 +266,13 @@ class TestLoFTR(BaseTester):
single = model.extract_features(images[1:])
assert batched["coarse"].shape == (2, 256, 13, 9)
assert batched["fine"].shape == (2, 128, 50, 34)
- # a batch of images gives each image's features (up to the backend's choice of kernel for the batch size)
- self.assert_close(batched["coarse"][1:], single["coarse"])
- self.assert_close(batched["fine"][1:], single["fine"])
+ # a batch of images gives each image's features (up to the backend's choice of kernel for the batch size).
+ # CUDA half convolutions accumulate in an order that depends on the batch size, so batched and single
+ # features differ by up to about eps times the largest feature (0.003 in float16, 0.03 in bfloat16), less
+ # than half features differ from float32 ones (0.006 and 0.05); CPU gives the same features for both.
+ low_tolerance = device.type == "cuda" and dtype in (torch.float16, torch.bfloat16)
+ self.assert_close(batched["coarse"][1:], single["coarse"], low_tolerance=low_tolerance)
+ self.assert_close(batched["fine"][1:], single["fine"], low_tolerance=low_tolerance)
def test_inputs_are_validated(self, device, dtype):
model = _loftr_matching_every_cell(device, dtype)
diff --git a/tests/models/test_small_sr.py b/tests/models/test_small_sr.py
index b48c6638e..ba705713c 100644
--- a/tests/models/test_small_sr.py
+++ b/tests/models/test_small_sr.py
@@ -290,9 +290,14 @@ class TestSmallSRNetWrapper(BaseTester):
output2 = model(x2)
output_batch = model(x_batch)
- # Individual processing should match batch processing
- self.assert_close(output1, output_batch[0:1], rtol=1e-4, atol=1e-4)
- self.assert_close(output2, output_batch[1:2], rtol=1e-4, atol=1e-4)
+ # Individual processing should match batch processing. CUDA half convolutions accumulate in an order that
+ # depends on the batch size, which moves an output by up to 0.0005 in float16 and 0.002 in bfloat16, while
+ # half outputs differ from float32 ones by about 0.0015 and 0.012, so CUDA half dtypes use the default half
+ # tolerance; CPU gives the same outputs for both.
+ cuda_half = device.type == "cuda" and dtype in (torch.float16, torch.bfloat16)
+ tol = {} if cuda_half else {"rtol": 1e-4, "atol": 1e-4}
+ self.assert_close(output1, output_batch[0:1], **tol)
+ self.assert_close(output2, output_batch[1:2], **tol)
class TestSmallSRNetWeights: