Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(tests): make cuDNN TF32 follow --tf32 (#6249)

Karnajeet Gosavi5673a356585e
diff --git a/conftest.py b/conftest.py
index 86ab5e673..db5322e29 100644
--- a/conftest.py
+++ b/conftest.py
@@ -175,37 +175,9 @@ def torch_optimizer(optimizer_backend):
@pytest.fixture
def cudnn_tf32_follows_option(request):
- """Compute convolutions in real float32 on CUDA, so a float32 tolerance means float32.
-
- The root ``conftest.py``'s ``--tf32`` option drives only ``set_float32_matmul_precision``, which
- does not reach cuDNN: ``torch.backends.cudnn.allow_tf32`` keeps PyTorch's ``True`` default in
- every run. TF32 rounds a convolution's inputs to 10 mantissa bits, and eager and inductor do not
- have to pick the same kernel, so on a deep conv stack the two disagree by ~2e-4 -- twenty times
- ``assert_close``'s float32 ``atol`` -- while the same comparison agrees to 6e-7 once cuDNN is in
- real float32. A compile-vs-eager test that does not take TF32 out of the picture is measuring the
- backend rather than the graph.
-
- The flag follows ``--tf32`` rather than being forced off, so a ``--tf32`` run still gets TF32 in
- cuDNN as well as in matmul. The repo-wide fix is for ``pytest_sessionstart`` to set
- ``cudnn.allow_tf32`` from the same option; ``tests/color/test_yuv.py`` carries the same local
- workaround for the same reason.
-
- The alternative -- keeping TF32 on and widening the CUDA float32 bound to ~1e-3, TF32's own
- mantissa -- was tried and dropped: it leaves the assertion a hundred times looser than the signal
- it exists to check, so a genuine compile-vs-eager divergence below 1e-3 would pass. The objection
- to this route is that inductor's on-disk FX graph cache is not keyed on ``allow_tf32``, so a
- context entered after a TF32-on compile in the same process can raise out of
- ``assert_tensor_metadata``. That applies to ``torch.backends.cudnn.flags(...)``, which swaps
- several flags at once; assigning the single attribute, as here and in ``test_yuv.py``, does not
- reproduce it -- ``tests/feature -k "dynamo or compile"`` on CUDA float32 is green with this fixture
- on both a cold and a warm inductor cache.
- """
- previous = torch.backends.cudnn.allow_tf32
- torch.backends.cudnn.allow_tf32 = bool(request.config.getoption("--tf32"))
- try:
- yield
- finally:
- torch.backends.cudnn.allow_tf32 = previous
+ """Assert that cuDNN precision follows the session's opt-in TF32 setting."""
+ if torch.backends.cudnn.allow_tf32 != request.config.getoption("--tf32"):
+ pytest.fail("cuDNN TF32 setting does not match --tf32")
def _parse_test_option(config, option: str, all_values: dict | set) -> list[str]:
@@ -547,8 +519,7 @@ def pytest_addoption(parser):
action="store_true",
default=os.environ.get("KORNIA_TEST_TF32", "false").lower() == "true",
help=(
- "Enable TF32 (TensorFloat-32) mode for CUDA matrix multiplications "
- "(torch.set_float32_matmul_precision('high')). "
+ "Enable TF32 (TensorFloat-32) mode for CUDA matrix multiplications and cuDNN convolutions. "
"Tests marked @pytest.mark.tf32 are expected to fail under TF32 and are skipped unless this flag is set. "
"(env: KORNIA_TEST_TF32)"
),
@@ -649,6 +620,8 @@ def pytest_sessionstart(session):
# and to run the @pytest.mark.tf32-marked tests.
if session.config.getoption("--tf32"):
torch.set_float32_matmul_precision("high")
+ # cuDNN allows TF32 by default independently of matmul precision; keep it opt-in too.
+ torch.backends.cudnn.allow_tf32 = session.config.getoption("--tf32")
# Warm torch.compile only for explicit optimizer jobs. Ordinary jobs deselect compile tests,
# while subprocess mode also avoids startup overhead and captured-output pollution.
diff --git a/pyproject.toml b/pyproject.toml
index 4a7945046..76152f123 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -177,7 +177,7 @@ markers = [
"mps_process_abort: aborts the pytest process on MPS on virtualized Apple GPUs (#4204); skipped there unless --run-mps-process-abort",
"onnx_conformance: ONNX conformance of the promised operators (#5819); deselected unless --onnx-conformance selects packages",
"slow: mark test as slow to run",
- "tf32: mark a test as sensitive to TF32 (TensorFloat-32) CUDA matmul precision; xfail by default, run with --tf32 to activate",
+ "tf32: mark a test as sensitive to TF32 (TensorFloat-32) CUDA matmul and cuDNN precision; xfail by default, run with --tf32 to activate",
]
testpaths = ["tests"]
# Test configuration via environment variables:
diff --git a/tests/color/test_yuv.py b/tests/color/test_yuv.py
index f01a81a23..88d93000a 100644
--- a/tests/color/test_yuv.py
+++ b/tests/color/test_yuv.py
@@ -156,31 +156,9 @@ def _unit_atol(base: float, dtype: torch.dtype) -> float:
return base + {torch.float64: 0.0, torch.float32: 1e-6, torch.float16: 2e-3, torch.bfloat16: 1.2e-2}.get(dtype, 0.0)
[email protected](autouse=True)
-def _cudnn_tf32_disabled(request):
- """Compute in real float32 on CUDA by default, so every tolerance here means what it derives.
-
- Off cpu, ``_apply_linear_transformation`` takes the ``F.conv2d`` branch, and cuDNN keeps
- PyTorch's ``allow_tf32 = True`` default -- conftest's ``--tf32`` flag drives only
- ``set_float32_matmul_precision``, which does not reach cuDNN. TF32 rounds both conv inputs
- and every kernel literal to 10 mantissa bits, so one forward transform carries up to
- ``2 * 2**-11 * sum|k_i * x_i|`` = 1.2e-3 of backend noise on the V row alone -- twice
- _FORWARD_ATOL, and the same order as the 1.356e-3 constants defect this file exists to pin.
- Every tolerance here is derived from the *constants*, not from a backend's kernel precision,
- so an ordinary run takes TF32 out of the picture rather than budgeting it into each assertion.
-
- The flag follows ``--tf32`` rather than being forced off, so this module does not become the
- one place in the suite that ignores the option: a ``--tf32`` run gets TF32 in cuDNN as well
- as in matmul, and measures the backend instead of the constants. The repo-wide fix is for
- ``pytest_sessionstart`` to set ``cudnn.allow_tf32`` from the same option -- until then this
- is the only module whose derived bounds depend on it.
- """
- previous = torch.backends.cudnn.allow_tf32
- torch.backends.cudnn.allow_tf32 = bool(request.config.getoption("--tf32"))
- try:
- yield
- finally:
- torch.backends.cudnn.allow_tf32 = previous
+# These bounds cover differences between the pinned constants and the reference transform, not
+# backend precision. The root pytest session follows --tf32 for cuDNN as well as matmul, so the
+# default run uses real float32 and an explicit --tf32 run measures the lower-precision backend.
# The three generators below deliberately build on cpu/float64 and leave the move to the
diff --git a/tests/test_conftest.py b/tests/test_conftest.py
index 3437c1c22..a92170871 100644
--- a/tests/test_conftest.py
+++ b/tests/test_conftest.py
@@ -38,6 +38,10 @@ pytest_plugins = ["pytester"]
_skip_fixture_fn = getattr(skip_half_precision_on_cuda, "__wrapped__", skip_half_precision_on_cuda)
+def test_cudnn_tf32_follows_tf32_option(request):
+ assert torch.backends.cudnn.allow_tf32 is request.config.getoption("--tf32")
+
+
@pytest.fixture(autouse=True)
def _neutral_runner_env(monkeypatch):
"""Keep the ambient run's runner flags out of the pytester subprocesses.