Skip to content

Commit

Permalink
[ONNX] Aligned behavior of ReduceMin operator (#23619)
Browse files Browse the repository at this point in the history
### Details:
Align the behaviour of the ReduceMin operator in the ONNX frontend.

### Tickets:
Closes #20557

Credit: Smooth implementation thanks to
#23475.

---------

Co-authored-by: Georgy Krivoruchko <georgy.krivoruchko@intel.com>
  • Loading branch information
YaritaiKoto and gkrivor authored Apr 2, 2024
1 parent a0a80e0 commit 668cc41
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 13 deletions.
24 changes: 24 additions & 0 deletions src/frontends/onnx/frontend/src/op/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,21 @@ ov::OutputVector reduce_sum(const ov::frontend::onnx::Node& node) {
ov::OutputVector reduce_max(const ov::frontend::onnx::Node& node) {
return {make_ov_reduction_op<v1::ReduceMax>(node, node.get_ov_inputs().at(0), supported_types_v3)};
}

ov::OutputVector reduce_min(const ov::frontend::onnx::Node& node) {
return {make_ov_reduction_op<v1::ReduceMin>(node, node.get_ov_inputs().at(0), supported_types_v3)};
}
} // namespace set_13

namespace set_18 {
ov::OutputVector reduce_max(const ov::frontend::onnx::Node& node) {
return {make_ov_reduction_op<v1::ReduceMax>(node, node.get_ov_inputs().at(0), supported_types_v3, false)};
}

ov::OutputVector reduce_min(const ov::frontend::onnx::Node& node) {
return {make_ov_reduction_op<v1::ReduceMin>(node, node.get_ov_inputs().at(0), supported_types_v3, false)};
}

ov::OutputVector reduce_log_sum(const ov::frontend::onnx::Node& node) {
const ov::Output<ov::Node> sum_node =
make_ov_reduction_op<v1::ReduceSum>(node, node.get_ov_inputs().at(0), supported_types_v2, false);
Expand All @@ -229,6 +238,21 @@ ov::OutputVector reduce_max(const ov::frontend::onnx::Node& node) {
element::boolean)};
}
}

ov::OutputVector reduce_min(const ov::frontend::onnx::Node& node) {
auto data = node.get_ov_inputs().at(0);
if (data.get_element_type() != element::boolean) {
return {make_ov_reduction_op<v1::ReduceMin>(node, data, supported_types_v3, false)};
} else {
// Handling boolean as a uint8
return {std::make_shared<v0::Convert>(
make_ov_reduction_op<v1::ReduceMin>(node,
std::make_shared<ov::op::v0::Convert>(data, element::u8),
supported_types_v4,
false),
element::boolean)};
}
}
} // namespace set_20
} // namespace op
} // namespace onnx
Expand Down
9 changes: 9 additions & 0 deletions src/frontends/onnx/frontend/src/op/reduce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ ov::OutputVector reduce_mean(const ov::frontend::onnx::Node& node);
namespace set_1 {
ov::OutputVector reduce_min(const ov::frontend::onnx::Node& node);
} // namespace set_1
namespace set_13 {
ov::OutputVector reduce_min(const ov::frontend::onnx::Node& node);
} // namespace set_13
namespace set_18 {
ov::OutputVector reduce_min(const ov::frontend::onnx::Node& node);
} // namespace set_18
namespace set_20 {
ov::OutputVector reduce_min(const ov::frontend::onnx::Node& node);
} // namespace set_20

