π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fixes #3595 - avoid unnecessary copy when converting ORT outputs to torch tensors (#3598)
Varun Said0e829ea4f4c
diff --git a/kornia/feature/lightglue_onnx/lightglue.py b/kornia/feature/lightglue_onnx/lightglue.pyindex 098697fa1..7e7302422 100644--- a/kornia/feature/lightglue_onnx/lightglue.py+++ b/kornia/feature/lightglue_onnx/lightglue.py@@ -20,6 +20,7 @@ from __future__ import annotationsfrom typing import ClassVar, Unionimport torch+from torch.utils import dlpackfrom kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SAME_DEVICES, KORNIA_CHECK_SHAPE@@ -176,12 +177,16 @@ class OnnxLightGlue:self.session.run_with_iobinding(binding)matches, mscores = binding.get_outputs()-- # TODO: The following is an unnecessary copy. Replace with a better solution when torch supports- # constructing a torch.Tensor from a data pointer, or when ORT supports converting to torch torch.Tensor.- # https://github.com/microsoft/onnxruntime/issues/15963- outputs = {- "matches": torch.from_dlpack(matches.numpy()).to(self.device),- "scores": torch.from_dlpack(mscores.numpy()).to(self.device),- }+ # Prefer DLPack-based conversion when available for zero-copy transfer between them+ # The fallback path uses NumPy, which incurs a device-to-host copy and is slower.+ if hasattr(matches, "to_dlpack"):+ outputs = {+ "matches": dlpack.from_dlpack(matches.to_dlpack()).to(self.device),+ "scores": dlpack.from_dlpack(mscores.to_dlpack()).to(self.device),+ }+ else:+ outputs = {+ "matches": torch.from_numpy(matches.numpy()).to(self.device),+ "scores": torch.from_numpy(mscores.numpy()).to(self.device),+ }return outputs