Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(api): privatize LightGlue ONNX dependency aliases (#5725)

Puttala Jeevan Kumar896d3cb322ce
diff --git a/kornia/feature/lightglue_onnx/lightglue.py b/kornia/feature/lightglue_onnx/lightglue.py
index 76caa65e1..0821fb711 100644
--- a/kornia/feature/lightglue_onnx/lightglue.py
+++ b/kornia/feature/lightglue_onnx/lightglue.py
@@ -23,8 +23,8 @@ from typing import ClassVar, Union
import torch
from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SAME_DEVICES, KORNIA_CHECK_SHAPE
-from kornia.core.external import numpy as np
-from kornia.core.external import onnxruntime as ort
+from kornia.core.external import numpy as _np
+from kornia.core.external import onnxruntime as _ort
from .utils import download_onnx_from_url, normalize_keypoints
@@ -68,7 +68,7 @@ class OnnxLightGlue:
# Import onnxruntime through the lazy loader, not an `importlib.util.find_spec` probe, so that
# `kornia_config.lazyloader.installation_mode` handles a missing module before the weights are downloaded.
# `__version__` is module metadata, which the loader imports the module for; `__doc__` would not import it.
- _ = ort.__version__
+ _ = _ort.__version__
KORNIA_CHECK(importlib.util.find_spec("numpy") is not None, "numpy is not installed.")
device = torch.device(device) # type: ignore
@@ -94,7 +94,7 @@ class OnnxLightGlue:
weights = download_onnx_from_url(url)
- self.session = ort.InferenceSession(weights, providers=providers)
+ self.session = _ort.InferenceSession(weights, providers=providers)
def __call__(self, data: dict[str, dict[str, torch.Tensor]]) -> dict[str, torch.Tensor]:
"""Run the ONNX LightGlue matcher.
@@ -179,7 +179,7 @@ class OnnxLightGlue:
name,
device_type=self.device.type,
device_id=0,
- element_type=np.float32,
+ element_type=_np.float32,
shape=tuple(tensor.shape),
buffer_ptr=tensor.data_ptr(),
)
diff --git a/tests/feature/test_lightglue_onnx.py b/tests/feature/test_lightglue_onnx.py
index b42a0d701..95a5a0ce9 100644
--- a/tests/feature/test_lightglue_onnx.py
+++ b/tests/feature/test_lightglue_onnx.py
@@ -77,6 +77,43 @@ def test_missing_onnxruntime_applies_the_installation_mode(monkeypatch, without_
@pytest.mark.skipif(ort is None, reason="OnnxLightGlue requires onnxruntime-gpu")
@pytest.mark.skipif(torch_version_le(1, 9, 1), reason="Needs dlpack")
class TestOnnxLightGlue:
+ @pytest.mark.device_agnostic
+ def test_local_model_inference(self, tmp_path):
+ from onnx import TensorProto, helper, save
+
+ # A tiny local graph exercises session creation and all four NumPy-typed input
+ # bindings without downloading pretrained weights or requiring CUDA.
+ inputs = [
+ helper.make_tensor_value_info(name, TensorProto.FLOAT, [1, 2, width])
+ for name, width in [("kpts0", 2), ("kpts1", 2), ("desc0", 4), ("desc1", 4)]
+ ]
+ outputs = [
+ helper.make_tensor_value_info("matches0", TensorProto.INT64, [1, 2]),
+ helper.make_tensor_value_info("mscores0", TensorProto.FLOAT, [1]),
+ ]
+ nodes = [
+ helper.make_node(
+ "Constant", [], ["matches0"], value=helper.make_tensor("matches", TensorProto.INT64, [1, 2], [0, 1])
+ ),
+ helper.make_node(
+ "Constant", [], ["mscores0"], value=helper.make_tensor("scores", TensorProto.FLOAT, [1], [0.75])
+ ),
+ ]
+ graph = helper.make_graph(nodes, "lightglue_alias_smoke", inputs, outputs)
+ model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 17)], ir_version=8)
+ path = tmp_path / "lightglue.onnx"
+ save(model, path)
+
+ matcher = OnnxLightGlue(str(path), device="cpu")
+ features = {
+ "keypoints": torch.tensor([[[0.0, 0.0], [5.0, 5.0]]]),
+ "descriptors": torch.ones(1, 2, 4),
+ "image_size": torch.tensor([[10, 10]]),
+ }
+ result = matcher({"image0": features, "image1": features})
+ torch.testing.assert_close(result["matches"], torch.tensor([[0, 1]]))
+ torch.testing.assert_close(result["scores"], torch.tensor([0.75]))
+
@pytest.mark.slow
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Test requires CUDA")
@pytest.mark.parametrize("weights", ["disk", "superpoint", "disk_fp16", "superpoint_fp16"])
diff --git a/tests/test_api_surface.py b/tests/test_api_surface.py
index 3098be578..af3b19902 100644
--- a/tests/test_api_surface.py
+++ b/tests/test_api_surface.py
@@ -90,6 +90,20 @@ def test_no_public_name_removed(module_name):
)
[email protected]("alias", ["np", "ort"])
+def test_lightglue_onnx_dependencies_are_private(alias):
+ # #4026 Group 2: lazy imports must not reintroduce accidental public globals.
+ from kornia.core import external
+ from kornia.feature import OnnxLightGlue
+ from kornia.feature.lightglue_onnx import lightglue
+
+ assert lightglue.__all__ == ["OnnxLightGlue"]
+ assert lightglue.OnnxLightGlue is OnnxLightGlue
+ assert not hasattr(lightglue, alias)
+ dependency = {"np": external.numpy, "ort": external.onnxruntime}[alias]
+ assert getattr(lightglue, f"_{alias}") is dependency
+
+
def _foreign_names(module_name: str) -> set:
"""Public names of ``module_name`` bound to an import rather than to its own API.