From c93cae27c29696aff9f932622282ff0ae54fbb80 Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:21:38 +0000 Subject: [PATCH 1/3] Keep axis-angle conversion gradients finite at identity Fixes #9250. Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> --- CONTRIBUTORS.md | 1 + .../axis-angle-identity-gradients.fixed.rst | 1 + source/isaaclab/isaaclab/utils/math.py | 7 +++++-- source/isaaclab/test/utils/test_math.py | 12 ++++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 source/isaaclab/changelog.d/axis-angle-identity-gradients.fixed.rst diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 63c581fd6dac..06a9de465bdb 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -224,6 +224,7 @@ Guidelines for modifications: * Stefan Van de Mosselaer * Stephan Pleines * Sujal +* Sylvester Kaczmarek * Tiffany Chen * Trushant Adeshara * Tsz Ki GAO diff --git a/source/isaaclab/changelog.d/axis-angle-identity-gradients.fixed.rst b/source/isaaclab/changelog.d/axis-angle-identity-gradients.fixed.rst new file mode 100644 index 000000000000..b91ab338e0d2 --- /dev/null +++ b/source/isaaclab/changelog.d/axis-angle-identity-gradients.fixed.rst @@ -0,0 +1 @@ +* Fixed nonfinite gradients when converting identity quaternions to axis-angle rotation vectors. diff --git a/source/isaaclab/isaaclab/utils/math.py b/source/isaaclab/isaaclab/utils/math.py index 3248a027656d..fdbc52cead1d 100644 --- a/source/isaaclab/isaaclab/utils/math.py +++ b/source/isaaclab/isaaclab/utils/math.py @@ -493,9 +493,12 @@ def axis_angle_from_quat(quat: torch.Tensor, eps: float = 1.0e-6) -> torch.Tenso mag = torch.linalg.norm(quat[..., :3], dim=-1) half_angle = torch.atan2(mag, quat[..., 3]) angle = 2.0 * half_angle - # check whether to apply Taylor approximation + # Avoid dividing by zero even in the unselected branch of torch.where: + # autograd can otherwise propagate NaN gradients at the identity. + use_exact = angle.abs() > eps + safe_angle = torch.where(use_exact, angle, torch.ones_like(angle)) sin_half_angles_over_angles = torch.where( - angle.abs() > eps, torch.sin(half_angle) / angle, 0.5 - angle * angle / 48 + use_exact, torch.sin(half_angle) / safe_angle, 0.5 - angle * angle / 48 ) return quat[..., :3] / sin_half_angles_over_angles.unsqueeze(-1) diff --git a/source/isaaclab/test/utils/test_math.py b/source/isaaclab/test/utils/test_math.py index 60192840632e..ad4fe854d3c3 100644 --- a/source/isaaclab/test/utils/test_math.py +++ b/source/isaaclab/test/utils/test_math.py @@ -149,6 +149,18 @@ def test_is_identity_pose(device): assert math_utils.is_identity_pose(identity_pos, identity_rot) is False +def test_axis_angle_from_quat_identity_gradients() -> None: + """Both quaternion signs of the identity rotation have finite local derivatives.""" + quats = torch.tensor([[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, -1.0]], dtype=torch.float64) + quats.requires_grad_() + rotation = math_utils.axis_angle_from_quat(quats) + torch.testing.assert_close(rotation, torch.zeros_like(rotation)) + # Near identity, the rotation vector is 2 * xyz after canonicalizing the quaternion sign. + (grad,) = torch.autograd.grad(rotation.sum(), quats) + expected = torch.tensor([[2.0, 2.0, 2.0, 0.0], [-2.0, -2.0, -2.0, 0.0]], dtype=quats.dtype) + torch.testing.assert_close(grad, expected) + + @pytest.mark.parametrize("device", test_devices()) def test_axis_angle_from_quat_approximation(device): """Test the Taylor approximation from axis_angle_from_quat method. From 76e3a20183b03936c15c41cac3fe9dd948ff43a3 Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:28:33 +0000 Subject: [PATCH 2/3] Use scalar safe denominator and mark identity regression kitless --- source/isaaclab/isaaclab/utils/math.py | 2 +- source/isaaclab/test/utils/test_math.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/source/isaaclab/isaaclab/utils/math.py b/source/isaaclab/isaaclab/utils/math.py index fdbc52cead1d..b54156ccbe6c 100644 --- a/source/isaaclab/isaaclab/utils/math.py +++ b/source/isaaclab/isaaclab/utils/math.py @@ -496,7 +496,7 @@ def axis_angle_from_quat(quat: torch.Tensor, eps: float = 1.0e-6) -> torch.Tenso # Avoid dividing by zero even in the unselected branch of torch.where: # autograd can otherwise propagate NaN gradients at the identity. use_exact = angle.abs() > eps - safe_angle = torch.where(use_exact, angle, torch.ones_like(angle)) + safe_angle = torch.where(use_exact, angle, 1.0) sin_half_angles_over_angles = torch.where( use_exact, torch.sin(half_angle) / safe_angle, 0.5 - angle * angle / 48 ) diff --git a/source/isaaclab/test/utils/test_math.py b/source/isaaclab/test/utils/test_math.py index ad4fe854d3c3..37a6b828c138 100644 --- a/source/isaaclab/test/utils/test_math.py +++ b/source/isaaclab/test/utils/test_math.py @@ -149,6 +149,7 @@ def test_is_identity_pose(device): assert math_utils.is_identity_pose(identity_pos, identity_rot) is False +@pytest.mark.kitless def test_axis_angle_from_quat_identity_gradients() -> None: """Both quaternion signs of the identity rotation have finite local derivatives.""" quats = torch.tensor([[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, -1.0]], dtype=torch.float64) From 000dac1e2157a53ee349b8620095073fe5622396 Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Wed, 2 Dec 2026 23:01:47 +0000 Subject: [PATCH 3/3] Format the axis-angle denominator guard Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> --- source/isaaclab/isaaclab/utils/math.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/isaaclab/isaaclab/utils/math.py b/source/isaaclab/isaaclab/utils/math.py index b54156ccbe6c..60bb0c41b4a5 100644 --- a/source/isaaclab/isaaclab/utils/math.py +++ b/source/isaaclab/isaaclab/utils/math.py @@ -497,9 +497,7 @@ def axis_angle_from_quat(quat: torch.Tensor, eps: float = 1.0e-6) -> torch.Tenso # autograd can otherwise propagate NaN gradients at the identity. use_exact = angle.abs() > eps safe_angle = torch.where(use_exact, angle, 1.0) - sin_half_angles_over_angles = torch.where( - use_exact, torch.sin(half_angle) / safe_angle, 0.5 - angle * angle / 48 - ) + sin_half_angles_over_angles = torch.where(use_exact, torch.sin(half_angle) / safe_angle, 0.5 - angle * angle / 48) return quat[..., :3] / sin_half_angles_over_angles.unsqueeze(-1)