From df4f4c0b4bccd775af25967fdf057392c1a2826e Mon Sep 17 00:00:00 2001 From: "Sevin F. Varoglu" Date: Wed, 8 Jun 2022 14:08:06 -0700 Subject: [PATCH] [ONNX] Add ReduceSum opset13 support (non-dynamic) (#11606) * [ONNX] Add ReduceSum opset13 support (non-dynamic) * Add check * Add support for constant axis * noop * Rework logic --- python/tvm/relay/frontend/onnx.py | 26 ++++++++++++++++++++++ tests/python/frontend/onnx/test_forward.py | 4 ---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index abfa5629d553..29c0a778ef6e 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -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.""" diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index ebaad9b4cb13..967597f7d12b 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -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",