π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix NaN gradients at the rotation singularity in quaternion and axis-angle conversions (#3980)
Chandan P58509ecb1e76
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.pyindex 4780131c8..303ce7f30 100644--- a/kornia/geometry/conversions.py+++ b/kornia/geometry/conversions.py@@ -958,13 +958,11 @@ def quaternion_to_axis_angle(quaternion: torch.Tensor) -> torch.Tensor:- ``quaternion_to_angle_axis`` is the deprecated alias of this functionsince 0.7.0; see the alias warning on:func:`~kornia.geometry.conversions.axis_angle_to_rotation_matrix`-- .. warning::- The gradient at the identity quaternion is ``nan``:- ``quaternion_to_axis_angle(torch.tensor([1., 0., 0., 0.])).sum().backward()``- leaves ``[nan, nan, nan, nan]`` in ``.grad``, from an unclamped- ``sqrt(0)``. Away from the identity the gradient is finite. Tracked in- `#3949 <https://github.com/kornia/kornia/issues/3949>`_.+ - differentiable at the identity quaternion. The ``sqrt`` whose+ derivative is unbounded there never sees its zero radicand, so the+ gradient is the analytic limit of the surrounding map rather than+ ``nan``. The guard is elementwise, and away from the identity it+ moves no forward bitArgs:quaternion: tensor with quaternions.@@ -996,14 +994,18 @@ def quaternion_to_axis_angle(quaternion: torch.Tensor) -> torch.Tensor:sin_squared_theta: torch.Tensor = q1 * q1 + q2 * q2 + q3 * q3- sin_theta: torch.Tensor = torch.sqrt(sin_squared_theta)+ # the sqrt and the division below are only taken where the radicand is non zero. the inner+ # `where` keeps the identity rotation away from the sqrt so its backward pass stays finite.+ pos: torch.Tensor = sin_squared_theta > 0.0+ safe_sin_squared_theta: torch.Tensor = torch.where(pos, sin_squared_theta, torch.ones_like(sin_squared_theta))+ sin_theta: torch.Tensor = torch.where(pos, torch.sqrt(safe_sin_squared_theta), torch.zeros_like(sin_squared_theta))two_theta: torch.Tensor = 2.0 * torch.where(cos_theta < 0.0, torch.atan2(-sin_theta, -cos_theta), torch.atan2(sin_theta, cos_theta))- k_pos: torch.Tensor = two_theta / sin_theta+ k_pos: torch.Tensor = two_theta / torch.where(pos, sin_theta, torch.ones_like(sin_theta))k_neg: torch.Tensor = 2.0 * torch.ones_like(sin_theta)- k: torch.Tensor = torch.where(sin_squared_theta > 0.0, k_pos, k_neg)+ k: torch.Tensor = torch.where(pos, k_pos, k_neg)axis_angle: torch.Tensor = torch.zeros_like(quaternion)[..., :3]axis_angle[..., 0] += q1 * k@@ -1212,12 +1214,10 @@ def axis_angle_to_quaternion(axis_angle: torch.Tensor) -> torch.Tensor:``sqrt`` on the way in already produces a floating tensor, and theoutput is allocated from that rather than from the input, so theresult comes back at the ambient default floating dtype-- .. warning::- The gradient at the zero rotation is ``nan``:- ``axis_angle_to_quaternion(torch.zeros(3)).sum().backward()`` leaves- ``[nan, nan, nan]`` in ``.grad``, from an unclamped ``sqrt(0)``.- Tracked in `#3949 <https://github.com/kornia/kornia/issues/3949>`_.+ - differentiable at the zero rotation. The ``sqrt`` whose derivative is+ unbounded there never sees its zero radicand, so the gradient is the+ analytic limit of the surrounding map rather than ``nan``. The guard+ is elementwise, and away from zero it moves no forward bitArgs:axis_angle: tensor with axis angle in radians.@@ -1247,14 +1247,18 @@ def axis_angle_to_quaternion(axis_angle: torch.Tensor) -> torch.Tensor:a2: torch.Tensor = axis_angle[..., 2:3]theta_squared: torch.Tensor = a0 * a0 + a1 * a1 + a2 * a2- theta: torch.Tensor = torch.sqrt(theta_squared)+ mask: torch.Tensor = theta_squared > 0.0++ # the sqrt and the division below are only taken where the radicand is non zero. the inner+ # `where` keeps the zero rotation away from the sqrt so its backward pass stays finite.+ safe_theta_squared: torch.Tensor = torch.where(mask, theta_squared, torch.ones_like(theta_squared))+ theta: torch.Tensor = torch.where(mask, torch.sqrt(safe_theta_squared), torch.zeros_like(theta_squared))half_theta: torch.Tensor = theta * 0.5- mask: torch.Tensor = theta_squared > 0.0ones: torch.Tensor = torch.ones_like(half_theta)k_neg: torch.Tensor = 0.5 * ones- k_pos: torch.Tensor = torch.sin(half_theta) / theta+ k_pos: torch.Tensor = torch.sin(half_theta) / torch.where(mask, theta, ones)k: torch.Tensor = torch.where(mask, k_pos, k_neg)w: torch.Tensor = torch.where(mask, torch.cos(half_theta), ones)diff --git a/tests/geometry/test_conversions.py b/tests/geometry/test_conversions.pyindex bccb19e83..1868bf220 100644--- a/tests/geometry/test_conversions.py+++ b/tests/geometry/test_conversions.py@@ -712,6 +712,33 @@ class TestAngleAxisToQuaternion(BaseTester):expected = torch.tensor((np.cos(0.5), np.sin(0.5), 0.0, 0.0), device=device, dtype=input_dtype)self.assert_close(quaternion, expected, atol=1.0e-2, rtol=1.0e-2)+ def test_convention_the_guarded_sqrt_moves_no_forward_value_3949(self, device, dtype):+ # The #3949 fix is a BACKWARD-pass fix: away from theta == 0 the guarded sqrt must return+ # the very same BITS as the plain `sqrt(theta_squared)` formula it replaced, so no existing+ # forward value moves. torch.equal, not assert_close, for exactly that reason -- a+ # tolerance would hide the reassociation this pin exists to rule out.+ # The fourth row is the near-singular one, and it is derived from the dtype rather than+ # written as a literal: a fixed 1e-9 UNDERFLOWS to 0 at float16 (its smallest subnormal is+ # 5.96e-8), which puts the row on the guarded side of the branch, makes the reference+ # formula's own sin(0)/0 a NaN, and fails this pin under `pixi run test-half` while passing+ # everywhere else. finfo(dtype).eps squares to something representable in all four dtypes,+ # so the row stays near-singular AND stays on the unguarded side in each.+ # Companion: test_convention_gradients_at_the_identity_are_finite_3949 covers theta == 0+ # itself, where the two formulas deliberately DISAGREE.+ near_singular = torch.finfo(dtype).eps+ axis_angle = torch.tensor(+ ((0.1, 0.2, 0.3), (1.0, 0.0, 0.0), (3.0, -2.0, 0.5), (near_singular, 0.0, 0.0)),+ device=device,+ dtype=dtype,+ )+ a0, a1, a2 = axis_angle[..., 0:1], axis_angle[..., 1:2], axis_angle[..., 2:3]+ theta = torch.sqrt(a0 * a0 + a1 * a1 + a2 * a2)+ half_theta = theta * 0.5+ k = torch.sin(half_theta) / theta+ expected = torch.cat((torch.cos(half_theta), a0 * k, a1 * k, a2 * k), dim=-1)+ quaternion = kornia.geometry.conversions.axis_angle_to_quaternion(axis_angle)+ assert torch.equal(quaternion, expected)+def test_gradcheck(self, device):dtype = torch.float64eps = torch.finfo(dtype).eps@@ -854,33 +881,47 @@ class TestAngleAxisToQuaternion(BaseTester):)torch.testing.assert_close(actual, expected, rtol=0, atol=0)- @pytest.mark.xfail(- raises=AssertionError,- reason="unguarded sqrt(0) in the axis-angle/quaternion pair makes the gradient NaN at the "- "identity rotation β kornia#3949",- strict=True,- )- @pytest.mark.parametrize(- ("op_name", "arg"),- [("quaternion_to_axis_angle", [1.0, 0.0, 0.0, 0.0]), ("axis_angle_to_quaternion", [0.0, 0.0, 0.0])],- )- def test_convention_gradients_at_the_identity_are_finite_3949(self, device, op_name, arg):- # Intended behavior: both directions of the axis-angle/quaternion pair are differentiable- # at the identity rotation -- the point every optimiser initialises at and converges to.- # They are not: each takes an unguarded sqrt of a quantity that is exactly 0 there, so the- # backward pass divides by 0 and the gradient is NaN for the whole input- # (quaternion_to_axis_angle at q = (1,0,0,0) and axis_angle_to_quaternion at aa = (0,0,0)).- # axis_angle_to_rotation_matrix is the in-kornia template for the fix: at the same point it- # returns a finite gradient [0, 0, 0] because it clamps theta2 to 1e-12 before the sqrt,- # with the comment "clamping to ensure no nan gradients". Away from the identity both- # gradients are already finite (measured at q = (0.938, 0.147, 0.196, 0.245):- # [-1.1751557857766646, 1.9227726795680562, 1.8829361000064004, 1.8430995204447442]).- # Both legs live in this class so the pair stays one edit even though- # quaternion_to_axis_angle is exercised by TestQuaternionToAngleAxis. float64 is hardcoded- # and the dtype fixture dropped because gradient claims in this file are float64 claims- # (see test_rad2deg_gradcheck); the NaN is not float64-specific -- float32 gives NaN too.- # Marked xfail(strict=True) so fixing #3949 makes both cases XPASS and forces this mark- # out. Companion wart: test_wart_gradients_at_the_identity_are_nan_3949.+ # (op name, input at the identity rotation, expected gradient of the output sum) for the #3949+ # pin below. The gradients are analytic, not measured: around theta == 0 axis_angle_to_quaternion+ # is v -> (1, v/2) so d(sum)/dv = (0.5, 0.5, 0.5), and around the identity quaternion_to_axis_angle+ # is q -> 2*(x, y, z) so d(sum)/dq = (0, 2, 2, 2) -- the w component does not reach the output.+ # Pinning the VALUES and not just finiteness is what separates "the NaN is gone" from "the NaN+ # was replaced by the right number": a guard that clamped the radicand to 1e-12 the way+ # axis_angle_to_rotation_matrix does would also be finite here, and wrong by a factor of the+ # clamp. One table, two cells, because the two functions have independent sqrt guards.+ _IDENTITY_GRADIENT_CASES = [+ ("quaternion_to_axis_angle", [1.0, 0.0, 0.0, 0.0], [0.0, 2.0, 2.0, 2.0]),+ ("axis_angle_to_quaternion", [0.0, 0.0, 0.0], [0.5, 0.5, 0.5]),+ ]++ @pytest.mark.parametrize(("op_name", "arg", "expected_grad"), _IDENTITY_GRADIENT_CASES)+ def test_convention_gradients_at_the_identity_are_finite_3949(self, device, op_name, arg, expected_grad):+ # Convention: both directions of the axis-angle/quaternion pair are differentiable at the+ # identity rotation -- the point every optimiser initialises at and converges to. Until+ # kornia#3949 was fixed they were not: each took an unguarded sqrt of a quantity that is+ # exactly 0 there, so the backward pass divided by 0 and the gradient was NaN for the whole+ # input (quaternion_to_axis_angle at q = (1,0,0,0), axis_angle_to_quaternion at aa = (0,0,0)).+ # The fix keeps the singular point away from the sqrt with a `where` on the radicand rather+ # than clamping it, so the forward value at the identity is unchanged -- pinned separately+ # by test_convention_the_guarded_sqrt_moves_no_forward_value_3949 in each class.+ # Three claims per cell, in order:+ # (1) the gradient is finite -- the #3949 headline;+ # (2) it equals the analytic value from _IDENTITY_GRADIENT_CASES, so a merely-finite+ # wrong number (a clamped radicand) does not pass;+ # (3) the guard is ELEMENTWISE: in a batch holding an identity row and an ordinary one,+ # the identity row gets the identity gradient AND the ordinary row gets exactly the+ # gradient it gets on its own. Not a NaN-containment claim -- NaN never crossed rows,+ # measured on the unfixed code, since the ops are elementwise and .sum() sends an+ # independent gradient to each. What it rules out is a guard written as a PYTHON+ # branch (`if theta_squared > 0: ...`), which takes one branch for the whole tensor:+ # that is the shape a naive fix takes, it passes both scalar cells, and it is wrong+ # for every mixed batch. The ordinary row's own gradient is measured in the test+ # rather than pinned as a literal, so this cell stays a statement about+ # batched-vs-unbatched agreement and not a second copy of the numerical pins.+ # Both legs live in this class so the pair stays one edit even though quaternion_to_axis_angle+ # is exercised by TestQuaternionToAngleAxis. float64 is hardcoded and the dtype fixture+ # dropped because gradient claims in this file are float64 claims (see test_rad2deg_gradcheck);+ # the NaN was not float64-specific -- float32 gave NaN too._skip_if_dtype_unavailable(device, torch.float64)op = getattr(kornia.geometry.conversions, op_name)@@ -888,35 +929,23 @@ class TestAngleAxisToQuaternion(BaseTester):op(x).sum().backward()+ assert x.grad is not Noneassert torch.isfinite(x.grad).all(), f"kornia#3949: {op_name} has a non-finite gradient at the identity"+ self.assert_close(x.grad, torch.tensor(expected_grad, device=device, dtype=torch.float64))- @pytest.mark.parametrize(- ("op_name", "arg"),- [("quaternion_to_axis_angle", [1.0, 0.0, 0.0, 0.0]), ("axis_angle_to_quaternion", [0.0, 0.0, 0.0])],- )- def test_wart_gradients_at_the_identity_are_nan_3949(self, device, op_name, arg):- # Wart pin for kornia#3949, companion to the strict xfail above: assert that the gradient- # is CURRENTLY all-NaN at the identity rotation. Two cells because the two functions have- # independent sqrt guards -- clamping inside quaternion_to_axis_angle does nothing for- # axis_angle_to_quaternion and vice versa, so a half fix flips exactly one cell while the- # xfail's other case stays silently XFAIL. If either fails, #3949 was (partly) fixed --- # remove the corresponding case here and flip/remove the strict xfail above. NOT a contract- # that these gradients must stay NaN.- # Snippet used to generate expected (torch only, executed on cpu at float64 and float32):- # q = torch.tensor([1., 0., 0., 0.], dtype=torch.float64, requires_grad=True)- # quaternion_to_axis_angle(q).sum().backward(); q.grad -> [nan, nan, nan, nan]- # a = torch.zeros(3, dtype=torch.float64, requires_grad=True)- # axis_angle_to_quaternion(a).sum().backward(); a.grad -> [nan, nan, nan]- # axis_angle_to_rotation_matrix(torch.zeros(1, 3, dtype=torch.float64, requires_grad=True))- # .sum().backward() -> [[0.0, 0.0, 0.0]] (finite: the clamped-theta2 reference)- _skip_if_dtype_unavailable(device, torch.float64)+ # The batch leg: the identity row first, an ordinary rotation second.+ regular = [0.9, 0.1, 0.2, 0.3] if op_name == "quaternion_to_axis_angle" else [0.1, 0.2, 0.3]- op = getattr(kornia.geometry.conversions, op_name)- x = torch.tensor(arg, device=device, dtype=torch.float64, requires_grad=True)+ alone = torch.tensor(regular, device=device, dtype=torch.float64, requires_grad=True)+ op(alone).sum().backward()- op(x).sum().backward()+ batch = torch.tensor([arg, regular], device=device, dtype=torch.float64, requires_grad=True)+ op(batch).sum().backward()- assert torch.isnan(x.grad).all(), f"kornia#3949: {op_name} no longer has an all-NaN gradient at the identity"+ assert batch.grad is not None+ assert torch.isfinite(batch.grad).all(), f"kornia#3949: {op_name} has a non-finite gradient in a batch"+ self.assert_close(batch.grad[0], torch.tensor(expected_grad, device=device, dtype=torch.float64))+ self.assert_close(batch.grad[1], alone.grad, atol=0.0, rtol=0.0)class TestQuaternionToAngleAxis(BaseTester):@@ -976,6 +1005,33 @@ class TestQuaternionToAngleAxis(BaseTester):axis_angle = kornia.geometry.conversions.quaternion_to_axis_angle(quaternion)self.assert_close(axis_angle, expected, atol=atol, rtol=rtol)+ def test_convention_the_guarded_sqrt_moves_no_forward_value_3949(self, device, dtype):+ # The quaternion_to_axis_angle half of the pin documented on+ # TestAngleAxisToQuaternion.test_convention_the_guarded_sqrt_moves_no_forward_value_3949 --+ # same claim, same reason the near-singular row is derived from finfo(dtype).eps rather+ # than written as a fixed 1e-9, which underflows to 0 at float16. Kept in this class rather+ # than as a third cell of that one because the reference formula is a different expression.+ quaternion = torch.tensor(+ (+ (0.9, 0.1, 0.2, 0.3),+ (0.0, 1.0, 0.0, 0.0),+ (0.5, 3.0, -2.0, 0.5),+ (1.0, torch.finfo(dtype).eps, 0.0, 0.0),+ ),+ device=device,+ dtype=dtype,+ )+ cos_theta = quaternion[..., 0]+ q1, q2, q3 = quaternion[..., 1], quaternion[..., 2], quaternion[..., 3]+ sin_theta = torch.sqrt(q1 * q1 + q2 * q2 + q3 * q3)+ two_theta = 2.0 * torch.where(+ cos_theta < 0.0, torch.atan2(-sin_theta, -cos_theta), torch.atan2(sin_theta, cos_theta)+ )+ k = two_theta / sin_theta+ expected = torch.stack((q1 * k, q2 * k, q3 * k), dim=-1)+ axis_angle = kornia.geometry.conversions.quaternion_to_axis_angle(quaternion)+ assert torch.equal(axis_angle, expected)+def test_gradcheck(self, device):dtype = torch.float64eps = torch.finfo(dtype).eps