Skip to content

Commit

Permalink
[ONNX] Add ReduceSum opset13 support (non-dynamic) (#11606)
Browse files Browse the repository at this point in the history
* [ONNX] Add ReduceSum opset13 support (non-dynamic)

* Add check

* Add support for constant axis

* noop

* Rework logic
  • Loading branch information
sfvaroglu authored Jun 8, 2022
1 parent 97e681d commit df4f4c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 26 additions & 0 deletions python/tvm/relay/frontend/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,32 @@ def _impl_v12(cls, inputs, attr, params):

return cls._impl_v1(inputs, attr, params)

@classmethod
def _impl_v13(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)

noop_with_empty_axes = attr.get("noop_with_empty_axes", 0)
num_axis = int(infer_type(inputs[1]).checked_type.shape[0]) if inputs[1] is not None else 0

if noop_with_empty_axes and num_axis == 0:
return inputs[0]

if len(inputs) == 2:
if isinstance(inputs[1], _expr.Constant):
# Get axis and unpack scalar
constant_axis = int(inputs[1].data.numpy()[0])
return cls.run_calculation([inputs[0]], constant_axis, attr.get("keepdims", True))

if num_axis > 0:
raise ValueError("Dynamic Reduce is not supported yet!")

axis_len = len(infer_shape(inputs[0]))
axis = list(range(axis_len))
return cls.run_calculation([inputs[0]], axis, attr.get("keepdims", True))

return cls._impl_v1(inputs, attr, params)


class ReduceMax(Reduce):
"""Operator converter for ReduceMax."""
Expand Down
4 changes: 0 additions & 4 deletions tests/python/frontend/onnx/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -5172,12 +5172,8 @@ def verify_eyelike(indata):
"test_qlinearmatmul_3D",
"test_range_float_type_positive_delta_expanded",
"test_range_int32_type_negative_delta_expanded",
"test_reduce_sum_default_axes_keepdims_example",
"test_reduce_sum_default_axes_keepdims_random",
"test_reduce_sum_do_not_keepdims_example",
"test_reduce_sum_do_not_keepdims_random",
"test_reduce_sum_empty_axes_input_noop_example",
"test_reduce_sum_empty_axes_input_noop_random",
"test_reduce_sum_keepdims_example",
"test_reduce_sum_keepdims_random",
"test_reduce_sum_negative_axes_keepdims_example",
Expand Down

0 comments on commit df4f4c0

Please sign in to comment.