namespace set_1 {
ov::OutputVector reduce_prod(const ov::frontend::onnx::Node& node);
Expand Down
3 changes: 3 additions & 0 deletions src/frontends/onnx/frontend/src/ops_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ OperatorsBridge::OperatorsBridge() {
REGISTER_OPERATOR("ReduceMax", 20, reduce_max);
REGISTER_OPERATOR("ReduceMean", 1, reduce_mean);
REGISTER_OPERATOR("ReduceMin", 1, reduce_min);
REGISTER_OPERATOR("ReduceMin", 13, reduce_min);
REGISTER_OPERATOR("ReduceMin", 18, reduce_min);
REGISTER_OPERATOR("ReduceMin", 20, reduce_min);
REGISTER_OPERATOR("ReduceProd", 1, reduce_prod);
REGISTER_OPERATOR("ReduceSum", 1, reduce_sum);
REGISTER_OPERATOR("ReduceSum", 13, reduce_sum);
Expand Down
1 change: 0 additions & 1 deletion src/frontends/onnx/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ def xfail_test(reason="Mark the test as expected to fail", strict=True):
xfail_issue_125491 = xfail_test(reason="AveragePool mismatch with differences in shapes")
xfail_issue_125492 = xfail_test(reason="DFT mismatch")
xfail_issue_125493 = xfail_test(reason="Reduce* mismatch")
xfail_issue_125495 = xfail_test(reason="ReduceMin/Max doesn't support boolean")
xfail_issue_122776 = xfail_test(reason="test_mish_expanded_cpu - "
"Not equal to tolerance")
xfail_issue_122775 = xfail_test(reason="test_resize_downsample_scales_linear_cpu - "
Expand Down
70 changes: 70 additions & 0 deletions src/frontends/onnx/tests/models/reduce_min_18.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
ir_version: 3
producer_name: "OpenVINO ONNX Frontend"
graph {
node {
input: "A"
input: "axes"
output: "B"
op_type: "ReduceMin"
}
name: "compute_graph"
initializer {
data_type: 6
dims: 1
name: "axes"
raw_data: "\002\000\000\000"
}
input {
name: "A"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 1
}
dim {
dim_value: 1
}
dim {
dim_value: 4
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "axes"
type {
tensor_type {
elem_type: 6
shape {
dim {
dim_value: 1
}
}
}
}
}
output {
name: "B"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 18
}

# Acknowledgement: this model is a modified version of reduce_max_18.prototxt
70 changes: 70 additions & 0 deletions src/frontends/onnx/tests/models/reduce_min_20.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
ir_version: 3
producer_name: "OpenVINO ONNX Frontend"
graph {
node {
input: "A"
input: "axes"
output: "B"
op_type: "ReduceMin"
}
name: "compute_graph"
initializer {
data_type: 6
dims: 1
name: "axes"
raw_data: "\002\000\000\000"
}
input {
name: "A"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 1
}
dim {
dim_value: 1
}
dim {
dim_value: 4
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "axes"
type {
tensor_type {
elem_type: 6
shape {
dim {
dim_value: 1
}
}
}
}
}
output {
name: "B"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 20
}

# Acknowledgement: this model is a modified version of reduce_max_18.prototxt
48 changes: 48 additions & 0 deletions src/frontends/onnx/tests/onnx_import.in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6557,3 +6557,51 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_mish_activation) {

test_case.run_with_tolerance_as_fp(0.000001f);
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reduce_min_18) {
// Credit: This function is a modified version of PR 23475's test function.
// TEMPLATE plugin has an issue with evaluation for u8 type
if (std::string("${BACKEND_NAME}") == std::string("INTERPRETER")) {
GTEST_SKIP();
}

auto model = convert_model("reduce_min_18.onnx");

// input data shape (1, 1, 4, 4)
std::vector<std::vector<uint8_t>> inputs{
ov::test::NDArray<uint8_t, 4>({{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}}})
.get_vector()};

// output data shape (1,)
auto expected_output = ov::test::NDArray<uint8_t, 1>({1, 2, 3, 4}).get_vector();

auto test_case = ov::test::TestCase(model, s_device);
test_case.add_multiple_inputs(inputs);
test_case.add_expected_output(expected_output);
test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reduce_min_20_boolean) {
// Credit: This function is a modified version of PR 23475's test function.
// TEMPLATE plugin has an issue with evaluation for u8 type
if (std::string("${BACKEND_NAME}") == std::string("INTERPRETER")) {
GTEST_SKIP();
}

auto model = convert_model("reduce_min_20.onnx");

// input data shape (1, 1, 4, 4)
std::vector<std::vector<bool>> inputs{ov::test::NDArray<bool, 4>({{{{true, true, false, false},
{true, false, false, false},
{true, false, false, false},
{true, true, false, false}}}})
.get_vector()};

// output data shape (1,)
auto expected_output = ov::test::NDArray<uint8_t, 1>({1, 0, 0, 0}).get_vector();

auto test_case = ov::test::TestCase(model, s_device);
test_case.add_multiple_inputs(inputs);
test_case.add_expected_output(expected_output);
test_case.run();
}
12 changes: 0 additions & 12 deletions src/frontends/onnx/tests/tests_python/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
xfail_issue_125491,
xfail_issue_125492,
xfail_issue_125493,
xfail_issue_125495,
xfail_issue_122775,
xfail_issue_122776,
skip_misalignment,
Expand Down Expand Up @@ -489,12 +488,6 @@ def expect_fail(test_case_path, xfail): # type: (str) -> None
"OnnxBackendNodeModelTest.test_reduce_mean_negative_axes_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_mean_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_mean_negative_axes_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_min_do_not_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_min_do_not_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_min_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_min_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_min_negative_axes_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_min_negative_axes_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_prod_do_not_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_prod_do_not_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_prod_keepdims_example_cpu",
Expand Down Expand Up @@ -698,14 +691,9 @@ def expect_fail(test_case_path, xfail): # type: (str) -> None
"OnnxBackendNodeModelTest.test_reduce_l1_empty_set_cpu",
"OnnxBackendNodeModelTest.test_reduce_l2_empty_set_cpu",
"OnnxBackendNodeModelTest.test_reduce_log_sum_exp_empty_set_cpu",
"OnnxBackendNodeModelTest.test_reduce_min_empty_set_cpu",
"OnnxBackendNodeModelTest.test_reduce_prod_empty_set_cpu",
"OnnxBackendNodeModelTest.test_reduce_sum_square_empty_set_cpu",
),
(
xfail_issue_125495,
"OnnxBackendNodeModelTest.test_reduce_min_bool_inputs_cpu",
),
(
skip_misalignment,
"OnnxBackendNodeModelTest.test_gelu_default_2_expanded_cpu",
Expand Down

0 comments on commit 668cc41

Please sign in to comment.