Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test(metrics): keep cuDNN out of TF32 in the ssim3d tests; document the TF32 effect (#6099)

Dmytro Mishkin28bf0423f4ab
diff --git a/kornia/metrics/ssim3d.py b/kornia/metrics/ssim3d.py
index c0b2c5cf1..4ccb3bb33 100644
--- a/kornia/metrics/ssim3d.py
+++ b/kornia/metrics/ssim3d.py
@@ -93,6 +93,10 @@ def ssim3d(
Integer images are converted to float32 before computing the local moments.
Half-precision inputs are evaluated in float32 for numerical stability.
Filtering runs with autocast disabled; the result uses the promoted input dtype.
+ On CUDA the float32 filtering follows ``torch.backends.cudnn.allow_tf32``, which PyTorch enables by default.
+ cuDNN can then filter a single-channel volume with a TF32 kernel, which rounds the inputs of the local
+ moments to 10 mantissa bits. The variances are differences of those moments, so in flat regions the map can
+ be off by more than ``1e-2``. Set ``torch.backends.cudnn.allow_tf32 = False`` when float32 accuracy matters.
Examples:
>>> input1 = torch.rand(1, 4, 5, 5, 5)
diff --git a/tests/metrics/test_ssim3d.py b/tests/metrics/test_ssim3d.py
index 74824c1e3..381843927 100644
--- a/tests/metrics/test_ssim3d.py
+++ b/tests/metrics/test_ssim3d.py
@@ -24,6 +24,12 @@ from kornia.core.exceptions import BaseError
from testing.base import BaseTester
+# These tests assert float32 tolerances, but cuDNN runs a float32 conv3d in TF32 unless `cudnn.allow_tf32` is off, and
+# it picks a TF32 kernel for the single-channel volumes most of them use. Rounding the inputs of the local moments to
+# 10 mantissa bits moves the map by up to 1.1e-2 here (a 128 vs 64 pair at max_val=255), where real float32 is within
+# 7e-6. `cudnn_tf32_follows_option` keeps cuDNN in float32 unless `--tf32` is passed (#4778).
+pytestmark = pytest.mark.usefixtures("cudnn_tf32_follows_option")
+
class TestSSIM3d(BaseTester):
@pytest.mark.parametrize("image_dtype", [torch.uint8, torch.int16])