Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(onnx): reject runtime input count mismatches (#5799)

Arc000007e2ef21c25afb
diff --git a/changelog.d/5799.breaking.md b/changelog.d/5799.breaking.md
new file mode 100644
index 000000000..aa8bc02d9
--- /dev/null
+++ b/changelog.d/5799.breaking.md
@@ -0,0 +1 @@
+`kornia.onnx.ONNXSequential` and `kornia.onnx.ONNXModule` raise a `ValueError` naming the expected and received counts when they are called with a different number of arrays than the session has inputs. Extra arrays used to be dropped without a warning, so the call returned the result for the leading arrays only, and too few arrays raised an `IndexError`.
diff --git a/kornia/core/mixin/onnx.py b/kornia/core/mixin/onnx.py
index 89ff071cb..ba5682b3f 100644
--- a/kornia/core/mixin/onnx.py
+++ b/kornia/core/mixin/onnx.py
@@ -382,9 +382,14 @@ class ONNXRuntimeMixin:
Returns:
list: The outputs from the ONNX model inference.
+ Raises:
+ ValueError: If the number of inputs differs from the length of ``get_session().get_inputs()``.
+
"""
ort_inputs = self._session.get_inputs()
- ort_input_values = {ort_inputs[i].name: inputs[i] for i in range(len(ort_inputs))}
+ if len(inputs) != len(ort_inputs):
+ raise ValueError(f"Expected {len(ort_inputs)} ONNX inputs, got {len(inputs)}.")
+ ort_input_values = {ort_input.name: input_value for ort_input, input_value in zip(ort_inputs, inputs)}
return self._session.run(None, ort_input_values)
diff --git a/tests/core/test_onnx_mixin.py b/tests/core/test_onnx_mixin.py
index 56ab5abb8..d549c6b84 100644
--- a/tests/core/test_onnx_mixin.py
+++ b/tests/core/test_onnx_mixin.py
@@ -235,6 +235,32 @@ class TestONNXRuntimeMixin(BaseTester):
x = torch.rand(1, 3, 4, 5)
self.assert_close(torch.from_numpy(ONNXSequential(op, session_options=_options())(x.numpy())[0]), x.sigmoid())
+ def test_runtime_rejects_wrong_input_count(self, op):
+ from kornia.onnx import ONNXSequential
+
+ runtime = ONNXSequential(op)
+ x = torch.rand(1, 3, 4, 5).numpy()
+ with pytest.raises(ValueError, match="Expected 1 ONNX inputs, got 0"):
+ runtime()
+ with pytest.raises(ValueError, match="Expected 1 ONNX inputs, got 2"):
+ runtime(x, x)
+
+ def test_runtime_feeds_inputs_in_session_order(self):
+ from kornia.onnx import ONNXModule, ONNXSequential
+
+ def _value(name):
+ return onnx.helper.make_tensor_value_info(name, onnx.TensorProto.FLOAT, [1])
+
+ node = onnx.helper.make_node("Sub", ["x", "y"], ["output"])
+ graph = onnx.helper.make_graph([node], "sub", [_value("x"), _value("y")], [_value("output")])
+ op = onnx.helper.make_model(graph, opset_imports=[onnx.helper.make_opsetid("", 17)], ir_version=9)
+ a, b = torch.tensor([2.0]).numpy(), torch.tensor([99.0]).numpy()
+ for runtime in (ONNXSequential(op), ONNXModule(op)):
+ assert runtime(a, b)[0].tolist() == [-97.0]
+ assert runtime(b, a)[0].tolist() == [97.0]
+ with pytest.raises(ValueError, match="Expected 2 ONNX inputs, got 3"):
+ runtime(a, b, a)
+
def test_add_metadata_overwrites_existing_keys(self, op):
keys = [prop.key for prop in op.metadata_props]
assert sorted(keys) == ["source", "version"]