Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(feature): apply the installation mode to OnnxLightGlue (#5287)

yi11141e6aaa5111e
diff --git a/changelog.d/5287.fixed.md b/changelog.d/5287.fixed.md
new file mode 100644
index 000000000..08d9d80f3
--- /dev/null
+++ b/changelog.d/5287.fixed.md
@@ -0,0 +1,7 @@
+`OnnxLightGlue` now goes through the `onnxruntime` lazy loader instead of probing the module with
+`importlib.util.find_spec`, so `kornia_config.lazyloader.installation_mode` applies to it. The probe returned before
+anything reached `kornia.core.external.onnxruntime`, so with the mode set to `"auto"` the constructor raised
+`ImportError` and installed nothing, and with `"ask"` it never asked, while every other `onnxruntime` consumer
+installed `kornia[onnx]` or prompted. Under the default `"raise"` mode a missing `onnxruntime` still raises an
+`ImportError` naming the extra to install, now with the loader's wording: `Optional dependency 'onnxruntime' is not
+installed. Install it with: pip install "kornia[onnx]".`
diff --git a/kornia/feature/lightglue_onnx/lightglue.py b/kornia/feature/lightglue_onnx/lightglue.py
index d7dc0352a..91f1cf87e 100644
--- a/kornia/feature/lightglue_onnx/lightglue.py
+++ b/kornia/feature/lightglue_onnx/lightglue.py
@@ -58,9 +58,10 @@ class OnnxLightGlue:
required_data_keys: ClassVar[list[str]] = ["image0", "image1"]
def __init__(self, weights: str | None = None, device: Union[str, torch.device, None] = "cpu") -> None:
- # An ImportError that names the extra, like the LazyLoader handles raise, rather than a bare check.
- if importlib.util.find_spec("onnxruntime") is None:
- raise ImportError('onnxruntime is not installed. Install it with: pip install "kornia[onnx]".')
+ # 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__
KORNIA_CHECK(importlib.util.find_spec("numpy") is not None, "numpy is not installed.")
device = torch.device(device) # type: ignore
diff --git a/tests/feature/test_lightglue_onnx.py b/tests/feature/test_lightglue_onnx.py
index 83c2c0548..49a01dd37 100644
--- a/tests/feature/test_lightglue_onnx.py
+++ b/tests/feature/test_lightglue_onnx.py
@@ -15,11 +15,16 @@
# limitations under the License.
#
+import subprocess
+import sys
+
import pytest
pytest.importorskip("onnx")
import torch
+from kornia.config import LazyLoaderConfig, kornia_config
+from kornia.core import external
from kornia.core._compat import torch_version_le
from kornia.feature import OnnxLightGlue
from kornia.feature.lightglue_onnx.utils import normalize_keypoints
@@ -30,19 +35,43 @@ except ImportError:
ort = None
-def test_missing_onnxruntime_names_the_extra(monkeypatch):
+def without_onnxruntime(monkeypatch):
+ """Make `onnxruntime` unimportable and forget what the loader cached, so the loader handles the miss itself."""
+ monkeypatch.delenv("KORNIA_INSTALLATION_MODE", raising=False)
+ monkeypatch.setattr(kornia_config, "lazyloader", LazyLoaderConfig())
+ monkeypatch.setitem(sys.modules, "onnxruntime", None)
+ monkeypatch.setattr(external.onnxruntime, "module", None)
+ monkeypatch.setattr(external.onnxruntime, "_install_error", None)
+
+ # The constructor must settle onnxruntime before it fetches the weights; this also keeps the tests off the network.
+ def refuse_download(*args, **kwargs):
+ raise AssertionError("the constructor downloaded the weights")
+
+ monkeypatch.setattr("kornia.feature.lightglue_onnx.lightglue.download_onnx_from_url", refuse_download)
+
+
+def test_missing_onnxruntime_names_the_extra(without_onnxruntime):
# Without onnxruntime the constructor must fail the way the LazyLoader handles do: an ImportError that
# says `pip install "kornia[onnx]"`, which the installation page promises.
- import importlib.util
+ with pytest.raises(ImportError, match=r"kornia\[onnx\]"):
+ OnnxLightGlue()
- real_find_spec = importlib.util.find_spec
- def hide_onnxruntime(name, *args, **kwargs):
- return None if name == "onnxruntime" else real_find_spec(name, *args, **kwargs)
+def test_missing_onnxruntime_applies_the_installation_mode(monkeypatch, without_onnxruntime):
+ # #5279: the constructor goes through the onnxruntime loader, so "auto" installs the `onnx` extra with this
+ # interpreter's pip. The installer is replaced, so nothing is installed.
+ kornia_config.lazyloader.installation_mode = "auto"
+ commands = []
- monkeypatch.setattr(importlib.util, "find_spec", hide_onnxruntime)
- with pytest.raises(ImportError, match=r"kornia\[onnx\]"):
+ def refuse(command, *args, **kwargs):
+ commands.append(command)
+ raise AssertionError("the loader tried to start the installer")
+
+ monkeypatch.setattr(subprocess, "run", refuse)
+ with pytest.raises(AssertionError, match="tried to start the installer"):
OnnxLightGlue()
+ assert commands == [[sys.executable, "-m", "pip", "install", "kornia[onnx]"]]
@pytest.mark.skipif(ort is None, reason="OnnxLightGlue requires onnxruntime-gpu")