Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ONNX] fix reduce crash on scalar inputs #10780

Merged
merged 5 commits into from
Mar 25, 2022
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
18 changes: 18 additions & 0 deletions python/tvm/relay/frontend/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,9 @@ def run_calculation(cls, inputs, axis, keepdims):

@classmethod
def _impl_v1(cls, inputs, attr, params):
if not infer_shape(inputs[0]): # promote scalar to 1-D tensor
inputs[0] = _op.expand_dims(inputs[0], axis=0)

if "axes" in attr:
axis = attr.get("axes", 0)
else:
Expand All @@ -1885,6 +1888,9 @@ def _impl_v1(cls, inputs, attr, params):

@classmethod
def _impl_v12(cls, inputs, attr, params):
if not infer_shape(inputs[0]): # promote scalar to 1-D tensor
inputs[0] = _op.expand_dims(inputs[0], axis=0)

if len(inputs) == 2:
if isinstance(inputs[1], _expr.Constant):
# Get axis and unpack scalar
Expand Down Expand Up @@ -1937,6 +1943,9 @@ class ReduceSumSquare(OnnxOpConverter):

@classmethod
def _impl_v1(cls, inputs, attr, params):
if not infer_shape(inputs[0]): # promote scalar to 1-D tensor
inputs[0] = _op.expand_dims(inputs[0], axis=0)

if "axes" in attr:
axis = attr.get("axes", 0)
else:
Expand All @@ -1953,6 +1962,9 @@ class ReduceL1(OnnxOpConverter):

@classmethod
def _impl_v1(cls, inputs, attr, params):
if not infer_shape(inputs[0]): # promote scalar to 1-D tensor
inputs[0] = _op.expand_dims(inputs[0], axis=0)

if "axes" in attr:
axis = attr.get("axes", 0)
else:
Expand All @@ -1969,6 +1981,9 @@ class ReduceL2(OnnxOpConverter):

@classmethod
def _impl_v1(cls, inputs, attr, params):
if not infer_shape(inputs[0]): # promote scalar to 1-D tensor
inputs[0] = _op.expand_dims(inputs[0], axis=0)

if "axes" in attr:
axis = attr.get("axes", 0)
else:
Expand All @@ -1986,6 +2001,9 @@ class ReduceLogSum(OnnxOpConverter):

@classmethod
def _impl_v1(cls, inputs, attr, params):
if not infer_shape(inputs[0]): # promote scalar to 1-D tensor
inputs[0] = _op.expand_dims(inputs[0], axis=0)

if "axes" in attr:
axis = attr.get("axes", 0)
else:
Expand Down
2 changes: 2 additions & 0 deletions tests/python/frontend/onnx/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -1934,6 +1934,8 @@ def verify_reduce_func(func, data, axis, keepdims):
]

for func in funcs:
verify_reduce_func(func, np.array(1.0).astype(np.float32), axis=None, keepdims=False)

for keepdims in [True, False]:
verify_reduce_func(
func, np.random.randn(3, 2, 2).astype(np.float32), axis=None, keepdims=keepdims
Expand Down