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

[v1.x] Onnx Reshpe support for special caes #19804

Merged
merged 3 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,7 @@ def convert_floor(node, **kwargs):
"""
return create_basic_op_node('Floor', node, kwargs)


# Changing shape and type.
@mx_op.register("Reshape")
def convert_reshape(node, **kwargs):
Expand All @@ -1612,6 +1613,17 @@ def convert_reshape(node, **kwargs):
reverse = attrs.get('reverse', 'False')
targ_shape = convert_string_to_list(attrs["shape"])

# In general -2, -3, -4 in the target shape are not supoorted, but there are
# a few special cases that we can convert to supported scenarios

# If -2 and -4 are not used, then we can just remove the -4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be -2 and -3 are not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! fixed

if -4 in targ_shape and -3 not in targ_shape and -2 not in targ_shape:
targ_shape = [i for i in targ_shape if i != -4]

if targ_shape == [-3, 0]:
targ_shape = [-1, 0]
reverse = 'True'

not_supported_shape = [-2, -3, -4]
for val in targ_shape:
if val in not_supported_shape:
Expand Down
17 changes: 17 additions & 0 deletions tests/python-pytest/onnx/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ def test_onnx_export_reshape(tmp_path, dtype):
op_export_test('reshape_3', M3, [x], tmp_path)


@pytest.mark.parametrize('dtype', ['float32', 'float64', 'int32', 'int64'])
def test_onnx_export_reshape_special_cases(tmp_path, dtype):
x1 = mx.nd.ones((8, 9), dtype=dtype)
M1 = def_model('reshape', shape=(0, -4, 1, -1))
op_export_test('reshape_spec_1', M1, [x1], tmp_path)

x2 = mx.nd.ones((8, 9, 10), dtype=dtype)

M2 = def_model('reshape', shape=(0, -4, 3, -1, 10))
op_export_test('reshape_spec_2', M2, [x2], tmp_path)
M3 = def_model('reshape', shape=(-4, 2, -1, 10, 9))
op_export_test('reshape_spec_3', M3, [x2], tmp_path)

M4 = def_model('reshape', shape=(-3, 0))
op_export_test('reshape_spec_4', M4, [x2], tmp_path)


@pytest.mark.parametrize('dtype', ['int32', 'int64'])
def test_onnx_export_embedding(tmp_path, dtype):
x = mx.nd.array([[ 1., 3.],
Expand Down