Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): closed-form 3x3 inverse in warp/normalize (cusolver-free) (#3834)

Edgar Ribad3121601d678
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index b8f0ebf83..b636a57bb 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -25,7 +25,7 @@ import torch.nn.functional as F
from kornia.constants import pi
from kornia.core._compat import deprecated
from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE
-from kornia.core.utils import _torch_inverse_cast
+from kornia.core.utils import _inverse_3x3_closed_form, _torch_inverse_cast
__all__ = [
"ARKitQTVecs_to_ColmapQTVecs",
@@ -1088,7 +1088,9 @@ def normalize_homography(
# compute the transformation pixel/norm for src/dst
src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)
- src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix)
+ # Closed-form 3x3 inverse of the (well-conditioned) pixel-normalization matrix: cusolver-free,
+ # so homography normalization runs on the Jetson wheel where ``torch.linalg.inv`` dlopen-fails.
+ src_pix_trans_src_norm = _inverse_3x3_closed_form(src_norm_trans_src_pix)
dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
# compute chain transformations
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 5faacd6cf..5f18751b1 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -133,7 +133,11 @@ def warp_perspective(
# we F.normalize the 3x3 transformation matrix and convert to 3x4
dst_norm_trans_src_norm: torch.Tensor = normalize_homography(M, (H, W), (h_out, w_out)) # Bx3x3
- src_norm_trans_dst_norm = _torch_inverse_cast(dst_norm_trans_src_norm) # Bx3x3
+ # Closed-form 3x3 inverse (pure arithmetic) instead of ``torch.linalg.inv``: numerically
+ # equivalent for these well-conditioned transforms, and it runs where the LAPACK/cusolver
+ # backend is unavailable (e.g. the Jetson wheel, where ``linalg.inv`` dlopen-fails and would
+ # otherwise crash every warp on GPU).
+ src_norm_trans_dst_norm = _inverse_3x3_closed_form(dst_norm_trans_src_norm) # Bx3x3
# this piece of code substitutes F.affine_grid since it does not support 3x3
grid = (
@@ -213,7 +217,9 @@ def warp_affine(
M_3x3: torch.Tensor = convert_affinematrix_to_homography(M)
dst_norm_trans_src_norm: torch.Tensor = normalize_homography(M_3x3, (H, W), dsize)
- src_norm_trans_dst_norm = _torch_inverse_cast(dst_norm_trans_src_norm)
+ # Closed-form 3x3 inverse (see warp_perspective) β€” cusolver-free, so affine warps run on the
+ # Jetson wheel where ``torch.linalg.inv`` dlopen-fails.
+ src_norm_trans_dst_norm = _inverse_3x3_closed_form(dst_norm_trans_src_norm)
grid = F.affine_grid(src_norm_trans_dst_norm[:, :2, :], [B, C, dsize[0], dsize[1]], align_corners=align_corners)