From 3b0c7f34115d4484e6381b739914085fd9d55682 Mon Sep 17 00:00:00 2001 From: Masahiro Masuda Date: Thu, 29 Apr 2021 11:26:30 +0900 Subject: [PATCH 1/7] add ONNX EyeLike converter --- python/tvm/relay/frontend/onnx.py | 15 +++++++++++++ tests/python/frontend/onnx/test_forward.py | 26 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index e4a6885efeb7..1911c0c3ff34 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -1440,6 +1440,20 @@ def _impl_v11(cls, inputs, attr, params): ) +class EyeLike(OnnxOpConverter): + """Operator converter for EyeLike.""" + + @classmethod + def _impl_v9(cls, inputs, attr, params): + dtype = attr.get("mode", "float32") + in_dtype = infer_type(inputs[0]).checked_type.dtype + zeros = _op.zeros_like(inputs[0]) + dim = infer_shape(zeros)[0] + indices = _op.arange(_op.const(0), _op.const(dim), dtype="int32") + ones = _op.full(_op.const(1), (dim,), dtype=in_dtype) + return _op.scatter_nd(zeros, _op.stack([indices, indices], axis=0), ones, "update") + + class Greater(OnnxOpConverter): """Operator logical greater.""" @@ -2991,6 +3005,7 @@ def _get_convert_map(opset): "Scatter": Scatter.get_converter(opset), "ScatterElements": Scatter.get_converter(opset), "ScatterND": ScatterND.get_converter(opset), + "EyeLike": EyeLike.get_converter(opset), "Squeeze": AttrCvt("squeeze", {"axes": "axis"}), "Unsqueeze": Unsqueeze.get_converter(opset), "Pad": Pad.get_converter(opset), diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 89655840da2a..d45607bed765 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -4095,6 +4095,7 @@ def verify_softplus(indata): verify_softplus(input_data) +@tvm.testing.uses_gpu def test_cumsum(): def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"): cumsum_node = onnx.helper.make_node( @@ -4171,6 +4172,30 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"): verify_cumsum(data, 1, 1, 1, type="int32") +@tvm.testing.uses_gpu +def test_eyelike(): + def verify_eyelike(indata): + node = helper.make_node( + "EyeLike", + inputs=["X"], + outputs=["Y"], + ) + + graph = helper.make_graph( + [node], + "eyelike_test", + inputs=[helper.make_tensor_value_info("X", TensorProto.FLOAT, list(indata.shape))], + outputs=[helper.make_tensor_value_info("Y", TensorProto.FLOAT, list(indata.shape))], + ) + + model = helper.make_model(graph, producer_name="eyelike_test") + + verify_with_ort_with_inputs(model, [indata], dtype="float32", opset=9) + + input_data = np.zeros((5, 5), dtype=np.float32) + verify_eyelike(input_data) + + """ The following parameterized tests loads the tests that ONNX ships as serialized ONNX files, inputs, and outputs. The goal of this test @@ -4466,3 +4491,4 @@ def test_reverse_sequence(): test_wrong_input() test_aten() test_reverse_sequence() + test_eyelike() From 97b53a160cbee4df27cc426bb117966ec3cc6932 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Wed, 19 May 2021 13:59:06 -0400 Subject: [PATCH 2/7] need to implement k --- python/tvm/relay/frontend/onnx.py | 2 +- tests/python/frontend/onnx/test_forward.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 1911c0c3ff34..9bbf4ab6381a 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -1445,7 +1445,7 @@ class EyeLike(OnnxOpConverter): @classmethod def _impl_v9(cls, inputs, attr, params): - dtype = attr.get("mode", "float32") + dtype = attr.get("dtype", "float32") in_dtype = infer_type(inputs[0]).checked_type.dtype zeros = _op.zeros_like(inputs[0]) dim = infer_shape(zeros)[0] diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index d45607bed765..21bb06af5d1a 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -4232,9 +4232,9 @@ def verify_eyelike(indata): "test_cumsum_2d_negative_axis/", "test_det_2d/", "test_det_nd/", - "test_eyelike_populate_off_main_diagonal/", - "test_eyelike_with_dtype/", - "test_eyelike_without_dtype/", + # "test_eyelike_populate_off_main_diagonal/", + # "test_eyelike_with_dtype/", + # "test_eyelike_without_dtype/", "test_matmulinteger/", "test_maxpool_2d_same_lower/", "test_maxpool_2d_same_upper/", From 0aa7347aaf27493d53492c9f0be305bf8358760b Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Fri, 21 May 2021 17:12:32 -0400 Subject: [PATCH 3/7] test pass --- src/driver/driver_api.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc index f30cecbf7f05..4d93ef6f2f37 100644 --- a/src/driver/driver_api.cc +++ b/src/driver/driver_api.cc @@ -185,6 +185,17 @@ IRModule lower(te::Schedule sch, const Array& args, const std::strin return mod; } +TVM_REGISTER_GLOBAL("driver.lower").set_body_typed([](te::Schedule sch, const Array& args, const String& name, const Map& binds) { + std::unordered_map c_binds; + // Check to make sure binds is not null before doing hte conversion; + if (binds.get() != NULL) { // TODO: figure out why this is not compiling C++ sucks + for (auto kv : binds) { + c_binds.insert(std::pair(kv.first, kv.second)); + } + } + return lower(sch, args, name, c_binds); +}); + std::pair SplitDevHostFuncs(IRModule mod_mixed, const Target& target_arg, const Target& target_host_arg, const transform::PassContext& pass_ctx) { @@ -339,3 +350,4 @@ runtime::Module build(const IRModule& funcs, const Target& target_arg, } } // namespace tvm + From 06f94ea9a954ef0fc2f42f509745b6cbaa6aca2b Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 3 Jun 2021 17:52:27 -0400 Subject: [PATCH 4/7] eyelike tests all pass --- python/tvm/relay/frontend/onnx.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 9bbf4ab6381a..907378eee606 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -1445,13 +1445,20 @@ class EyeLike(OnnxOpConverter): @classmethod def _impl_v9(cls, inputs, attr, params): - dtype = attr.get("dtype", "float32") - in_dtype = infer_type(inputs[0]).checked_type.dtype - zeros = _op.zeros_like(inputs[0]) - dim = infer_shape(zeros)[0] + in_checked_type = infer_type(inputs[0]).checked_type + in_dtype = in_checked_type.dtype + in_shape = list(get_const_tuple(in_checked_type.shape)) + dtype = attr.get("dtype", None) + if dtype == None: + dtype = in_dtype + else: + dtype = get_type(dtype) + zeros = _op.zeros(in_shape, dtype) + dim = in_shape[0] indices = _op.arange(_op.const(0), _op.const(dim), dtype="int32") - ones = _op.full(_op.const(1), (dim,), dtype=in_dtype) - return _op.scatter_nd(zeros, _op.stack([indices, indices], axis=0), ones, "update") + ones = _op.full(_op.const(1), (dim,), dtype=dtype) + k = _op.const(attr.get("k" , 0), dtype= "int32") + return _op.scatter_nd(zeros, _op.stack([indices, indices+k], axis=0), ones, "update") class Greater(OnnxOpConverter): From 5d2a6721b6393171c87188a94dba9fb332bad305 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 3 Jun 2021 20:15:19 -0400 Subject: [PATCH 5/7] Revert "test pass" This reverts commit 0aa7347aaf27493d53492c9f0be305bf8358760b. --- src/driver/driver_api.cc | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/driver/driver_api.cc b/src/driver/driver_api.cc index 4d93ef6f2f37..f30cecbf7f05 100644 --- a/src/driver/driver_api.cc +++ b/src/driver/driver_api.cc @@ -185,17 +185,6 @@ IRModule lower(te::Schedule sch, const Array& args, const std::strin return mod; } -TVM_REGISTER_GLOBAL("driver.lower").set_body_typed([](te::Schedule sch, const Array& args, const String& name, const Map& binds) { - std::unordered_map c_binds; - // Check to make sure binds is not null before doing hte conversion; - if (binds.get() != NULL) { // TODO: figure out why this is not compiling C++ sucks - for (auto kv : binds) { - c_binds.insert(std::pair(kv.first, kv.second)); - } - } - return lower(sch, args, name, c_binds); -}); - std::pair SplitDevHostFuncs(IRModule mod_mixed, const Target& target_arg, const Target& target_host_arg, const transform::PassContext& pass_ctx) { @@ -350,4 +339,3 @@ runtime::Module build(const IRModule& funcs, const Target& target_arg, } } // namespace tvm - From 8e72980213402979d62f9b2fa0b606edd6a126dc Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 3 Jun 2021 20:31:43 -0400 Subject: [PATCH 6/7] removed comments, black'd, lint --- python/tvm/relay/frontend/onnx.py | 4 ++-- tests/python/frontend/onnx/test_forward.py | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index c11d1c3c3744..006728a2c776 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -1492,8 +1492,8 @@ def _impl_v9(cls, inputs, attr, params): dim = in_shape[0] indices = _op.arange(_op.const(0), _op.const(dim), dtype="int32") ones = _op.full(_op.const(1), (dim,), dtype=dtype) - k = _op.const(attr.get("k" , 0), dtype= "int32") - return _op.scatter_nd(zeros, _op.stack([indices, indices+k], axis=0), ones, "update") + k = _op.const(attr.get("k", 0), dtype="int32") + return _op.scatter_nd(zeros, _op.stack([indices, indices + k], axis=0), ones, "update") class Greater(OnnxOpConverter): diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 0d330a2787dd..423f031e49a9 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -4266,9 +4266,6 @@ def verify_eyelike(indata): "test_cumsum_2d_negative_axis/", "test_det_2d/", "test_det_nd/", - # "test_eyelike_populate_off_main_diagonal/", - # "test_eyelike_with_dtype/", - # "test_eyelike_without_dtype/", "test_matmulinteger/", "test_maxpool_2d_same_lower/", "test_maxpool_2d_same_upper/", From 8cc15652d517dd77d5c9394f08339c1cb618c327 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 3 Jun 2021 20:51:01 -0400 Subject: [PATCH 7/7] changed == to is in onnx.py --- python/tvm/relay/frontend/onnx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 006728a2c776..13505fd0f738 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -1484,7 +1484,7 @@ def _impl_v9(cls, inputs, attr, params): in_dtype = in_checked_type.dtype in_shape = list(get_const_tuple(in_checked_type.shape)) dtype = attr.get("dtype", None) - if dtype == None: + if dtype is None: dtype = in_dtype else: dtype = get_type(dtype)