#3952·kornia

normalize_quaternion returns a non-unit quaternion below eps ([1e-13,0,0,0] -> [0.1,0,0,0]), so quaternion_to_rotation_matrix(zeros(4)) is silently the identity

Author: ducha-aikiCreated Aug 10, 2026Updated Sep 28, 2026

normalize_quaternion is return F.normalize(quaternion, p=2.0, dim=-1, eps=eps) (kornia/geometry/conversions.py:643), and F.normalize divides by max(||q||, eps). So a quaternion whose norm is below the default eps = 1e-12 comes back scaled, not normalized — silently, with no error and no warning:

python
>>> normalize_quaternion(torch.tensor([1e-13, 0., 0., 0.], dtype=torch.float64)).tolist()
[0.1, 0.0, 0.0, 0.0]        # ||.|| = 0.1, not 1
>>> normalize_quaternion(torch.zeros(4)).tolist()
[0.0, 0.0, 0.0, 0.0]        # ||.|| = 0

1e-13 / 1e-12 = 0.1 — the division by eps instead of by the norm is applied verbatim. The function's only documented contract is "Normalize a quaternion" returning "the normalized quaternion", and its eps is documented as "small value to avoid division by zero".

quaternion_to_rotation_matrix calls normalize_quaternion first, so it inherits the defect one level up: the zero quaternion silently yields the identity matrix, and so does any sub-eps quaternion.

python
>>> quaternion_to_rotation_matrix(torch.zeros(4))
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

Repro

python
import torch
from kornia.geometry.conversions import (
    normalize_quaternion as nq,
    quaternion_to_rotation_matrix as q2R,
)

tiny = torch.tensor([1e-13, 0.0, 0.0, 0.0], dtype=torch.float64)   # ||q|| = 1e-13 < eps = 1e-12
print("nq([1e-13,0,0,0])          =", nq(tiny).tolist(), " ||.|| =", nq(tiny).norm().item())
print("nq(zeros(4))               =", nq(torch.zeros(4)).tolist())
print("nq(zeros(4), eps=0.0)      =", nq(torch.zeros(4), eps=0.0).tolist())
print("float32 too                :", nq(torch.tensor([1e-13, 0.0, 0.0, 0.0])).tolist())

# the consequence one level up
print("\nq2R(zeros(4)) =\n", q2R(torch.zeros(4)))
print("q2R([1e-13,0,0,0]) =\n", q2R(tiny))

Observed

nq([1e-13,0,0,0])          = [0.1, 0.0, 0.0, 0.0]  ||.|| = 0.1
nq(zeros(4))               = [0.0, 0.0, 0.0, 0.0]
nq(zeros(4), eps=0.0)      = [nan, nan, nan, nan]
float32 too                : [0.10000000149011612, 0.0, 0.0, 0.0]

q2R(zeros(4)) =
 tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
q2R([1e-13,0,0,0]) =
 tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]], dtype=torch.float64)

(The eps=0.0 line shows what the guard is protecting against: without it the same input is [nan, nan, nan, nan].)

Expected

A function called normalize_quaternion should either return a unit quaternion or refuse. The two defensible designs:

  • Raise on ||q|| < eps — the zero quaternion does not represent a rotation, and neither does 1e-13·û in any useful numerical sense.
  • Return a documented sentinel, e.g. the identity quaternion (1, 0, 0, 0), so the postcondition ||output|| == 1 holds unconditionally and downstream quaternion_to_rotation_matrix still gets a valid rotation.

What should not happen is the current outcome: an output that is neither unit nor zero nor flagged (0.1 for 1e-13), whose scale depends on how far below eps the input happened to fall. Any caller that relies on the documented postcondition — including quaternion_to_rotation_matrix, quaternion_to_axis_angle and every user-side "normalize then use" pattern — is silently given a non-quaternion.

For quaternion_to_rotation_matrix(zeros(4)) the current answer (the identity) is at least a valid rotation matrix, but it is an invented one: the zero quaternion carries no rotation, and returning I means "no rotation" was assumed on the user's behalf without saying so. If the sentinel design is chosen for normalize_quaternion this becomes the documented behavior rather than an accident; if the raise design is chosen it propagates as an error, which is arguably better for a function whose input is meaningless.

Same class as #3940 (degenerate H/W/D accepted silently): a degenerate input is accepted and answered with a plausible-looking value instead of being rejected.

Classification

Semantic / default change. Unlike a NaN or a crash, the current behavior is a choice about degenerate input that some caller might be depending on (e.g. code that feeds accumulated-to-zero quaternions and expects the identity back rather than an exception). Changing it changes observable output on the default path for inputs that today run without error, which per the reference-core lifecycle (Part I / Part III §2) puts it in the coordinated Repair & Deprecation window rather than the early-landing broken-output class — with old-vs-new conformance vectors for the sub-eps and zero cases.

The interim documentation of this behavior is being handled in the batch-3 conversions convention-block PR, which will state the observed [1e-13,0,0,0] → [0.1,0,0,0] and q2R(0) = I facts explicitly and link here.

Environment

kornia 0.9.0rc1 @ 3358ef16, torch 2.9.1, cpu. Executed from a worktree carrying the batch-3a docs/tests commits on top of 3358ef16; normalize_quaternion and quaternion_to_rotation_matrix are byte-identical to origin/main there (no diff hunk lands in either; function bodies compare AST-identical).

Posted on behalf of @ducha-aiki by Claude (Fable 5).