From 7f68ef4d0bddfb0bc755d48d167d57626edfa9ab Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Thu, 25 Mar 2021 21:33:37 +0000 Subject: [PATCH 01/13] move mx2onnx and add deprecation message --- python/mxnet/contrib/onnx/__init__.py | 7 ++- python/mxnet/contrib/onnx/mx2onnx/LICENSE | 44 ------------------- .../onnx/mx2onnx => onnx}/__init__.py | 2 +- .../onnx/mx2onnx => onnx}/_export_helper.py | 0 .../onnx/mx2onnx => onnx}/_op_translations.py | 0 .../onnx/mx2onnx => onnx}/export_model.py | 4 +- .../onnx/mx2onnx => onnx}/export_onnx.py | 2 +- 7 files changed, 10 insertions(+), 49 deletions(-) delete mode 100644 python/mxnet/contrib/onnx/mx2onnx/LICENSE rename python/mxnet/{contrib/onnx/mx2onnx => onnx}/__init__.py (95%) rename python/mxnet/{contrib/onnx/mx2onnx => onnx}/_export_helper.py (100%) rename python/mxnet/{contrib/onnx/mx2onnx => onnx}/_op_translations.py (100%) rename python/mxnet/{contrib/onnx/mx2onnx => onnx}/export_model.py (99%) rename python/mxnet/{contrib/onnx/mx2onnx => onnx}/export_onnx.py (99%) diff --git a/python/mxnet/contrib/onnx/__init__.py b/python/mxnet/contrib/onnx/__init__.py index 9f27060d3d6f..2aa6e2afc2b0 100644 --- a/python/mxnet/contrib/onnx/__init__.py +++ b/python/mxnet/contrib/onnx/__init__.py @@ -18,4 +18,9 @@ from .onnx2mx.import_model import import_model, get_model_metadata from .onnx2mx.import_to_gluon import import_to_gluon -from .mx2onnx.export_model import export_model +from ...onnx.export_model import export_model +print('Calling ONNX module through mxnet.contrib.onnx...') +print('Please be advised that mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon ' + 'will be deprecated in the upcoming MXNet v1.10 release. mxnet.contrib.onnx.export_model ' + 'has been moved to mxnet.onnx.export_model. The mxnet.contrib.onnx.export_model ' + 'alias will also be deprecated in the MXNet v1.10 release.') diff --git a/python/mxnet/contrib/onnx/mx2onnx/LICENSE b/python/mxnet/contrib/onnx/mx2onnx/LICENSE deleted file mode 100644 index 3abe1ee8a8ee..000000000000 --- a/python/mxnet/contrib/onnx/mx2onnx/LICENSE +++ /dev/null @@ -1,44 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# -# Based on -# https://github.com/NVIDIA/mxnet_to_onnx/blob/master/mx2onnx_converter/# -# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# * Neither the name of NVIDIA CORPORATION nor the names of its -# contributors may be used to endorse or promote products derived -# from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/python/mxnet/contrib/onnx/mx2onnx/__init__.py b/python/mxnet/onnx/__init__.py similarity index 95% rename from python/mxnet/contrib/onnx/mx2onnx/__init__.py rename to python/mxnet/onnx/__init__.py index 779ce86621d6..c8f3f35277c7 100644 --- a/python/mxnet/contrib/onnx/mx2onnx/__init__.py +++ b/python/mxnet/onnx/__init__.py @@ -18,6 +18,6 @@ # coding: utf-8 """ONNX Export module""" -from . import export_model +from .export_model import export_model from . import export_onnx from . import _op_translations diff --git a/python/mxnet/contrib/onnx/mx2onnx/_export_helper.py b/python/mxnet/onnx/_export_helper.py similarity index 100% rename from python/mxnet/contrib/onnx/mx2onnx/_export_helper.py rename to python/mxnet/onnx/_export_helper.py diff --git a/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py b/python/mxnet/onnx/_op_translations.py similarity index 100% rename from python/mxnet/contrib/onnx/mx2onnx/_op_translations.py rename to python/mxnet/onnx/_op_translations.py diff --git a/python/mxnet/contrib/onnx/mx2onnx/export_model.py b/python/mxnet/onnx/export_model.py similarity index 99% rename from python/mxnet/contrib/onnx/mx2onnx/export_model.py rename to python/mxnet/onnx/export_model.py index 1c50db56058c..cec72d27ddd1 100644 --- a/python/mxnet/contrib/onnx/mx2onnx/export_model.py +++ b/python/mxnet/onnx/export_model.py @@ -22,8 +22,8 @@ import logging import numpy as np -from ....base import string_types -from .... import symbol +from ..base import string_types +from .. import symbol from .export_onnx import MXNetGraph from ._export_helper import load_module diff --git a/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py b/python/mxnet/onnx/export_onnx.py similarity index 99% rename from python/mxnet/contrib/onnx/mx2onnx/export_onnx.py rename to python/mxnet/onnx/export_onnx.py index 4cec6985a9c7..045c4631b0b5 100644 --- a/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py +++ b/python/mxnet/onnx/export_onnx.py @@ -50,7 +50,7 @@ import logging import json -from .... import ndarray as nd +from .. import ndarray as nd class MXNetGraph(object): From 8c9b062169f3be02ed442a3da9431b331305eade Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Thu, 25 Mar 2021 21:53:32 +0000 Subject: [PATCH 02/13] fix --- python/mxnet/contrib/onnx/__init__.py | 43 ++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/python/mxnet/contrib/onnx/__init__.py b/python/mxnet/contrib/onnx/__init__.py index 2aa6e2afc2b0..fc1251ac629d 100644 --- a/python/mxnet/contrib/onnx/__init__.py +++ b/python/mxnet/contrib/onnx/__init__.py @@ -16,11 +16,38 @@ # under the License. """Module for ONNX model format support for Apache MXNet.""" -from .onnx2mx.import_model import import_model, get_model_metadata -from .onnx2mx.import_to_gluon import import_to_gluon -from ...onnx.export_model import export_model -print('Calling ONNX module through mxnet.contrib.onnx...') -print('Please be advised that mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon ' - 'will be deprecated in the upcoming MXNet v1.10 release. mxnet.contrib.onnx.export_model ' - 'has been moved to mxnet.onnx.export_model. The mxnet.contrib.onnx.export_model ' - 'alias will also be deprecated in the MXNet v1.10 release.') +from .onnx2mx.import_model import import_model as import_model_ +from .onnx2mx.import_model import get_model_metadata as get_model_metadata_ +from .onnx2mx.import_to_gluon import import_to_gluon as import_to_gluon_ +from ...onnx.export_model import export_model as export_model_ + +def import_model(*args, **kwargs): + print('Calling mxnet.contrib.onnx.import_model...') + print('Please be advised that importing ONNX models into MXNet is going to be deprecated ' + 'in the upcoming MXNet v1.10 release. The following apis will be deleted: ' + 'mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon.') + import_model_(*args, **kwargs) + + +def get_model_metadata(*args, **kwargs): + print('Calling mxnet.contrib.onnx.get_model_metadata...') + print('Please be advised that importing ONNX models into MXNet is going to be deprecated ' + 'in the upcoming MXNet v1.10 release. The following apis will be deleted: ' + 'mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon.') + get_model_metadata_(*args, **kwargs) + + +def import_to_gluon(*args, **kwargs): + print('Calling mxnet.contrib.onnx.import_to_gluon...') + print('Please be advised that importing ONNX models into MXNet is going to be deprecated ' + 'in the upcoming MXNet v1.10 release. The following apis will be deleted: ' + 'mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon.') + import_to_gluon_(*args, **kwargs) + + +def export_model(*args, **kwargs): + print('Calling mxnet.contrib.onnx.export_model...') + print('Please be advised that the ONNX module has been moved to mxnet.onnx and ' + 'mxnet.onnx.export_model is the preferred path. The current path will be deprecated ' + 'in the upcoming MXNet v1.10 release.') + export_model_(*args, **kwargs) From 1c2c33cbf65e6598de02a9d0192d6832abc3e881 Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Thu, 25 Mar 2021 22:08:22 +0000 Subject: [PATCH 03/13] change test import path --- tests/python-pytest/onnx/backend.py | 2 +- tests/python-pytest/onnx/test_onnxruntime.py | 56 ++++++++++---------- tests/python-pytest/onnx/test_operators.py | 4 +- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/tests/python-pytest/onnx/backend.py b/tests/python-pytest/onnx/backend.py index 6d8b1af6baff..d294b93b3cff 100644 --- a/tests/python-pytest/onnx/backend.py +++ b/tests/python-pytest/onnx/backend.py @@ -19,7 +19,7 @@ """MXNet/Gluon backend wrapper for onnx test infrastructure""" from mxnet.contrib.onnx.onnx2mx.import_onnx import GraphProto -from mxnet.contrib.onnx.mx2onnx.export_onnx import MXNetGraph +from mxnet.onnx.export_onnx import MXNetGraph import mxnet as mx import numpy as np diff --git a/tests/python-pytest/onnx/test_onnxruntime.py b/tests/python-pytest/onnx/test_onnxruntime.py index 6ad0794f875d..535bc5e0aeec 100644 --- a/tests/python-pytest/onnx/test_onnxruntime.py +++ b/tests/python-pytest/onnx/test_onnxruntime.py @@ -51,15 +51,15 @@ def export(self): def export_onnx(self): onnx_file = self.modelpath + ".onnx" - mx.contrib.onnx.export_model(self.modelpath + "-symbol.json", self.modelpath + "-0000.params", - [self.input_shape], self.input_dtype, onnx_file) + mx.onnx.export_model(self.modelpath + "-symbol.json", self.modelpath + "-0000.params", + [self.input_shape], self.input_dtype, onnx_file) return onnx_file def export_onnx_dynamic(self, dynamic_input_shapes): onnx_file = self.modelpath + ".onnx" - mx.contrib.onnx.export_model(self.modelpath + "-symbol.json", self.modelpath + "-0000.params", - [self.input_shape], self.input_dtype, onnx_file, dynamic=True, - dynamic_input_shapes=dynamic_input_shapes) + mx.onnx.export_model(self.modelpath + "-symbol.json", self.modelpath + "-0000.params", + [self.input_shape], self.input_dtype, onnx_file, dynamic=True, + dynamic_input_shapes=dynamic_input_shapes) return onnx_file def predict(self, data): @@ -103,7 +103,7 @@ def test_bert_inference_onnxruntime(tmp_path, model): input_shapes = [(batch, seq_length), (batch, seq_length), (batch,)] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, np.float32, onnx_file) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, np.float32, onnx_file) # create onnxruntime session using the generated onnx file @@ -598,9 +598,9 @@ def test_roberta_inference_onnxruntime(tmp_path, model_name): params_file = "%s-0000.params" % prefix onnx_file = "%s.onnx" % prefix input_shapes = [(batch, seq_length), (batch,), (batch, num_masked_positions)] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, - [np.float32, np.float32, np.int32], - onnx_file, verbose=True) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, + [np.float32, np.float32, np.int32], + onnx_file, verbose=True) sess_options = onnxruntime.SessionOptions() sess_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL @@ -654,7 +654,7 @@ def test_bert_inference_onnxruntime(tmp_path, model): input_shapes = [(batch, seq_length), (batch, seq_length), (batch,)] input_types = [np.float32, np.float32, np.float32] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, input_types, onnx_file) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, input_types, onnx_file) # create onnxruntime session using the generated onnx file @@ -703,9 +703,9 @@ def test_distilbert_inference_onnxruntime(tmp_path, model_name): onnx_file = "%s.onnx" % prefix input_shapes = [(batch, seq_length), (batch,)] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, - [np.float32, np.float32], - onnx_file, verbose=True) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, + [np.float32, np.float32], + onnx_file, verbose=True) sess_options = onnxruntime.SessionOptions() sess_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL sess = onnxruntime.InferenceSession(onnx_file, sess_options) @@ -753,9 +753,9 @@ def test_standard_rnn_lstm_pretrained_inference_onnxruntime(tmp_path, model_name onnx_file = "%s.onnx" % prefix input_shapes = [(seq_length, batch), np.shape(begin_state[0]), np.shape(begin_state[1])] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, - [np.float32, np.float32, np.float32], - onnx_file, verbose=True) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, + [np.float32, np.float32, np.float32], + onnx_file, verbose=True) sess_options = onnxruntime.SessionOptions() sess_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL sess = onnxruntime.InferenceSession(onnx_file, sess_options) @@ -839,10 +839,10 @@ def test_dynamic_shape_bert_inference_onnxruntime(tmp_path, model): dynamic_input_shapes = [(None, seq_length), (None, seq_length), (None,)] input_shapes = [(batch, seq_length), (batch, seq_length), (batch,)] input_types = [np.float32, np.float32, np.float32] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, - input_types, onnx_file, - dynamic=True, - dynamic_input_shapes=dynamic_input_shapes) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, + input_types, onnx_file, + dynamic=True, + dynamic_input_shapes=dynamic_input_shapes) # create onnxruntime session using the generated onnx file ses_opt = onnxruntime.SessionOptions() @@ -906,8 +906,8 @@ def test_awd_rnn_lstm_pretrained_inference_onnxruntime(tmp_path, model_name, seq np.shape(begin_state[2][0]), np.shape(begin_state[2][1])] input_types = [np.float32, np.float32, np.float32, np.float32, np.float32, np.float32, np.float32] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, - input_types, onnx_file, verbose=True) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, + input_types, onnx_file, verbose=True) sess_options = onnxruntime.SessionOptions() sess_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL @@ -969,8 +969,8 @@ def test_ernie_inference_onnxruntime(tmp_path, model_name): input_shapes = [(batch, seq_length), (batch, seq_length), (batch,)] input_types = [np.float32, np.float32, np.float32] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, - input_types, onnx_file) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, + input_types, onnx_file) # create onnxruntime session using the generated onnx file ses_opt = onnxruntime.SessionOptions() @@ -1036,8 +1036,8 @@ def export_to_onnx(prefix, input_shapes, input_types, **kwargs): sym_file = "%s-symbol.json" % prefix params_file = "%s-0000.params" % prefix onnx_file = "%s.onnx" % prefix - return mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, input_types, - onnx_file, **kwargs) + return mx.onnx.export_model(sym_file, params_file, input_shapes, input_types, + onnx_file, **kwargs) def onnx_runtime_predict(onnx_file, onnx_inputs): ses_opt = onnxruntime.SessionOptions() @@ -1211,8 +1211,8 @@ def test_gpt_pretrained_inference_onnxruntime(tmp_path, model_params): input_shapes = [(batch, seq_length)] input_types = [np.float32] - converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, - input_types, onnx_file) + converted_model_path = mx.onnx.export_model(sym_file, params_file, input_shapes, + input_types, onnx_file) ses_opt = onnxruntime.SessionOptions() ses_opt.log_severity_level = 3 diff --git a/tests/python-pytest/onnx/test_operators.py b/tests/python-pytest/onnx/test_operators.py index 520ac407e49b..adfba25a1a4a 100644 --- a/tests/python-pytest/onnx/test_operators.py +++ b/tests/python-pytest/onnx/test_operators.py @@ -46,8 +46,8 @@ def export_to_onnx(model, model_name, inputs): sym_file = '{}-symbol.json'.format(model_path) params_file = '{}-0000.params'.format(model_path) onnx_file = '{}/{}.onnx'.format(tmp_path, model_name) - mx.contrib.onnx.export_model(sym_file, params_file, [inp.shape for inp in inputs], - [inp.dtype for inp in inputs], onnx_file) + mx.onnx.export_model(sym_file, params_file, [inp.shape for inp in inputs], + [inp.dtype for inp in inputs], onnx_file) return onnx_file def onnx_rt(onnx_file, inputs): From 62ec944d1a0d859a34e0828b3a99237e7a6fc593 Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Mon, 29 Mar 2021 22:01:39 +0000 Subject: [PATCH 04/13] ad license --- python/mxnet/onnx/LICENSE | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 python/mxnet/onnx/LICENSE diff --git a/python/mxnet/onnx/LICENSE b/python/mxnet/onnx/LICENSE new file mode 100644 index 000000000000..3abe1ee8a8ee --- /dev/null +++ b/python/mxnet/onnx/LICENSE @@ -0,0 +1,44 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Based on +# https://github.com/NVIDIA/mxnet_to_onnx/blob/master/mx2onnx_converter/# +# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of NVIDIA CORPORATION nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. From 55e6dc3959029371e8ee36e10fd54146f99cc2e2 Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Mon, 29 Mar 2021 23:45:45 +0000 Subject: [PATCH 05/13] refactory and add setup.py --- python/mxnet/contrib/onnx/__init__.py | 2 +- python/mxnet/onnx/__init__.py | 4 +--- python/mxnet/onnx/{ => mx2onnx}/LICENSE | 0 python/mxnet/onnx/mx2onnx/__init__.py | 22 ++++++++++++++++++ .../onnx/{ => mx2onnx}/_export_helper.py | 0 .../_export_model.py} | 6 ++--- .../_export_onnx.py} | 2 +- .../onnx/{ => mx2onnx}/_op_translations.py | 2 +- python/mxnet/onnx/setup.py | 23 +++++++++++++++++++ 9 files changed, 52 insertions(+), 9 deletions(-) rename python/mxnet/onnx/{ => mx2onnx}/LICENSE (100%) create mode 100644 python/mxnet/onnx/mx2onnx/__init__.py rename python/mxnet/onnx/{ => mx2onnx}/_export_helper.py (100%) rename python/mxnet/onnx/{export_model.py => mx2onnx/_export_model.py} (98%) rename python/mxnet/onnx/{export_onnx.py => mx2onnx/_export_onnx.py} (99%) rename python/mxnet/onnx/{ => mx2onnx}/_op_translations.py (99%) create mode 100644 python/mxnet/onnx/setup.py diff --git a/python/mxnet/contrib/onnx/__init__.py b/python/mxnet/contrib/onnx/__init__.py index fc1251ac629d..50ccbc510d89 100644 --- a/python/mxnet/contrib/onnx/__init__.py +++ b/python/mxnet/contrib/onnx/__init__.py @@ -19,7 +19,7 @@ from .onnx2mx.import_model import import_model as import_model_ from .onnx2mx.import_model import get_model_metadata as get_model_metadata_ from .onnx2mx.import_to_gluon import import_to_gluon as import_to_gluon_ -from ...onnx.export_model import export_model as export_model_ +from ...onnx import export_model as export_model_ def import_model(*args, **kwargs): print('Calling mxnet.contrib.onnx.import_model...') diff --git a/python/mxnet/onnx/__init__.py b/python/mxnet/onnx/__init__.py index c8f3f35277c7..3caab21a6202 100644 --- a/python/mxnet/onnx/__init__.py +++ b/python/mxnet/onnx/__init__.py @@ -18,6 +18,4 @@ # coding: utf-8 """ONNX Export module""" -from .export_model import export_model -from . import export_onnx -from . import _op_translations +from .mx2onnx import export_model diff --git a/python/mxnet/onnx/LICENSE b/python/mxnet/onnx/mx2onnx/LICENSE similarity index 100% rename from python/mxnet/onnx/LICENSE rename to python/mxnet/onnx/mx2onnx/LICENSE diff --git a/python/mxnet/onnx/mx2onnx/__init__.py b/python/mxnet/onnx/mx2onnx/__init__.py new file mode 100644 index 000000000000..d8a6d5a50a31 --- /dev/null +++ b/python/mxnet/onnx/mx2onnx/__init__.py @@ -0,0 +1,22 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# coding: utf-8 +"""ONNX Export module""" + +from ._export_model import export_model +from . import _op_translations diff --git a/python/mxnet/onnx/_export_helper.py b/python/mxnet/onnx/mx2onnx/_export_helper.py similarity index 100% rename from python/mxnet/onnx/_export_helper.py rename to python/mxnet/onnx/mx2onnx/_export_helper.py diff --git a/python/mxnet/onnx/export_model.py b/python/mxnet/onnx/mx2onnx/_export_model.py similarity index 98% rename from python/mxnet/onnx/export_model.py rename to python/mxnet/onnx/mx2onnx/_export_model.py index cec72d27ddd1..d9be998ed24f 100644 --- a/python/mxnet/onnx/export_model.py +++ b/python/mxnet/onnx/mx2onnx/_export_model.py @@ -22,9 +22,9 @@ import logging import numpy as np -from ..base import string_types -from .. import symbol -from .export_onnx import MXNetGraph +from mxnet.base import string_types +from mxnet import symbol +from ._export_onnx import MXNetGraph from ._export_helper import load_module diff --git a/python/mxnet/onnx/export_onnx.py b/python/mxnet/onnx/mx2onnx/_export_onnx.py similarity index 99% rename from python/mxnet/onnx/export_onnx.py rename to python/mxnet/onnx/mx2onnx/_export_onnx.py index 045c4631b0b5..903b0cd1c51f 100644 --- a/python/mxnet/onnx/export_onnx.py +++ b/python/mxnet/onnx/mx2onnx/_export_onnx.py @@ -50,7 +50,7 @@ import logging import json -from .. import ndarray as nd +from mxnet import ndarray as nd class MXNetGraph(object): diff --git a/python/mxnet/onnx/_op_translations.py b/python/mxnet/onnx/mx2onnx/_op_translations.py similarity index 99% rename from python/mxnet/onnx/_op_translations.py rename to python/mxnet/onnx/mx2onnx/_op_translations.py index 22e6282b43a4..e1feb452e453 100644 --- a/python/mxnet/onnx/_op_translations.py +++ b/python/mxnet/onnx/mx2onnx/_op_translations.py @@ -56,7 +56,7 @@ import re import logging import numpy as np -from .export_onnx import MXNetGraph as mx_op +from ._export_onnx import MXNetGraph as mx_op try: import onnx except ImportError: diff --git a/python/mxnet/onnx/setup.py b/python/mxnet/onnx/setup.py new file mode 100644 index 000000000000..89a706010788 --- /dev/null +++ b/python/mxnet/onnx/setup.py @@ -0,0 +1,23 @@ +from setuptools import setup, find_packages + +setup( + name='mx2onnx', + version='0.0.0', + description='Module to convert MXNet models to the ONNX format', + author='', + author_email='', + url='https://github.com/apache/incubator-mxnet/tree/v1.x/python/mxnet/onnx', + install_requires=[ + 'onnx', + 'onnxoptimizer >= 0.2.4', + 'onnxruntime >= 1.6.0', + 'protobuf >= 3.7.0' + ], + classifiers=[ + 'Intended Audience :: Developers', + 'License :: OSI Approved :: Apache Software License', + 'Programming Language :: Python :: 3 :: Only', + ], + packages=find_packages(), + python_requires='>=3.6' +) From 007dee0148f42947eb760d158eb7e2f72c5dc22e Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Mon, 29 Mar 2021 23:47:34 +0000 Subject: [PATCH 06/13] fix dependency --- python/mxnet/onnx/setup.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/python/mxnet/onnx/setup.py b/python/mxnet/onnx/setup.py index 89a706010788..884f1a3841d0 100644 --- a/python/mxnet/onnx/setup.py +++ b/python/mxnet/onnx/setup.py @@ -8,10 +8,7 @@ author_email='', url='https://github.com/apache/incubator-mxnet/tree/v1.x/python/mxnet/onnx', install_requires=[ - 'onnx', - 'onnxoptimizer >= 0.2.4', - 'onnxruntime >= 1.6.0', - 'protobuf >= 3.7.0' + 'onnx >= 1.7.0', ], classifiers=[ 'Intended Audience :: Developers', From fc5d8d73031cf861555ca4bcad4cc5cb9870bdf5 Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Mon, 29 Mar 2021 23:58:43 +0000 Subject: [PATCH 07/13] add license to setup.py --- python/mxnet/onnx/setup.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/python/mxnet/onnx/setup.py b/python/mxnet/onnx/setup.py index 884f1a3841d0..bbba04d4b4f6 100644 --- a/python/mxnet/onnx/setup.py +++ b/python/mxnet/onnx/setup.py @@ -1,3 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + from setuptools import setup, find_packages setup( From b7916595452975ddab5275415e6421e89012bdc6 Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Wed, 31 Mar 2021 20:05:15 +0000 Subject: [PATCH 08/13] white-list for header check --- tools/license_header.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/license_header.py b/tools/license_header.py index a745ea20fc7d..5480f9f679c4 100755 --- a/tools/license_header.py +++ b/tools/license_header.py @@ -122,6 +122,10 @@ # This file 'tools/license_header.py', + # Dual-Licensed under Apache 2.0 and Nvidia BSD-3 + 'python/mxnet/onnx/mx2onnx/_export_onnx.py' + 'python/mxnet/onnx/mx2onnx/_op_translations.py' + # Github template '.github/ISSUE_TEMPLATE/bug_report.md', '.github/ISSUE_TEMPLATE/feature_request.md', From dbfdad7a59ff615d86bf77bf7893ef9dc8a77f5c Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Wed, 31 Mar 2021 20:12:57 +0000 Subject: [PATCH 09/13] white-list fix --- tools/license_header.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/license_header.py b/tools/license_header.py index 5480f9f679c4..71b28115b3e5 100755 --- a/tools/license_header.py +++ b/tools/license_header.py @@ -123,8 +123,8 @@ 'tools/license_header.py', # Dual-Licensed under Apache 2.0 and Nvidia BSD-3 - 'python/mxnet/onnx/mx2onnx/_export_onnx.py' - 'python/mxnet/onnx/mx2onnx/_op_translations.py' + 'python/mxnet/onnx/mx2onnx/_export_onnx.py', + 'python/mxnet/onnx/mx2onnx/_op_translations.py', # Github template '.github/ISSUE_TEMPLATE/bug_report.md', From 7411a2f1c19a25d5949892960094e326b272227b Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Wed, 31 Mar 2021 20:34:09 +0000 Subject: [PATCH 10/13] add readme --- python/mxnet/onnx/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 python/mxnet/onnx/README.md diff --git a/python/mxnet/onnx/README.md b/python/mxnet/onnx/README.md new file mode 100644 index 000000000000..08f9468b3c2c --- /dev/null +++ b/python/mxnet/onnx/README.md @@ -0,0 +1,21 @@ +# ONNX Export Support for MXNet + +### Overview +[ONNX](https://onnx.ai/), or Open Neural Network Exchange, is an open source deep learning model format that acts as a framework neutral graph representation between DL frameworks or between training and inference. With the ability to export models to the ONNX format, MXNet users can enjoy faster inference and a wider range of deployment device choices, including edge and mobile devices where MXNet installation may be constrained. Popular hardware-accelerated and/or cross-platform ONNX runtime frameworks include Nvidia [TensorRT](https://github.com/onnx/onnx-tensorrt), Microsoft [ONNXRuntime](https://github.com/microsoft/onnxruntime), Apple [CoreML](https://github.com/onnx/onnx-coreml) and [TVM](https://tvm.apache.org/docs/tutorials/frontend/from_onnx.html), etc. + +### ONNX Versions Supported +ONNX 1.7 -- Fully Supported +ONNX 1.8 -- Work in Progress + +### Installation +From the 1.9 release and on, the ONNX export module has become an offical, built-in module in MXNet. You can access the module at `mxnet.onnx`. + +If you are a user of earlier MXNet versions and do not want to upgrade MXNet, you can still enjoy the latest ONNX suppor by pulling the MXNet source code and building the wheel for only the mx2onnx module. Just do `cd python/mxnet/onnx` and then build the wheel with `python3 -m build`. You should be able to find the wheel under `python/mxnet/onnx/dist/mx2onnx-0.0.0-py3-none-any.whl` and install it with `pip install mx2onnx-0.0.0-py3-none-any.whl`. You should be able to access the module with `import mx2onnx` then. + +### APIs + +### Operator Support Matrix - ONNX 1.7 + +### GluonCV Pretrained Model Support Matrix + +### GluonNLP Pretrained Model Support Matrix From 6a7635b5ac3ed9822091995a2717e4d493caf9e3 Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Wed, 31 Mar 2021 20:48:08 +0000 Subject: [PATCH 11/13] fix sanity --- python/mxnet/contrib/onnx/__init__.py | 2 +- python/mxnet/onnx/README.md | 16 ++++++++++++++++ python/mxnet/onnx/setup.py | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/python/mxnet/contrib/onnx/__init__.py b/python/mxnet/contrib/onnx/__init__.py index 50ccbc510d89..85127ccdf5ea 100644 --- a/python/mxnet/contrib/onnx/__init__.py +++ b/python/mxnet/contrib/onnx/__init__.py @@ -33,7 +33,7 @@ def get_model_metadata(*args, **kwargs): print('Calling mxnet.contrib.onnx.get_model_metadata...') print('Please be advised that importing ONNX models into MXNet is going to be deprecated ' 'in the upcoming MXNet v1.10 release. The following apis will be deleted: ' - 'mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon.') + 'mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon.') get_model_metadata_(*args, **kwargs) diff --git a/python/mxnet/onnx/README.md b/python/mxnet/onnx/README.md index 08f9468b3c2c..e46a3328b57a 100644 --- a/python/mxnet/onnx/README.md +++ b/python/mxnet/onnx/README.md @@ -1,3 +1,19 @@ + + + + + + + + + + + + + + + + # ONNX Export Support for MXNet ### Overview diff --git a/python/mxnet/onnx/setup.py b/python/mxnet/onnx/setup.py index bbba04d4b4f6..ed2a85d9ea50 100644 --- a/python/mxnet/onnx/setup.py +++ b/python/mxnet/onnx/setup.py @@ -15,6 +15,8 @@ # specific language governing permissions and limitations # under the License. +# pylint: disable=missing-module-docstring + from setuptools import setup, find_packages setup( From 7ce3101ce394b7a0cd11165ff3f1f4fdad1ce7cc Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Wed, 31 Mar 2021 21:12:11 +0000 Subject: [PATCH 12/13] fix sanity --- python/mxnet/onnx/setup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/mxnet/onnx/setup.py b/python/mxnet/onnx/setup.py index ed2a85d9ea50..d0ef2332d32f 100644 --- a/python/mxnet/onnx/setup.py +++ b/python/mxnet/onnx/setup.py @@ -15,7 +15,9 @@ # specific language governing permissions and limitations # under the License. -# pylint: disable=missing-module-docstring +""" +setup.py for mx2onnx +""" from setuptools import setup, find_packages From 6c4e38d88ddceb45f559e34eac8a818e31b99db3 Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Thu, 1 Apr 2021 00:34:37 +0000 Subject: [PATCH 13/13] fix return --- python/mxnet/contrib/onnx/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/mxnet/contrib/onnx/__init__.py b/python/mxnet/contrib/onnx/__init__.py index 85127ccdf5ea..30ac62bb1300 100644 --- a/python/mxnet/contrib/onnx/__init__.py +++ b/python/mxnet/contrib/onnx/__init__.py @@ -26,7 +26,7 @@ def import_model(*args, **kwargs): print('Please be advised that importing ONNX models into MXNet is going to be deprecated ' 'in the upcoming MXNet v1.10 release. The following apis will be deleted: ' 'mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon.') - import_model_(*args, **kwargs) + return import_model_(*args, **kwargs) def get_model_metadata(*args, **kwargs): @@ -34,7 +34,7 @@ def get_model_metadata(*args, **kwargs): print('Please be advised that importing ONNX models into MXNet is going to be deprecated ' 'in the upcoming MXNet v1.10 release. The following apis will be deleted: ' 'mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon.') - get_model_metadata_(*args, **kwargs) + return get_model_metadata_(*args, **kwargs) def import_to_gluon(*args, **kwargs): @@ -42,7 +42,7 @@ def import_to_gluon(*args, **kwargs): print('Please be advised that importing ONNX models into MXNet is going to be deprecated ' 'in the upcoming MXNet v1.10 release. The following apis will be deleted: ' 'mxnet.contrib.onnx.import_model/get_model_metadata/import_to_gluon.') - import_to_gluon_(*args, **kwargs) + return import_to_gluon_(*args, **kwargs) def export_model(*args, **kwargs): @@ -50,4 +50,4 @@ def export_model(*args, **kwargs): print('Please be advised that the ONNX module has been moved to mxnet.onnx and ' 'mxnet.onnx.export_model is the preferred path. The current path will be deprecated ' 'in the upcoming MXNet v1.10 release.') - export_model_(*args, **kwargs) + return export_model_(*args, **kwargs)