Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[v1.x] More onnx export updates #19692

Merged
merged 5 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/docker/runtime_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ integrationtest_ubuntu_cpu_onnx() {
export PYTHONPATH=./python/
export MXNET_SUBGRAPH_VERBOSE=0
export DMLC_LOG_STACK_TRACE_DEPTH=10
tests/python-pytest/onnx/backend_test.py
#tests/python-pytest/onnx/backend_test.py
COV_ARG="--cov=./ --cov-report=xml --cov-append"
pytest $COV_ARG --verbose tests/python-pytest/onnx/mxnet_export_test.py
pytest $COV_ARG --verbose tests/python-pytest/onnx/test_models.py
Expand Down
19 changes: 14 additions & 5 deletions python/mxnet/contrib/onnx/mx2onnx/export_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def split_params(sym, params):
return arg_params, aux_params

@staticmethod
def get_outputs(sym, params, in_shape, in_label):
def get_outputs(sym, params, in_shape, in_label, in_type):
""" Infer output shapes and return dictionary of output name to shape

:param :class:`~mxnet.symbol.Symbol` sym: symbol to perform infer shape on
Expand All @@ -127,6 +127,7 @@ def get_outputs(sym, params, in_shape, in_label):
:return: dictionary of output name to shape
:rtype: dict of (str, tuple(int, ...))
"""
from onnx import mapping
# remove any input listed in params from sym.list_inputs() and bind them to the input shapes provided
# by user. Also remove in_label, which is the name of the label symbol that may have been used
# as the label for loss during training.
Expand All @@ -146,8 +147,16 @@ def get_outputs(sym, params, in_shape, in_label):
out_names.append(name)

assert len(out_shapes) == len(out_names)

# infer output types
args = {n: mapping.TENSOR_TYPE_TO_NP_TYPE[in_type] for n in sym.list_inputs()}
_, out_type, _ = sym.infer_type(**args)
out_types = [mapping.NP_TYPE_TO_TENSOR_TYPE[o(0).dtype] for o in out_type]

assert len(out_types) == len(out_names)

# bind output shapes with output names
graph_outputs = {n: s for n, s in zip(out_names, out_shapes)}
graph_outputs = {n: {'shape': s, 'dtype': d} for n, s, d in zip(out_names, out_shapes, out_types)}

return graph_outputs

Expand Down Expand Up @@ -210,7 +219,7 @@ def create_onnx_graph_proto(self, sym, params, in_shape, in_type, verbose=False,
index_lookup = []

# Determine output shape
graph_outputs = MXNetGraph.get_outputs(sym, params, in_shape, output_label)
graph_outputs = MXNetGraph.get_outputs(sym, params, in_shape, output_label, in_type)

graph_input_idx = 0
for idx, node in enumerate(mx_graph):
Expand Down Expand Up @@ -273,8 +282,8 @@ def create_onnx_graph_proto(self, sym, params, in_shape, in_type, verbose=False,
onnx_processed_outputs.append(
make_tensor_value_info(
name=nodename,
elem_type=in_type,
shape=graph_outputs[nodename]
elem_type=graph_outputs[nodename]['dtype'],
shape=graph_outputs[nodename]['shape']
)
)
if verbose:
Expand Down
9 changes: 9 additions & 0 deletions tests/python-pytest/onnx/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,12 @@ def test_onnx_export_dropout(tmp_path, dtype, p):
M = def_model('Dropout', p=p)
x = mx.nd.array([[3,0.5,-0.5,2,7],[2,-0.4,7,3,0.2]], dtype=dtype)
op_export_test('Dropout', M, [x], tmp_path)


@pytest.mark.parametrize('src_dtype', ['float16', 'float32', 'float64'])
@pytest.mark.parametrize('dst_dtype', ['bool', 'float16', 'float32', 'float64', 'int32', 'int64', 'int8', 'uint8'])
@pytest.mark.parametrize('shape', [(2,3), (4,5,6)])
def test_onnx_export_cast(tmp_path, src_dtype, dst_dtype, shape):
M = def_model('Cast', dtype=dst_dtype)
x = mx.nd.ones(shape, dtype=src_dtype)
op_export_test('Cast', M, [x], tmp_path)