Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): allocate axis_angle_to_quaternion output from the computed values (#3978)

Chandan Pd2966e4b44ff
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 42d0ae764..4780131c8 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -1208,6 +1208,10 @@ def axis_angle_to_quaternion(axis_angle: torch.Tensor) -> torch.Tensor:
- ``angle_axis_to_quaternion`` is the deprecated alias of this function
since 0.7.0; see the alias warning on
:func:`~kornia.geometry.conversions.axis_angle_to_rotation_matrix`
+ - an integral or boolean input is **promoted**, not rejected: the
+ ``sqrt`` on the way in already produces a floating tensor, and the
+ output is allocated from that rather than from the input, so the
+ result comes back at the ambient default floating dtype
.. warning::
The gradient at the zero rotation is ``nan``:
@@ -1215,14 +1219,6 @@ def axis_angle_to_quaternion(axis_angle: torch.Tensor) -> torch.Tensor:
``[nan, nan, nan]`` in ``.grad``, from an unclamped ``sqrt(0)``.
Tracked in `#3949 <https://github.com/kornia/kornia/issues/3949>`_.
- .. warning::
- An integer tensor returns an all-zero integer tensor instead of a
- quaternion: ``axis_angle_to_quaternion(torch.tensor([1, 0, 0]))`` is
- ``tensor([0, 0, 0, 0])`` of dtype ``int64``, against the ``float32``
- answer ``[0.8776, 0.4794, 0., 0.]``, because the output buffer is
- allocated with the input dtype. Tracked in
- `#3948 <https://github.com/kornia/kornia/issues/3948>`_.
-
Args:
axis_angle: tensor with axis angle in radians.
@@ -1262,9 +1258,10 @@ def axis_angle_to_quaternion(axis_angle: torch.Tensor) -> torch.Tensor:
k: torch.Tensor = torch.where(mask, k_pos, k_neg)
w: torch.Tensor = torch.where(mask, torch.cos(half_theta), ones)
- quaternion: torch.Tensor = torch.zeros(
- size=(*axis_angle.shape[:-1], 4), dtype=axis_angle.dtype, device=axis_angle.device
- )
+ # the output dtype comes from the computed values, not from the input: an integer input is
+ # promoted to floating point by the sqrt above, and writing that back into an integer buffer
+ # would truncate every component to zero.
+ quaternion: torch.Tensor = torch.zeros(size=(*axis_angle.shape[:-1], 4), dtype=w.dtype, device=axis_angle.device)
quaternion[..., 1:2] = a0 * k
quaternion[..., 2:3] = a1 * k
quaternion[..., 3:4] = a2 * k
diff --git a/tests/geometry/test_conversions.py b/tests/geometry/test_conversions.py
index 7c669167c..bccb19e83 100644
--- a/tests/geometry/test_conversions.py
+++ b/tests/geometry/test_conversions.py
@@ -672,6 +672,46 @@ class TestAngleAxisToQuaternion(BaseTester):
quaternion = kornia.geometry.conversions.axis_angle_to_quaternion(axis_angle)
self.assert_close(quaternion, expected, atol=atol, rtol=rtol)
+ @pytest.mark.parametrize("input_dtype", (torch.int16, torch.int32, torch.int64, torch.uint8))
+ def test_convention_integer_input_is_promoted_to_float_3948(self, input_dtype, device):
+ # Convention, and the answer kornia#3948 settled: an integer axis-angle is PROMOTED, not
+ # rejected. The output buffer used to be allocated with dtype=axis_angle.dtype, so an
+ # integer input got an integer buffer and every component was truncated on the way in --
+ # the function returned tensor([0, 0, 0, 0]), not merely a wrong quaternion but a zero-norm
+ # one, with no error and no warning. The buffer now takes its dtype from the computed
+ # values (sqrt already promotes them), so the result is the float quaternion below.
+ # The wart this replaces said the intended behavior was undecided -- promote, or raise a
+ # TypeError the way a dtype guard would. Promotion is the choice; the sibling wart
+ # TestNormalTransformPixel.test_wart_integer_dtype_truncates_the_scale_to_zero_3959 pins
+ # the same family of defect and is still open, so this pin is also the precedent it should
+ # follow.
+ # Four cells because the promotion happens through torch's own type rules rather than an
+ # explicit .float(): a signed/unsigned or narrow/wide difference would show up here.
+ # The dtype fixture is dropped because the claim is about the input dtype itself.
+ # Snippet used to generate expected (torch + stdlib, executed on cpu):
+ # axis_angle_to_quaternion(torch.tensor([1., 0., 0.])) ->
+ # [0.8775825500488281, 0.4794255495071411, 0.0, 0.0] (float32 reference)
+ # math.cos(0.5), math.sin(0.5) -> (0.8775825618903728, 0.479425538604203)
+ axis_angle = torch.tensor((1, 0, 0), device=device, dtype=input_dtype)
+ quaternion = kornia.geometry.conversions.axis_angle_to_quaternion(axis_angle)
+ assert quaternion.is_floating_point()
+ expected = torch.tensor((np.cos(0.5), np.sin(0.5), 0.0, 0.0), device=device, dtype=quaternion.dtype)
+ self.assert_close(quaternion, expected, atol=1.0e-4, rtol=1.0e-4)
+
+ @pytest.mark.parametrize("input_dtype", (torch.float16, torch.bfloat16, torch.float32, torch.float64))
+ def test_convention_float_input_keeps_its_dtype_3948(self, input_dtype, device):
+ # The other side of the #3948 buffer change, and the regression it could have introduced:
+ # taking the buffer dtype from the computed values must NOT widen a float input. float16
+ # and bfloat16 are the cells that matter -- a fix written as `.float()` or as an explicit
+ # torch.float32 buffer would silently upcast them, which is exactly the half-precision
+ # surface kornia treats as its own. The tolerance is set for float16, the widest of the
+ # four; the float32/float64 values are pinned exactly by the numerical tests above.
+ axis_angle = torch.tensor((1.0, 0.0, 0.0), device=device, dtype=input_dtype)
+ quaternion = kornia.geometry.conversions.axis_angle_to_quaternion(axis_angle)
+ assert quaternion.dtype == input_dtype
+ 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_gradcheck(self, device):
dtype = torch.float64
eps = torch.finfo(dtype).eps
@@ -814,30 +854,6 @@ class TestAngleAxisToQuaternion(BaseTester):
)
torch.testing.assert_close(actual, expected, rtol=0, atol=0)
- def test_wart_integer_input_returns_the_zero_quaternion_3948(self, device):
- # Wart pin for kornia#3948: axis_angle_to_quaternion allocates its output buffer with
- # dtype=axis_angle.dtype, so an integer input gets an int64 buffer and every component is
- # truncated to 0. The function returns tensor([0, 0, 0, 0]) -- not merely a wrong
- # quaternion but a zero-norm one, with no error and no warning. If this fails, #3948 was
- # (partly) fixed -- remove this pin. NOT a contract that integer input must keep returning
- # zeros.
- # There is deliberately NO companion strict xfail here, unlike every other bug pinned in
- # this batch: the intended behavior is genuinely undecided. A fix could promote to float
- # and return the float reference in the snippet below, or it could raise TypeError the way
- # a dtype guard would; an assertion-shaped xfail can express only the first, so it would
- # stay silently XFAIL forever if the fix chose to raise. Same reasoning, and same shape, as
- # TestRadDegConversions.test_wart_integer_input_truncates_pi_to_3_3937.
- # Snippet used to generate expected (torch + stdlib, executed on cpu):
- # axis_angle_to_quaternion(torch.tensor([1, 0, 0])) -> tensor([0, 0, 0, 0]), int64
- # (torch.int32 and torch.int16 inputs give [0, 0, 0, 0] in their own dtype too)
- # axis_angle_to_quaternion(torch.tensor([1., 0., 0.])) ->
- # [0.8775825500488281, 0.4794255495071411, 0.0, 0.0] (float32 reference)
- # math.cos(0.5), math.sin(0.5) -> (0.8775825618903728, 0.479425538604203)
- out = kornia.geometry.conversions.axis_angle_to_quaternion(torch.tensor([1, 0, 0], device=device))
-
- assert out.dtype == torch.int64, "kornia#3948: integer input no longer keeps its integer output buffer"
- assert out.tolist() == [0, 0, 0, 0], "kornia#3948: integer input no longer truncates to the zero quaternion"
-
@pytest.mark.xfail(
raises=AssertionError,
reason="unguarded sqrt(0) in the axis-angle/quaternion pair makes the gradient NaN at the "
@@ -3969,11 +3985,14 @@ class TestNormalTransformPixel(BaseTester):
# runs, which CUDA would not implement). The 3-D cell keeps depth = 2 so that its z scale, 2/1 = 2,
# survives the truncation: a partial fix that only promotes float scales would still have
# to flip the two zeroed axes.
- # There is deliberately NO companion strict xfail, for the same reason as
- # test_wart_integer_input_returns_the_zero_quaternion_3948: the intended behavior is
- # undecided (promote to float, or raise), and an assertion-shaped xfail can express only
- # the first. If either cell fails, #3959 was (partly) fixed -- remove this pin. NOT a
- # contract that an integer dtype must keep returning a degenerate matrix.
+ # There is deliberately NO companion strict xfail: the intended behavior is undecided
+ # (promote to float, or raise), and an assertion-shaped xfail can express only the first.
+ # kornia#3948, the same defect in axis_angle_to_quaternion, was settled by PROMOTING --
+ # see TestAngleAxisToQuaternion.test_convention_integer_input_is_promoted_to_float_3948 --
+ # so that is the precedent a fix here should follow, but it is precedent and not a decision
+ # taken for this function, which is why the pin stays a wart rather than becoming an xfail.
+ # If either cell fails, #3959 was (partly) fixed -- remove this pin. NOT a contract that an
+ # integer dtype must keep returning a degenerate matrix.
# The dtype fixture is dropped because the claim is about the dtype argument itself.
# Snippet used to generate expected (torch only, executed on cpu):
# normal_transform_pixel(4, 5, dtype=torch.int64)
@@ -5512,7 +5531,7 @@ class TestCamtoworldRtToPoseRt(BaseTester):
# There is deliberately NO companion strict xfail: the intended behavior is undecided -- a
# fix could raise on a non-orthogonal R, or fall back to a true inverse -- and an
# assertion-shaped xfail could express only one of those and would stay silently XFAIL if
- # the other were chosen. Same shape as the #3948 wart pins in this file.
+ # the other were chosen. Same shape as the #3959 and #3957 wart pins in this file.
# If any cell fails, #3961 was (partly) fixed -- remove this pin. NOT a contract that a
# non-orthogonal R must keep producing these numbers.
# R = [[1, 0.5, 0], [0, 1, 0], [0, 0, 2]] has det = 2 and dyadic entries, so every literal