Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test(geometry): accept a rounding-level SVD gap in the null-space gradient test (#6098)

Dmytro Mishkin3ba87b8bde73
diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.py
index a9b0fcf8c..b4fd6965c 100644
--- a/kornia/geometry/epipolar/essential.py
+++ b/kornia/geometry/epipolar/essential.py
@@ -195,10 +195,12 @@ class _NullSpaceBasis(torch.autograd.Function):
the subspace :math:`S`, by
:math:`dv_i = \sum_{j \notin S} v_j \, v_j^\top d(X^\top X) \, v_i / (\lambda_i - \lambda_j)`.
That needs a gap between the fourth and fifth smallest singular values. Without one the subspace
- itself is not unique, and a nonzero incoming gradient gives a gradient that is not finite, as for
- ``torch.linalg.svd``; a zero incoming gradient, as from a sample whose candidates were discarded,
- gives zero. The forward pass is the same ``_torch_svd_cast`` call as before, so its result is
- unchanged.
+ itself is not unique, and a nonzero incoming gradient gives no usable gradient, as for
+ ``torch.linalg.svd``: one that is not finite where the SVD returns the missing gap as exactly zero,
+ as LAPACK does, and a finite one many orders of magnitude past ``1 / eps`` where it leaves the gap at
+ rounding level, as cuSOLVER's Jacobi SVD, torch's default on CUDA, does. A zero incoming gradient, as
+ from a sample whose candidates were discarded, gives zero. The forward pass is the same
+ ``_torch_svd_cast`` call as before, so its result is unchanged.
"""
# forward and setup_context are separate, so torch.func transforms (grad, vjp, jacrev) accept it
diff --git a/tests/geometry/epipolar/test_essential.py b/tests/geometry/epipolar/test_essential.py
index 6e4ab91ee..5a6bbfd04 100644
--- a/tests/geometry/epipolar/test_essential.py
+++ b/tests/geometry/epipolar/test_essential.py
@@ -369,11 +369,17 @@ class TestFindEssential(BaseTester):
(basis * 0.0).sum().backward()
assert torch.isfinite(design.grad).all()
assert (design.grad == 0).all()
- # A nonzero incoming gradient has no finite derivative there, as for torch.linalg.svd. It must not
- # come out as a silent zero, which is what the dropped gradient of #4855 looked like.
+ # A nonzero incoming gradient has no usable derivative there, as for torch.linalg.svd. It must not
+ # come out as a silent zero, which is what the dropped gradient of #4855 looked like. How it shows
+ # depends on the SVD: LAPACK returns the four zero singular values as exact zeros, so the backward
+ # divides by a zero gap and the gradient is not finite, while cuSOLVER's Jacobi SVD, torch's default
+ # on CUDA, leaves them at rounding level (1.6e-16 down to 1.5e-61) and the gradient is finite but
+ # near 6e60, as torch.linalg.svd's own backward is on the same input. Past 1 / eps, a gradient can
+ # only come from a gap at rounding level.
design.grad = None
epi.essential._NullSpaceBasis.apply(design)[0].sum().backward()
- assert not torch.isfinite(design.grad).all()
+ usable = torch.isfinite(design.grad) & (design.grad.abs() < 1 / torch.finfo(design.dtype).eps)
+ assert not usable.all()
def test_torch_func_grad(self, device):
# torch.func transforms accept an autograd.Function only if it defines setup_context. With 5