π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
test: give CUDA gradchecks a 1e-10 nondet_tol for run-to-run rounding of the default kernels (#6587)
Dmytro Mishkin7645e81acb43
diff --git a/testing/base.py b/testing/base.pyindex d34041c17..e02376639 100644--- a/testing/base.py+++ b/testing/base.py@@ -41,6 +41,16 @@ DYNAMIC_EXPORT_UNAVAILABLE_REASON = ()+# gradcheck computes the analytical Jacobian twice and, with its default nondet_tol=0, fails when the two differ at all.+# On CUDA, cuDNN's default convolution algorithms and the backward kernels that accumulate with atomics need not repeat+# their rounding: two backward passes of raw_to_rgb differed by up to 8.9e-16 (gradients up to 5), and of+# HausdorffERLoss3D by up to 4.3e-19, in 1 to 3 % of random float64 cases. This tolerance is 1e5 times that and 1e5+# times below gradcheck's own atol, so the default kernels, the ones users run, stay under test. Deterministic+# algorithms are no substitute: they swap in other kernels, and torch's deterministic reflection_pad2d backward returns+# wrong gradients for large pads (pytorch/pytorch#203421).+CUDA_GRADCHECK_NONDET_TOL = 1e-10++@contextmanagerdef deterministic_algorithms_on_cuda(device: torch.device) -> Iterator[None]:"""Run the block with deterministic algorithms on CUDA, and restore the caller's mode afterwards.@@ -502,4 +512,6 @@ class BaseTester:for i, r, d in zip(inputs, requires_grad, dtypes)]+ if any(isinstance(t, torch.Tensor) and t.device.type == "cuda" for t in _all_inputs):+ kwargs.setdefault("nondet_tol", CUDA_GRADCHECK_NONDET_TOL)return gradcheck(func, inputs, raise_exception=raise_exception, fast_mode=fast_mode, **kwargs)diff --git a/tests/color/test_raw.py b/tests/color/test_raw.pyindex 9041aec58..a01f61cc9 100644--- a/tests/color/test_raw.py+++ b/tests/color/test_raw.py@@ -18,7 +18,6 @@import pytestimport torch-from torch.autograd import gradcheckimport kornia@@ -159,7 +158,7 @@ class TestRawToRgb(BaseTester):def test_gradcheck(self, device, dtype):B, C, H, W = 2, 1, 4, 4img = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)- assert gradcheck(kornia.color.raw_to_rgb, (img, kornia.color.raw.CFA.BG), raise_exception=True, fast_mode=True)+ assert self.gradcheck(kornia.color.raw_to_rgb, (img, kornia.color.raw.CFA.BG))def test_jit(self, device, dtype):B, C, H, W = 2, 1, 4, 4@@ -204,7 +203,7 @@ class TestRgbToRaw(BaseTester):def test_gradcheck(self, device, dtype):B, C, H, W = 2, 3, 4, 4img = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)- assert gradcheck(kornia.color.rgb_to_raw, (img, kornia.color.raw.CFA.BG), raise_exception=True, fast_mode=True)+ assert self.gradcheck(kornia.color.rgb_to_raw, (img, kornia.color.raw.CFA.BG))def test_jit(self, device, dtype):B, C, H, W = 2, 3, 4, 4diff --git a/tests/test_conftest.py b/tests/test_conftest.pyindex a92170871..97f9ece7f 100644--- a/tests/test_conftest.py+++ b/tests/test_conftest.py@@ -25,7 +25,7 @@ import torchfrom conftest import _is_subprocess_isolated_test, skip_half_precision_on_cuda-from testing.base import DYNAMO_UNAVAILABLE_REASON, dynamo_is_available+from testing.base import DYNAMO_UNAVAILABLE_REASON, BaseTester, dynamo_is_availablepytest_plugins = ["pytester"]# Deliberately *not* marked device_agnostic. These are the runner's own regression tests, and@@ -57,6 +57,29 @@ def _neutral_runner_env(monkeypatch):monkeypatch.delenv(var, raising=False)+class _LastBitsVaryingBackward(torch.autograd.Function):+ """Double the input; successive backward passes differ by 1e-12 relative, as an atomic-add kernel can."""++ calls = 0++ @staticmethod+ def forward(ctx, x):+ return 2 * x++ @staticmethod+ def backward(ctx, grad):+ _LastBitsVaryingBackward.calls += 1+ return grad * (2 + 2e-12 * (_LastBitsVaryingBackward.calls % 2))+++def test_base_tester_gradcheck_tolerates_last_bits_nondeterminism_on_cuda_only(device):+ # CUDA_GRADCHECK_NONDET_TOL: on CUDA two backward passes may round differently; CPU keeps nondet_tol=0.+ inputs = (torch.rand(3, device=device),)+ assert BaseTester.gradcheck(_LastBitsVaryingBackward.apply, inputs, raise_exception=False) == (+ device.type == "cuda"+ )++@pytest.mark.device_agnosticdef test_half_precision_isolation_probe(device, dtype):"""Skip under half precision so the isolation runner has a child-side skip to report.