From 55b69e2959525ca948816a35a22f745e7d91a347 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 29 May 2024 11:34:07 +0300 Subject: [PATCH 01/10] add squeeze addaptation test --- tests/test_adapt.py | 53 +++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 8ae22176..d98c102b 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -4,6 +4,7 @@ import numpy import onnx import onnx.parser +import onnxruntime as ort import pytest import spox.opset.ai.onnx.v18 as op18 @@ -71,27 +72,28 @@ def inline_old_identity_twice_graph(old_identity): return results(final=z).with_opset(("ai.onnx", 17)) -@pytest.fixture -def old_squeeze_graph(old_squeeze): - class Squeeze11(StandardNode): - @dataclass - class Attributes(BaseAttributes): - axes: AttrInt64s +class Squeeze11(StandardNode): + @dataclass + class Attributes(BaseAttributes): + axes: AttrInt64s + + @dataclass + class Inputs(BaseInputs): + data: Var - @dataclass - class Inputs(BaseInputs): - data: Var + @dataclass + class Outputs(BaseOutputs): + squeezed: Var - @dataclass - class Outputs(BaseOutputs): - squeezed: Var + op_type = OpType("Squeeze", "", 11) - op_type = OpType("Squeeze", "", 11) + attrs: Attributes + inputs: Inputs + outputs: Outputs - attrs: Attributes - inputs: Inputs - outputs: Outputs +@pytest.fixture +def old_squeeze_graph(old_squeeze): def squeeze11(_data: Var, _axes: Iterable[int]): return Squeeze11( Squeeze11.Attributes(AttrInt64s(_axes, "axes")), Squeeze11.Inputs(_data) @@ -233,3 +235,22 @@ def test_inline_model_custom_node_nested(old_squeeze: onnx.ModelProto): # Add another node to the model to trigger the adaption logic c = op18.identity(b) build({"a": a}, {"c": c}) + + +def test_if_adaptation_const(): + cond = argument(Tensor(numpy.bool_, ())) + b = argument(Tensor(numpy.float32, (1,))) + squeezed = Squeeze11.squeeze11(b, [0]) + out = op18.if_( + cond, + then_branch=lambda: [squeezed], + else_branch=lambda: [Squeeze11.squeeze11(b, [0])], + ) + model = build({"b": b, "cond": cond}, {"out": out[0]}) + + # predict on model + b = numpy.array([1.1], dtype=numpy.float32) + cond = numpy.array(True, dtype=numpy.bool_) + out = ort.InferenceSession(model.SerializeToString()).run( + None, {"b": b, "cond": cond} + ) From c1ed0c6028eb849612bf9526de091f7729c129e8 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 29 May 2024 11:38:08 +0300 Subject: [PATCH 02/10] Fix unit test --- tests/test_adapt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index d98c102b..7fc7f767 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -91,14 +91,14 @@ class Outputs(BaseOutputs): inputs: Inputs outputs: Outputs - -@pytest.fixture -def old_squeeze_graph(old_squeeze): def squeeze11(_data: Var, _axes: Iterable[int]): return Squeeze11( Squeeze11.Attributes(AttrInt64s(_axes, "axes")), Squeeze11.Inputs(_data) ).outputs.squeezed + +@pytest.fixture +def old_squeeze_graph(old_squeeze): (data,) = arguments( data=Tensor( numpy.float32, @@ -108,7 +108,7 @@ def squeeze11(_data: Var, _axes: Iterable[int]): ), ) ) - result = squeeze11(data, [0]) + result = Squeeze11.squeeze11(data, [0]) return results(final=result).with_opset(("ai.onnx", 17)) From 2226ecca7fe5516b723f35a25730c567a063876d Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 29 May 2024 11:41:24 +0300 Subject: [PATCH 03/10] fix static method --- tests/test_adapt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 7fc7f767..e879f9cd 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -91,7 +91,8 @@ class Outputs(BaseOutputs): inputs: Inputs outputs: Outputs - def squeeze11(_data: Var, _axes: Iterable[int]): + @classmethod + def squeeze11(cls, _data: Var, _axes: Iterable[int]): return Squeeze11( Squeeze11.Attributes(AttrInt64s(_axes, "axes")), Squeeze11.Inputs(_data) ).outputs.squeezed From 263fdff83e486daa2c8366562df4f0bbf67fd8b5 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 29 May 2024 15:03:22 +0300 Subject: [PATCH 04/10] add test from issue #145 --- tests/test_adapt.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index e879f9cd..55aca934 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -238,7 +238,7 @@ def test_inline_model_custom_node_nested(old_squeeze: onnx.ModelProto): build({"a": a}, {"c": c}) -def test_if_adaptation_const(): +def test_if_adapatation_squeeze(): cond = argument(Tensor(numpy.bool_, ())) b = argument(Tensor(numpy.float32, (1,))) squeezed = Squeeze11.squeeze11(b, [0]) @@ -255,3 +255,11 @@ def test_if_adaptation_const(): out = ort.InferenceSession(model.SerializeToString()).run( None, {"b": b, "cond": cond} ) + + +def test_if_adaptation_const(): + sq = op19.const(1.1453, dtype=numpy.float32) + b = argument(Tensor(numpy.float32, ("N",))) + cond = op18.equal(sq, b) + out = op18.if_(cond, then_branch=lambda: [sq], else_branch=lambda: [sq]) + build({"b": b}, {"out": out[0]}) From 5ea40d09b76ae2aa9bd33921fda38ba8095df905 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Tue, 23 Jul 2024 12:52:27 +0300 Subject: [PATCH 05/10] add domain assert --- tests/test_adapt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 55aca934..166cb2f0 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -262,4 +262,5 @@ def test_if_adaptation_const(): b = argument(Tensor(numpy.float32, ("N",))) cond = op18.equal(sq, b) out = op18.if_(cond, then_branch=lambda: [sq], else_branch=lambda: [sq]) - build({"b": b}, {"out": out[0]}) + model = build({"b": b}, {"out": out[0]}) + assert model.domain == "" or model.domain == "ai.onnx" From a0753bcc44ba26de562e461983c50d3ac05a1a62 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Tue, 23 Jul 2024 12:59:03 +0300 Subject: [PATCH 06/10] add version check --- tests/test_adapt.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 166cb2f0..5ead037f 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -264,3 +264,7 @@ def test_if_adaptation_const(): out = op18.if_(cond, then_branch=lambda: [sq], else_branch=lambda: [sq]) model = build({"b": b}, {"out": out[0]}) assert model.domain == "" or model.domain == "ai.onnx" + assert ( + model.opset_import[0].domain == "ai.onnx" or model.opset_import[0].domain == "" + ) + assert model.opset_import[0].version > 11 From 59fe23fcbf476e059fbd5c40ac6b7f3c0b363048 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov <54221777+aivanoved@users.noreply.github.com> Date: Thu, 1 Aug 2024 12:07:54 +0300 Subject: [PATCH 07/10] Update tests/test_adapt.py --- tests/test_adapt.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 5ead037f..b5d51fd6 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -91,7 +91,6 @@ class Outputs(BaseOutputs): inputs: Inputs outputs: Outputs - @classmethod def squeeze11(cls, _data: Var, _axes: Iterable[int]): return Squeeze11( Squeeze11.Attributes(AttrInt64s(_axes, "axes")), Squeeze11.Inputs(_data) From fb5e437c0ab338f208a39f547ddb9c0315ffd957 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov <54221777+aivanoved@users.noreply.github.com> Date: Thu, 1 Aug 2024 12:08:28 +0300 Subject: [PATCH 08/10] Update tests/test_adapt.py --- tests/test_adapt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index b5d51fd6..17716c67 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -91,7 +91,7 @@ class Outputs(BaseOutputs): inputs: Inputs outputs: Outputs - def squeeze11(cls, _data: Var, _axes: Iterable[int]): + def squeeze11(self, _data: Var, _axes: Iterable[int]): return Squeeze11( Squeeze11.Attributes(AttrInt64s(_axes, "axes")), Squeeze11.Inputs(_data) ).outputs.squeezed From e172bc2b63b7cf7e130df54e362f9825306aef89 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Thu, 1 Aug 2024 12:15:20 +0300 Subject: [PATCH 09/10] misc: change squeeze11 helper function --- tests/test_adapt.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 17716c67..f0fe4d80 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -91,10 +91,11 @@ class Outputs(BaseOutputs): inputs: Inputs outputs: Outputs - def squeeze11(self, _data: Var, _axes: Iterable[int]): - return Squeeze11( - Squeeze11.Attributes(AttrInt64s(_axes, "axes")), Squeeze11.Inputs(_data) - ).outputs.squeezed + +def squeeze11(_data: Var, _axes: Iterable[int]): + return Squeeze11( + Squeeze11.Attributes(AttrInt64s(_axes, "axes")), Squeeze11.Inputs(_data) + ).outputs.squeezed @pytest.fixture @@ -108,7 +109,7 @@ def old_squeeze_graph(old_squeeze): ), ) ) - result = Squeeze11.squeeze11(data, [0]) + result = squeeze11(data, [0]) return results(final=result).with_opset(("ai.onnx", 17)) @@ -240,11 +241,11 @@ def test_inline_model_custom_node_nested(old_squeeze: onnx.ModelProto): def test_if_adapatation_squeeze(): cond = argument(Tensor(numpy.bool_, ())) b = argument(Tensor(numpy.float32, (1,))) - squeezed = Squeeze11.squeeze11(b, [0]) + squeezed = squeeze11(b, [0]) out = op18.if_( cond, then_branch=lambda: [squeezed], - else_branch=lambda: [Squeeze11.squeeze11(b, [0])], + else_branch=lambda: [squeeze11(b, [0])], ) model = build({"b": b, "cond": cond}, {"out": out[0]}) From 0b8b437d052b4f14320607b64acbc0277fbd889b Mon Sep 17 00:00:00 2001 From: Christian Bourjau Date: Thu, 1 Aug 2024 18:45:03 +0200 Subject: [PATCH 10/10] numpy -> np --- tests/test_adapt.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 05bc7149..b8c9e914 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -239,8 +239,8 @@ def test_inline_model_custom_node_nested(old_squeeze: onnx.ModelProto): def test_if_adapatation_squeeze(): - cond = argument(Tensor(numpy.bool_, ())) - b = argument(Tensor(numpy.float32, (1,))) + cond = argument(Tensor(np.bool_, ())) + b = argument(Tensor(np.float32, (1,))) squeezed = squeeze11(b, [0]) out = op18.if_( cond, @@ -250,16 +250,16 @@ def test_if_adapatation_squeeze(): model = build({"b": b, "cond": cond}, {"out": out[0]}) # predict on model - b = numpy.array([1.1], dtype=numpy.float32) - cond = numpy.array(True, dtype=numpy.bool_) + b = np.array([1.1], dtype=np.float32) + cond = np.array(True, dtype=np.bool_) out = ort.InferenceSession(model.SerializeToString()).run( None, {"b": b, "cond": cond} ) def test_if_adaptation_const(): - sq = op19.const(1.1453, dtype=numpy.float32) - b = argument(Tensor(numpy.float32, ("N",))) + sq = op19.const(1.1453, dtype=np.float32) + b = argument(Tensor(np.float32, ("N",))) cond = op18.equal(sq, b) out = op18.if_(cond, then_branch=lambda: [sq], else_branch=lambda: [sq]) model = build({"b": b}, {"out": out[0]})