Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): divide the _so3_small_angle_coefficients closed forms by theta twice, not by theta**3 and theta**2 (#4967)

εŒΊζ’“ηcbd863f1b7a1
diff --git a/changelog.d/4967.fixed.md b/changelog.d/4967.fixed.md
new file mode 100644
index 000000000..751ff4386
--- /dev/null
+++ b/changelog.d/4967.fixed.md
@@ -0,0 +1,5 @@
+`_so3_small_angle_coefficients` now divides its closed forms by `theta` one factor at a time instead of by `theta**2`
+and `theta**3`, which overflow float16 above 256 and 40.3 rad (#4965). From 41 rad the `[omega]_x^2` coefficient was
+0, so the float16 `Se3.exp(v).t` was off by 0.18 and `So3.right_jacobian` and `So3.left_jacobian` by 0.79 up to 250
+rad; they are now within about 2 float16 ulp there. The gradient of the coefficients, `nan` from 256 rad, is finite.
+Above the series switch the float32 and float64 coefficients move by at most about 2 ulp; below it they are unchanged.
diff --git a/kornia/geometry/liegroup/so3.py b/kornia/geometry/liegroup/so3.py
index a6985952e..f030c56bf 100644
--- a/kornia/geometry/liegroup/so3.py
+++ b/kornia/geometry/liegroup/so3.py
@@ -42,7 +42,8 @@ def _so3_small_angle_coefficients(theta: torch.Tensor) -> tuple[torch.Tensor, to
relative accuracy near it: all three evaluate to exactly 0 in float32 for :math:`\theta \le 10^{-4}` and in
float64 for :math:`\theta \le 10^{-8}`. Below ``theta_small`` the Taylor series through :math:`\theta^{10}`
is used instead. At the switch point the series is accurate to a fraction of an ulp and the closed forms to a
- few hundred ulps, so the two branches agree there.
+ few hundred ulps, so the two branches agree there. The closed forms divide by :math:`\theta` one factor at a
+ time: :math:`\theta^3` overflows float16 above 40.3 rad and :math:`\theta^2` above 256 rad.
Args:
theta: rotation angles of any shape, non-negative.
@@ -63,11 +64,10 @@ def _so3_small_angle_coefficients(theta: torch.Tensor) -> tuple[torch.Tensor, to
c_series = 1 / 12 + t2 * (
1 / 720 + t2 * (1 / 30240 + t2 * (1 / 1209600 + t2 * (1 / 47900160 + t2 * (691 / 1307674368000))))
)
- safe_sq = safe_theta * safe_theta
half = 0.5 * safe_theta
- a = torch.where(small, a_series, (1 - torch.cos(safe_theta)) / safe_sq)
- b = torch.where(small, b_series, (safe_theta - torch.sin(safe_theta)) / (safe_sq * safe_theta))
- c = torch.where(small, c_series, (1 - half * torch.cos(half) / torch.sin(half)) / safe_sq)
+ a = torch.where(small, a_series, (1 - torch.cos(safe_theta)) / safe_theta / safe_theta)
+ b = torch.where(small, b_series, (1 - torch.sin(safe_theta) / safe_theta) / safe_theta / safe_theta)
+ c = torch.where(small, c_series, (1 - half * torch.cos(half) / torch.sin(half)) / safe_theta / safe_theta)
return a, b, c
diff --git a/tests/geometry/liegroup/test_se3.py b/tests/geometry/liegroup/test_se3.py
index 756214d28..d5455b926 100644
--- a/tests/geometry/liegroup/test_se3.py
+++ b/tests/geometry/liegroup/test_se3.py
@@ -81,6 +81,17 @@ class TestSe3(BaseTester):
Se3(So3(Quaternion(data[:, :4])), data[:, 4:]).log().sum().backward()
assert bool(torch.isfinite(data.grad).all()), data.grad
+ def test_exp_matches_float64_above_40_rad_4965(self, device, dtype):
+ # #4965: _so3_small_angle_coefficients divided by theta**3, which overflows float16 above 40.3 rad, so the
+ # [omega]_x^2 term of V dropped out and the float16 exp(v).t was off by 0.18 from 41 rad. The reference
+ # is the float64 path on the CPU, run on the same rounded input.
+ eps = torch.finfo(dtype).eps
+ axis = torch.tensor([0.48, 0.6, 0.64], dtype=torch.float64)
+ for theta in (41.0, 60.0):
+ v = torch.cat((torch.ones(3, dtype=torch.float64), theta * axis)).to(device=device, dtype=dtype)
+ t_ref = Se3.exp(v.cpu().double()).t.to(device=device, dtype=dtype)
+ self.assert_close(Se3.exp(v).t, t_ref, rtol=8 * eps, atol=8 * eps)
+
def test_gradient_at_the_identity_couples_rotation_and_translation_4953(self, device, dtype):
# #4953: exp fell back to t = upsilon and log to upsilon = t at omega = 0. The values were
# right, V(0) = I, but the fallback did not depend on omega, so autograd returned
diff --git a/tests/geometry/liegroup/test_so3.py b/tests/geometry/liegroup/test_so3.py
index 187fc12a6..26e8d2f05 100644
--- a/tests/geometry/liegroup/test_so3.py
+++ b/tests/geometry/liegroup/test_so3.py
@@ -152,16 +152,42 @@ class TestSo3(BaseTester):
# #4965: the series were evaluated on every angle and discarded above the switch by torch.where, which
# still differentiates them. In float16 their Horner terms overflow from about 50 rad, so 0 * inf = nan
# reached the gradient of the coefficients and of both Jacobians although the values came from the closed
- # forms. 200 rad stays below the float16 overflow of theta**2 in the closed forms themselves.
- theta = torch.tensor([50.0, 100.0, 200.0], device=device, dtype=dtype, requires_grad=True)
+ # forms. The closed forms then divided by theta**2, which overflows float16 above 256 rad and made the
+ # gradient nan again; they now divide by theta twice, so 300 and 1000 rad are finite as well.
+ theta = torch.tensor([50.0, 100.0, 200.0, 300.0, 1000.0], device=device, dtype=dtype, requires_grad=True)
sum(c.sum() for c in _so3_small_angle_coefficients(theta)).backward()
assert bool(torch.isfinite(theta.grad).all()), theta.grad
+ # [omega]_x^2 itself overflows float16 above 256 rad, so the Jacobians are checked up to 200 rad.
axis = torch.tensor([0.48, 0.6, 0.64], device=device, dtype=dtype)
for jacobian in (So3.right_jacobian, So3.left_jacobian):
- v = (theta.detach()[:, None] * axis).requires_grad_(True)
+ v = (theta.detach()[:3, None] * axis).requires_grad_(True)
jacobian(v).sum().backward()
assert bool(torch.isfinite(v.grad).all()), (jacobian.__name__, v.grad)
+ def test_small_angle_coefficients_and_jacobians_above_40_rad_4965(self, device, dtype):
+ # #4965: the closed forms divided by theta**3 and theta**2, which overflow float16 above 40.3 and 256
+ # rad. From 41 rad b was 0, so the [omega]_x^2 term of both Jacobians dropped out: at 41 rad about
+ # [0.48, 0.6, 0.64] the float16 right_jacobian was off by 0.79. 50-digit references generated with
+ # mpmath (mp.dps = 50): a = (1 - cos t) / t**2, b = (t - sin t) / t**3, c = (1 - (t / 2) * cot(t / 2)) / t**2.
+ finfo = torch.finfo(dtype)
+ ref = torch.tensor(
+ [
+ (41.0, 0.0011822363340415387, 0.0005971855119456292, 0.001568257196741034),
+ (60.0, 0.00054233693900421, 0.00027918893806065843, 0.001578777379124938),
+ (288.0, 5.813614141459137e-06, 1.2092140495844536e-05, 0.0030921829471326203),
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ # a and b at 288 rad are subnormal in float16, hence the atol of 8 subnormal steps
+ a, b, c = _so3_small_angle_coefficients(ref[:, 0])
+ self.assert_close(torch.stack((a, b, c), -1), ref[:, 1:], rtol=8 * finfo.eps, atol=8 * finfo.eps * finfo.tiny)
+ # the Jacobians against the float64 path on the CPU, below the float16 overflow of [omega]_x^2 itself
+ v = ref[:2, :1] * torch.tensor([0.48, 0.6, 0.64], device=device, dtype=dtype)
+ for jacobian in (So3.right_jacobian, So3.left_jacobian):
+ expected = jacobian(v.cpu().double()).to(device=device, dtype=dtype)
+ self.assert_close(jacobian(v), expected, rtol=8 * finfo.eps, atol=8 * finfo.eps)
+
def test_convention_log_identity_gradient_is_the_on_manifold_limit_4404(self, device, dtype):
# The value the guard leaves in place, pinned rather than merely asserted finite: at the
# identity log evaluates 2 * vec / real, so d(omega_x)/dq_x = 2 -- exact in every dtype,