π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): keep NaN out of the minimal-LU QR in find_homography_dlt (CUDA hang) (#4794)
Dmytro Mishkin750c1611e353
diff --git a/changelog.d/4794.fixed.md b/changelog.d/4794.fixed.mdnew file mode 100644index 000000000..fe2f12782--- /dev/null+++ b/changelog.d/4794.fixed.md@@ -0,0 +1 @@+`find_homography_dlt` no longer hangs on CUDA when a four-point sample contains NaN or infinite values: the minimal LU path keeps non-finite entries out of `torch.linalg.qr` and returns a NaN homography for the affected batch elements, as on CPU.diff --git a/docs/source/get-started/precision.rst b/docs/source/get-started/precision.rstindex 0dbde361b..477ca94f6 100644--- a/docs/source/get-started/precision.rst+++ b/docs/source/get-started/precision.rst@@ -173,9 +173,7 @@ rows are a local run at commit ``f8449854`` (2026-09-23; RTX 4090, Python 3.11,CUDA failure is re-run on its own (with ``--isolate-half-precision`` for the half dtypes) and counted by that result.Eight of the CUDA float32 failures are cuDNN TF32 accuracy misses in convolutions(`#4778 <https://github.com/kornia/kornia/issues/4778>`_); the other 14 are tests that assume CPU behavior-(`#4779 <https://github.com/kornia/kornia/issues/4779>`_). The CUDA rows deselect-``TestFindHomographyDLT::test_nocrash`` and ``test_nocrash_lu``, which hang on CUDA-(`#4770 <https://github.com/kornia/kornia/issues/4770>`_).+(`#4779 <https://github.com/kornia/kornia/issues/4779>`_).Reproduce a CPU half-precision row in that environment (the manifest header pins the OS, architecture, Python andPyTorch versions) with:diff --git a/kornia/geometry/homography.py b/kornia/geometry/homography.pyindex 0b2392ca9..dcf3da34f 100644--- a/kornia/geometry/homography.py+++ b/kornia/geometry/homography.py@@ -257,6 +257,12 @@ def find_homography_dlt(# is solved for the rest. A fixed h33=1 gauge is invalid whenever the bottom-right# entry is zero. Five or more points keep the normal-equation formulation above.Aw = A if w_full is None else A * w_full.transpose(-2, -1)+ # torch.linalg.qr on CUDA can spin forever on a design matrix that mixes NaN with the+ # structured zeros of the DLT rows (#4770). Hand QR and the solve finite entries only,+ # and report the affected batch elements as NaN below, as the CPU path already does.+ finite_entries = Aw.isfinite()+ finite = finite_entries.flatten(1).all(-1)+ Aw = torch.where(finite_entries, Aw, torch.zeros_like(Aw))gauge_dtype = torch.float64 if dtype == torch.float64 else torch.float32Q, _ = torch.linalg.qr(Aw.detach().to(gauge_dtype).transpose(-2, -1), mode="complete")null = Q[..., -1]@@ -277,6 +283,7 @@ def find_homography_dlt(sol = torch.where(positions == gauge[:, None], torch.ones_like(positions, dtype=sol.dtype), sol.gather(-1, source))+ sol = torch.where(finite[:, None], sol, torch.full_like(sol, float("nan")))H = sol.reshape(-1, 3, 3)else:raise NotImplementedErrordiff --git a/tests/geometry/test_homography.py b/tests/geometry/test_homography.pyindex 18707c1fa..e0150fb43 100644--- a/tests/geometry/test_homography.py+++ b/tests/geometry/test_homography.py@@ -213,6 +213,9 @@ class TestFindHomographyDLT(BaseTester):H = find_homography_dlt(points1, points2, weights)assert H.shape == (1, 3, 3)+ # A NaN in a minimal sample used to make torch.linalg.qr spin forever on CUDA (#4770). The+ # thread method aborts the session instead of letting a stuck kernel stall the whole run.+ @pytest.mark.timeout(120, method="thread")def test_nocrash(self, device, dtype):points1 = torch.rand(1, 4, 2, device=device, dtype=dtype)points2 = torch.rand(1, 4, 2, device=device, dtype=dtype)@@ -220,7 +223,10 @@ class TestFindHomographyDLT(BaseTester):points1[0, 0, 0] = float("nan")H = find_homography_dlt(points1, points2, weights)assert H.shape == (1, 3, 3)+ # Reading the values synchronizes the device, so a hang surfaces in this test (#4770).+ assert H.isnan().all().item()+ @pytest.mark.timeout(120, method="thread")def test_nocrash_lu(self, device, dtype):points1 = torch.rand(1, 4, 2, device=device, dtype=dtype)points2 = torch.rand(1, 4, 2, device=device, dtype=dtype)@@ -228,6 +234,19 @@ class TestFindHomographyDLT(BaseTester):points1[0, 0, 0] = float("nan")H = find_homography_dlt(points1, points2, weights, "lu")assert H.shape == (1, 3, 3)+ assert H.isnan().all().item()++ @pytest.mark.timeout(120, method="thread")+ def test_nonfinite_sample_leaves_batch_intact(self, device, dtype):+ points1 = torch.rand(3, 4, 2, device=device, dtype=dtype)+ points2 = torch.rand(3, 4, 2, device=device, dtype=dtype)+ weights = torch.ones(3, 4, device=device, dtype=dtype)+ expected = find_homography_dlt(points1[:1], points2[:1], weights[:1])+ points1[1, 0, 0] = float("nan")+ weights[2, 1] = float("inf")+ H = find_homography_dlt(points1, points2, weights)+ assert H[1:].isnan().all().item()+ self.assert_close(H[:1], expected)@pytest.mark.parametrize("batch_size, num_points", [(1, 4), (2, 5), (3, 6)])def test_shape(self, batch_size, num_points, device, dtype):