Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): finite float16 gradients in axis_angle_to_rotation_matrix near the identity (#4613)

Mohansree Vijayakumar2825fae9eae3
diff --git a/changelog.d/4613.fixed.md b/changelog.d/4613.fixed.md
new file mode 100644
index 000000000..e93cc59bf
--- /dev/null
+++ b/changelog.d/4613.fixed.md
@@ -0,0 +1 @@
+`axis_angle_to_rotation_matrix` returns finite gradients near the identity in `float16`; the discarded Rodrigues branch no longer differentiates `sqrt(0)` behind a `1e-12` floor that underflows to zero.
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 399101098..0bcc5c45b 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -403,6 +403,11 @@ def axis_angle_to_rotation_matrix(axis_angle: torch.Tensor) -> torch.Tensor:
function since 0.7.0; it emits a ``DeprecationWarning`` and forwards
to this function, returning an equal result β€” see the alias warning
below
+ - differentiable at the identity 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 the identity it
+ moves no forward bit
.. warning::
Calling any of this module's four deprecated aliases
@@ -441,7 +446,7 @@ def axis_angle_to_rotation_matrix(axis_angle: torch.Tensor) -> torch.Tensor:
raise ValueError(f"Input size must be a (*, 3) tensor. Got {axis_angle.shape}")
def _compute_rotation_matrix(axis_angle: torch.Tensor, theta2: torch.Tensor) -> torch.Tensor:
- theta = torch.sqrt(theta2.clamp(min=1e-12)) # clamping to ensure no nan gradients
+ theta = torch.sqrt(theta2)
wxyz = axis_angle / theta.unsqueeze(-1) # (*, 3)
wx, wy, wz = wxyz.unbind(dim=-1) # (*,)
@@ -507,13 +512,15 @@ def axis_angle_to_rotation_matrix(axis_angle: torch.Tensor) -> torch.Tensor:
return rot
theta2 = (axis_angle * axis_angle).sum(dim=-1)
+ mask = theta2 > 1e-6
- rot_normal = _compute_rotation_matrix(axis_angle, theta2) # (*,3,3)
+ # Rows on the Taylor branch feed the discarded Rodrigues branch a stand-in theta2 of 1, so its backward never
+ # differentiates sqrt at 0. A clamp floor is no guard here: 1e-12 underflows to 0 in float16.
+ safe_theta2 = torch.where(mask, theta2, torch.ones_like(theta2))
+ rot_normal = _compute_rotation_matrix(axis_angle, safe_theta2) # (*,3,3)
rot_taylor = _compute_rotation_matrix_taylor(axis_angle) # (*,3,3)
- mask = (theta2 > 1e-6)[..., None, None] # shape (*,1,1)
-
- rotation_matrix = torch.where(mask, rot_normal, rot_taylor)
+ rotation_matrix = torch.where(mask[..., None, None], rot_normal, rot_taylor)
return rotation_matrix
diff --git a/tests/geometry/test_conversions.py b/tests/geometry/test_conversions.py
index 715632b73..9eae0b688 100644
--- a/tests/geometry/test_conversions.py
+++ b/tests/geometry/test_conversions.py
@@ -2190,6 +2190,26 @@ class TestAngleAxisToRotationMatrix(BaseTester):
# evaluate function gradient
self.gradcheck(kornia.geometry.conversions.axis_angle_to_rotation_matrix, (axis_angle,))
+ def test_convention_gradient_is_finite_near_the_identity(self, device, dtype):
+ # Small angles take the Taylor branch, but torch.where still backpropagates through the discarded
+ # Rodrigues branch, whose sqrt(theta2) had a `clamp(min=1e-12)` floor. In float16 that floor is 0, so
+ # the gradient was nan wherever theta2 reached 0 there: the identity, and [1e-4, 0, 0] whose theta2 of
+ # 1e-8 underflows. The third row's theta2 of 2.9e-07 is a float16 subnormal, so it was already finite.
+ # At the identity d(sum R)/dv is the sum of the skew matrix [v]x, which is 0; the other two rows are a
+ # float64 torch.matrix_exp reference, and pinning them keeps this from passing on any finite gradient.
+ axis_angle = torch.tensor(
+ [[0.0, 0.0, 0.0], [1e-4, 0.0, 0.0], [0.0, 5e-4, 2e-4]], device=device, dtype=dtype, requires_grad=True
+ )
+ kornia.geometry.conversions.axis_angle_to_rotation_matrix(axis_angle).sum().backward()
+ assert bool(torch.isfinite(axis_angle.grad).all()), axis_angle.grad
+ self.assert_close(axis_angle.grad[0], torch.zeros(3, device=device, dtype=dtype))
+ self.assert_close(
+ axis_angle.grad[1:],
+ torch.tensor([[-2e-4, 1e-4, 1e-4], [7e-4, -8e-4, 1e-4]], device=device, dtype=dtype),
+ rtol=1e-2,
+ atol=1e-6,
+ )
+
def test_axis_angle_to_rotation_matrix(self, device, dtype, atol, rtol):
rmat_1 = torch.tensor(
(