Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): remove import-time torch.jit.script decorators (#3757)

Leonardo Vanni256945accb12
diff --git a/kornia/geometry/epipolar/_metrics.py b/kornia/geometry/epipolar/_metrics.py
index 8b7c8d0b0..235bd209c 100644
--- a/kornia/geometry/epipolar/_metrics.py
+++ b/kornia/geometry/epipolar/_metrics.py
@@ -17,7 +17,6 @@
"""Module including useful metrics for Structure from Motion."""
-import torch
from torch import Tensor, ones_like
from kornia.core.check import KORNIA_CHECK_IS_TENSOR
@@ -135,7 +134,6 @@ def _sampson_epipolar_distance_matmul_impl_(
return (out + eps).sqrt()
def sampson_epipolar_distance(
pts1: Tensor,
pts2: Tensor,
diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.py
index 07a1e464d..386761233 100644
--- a/kornia/geometry/epipolar/essential.py
+++ b/kornia/geometry/epipolar/essential.py
@@ -79,7 +79,6 @@ def run_5point(points1: torch.Tensor, points2: torch.Tensor, weights: Optional[t
return E
def _multiply_deg_one_poly(a: torch.Tensor, b: torch.Tensor, T_deg1: torch.Tensor) -> torch.Tensor:
# a, b: (..., 4)
product_basis = a.unsqueeze(2) * b.unsqueeze(1) # (..., 4, 4)
@@ -87,7 +86,6 @@ def _multiply_deg_one_poly(a: torch.Tensor, b: torch.Tensor, T_deg1: torch.Tenso
return product_vector @ T_deg1 # (..., 10)
def _multiply_deg_two_one_poly(a: torch.Tensor, b: torch.Tensor, T_deg2: torch.Tensor) -> torch.Tensor:
# a: (..., 10), b: (..., 4)
product_basis = a.unsqueeze(2) * b.unsqueeze(1) # (..., 10, 4)
@@ -95,7 +93,6 @@ def _multiply_deg_two_one_poly(a: torch.Tensor, b: torch.Tensor, T_deg2: torch.T
return product_vector @ T_deg2 # (..., 20)
def _determinant_to_polynomial_jit(
A: torch.Tensor,
multiplication_indices: torch.Tensor,
@@ -116,7 +113,6 @@ def _determinant_to_polynomial_jit(
return cs
def _solve_2x2_tikhonov_safe(A: torch.Tensor, b: torch.Tensor, eps: float = 1e-12) -> Tuple[torch.Tensor, torch.Tensor]:
r"""Solve (A)x=b for A (...,2,2), b (...,2,1) using Tikhonov regularization.
@@ -196,7 +192,6 @@ def _fun_select(mat: torch.Tensor, i: int, j: int, ratio: int = 3) -> torch.Tens
return mat[:, ratio * j + i]
def _null_to_Nister_solution_script(
X: torch.Tensor,
batch_size: int,
diff --git a/kornia/geometry/epipolar/fundamental.py b/kornia/geometry/epipolar/fundamental.py
index d9c052c25..58ad40f53 100644
--- a/kornia/geometry/epipolar/fundamental.py
+++ b/kornia/geometry/epipolar/fundamental.py
@@ -148,13 +148,11 @@ def _normalize_F(F: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
# Reference: Adapted from the 'run_7point' function in opencv
# https://github.com/opencv/opencv/blob/4.x/modules/calib3d/src/fundam.cpp
def _isclose0(x: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
# torch.isclose(x, 0) but TorchScript/GPU-friendly (no scalar tensor creation)
return x.abs() <= eps
def run_7point(points1: torch.Tensor, points2: torch.Tensor) -> torch.Tensor:
r"""Compute the fundamental matrix using the 7-point algorithm.
@@ -256,7 +254,6 @@ def run_7point(points1: torch.Tensor, points2: torch.Tensor) -> torch.Tensor:
return normalize_transformation(fmatrix)
def run_8point(
points1: torch.Tensor,
points2: torch.Tensor,
diff --git a/tests/test_import.py b/tests/test_import.py
new file mode 100644
index 000000000..5cc435b0d
--- /dev/null
+++ b/tests/test_import.py
@@ -0,0 +1,53 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import subprocess
+import sys
+import textwrap
+
+
+def test_import_without_jit_script_deprecation():
+ """Importing kornia must not eagerly call ``torch.jit.script``.
+
+ ``torch.jit.script`` is deprecated (and unsupported on Python 3.14+). Decorating
+ functions with ``@torch.jit.script`` runs the deprecated path at import time, which
+ makes ``python -W error -c "import kornia"`` fail. This guards against reintroducing
+ an import-time scripting call. See https://github.com/kornia/kornia/issues/3727.
+
+ A fresh interpreter is used so the result is independent of kornia already being
+ imported by the test session. Only the ``torch.jit.script`` deprecation is promoted
+ to an error to avoid coupling the test to unrelated third-party warnings.
+ """
+ script = textwrap.dedent(
+ """
+ import warnings
+
+ warnings.filterwarnings("error", message=r".*torch\\.jit\\.script.*")
+ warnings.filterwarnings("error", category=DeprecationWarning, module=r"kornia.*")
+ warnings.filterwarnings("error", category=FutureWarning, module=r"kornia.*")
+
+ import kornia # noqa: F401
+ """
+ )
+ # Trusted, fixed command (the current interpreter running a literal script); no external input.
+ result = subprocess.run( # noqa: S603
+ [sys.executable, "-c", script],
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+ assert result.returncode == 0, f"importing kornia raised a deprecation warning:\n{result.stderr}"