π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(solvers): use float32's cubic-fallback tolerance for half-precision quartics (#4822)
msparsh1309553aabab8f
diff --git a/changelog.d/4822.fixed.md b/changelog.d/4822.fixed.mdnew file mode 100644index 000000000..37cf89588--- /dev/null+++ b/changelog.d/4822.fixed.md@@ -0,0 +1,5 @@+`solve_quartic` now takes its cubic fallback for `float16` and `bfloat16` inputs whose leading coefficient is below+`1e-6` in magnitude, the tolerance `float32` uses, since half-precision quartics are solved in `float32`. `float16`+used to round the fallback tolerance to 0: a zero leading coefficient returned NaN and one below `1e-6` returned an+infinite fourth root. `bfloat16` lost every root of `x^3 - 6x^2 + 11x - 6` behind a leading coefficient of `1e-9`.+Fixes #4498.diff --git a/kornia/geometry/solvers/polynomial_solver.py b/kornia/geometry/solvers/polynomial_solver.pyindex 585d48e49..51198a8df 100644--- a/kornia/geometry/solvers/polynomial_solver.py+++ b/kornia/geometry/solvers/polynomial_solver.py@@ -274,8 +274,9 @@ def solve_quartic(coeffs: torch.Tensor) -> torch.Tensor:solutions = torch.zeros((len(coeffs), 4), device=coeffs.device, dtype=coeffs.dtype)- # Numerical tolerances- zero_tol = 1e-6 if coeffs.dtype == torch.float32 else 1e-12+ # Numerical tolerances. Half-precision quartics are solved in float32 below, so they share+ # float32's cubic-fallback tolerance; float64's 1e-12 would also round to 0 in float16.+ zero_tol = 1e-6 if coeffs.dtype in (torch.float32, torch.float16, torch.bfloat16) else 1e-12# Cubic fallback for a approx 0mask_a_zero = torch.abs(a) < zero_toldiff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txtindex 3667f3961..08d8ed8ce 100644--- a/testing/half_precision_xfails/cpu_float16.txt+++ b/testing/half_precision_xfails/cpu_float16.txt@@ -318,7 +318,6 @@ call builtins.NotImplementedError tests/geometry/liegroup/test_so2.py::TestSo2::call builtins.NotImplementedError tests/geometry/liegroup/test_so2.py::TestSo2::test_random[cpu-float16-None]call builtins.AssertionError tests/geometry/liegroup/test_so3.py::TestSo3::test_ortho[cpu-float16-2]call builtins.AssertionError tests/geometry/solvers/test_polynomial_solver.py::TestQuarticSolver::test_random[cpu-float16]-call builtins.AssertionError tests/geometry/solvers/test_polynomial_solver.py::TestQuarticSolver::test_solve_quartic[cpu-float16-coeffs3-expected_solutions3]call builtins.AssertionError tests/geometry/subpix/test_spatial_softargmax.py::TestConvQuadInterp3dModule::test_diag[cpu-float16]call builtins.AssertionError tests/geometry/subpix/test_spatial_softargmax.py::TestConvSoftArgmax2d::test_cold_diag[cpu-float16]call builtins.AssertionError tests/geometry/subpix/test_spatial_softargmax.py::TestConvSoftArgmax2d::test_cold_diag_norm[cpu-float16]diff --git a/tests/geometry/solvers/test_polynomial_solver.py b/tests/geometry/solvers/test_polynomial_solver.pyindex fa59ce4b1..807a59c51 100644--- a/tests/geometry/solvers/test_polynomial_solver.py+++ b/tests/geometry/solvers/test_polynomial_solver.py@@ -741,3 +741,28 @@ class TestQuarticSolver(BaseTester):# zero-radicand gradient convention was fixed in #4339 and is covered above.assert bool(torch.isfinite(mixed.grad[0]).all()), mixed.gradself.assert_close(mixed.grad[0], alone.grad[0])++ @pytest.mark.parametrize("leading", [0.0, 1e-9, 5e-7])+ def test_cubic_fallback_for_near_zero_leading_coefficient(self, leading, device, dtype):+ # (x - 1)(x - 2)(x - 3) behind a leading coefficient below the 1e-6 cubic-fallback tolerance+ # that float32 and the half-precision inputs solved in float32 share (#4498). float16 rounded+ # float64's 1e-12 to 0 and returned nan, and bfloat16 lost all three roots at 1e-9.+ if leading != 0.0 and dtype == torch.float64:+ pytest.skip("float64 keeps its 1e-12 tolerance and solves these rows as quartics")+ coeffs = torch.tensor([[leading, 1.0, -6.0, 11.0, -6.0]], device=device, dtype=dtype)++ roots = solver.solve_quartic(coeffs)++ expected = torch.cat(+ [solver.solve_cubic(coeffs[:, 1:]), torch.zeros((1, 1), device=device, dtype=dtype)], dim=-1+ )+ self.assert_close(roots, expected, rtol=0.0, atol=0.0)++ def test_leading_coefficient_above_fallback_tolerance_is_solved_as_quartic(self, device, dtype):+ # Just above the 1e-6 tolerance the row keeps its fourth root, near -1 / a - 6 = -500006+ # (beyond float16's range, so -inf there), instead of the cubic fallback's 0.+ coeffs = torch.tensor([[2e-6, 1.0, -6.0, 11.0, -6.0]], device=device, dtype=dtype)++ roots = solver.solve_quartic(coeffs)++ assert roots.min().item() < -1e5, roots