Hyperplane.through in float16: a small valid triangle whose cross product underflows takes the SVD fallback and returns the opposite normal (scale 1e-4: +z instead of -z)
Hyperplane.through(p0, p1, p2) points the normal along (p2 − p0) × (p1 − p0) unless that cross product is tiny relative to the edges. In that case it takes an SVD fallback, and the SVD's sign is arbitrary. In float16 the cross product of a small but perfectly valid triangle underflows to zero, so the triangle takes the fallback and can come back with the opposite orientation. bfloat16 and float32 keep the orientation at the same scale.
import torch
from kornia.geometry.plane import Hyperplane
for scale in (1e-3, 1e-4):
for dt in (torch.float16, torch.bfloat16, torch.float32):
p0 = torch.tensor([0.0, 0.0, 0.0], dtype=dt)
p1 = torch.tensor([scale, 0.0, 0.0], dtype=dt)
p2 = torch.tensor([0.0, scale, 0.0], dtype=dt)
print(scale, dt, Hyperplane.through(p0, p1, p2).normal.data.tolist()) # (p2 - p0) x (p1 - p0) points along -z0.001 torch.float16 [0.0, 0.0, -1.0]
0.001 torch.bfloat16 [0.0, 0.0, -1.0]
0.001 torch.float32 [0.0, 0.0, -1.0]
0.0001 torch.float16 [0.0, 0.0, 1.0]
0.0001 torch.bfloat16 [0.0, 0.0, -1.0]
0.0001 torch.float32 [0.0, 0.0, -1.0]kornia main 0fdfcf20a (after #5055), torch 2.14.0, CPU. At scale 1e-4 the cross product's z component is 1e-8, which is below float16's smallest subnormal (6e-8). The norm is then 0, the fallback mask norm <= ‖v0‖·‖v1‖·eps is true, and the SVD returns +z.
Classification
Kornia defect, float16 only: the orientation of the returned normal depends on the dtype for a non-degenerate triangle. It flips the sign of signed_distance for every point. The rank check proposed in #5054 accepts this triangle, correctly, since it is not degenerate. So once #5054 lands the fallback is reached only through this underflow, and it still returns the wrong sign.
Disposition
Compute the cross product and its norm in float32 for half-precision inputs, as #5054's rank check already does for its singular values. Alternatively, sign-align the SVD normal with the cross product computed in float32. Pin the orientation of a scale-1e-4 triangle in float16. The batch-10 Hyperplane convention docstring states the (p2 − p0) × (p1 − p0) orientation, which this defect contradicts in float16.
Posted on behalf of @ducha-aiki by Claude (Opus 5.5).
Source: kornia/kornia