Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): make quaternion_exp_to_log and euler_from_quaternion scale-safe (#3953) (#5461)

Rachit Vikram Singh41129a4b74a7
diff --git a/changelog.d/5461.breaking.md b/changelog.d/5461.breaking.md
new file mode 100644
index 000000000..8b289087e
--- /dev/null
+++ b/changelog.d/5461.breaking.md
@@ -0,0 +1 @@
+`quaternion_exp_to_log` and `euler_from_quaternion` now normalize their input (#3953), matching `quaternion_to_rotation_matrix` and `quaternion_to_axis_angle`, so a rescaled quaternion gives the same result as the unit one. Unit inputs are unchanged to rounding; non-unit inputs that used to return a wrong value without error now return the value of the rotation they stand for. Before, `quaternion_exp_to_log([0.5, 0.5, 0, 0])` returned `[1.0471975511965976, 0, 0]`; it now returns `[0.7853981633974483, 0, 0]`. Before, a scalar part `w > 1` was clamped to `1`, so `quaternion_exp_to_log([2, 1, 0, 0])` returned the origin; it now returns `[0.4636476090008061, 0, 0]` (`[2, 0, 0, 0]` still returns the origin, the log of the identity). Before, `euler_from_quaternion` of `2 * q`, for the `q` of `(0.3, 0.7, 1.1)`, returned `[1.6560585860248003, 1.5707963267948966, 2.1048169977173687]`, a pitch of exactly `pi/2`; it now returns `(0.3, 0.7, 1.1)`. `quaternion_exp_to_log` computes the angle as `atan2(||v||, w)`, which needs no clamp and has a finite derivative at `w = +-1`.
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index b87e5c97c..2a0786564 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -797,11 +797,10 @@ def quaternion_to_rotation_matrix(quaternion: torch.Tensor) -> torch.Tensor:
matrix by the working dtype's rounding, as long as the norm of the
rescaled quaternion neither overflows nor falls below the
normalisation floor ``eps = 1e-12`` (see that function's warning)
- - :func:`~kornia.geometry.conversions.quaternion_to_axis_angle` is
- scale-safe too;
- :func:`~kornia.geometry.conversions.quaternion_exp_to_log` and
+ - :func:`~kornia.geometry.conversions.quaternion_to_axis_angle`,
+ :func:`~kornia.geometry.conversions.quaternion_exp_to_log`, and
:func:`~kornia.geometry.conversions.euler_from_quaternion` are
- **not** (see their warnings)
+ scale-safe too
- applied on the left to a column vector, ``+theta`` about ``+z`` maps
``x_hat`` to ``y_hat`` (right-hand rule)
- the output dtype follows the input at every shape
@@ -1083,7 +1082,8 @@ def quaternion_exp_to_log(quaternion: torch.Tensor, eps: float = 1.0e-8) -> torc
:func:`~kornia.geometry.conversions.quaternion_log_to_exp` takes
- for a **unit** ``q`` on the ``w >= 0`` half of the double cover the
result is ``quaternion_to_axis_angle(q) / 2``. On the ``w < 0`` half the two
- part company: this function applies ``acos(w)`` as given, while
+ part company: this function keeps the sign of ``w``, with the angle
+ ``atan2(||v||, w)`` in ``[0, pi]``, while
:func:`~kornia.geometry.conversions.quaternion_to_axis_angle`
collapses the double cover. For the ``q`` whose log is ``v``,
``quaternion_exp_to_log(-q)`` is ``-(pi - ||v||) * v / ||v||``
@@ -1096,15 +1096,8 @@ def quaternion_exp_to_log(quaternion: torch.Tensor, eps: float = 1.0e-8) -> torc
- ``float16``/``bfloat16`` are computed in ``float32``, so the identity
returns ``[0., 0., 0.]`` at every dtype, and the gradient is finite
at ``w = +-1``, the identity included
-
- .. warning::
- The input is **not** normalised, so a non-unit quaternion is silently
- given a wrong log: ``[0.5, 0.5, 0., 0.]`` returns
- ``[1.0471975511965976, 0., 0.]``, 33 % larger than the
- ``[0.7853981633974484, 0., 0.]`` of the same rotation normalised, and
- ``[2., 0., 0., 0.]`` returns the origin because ``w`` is clamped to
- ``1``. Tracked in
- `#3953 <https://github.com/kornia/kornia/issues/3953>`_.
+ - the map is scale-invariant: a non-unit quaternion produces the same
+ log as its normalisation
Args:
quaternion: a tensor containing a quaternion to be converted.
@@ -1133,25 +1126,18 @@ def quaternion_exp_to_log(quaternion: torch.Tensor, eps: float = 1.0e-8) -> torc
quaternion_scalar = work[..., 0:1]
quaternion_vector = work[..., 1:4]
- norm_q: torch.Tensor = torch.norm(quaternion_vector, p=2, dim=-1, keepdim=True).clamp(min=eps)
-
- # d(acos)/dw = -1/sqrt(1-w^2) is unbounded at w = +-1, and torch returns exactly -inf there
- # on every supported version -- that has not changed. At w = +-1 the vector part need not be
- # zero (a non-unit quaternion), so this is not always multiplied away -- w = 1 at the identity
- # quaternion is the common case where it is, and 0 * inf = nan there, killing every gradient
- # through this function from its most ordinary input. torch 2.14 masks that: its clamp
- # backward returns 0 at the closed boundary instead of the pass-through 1.0 of earlier
- # versions, killing the -inf before it can multiply anything. That is clamp's behaviour
- # changing, not acos's, and nothing here may depend on it. Route the boundary through .acos()
- # on a *detached* copy for the value (identical to the unguarded call: acos is continuous at
- # +-1, only its derivative diverges) and through .acos() on a substituted safe argument for
- # the gradient, so autograd never differentiates acos at +-1 at all.
- w_clamped = torch.clamp(quaternion_scalar, min=-1.0, max=1.0)
- at_boundary = w_clamped.abs() >= 1.0
- safe_w = torch.where(at_boundary, torch.zeros_like(w_clamped), w_clamped)
- acos_w = torch.where(at_boundary, w_clamped.detach().acos(), safe_w.acos())
-
- quaternion_log: torch.Tensor = (quaternion_vector * acos_w / norm_q).to(orig_dtype)
+ norm_v: torch.Tensor = torch.norm(quaternion_vector, p=2, dim=-1, keepdim=True)
+ norm_q: torch.Tensor = norm_v.clamp(min=eps)
+
+ # atan2(||v||, w) is scale-invariant, needs no clamp, and has a bounded derivative at w = +-1.
+ # Its derivative is 0/0 at the zero quaternion, where torch 2.5.1 returns nan and 2.14 returns 0:
+ # differentiate there at w = 1 instead. The value atan2(0, 1) = 0 multiplies a zero vector part,
+ # so the returned log is the same origin either way.
+ at_origin = (norm_v == 0) & (quaternion_scalar == 0)
+ safe_scalar = torch.where(at_origin, torch.ones_like(quaternion_scalar), quaternion_scalar)
+ theta = torch.atan2(norm_v, safe_scalar)
+
+ quaternion_log: torch.Tensor = (quaternion_vector * theta / norm_q).to(orig_dtype)
return quaternion_log
@@ -1277,6 +1263,9 @@ def euler_from_quaternion(
``2.2e-16``, while ``(0.2, 2.5, 0.3)`` comes back as
``(-2.9416, 0.6416, -2.8416)`` β€” a different triple for the same
rotation, to ``1.1e-16``
+ - the input is normalised with
+ :func:`~kornia.geometry.conversions.normalize_quaternion` first, so a
+ rescaled quaternion returns the same triple
.. warning::
At ``pitch = Β±pi/2`` the returned triple usually does not represent the
@@ -1300,15 +1289,6 @@ def euler_from_quaternion(
``roll`` and ``yaw`` are still not returned individually. Tracked in
`#3950 <https://github.com/kornia/kornia/issues/3950>`_.
- .. warning::
- The input is **not** normalised, so a non-unit quaternion silently gives
- a wrong triple: for the ``q`` of ``(0.3, 0.7, 1.1)``, passing ``2 * q``
- returns ``[1.6560585860248003, 1.5707963267948966,
- 2.1048169977173687]``. The middle value is exactly ``pi/2`` β€” the
- over-scaled input saturates the ``asin`` and is reported as
- gimbal-locked. Tracked in
- `#3953 <https://github.com/kornia/kornia/issues/3953>`_.
-
.. note::
``pitch``'s gradient is finite at gimbal lock, including exactly at ``pitch = +-pi/2``.
@@ -1326,6 +1306,10 @@ def euler_from_quaternion(
KORNIA_CHECK(x.shape == y.shape)
KORNIA_CHECK(y.shape == z.shape)
+ quaternion = torch.stack((w, x, y, z), dim=-1)
+ quaternion = normalize_quaternion(quaternion)
+ w, x, y, z = quaternion.unbind(-1)
+
yy = y * y
sinr_cosp = 2.0 * (w * x + y * z)
@@ -1337,8 +1321,7 @@ def euler_from_quaternion(
# d(asin)/dx = 1/sqrt(1-x^2) is unbounded at x = +-1 (gimbal lock), returning inf there on
# every supported torch version; the clamp above bounds the value only, and passes the
# gradient through on torch < 2.14 (2.14 zeroes it at the boundary, masking the defect).
- # Guard the gradient the same way quaternion_exp_to_log guards its own acos boundary
- # (delivered in kornia#4228) -- differentiate asin on a substituted safe argument, but take the
+ # Guard the gradient (kornia#4228) -- differentiate asin on a substituted safe argument, but take the
# value from a detached copy at the real (possibly +-1) argument, so the returned pitch is
# unchanged and only the gradient is finite.
at_boundary = sinp.abs() >= 1.0
diff --git a/kornia/geometry/quaternion.py b/kornia/geometry/quaternion.py
index 029dfc35f..a64d28e92 100644
--- a/kornia/geometry/quaternion.py
+++ b/kornia/geometry/quaternion.py
@@ -66,17 +66,17 @@ class Quaternion(nn.Module):
A float or tensor operand of ``+``, ``-``, ``*`` or ``/`` is the real quaternion ``[s, 0, 0, 0]``, and a
tensor of the shape of ``q.w`` holds one such scalar per quaternion of the batch.
- Nothing normalises the stored data or the results of ``*``, ``**`` and ``inv()``. ``matrix()``,
- ``to_axis_angle()``, ``polar_angle`` and ``slerp`` read only the direction of ``q``: rescaling ``q`` by a
- positive factor changes their result by roundoff only, as long as its squared components stay within the
- range of the dtype and its norm stays above ``1e-12``.
+ ``to_axis_angle()``, ``to_euler()``, ``polar_angle`` and ``slerp`` read only the direction of ``q``:
+ rescaling ``q`` by a positive factor changes their result by roundoff only, as long as its squared
+ components stay within the range of the dtype and its norm stays above ``1e-12``.
- At construction, plain tensor data is registered as a persistent buffer; an explicit ``nn.Parameter``
remains a parameter. Both are saved and restored by ``state_dict()`` and ``load_state_dict()`` and follow
an enclosing module's device and dtype conversions. Construction preserves the input tensor and its
autograd history. ``Quaternion.to()`` returns a new quaternion; enclosing module conversions update the
existing module's state.
- - Known defects: ``to_euler()`` returns a triple that does not reproduce the rotation for a non-unit ``q``
- (`#3953 <https://github.com/kornia/kornia/issues/3953>`_) and for most rotations at a pitch of
- :math:`\pm\pi/2` (`#3950 <https://github.com/kornia/kornia/issues/3950>`_); below a norm of ``1e-12``,
+ - Known defects: ``to_euler()`` returns a triple that does not reproduce the rotation for most rotations
+ at a pitch of :math:`\pm\pi/2` (`#3950 <https://github.com/kornia/kornia/issues/3950>`_);
+ below a norm of ``1e-12``,
``matrix()`` and ``slerp`` give wrong results, and the zero quaternion's ``matrix()`` is the identity
(`#3952 <https://github.com/kornia/kornia/issues/3952>`_).
diff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txt
index d09bdd633..a278493fd 100644
--- a/testing/half_precision_xfails/cpu_float16.txt
+++ b/testing/half_precision_xfails/cpu_float16.txt
@@ -398,15 +398,9 @@ call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfR
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16--45.0-x]
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16--45.0-y]
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16--45.0-z]
-call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16--90.0-x]
-call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16--90.0-y]
-call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16--90.0-z]
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16-45-x]
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16-45-y]
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16-45-z]
-call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16-90-x]
-call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16-90-y]
-call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_log_quaternion[cpu-float16-90-z]
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_quaternion[cpu-float16--179.9-x]
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_quaternion[cpu-float16--179.9-y]
call builtins.AssertionError tests/integration/test_conversions.py::TestAngleOfRotations::test_quaternion[cpu-float16--179.9-z]
diff --git a/tests/geometry/test_conversions.py b/tests/geometry/test_conversions.py
index 90a496a2f..2c42cc7ed 100644
--- a/tests/geometry/test_conversions.py
+++ b/tests/geometry/test_conversions.py
@@ -1717,16 +1717,25 @@ class TestQuaternionExpToLog(BaseTester):
fn(non_unit).sum().backward()
assert bool(torch.isfinite(non_unit.grad).all()), non_unit.grad
- # the guard changes no forward value. At the boundary the identity's log is all zeros;
- # off it the other branch of the torch.where runs and must equal the unguarded expression
- # exactly -- that is the branch proving the guard is inert away from w = +-1, which an
- # on-boundary input cannot exercise.
+ # the zero quaternion: atan2's derivative is 0/0 at (0, 0) -- nan on torch 2.5.1, 0 on
+ # 2.14 -- so this cell, too, discriminates on the 2.5.1 leg (kornia#3953 switched to atan2).
+ origin = torch.zeros(4, device=device, dtype=dtype, requires_grad=True)
+ origin_log = fn(origin)
+ origin_log.sum().backward()
+ self.assert_close(origin_log.detach(), torch.zeros(3, device=device, dtype=dtype))
+ assert bool(torch.isfinite(origin.grad).all()), origin.grad
+
+ # atan2(||v||, w) changes no forward value on unit input: the identity's log is all zeros,
+ # and off the boundary it equals the acos(w) expression the function used before
+ # kornia#3953.
expected_identity = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
self.assert_close(fn(identity.detach()), expected_identity)
- near_boundary = torch.tensor((1.0 - eps, 0.1, 0.0, 0.0), device=device, dtype=dtype)
+ near_boundary = kornia.geometry.conversions.normalize_quaternion(
+ torch.tensor((1.0 - eps, 0.1, 0.0, 0.0), device=device, dtype=dtype)
+ )
work_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype
- work = near_boundary.to(work_dtype)
+ work = kornia.geometry.conversions.normalize_quaternion(near_boundary.to(work_dtype))
unguarded = (
work[1:4]
* work[0:1].clamp(min=-1.0, max=1.0).acos()
@@ -1828,23 +1837,11 @@ class TestQuaternionExpToLog(BaseTester):
self.assert_close(out, torch.zeros(3, device=device, dtype=torch.float64), atol=1e-7, rtol=0.0)
- @pytest.mark.xfail(
- raises=AssertionError,
- reason="quaternion_exp_to_log does not normalise its input, so a non-unit quaternion gives "
- "a silently wrong log β€” kornia#3953",
- strict=True,
- )
def test_convention_exp_to_log_normalizes_its_input_3953(self, device, dtype):
# Intended behavior: the log of a quaternion depends only on the rotation it represents, so
# a rescaled quaternion gives the same answer -- which is what its scale-safe siblings do
# (quaternion_to_rotation_matrix normalises internally, quaternion_to_axis_angle is
- # homogeneous by construction; both are pinned above). quaternion_exp_to_log does neither:
- # it feeds the raw scalar part straight into acos, so q = (0.5, 0.5, 0, 0) -- the 90-degree
- # rotation about x, scaled by 1/sqrt(2) -- returns 1.0471975511965976 instead of
- # acos(1/sqrt(2)) = 0.7853981633974484, i.e. 33% too large, with no error and no warning.
- # Marked xfail(strict=True) so fixing #3953 makes this XPASS and forces the mark out.
- # Companion wart: test_wart_exp_to_log_ignores_the_quaternion_norm_3953; the
- # euler_from_quaternion half of the same issue is pinned in TestEulerFromQuaternion.
+ # homogeneous by construction; both are pinned above).
quaternion = torch.tensor([0.5, 0.5, 0.0, 0.0], device=device, dtype=dtype)
out = kornia.geometry.conversions.quaternion_exp_to_log(quaternion, eps=1e-8)
@@ -1855,23 +1852,13 @@ class TestQuaternionExpToLog(BaseTester):
msg=_issue_msg("kornia#3953: quaternion_exp_to_log did not normalise its input"),
)
- def test_wart_exp_to_log_ignores_the_quaternion_norm_3953(self, device, dtype):
- # Wart pin for kornia#3953, companion to the strict xfail above: assert the CURRENT
- # non-unit-input outputs. Two cells that discriminate the two plausible fix shapes:
- # (1) q = (0.5, 0.5, 0, 0) returns 1.0471975511965976 -- flips under a "normalise the
- # input" fix and under a "raise on non-unit input" fix alike;
- # (2) q = (2, 0, 0, 0) returns the origin because the scalar part is clamped into
- # [-1, 1] before the acos -- this does NOT flip under a normalising fix (the
- # normalised input is the identity, whose log is the origin), so it is the cell that
- # tells a normalising fix apart from a validating one.
- # If either fails, #3953 was (partly) fixed -- flip/remove the strict xfail above. NOT a
- # contract that non-unit input must keep these values. eps is passed explicitly so the
- # literals do not silently track a later change of the default.
- # Snippet used to generate expected (torch + stdlib, executed on cpu float64):
- # quaternion_exp_to_log(torch.tensor([0.5, 0.5, 0., 0.], dtype=torch.float64), eps=1e-8)
- # -> [1.0471975511965976, 0.0, 0.0]; math.acos(1 / math.sqrt(2)) -> 0.7853981633974484
- # quaternion_exp_to_log(torch.tensor([2., 0., 0., 0.], dtype=torch.float64), eps=1e-8)
- # -> [0.0, 0.0, 0.0]
+ def test_convention_exp_to_log_of_rescaled_quaternions_3953(self, device, dtype):
+ # Convention pin for kornia#3953: a non-unit input gives the log of its normalisation.
+ # Two cells:
+ # (1) q = (0.5, 0.5, 0, 0) returns acos(1/sqrt(2)) = 0.7853981633974484;
+ # (2) q = (2, 0, 0, 0) returns the origin under normalisation (the normalised input
+ # is the identity, whose log is the origin).
+ # eps is passed explicitly so the literals do not silently track a later change of the default.
exp_to_log = kornia.geometry.conversions.quaternion_exp_to_log
scaled_down = exp_to_log(torch.tensor([0.5, 0.5, 0.0, 0.0], device=device, dtype=dtype), eps=1e-8)
@@ -1879,13 +1866,13 @@ class TestQuaternionExpToLog(BaseTester):
assert_close(
scaled_down,
- torch.tensor([1.0471975511965976, 0.0, 0.0], device=device, dtype=dtype),
- msg=_issue_msg("kornia#3953: quaternion_exp_to_log no longer takes the raw scalar part at face value"),
+ torch.tensor([0.7853981633974484, 0.0, 0.0], device=device, dtype=dtype),
+ msg=_issue_msg("kornia#3953: quaternion_exp_to_log normalises its input"),
)
assert_close(
scaled_up,
torch.zeros(3, device=device, dtype=dtype),
- msg=_issue_msg("kornia#3953: the scalar-part clamp no longer sends an over-scaled quaternion to zero"),
+ msg=_issue_msg("kornia#3953: quaternion_exp_to_log of over-scaled identity is the origin"),
)
def test_convention_exp_to_log_of_the_identity_is_the_origin_in_float16_3966(self, device):
@@ -5341,10 +5328,11 @@ class TestEulerFromQuaternion(BaseTester):
# gimbal lock (pitch = +-pi/2); the guard mirrors quaternion_exp_to_log's own acos
# boundary fix. As there, asin's boundary derivative is inf on every supported torch
# version and it is clamp's backward that differs, so the torch 2.5.1 leg is the one that
- # fails on base. w=1, x=0, y=0.5, z=0 gives sinp = 2*(w*y - z*x) = 1.0 exactly.
- w = torch.tensor(1.0, device=device, dtype=dtype, requires_grad=True)
+ # fails on base. w=y=1/sqrt(2), x=z=0 gives sinp = 2*(w*y - z*x) = 1.0 exactly.
+ val = torch.tensor(0.5**0.5, device=device, dtype=dtype)
+ w = val.clone().detach().requires_grad_(True)
x = torch.tensor(0.0, device=device, dtype=dtype, requires_grad=True)
- y = torch.tensor(0.5, device=device, dtype=dtype, requires_grad=True)
+ y = val.clone().detach().requires_grad_(True)
z = torch.tensor(0.0, device=device, dtype=dtype, requires_grad=True)
_, pitch, _ = euler_from_quaternion(w, x, y, z)
@@ -5586,24 +5574,11 @@ class TestEulerFromQuaternion(BaseTester):
f"rotation to {error} -- the gimbal-lock defect looks fixed"
)
- @pytest.mark.xfail(
- raises=AssertionError,
- reason="euler_from_quaternion does not normalise its input, so a non-unit quaternion gives "
- "a silently wrong triple β€” kornia#3953",
- strict=True,
- )
def test_convention_euler_from_quaternion_normalizes_its_input_3953(self, device, dtype):
# Intended behavior: the euler angles of a quaternion depend only on the rotation it
# represents, so rescaling the quaternion must not change them -- which is what the
# scale-safe siblings do (quaternion_to_rotation_matrix normalises internally and returns a
# bit-identical matrix for 2q; quaternion_to_axis_angle is homogeneous by construction).
- # euler_from_quaternion does not normalise and does not check: feeding 2q returns
- # [1.6560585860248003, 1.5707963267948966, 2.1048169977173687] instead of the (0.3, 0.7,
- # 1.1) the unit quaternion gives -- and note the middle component, which is exactly pi/2:
- # the unnormalised argument saturates the asin, so a merely-scaled input is reported as
- # gimbal-locked. Marked xfail(strict=True) so fixing #3953 makes this XPASS and forces the
- # mark out. Companion wart: test_wart_euler_from_quaternion_ignores_the_norm_3953; the
- # quaternion_exp_to_log half of the same issue is pinned in TestQuaternionExpToLog.
roll = torch.tensor(0.3, device=device, dtype=dtype)
pitch = torch.tensor(0.7, device=device, dtype=dtype)
yaw = torch.tensor(1.1, device=device, dtype=dtype)
@@ -5617,25 +5592,9 @@ class TestEulerFromQuaternion(BaseTester):
msg=_issue_msg("kornia#3953: euler_from_quaternion did not normalise its input"),
)
- def test_wart_euler_from_quaternion_ignores_the_norm_3953(self, device, dtype):
- # Wart pin for kornia#3953, companion to the strict xfail above: assert the CURRENT triple
- # for a scaled-up quaternion. This is a separate cell from the quaternion_exp_to_log cells
- # in TestQuaternionExpToLog because the two functions are independent code paths and a fix
- # to one leaves the other broken, which would leave the other strict xfail silently XFAIL.
- # If it fails, the euler half of #3953 was fixed -- flip/remove the strict xfail above.
- # NOT a contract that a scaled quaternion must keep producing this triple.
- # Snippet used to generate expected (torch only, executed on cpu float64):
- # t = lambda x: torch.tensor(x, dtype=torch.float64)
- # q = quaternion_from_euler(t(0.3), t(0.7), t(1.1))
- # -> [0.8186292656554958, -0.057539988180335386, 0.3624200943552256, 0.44179967222724353]
- # [x.item() for x in euler_from_quaternion(*q)]
- # -> [0.2999999999999999, 0.6999999999999998, 1.0999999999999999] (the unit input)
- # [x.item() for x in euler_from_quaternion(*[2 * c for c in q])]
- # -> [1.6560585860248003, 1.5707963267948966, 2.1048169977173687]
- # (float32: [1.656058430671692, 1.5707963705062866, 2.1048169136047363];
- # float16: [1.6572265625, 1.5703125, 2.10546875];
- # bfloat16: [1.6484375, 1.5703125, 2.109375] -- all within the dtype's default tolerance
- # of the float64 literals below)
+ def test_convention_euler_from_quaternion_of_a_rescaled_quaternion_3953(self, device, dtype):
+ # Convention pin for kornia#3953: the triple for 2q is the triple of the unit q; before the
+ # fix it was [1.6560585860248003, 1.5707963267948966, 2.1048169977173687].
quaternion = quaternion_from_euler(
torch.tensor(0.3, device=device, dtype=dtype),
torch.tensor(0.7, device=device, dtype=dtype),
@@ -5646,8 +5605,8 @@ class TestEulerFromQuaternion(BaseTester):
assert_close(
out,
- torch.tensor([1.6560585860248003, 1.5707963267948966, 2.1048169977173687], device=device, dtype=dtype),
- msg=_issue_msg("kornia#3953: euler_from_quaternion no longer ignores the quaternion norm"),
+ torch.tensor([0.3, 0.7, 1.1], device=device, dtype=dtype),
+ msg=_issue_msg("kornia#3953: euler_from_quaternion normalises its input"),
)