Skip to content

Commit

Permalink
Align behavior of ONNX Frontend function ReduceSumSquare-11, 13, 18 w…
Browse files Browse the repository at this point in the history
…ith original framework (#23798)

Hello maintainers,

Since my previous PR
(#22993 (comment))
had diverged significantly from the master branch, and also included
additional changes, I thought it would be best to create a new PR.
@gkrivor, could you please take a look?

This closes
#20563 (comment).

---------

Co-authored-by: Georgy Krivoruchko <[email protected]>
  • Loading branch information
inbasperu and gkrivor authored May 23, 2024
1 parent c904b8e commit 9025003
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 10 deletions.
22 changes: 19 additions & 3 deletions src/frontends/onnx/frontend/src/op/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ std::shared_ptr<ov::Node> make_ov_reduction_op(const Node& node,
return set_1::identity(node).at(0).get_node_shared_ptr();
}
}

std::shared_ptr<ov::Node> onnx_reduce_sum_square(const ov::frontend::onnx::Node& node,
const std::set<element::Type>& supported_types,
const bool axes_as_attr = true) {
const auto input = ov::Output<ov::Node>{node.get_ov_inputs().at(0)};
const auto square_node = std::make_shared<v1::Multiply>(input, input);
return make_ov_reduction_op<v1::ReduceSum>(node, square_node, supported_types, axes_as_attr);
}
} // namespace

namespace set_1 {
Expand Down Expand Up @@ -180,9 +188,7 @@ ov::OutputVector reduce_sum(const ov::frontend::onnx::Node& node) {
}

ov::OutputVector reduce_sum_square(const ov::frontend::onnx::Node& node) {
const auto input = ov::Output<ov::Node>{node.get_ov_inputs().at(0)};
const auto square_node = std::make_shared<v1::Multiply>(input, input);
return {make_ov_reduction_op<v1::ReduceSum>(node, square_node, supported_types_v1)};
return {onnx_reduce_sum_square(node, supported_types_v1)};
}
} // namespace set_1

Expand All @@ -199,16 +205,22 @@ namespace set_13 {
ov::OutputVector reduce_sum(const ov::frontend::onnx::Node& node) {
return {make_ov_reduction_op<v1::ReduceSum>(node, node.get_ov_inputs().at(0), supported_types_v2, false)};
}

ov::OutputVector reduce_l2(const Node& node) {
return {make_ov_reduction_op<v4::ReduceL2>(node, node.get_ov_inputs().at(0), supported_types_v2)};
}

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)};
}

ov::OutputVector reduce_sum_square(const ov::frontend::onnx::Node& node) {
return {onnx_reduce_sum_square(node, supported_types_v2)};
}
} // namespace set_13

namespace set_18 {
Expand All @@ -228,6 +240,10 @@ ov::OutputVector reduce_log_sum(const ov::frontend::onnx::Node& node) {
make_ov_reduction_op<v1::ReduceSum>(node, node.get_ov_inputs().at(0), supported_types_v2, false);
return {std::make_shared<v0::Log>(sum_node)};
}

ov::OutputVector reduce_sum_square(const ov::frontend::onnx::Node& node) {
return {onnx_reduce_sum_square(node, supported_types_v2, false)};
}
} // namespace set_18

namespace set_20 {
Expand Down
6 changes: 6 additions & 0 deletions src/frontends/onnx/frontend/src/op/reduce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ ov::OutputVector reduce_sum(const ov::frontend::onnx::Node& node);
namespace set_1 {
ov::OutputVector reduce_sum_square(const ov::frontend::onnx::Node& node);
} // namespace set_1
namespace set_13 {
ov::OutputVector reduce_sum_square(const ov::frontend::onnx::Node& node);
} // namespace set_13
namespace set_18 {
ov::OutputVector reduce_sum_square(const ov::frontend::onnx::Node& node);
} // namespace set_18

} // namespace op
} // namespace onnx
Expand Down
2 changes: 2 additions & 0 deletions src/frontends/onnx/frontend/src/ops_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ OperatorsBridge::OperatorsBridge() {
REGISTER_OPERATOR("ReduceSum", 1, reduce_sum);
REGISTER_OPERATOR("ReduceSum", 13, reduce_sum);
REGISTER_OPERATOR("ReduceSumSquare", 1, reduce_sum_square);
REGISTER_OPERATOR("ReduceSumSquare", 13, reduce_sum_square);
REGISTER_OPERATOR("ReduceSumSquare", 18, reduce_sum_square);
REGISTER_OPERATOR("Relu", 1, relu);
REGISTER_OPERATOR("Reshape", 1, reshape);
REGISTER_OPERATOR("Resize", 1, resize);
Expand Down
48 changes: 48 additions & 0 deletions src/frontends/onnx/tests/models/reduce_sum_square_13.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
ir_version: 3
producer_name: "OpenVINO ONNX Frontend"
graph {
node {
input: "A"
output: "B"
op_type: "ReduceSumSquare"
}
name: "compute_graph"
input {
name: "A"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 1
}
dim {
dim_value: 4
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "B"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 13
}
48 changes: 48 additions & 0 deletions src/frontends/onnx/tests/models/reduce_sum_square_18.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
ir_version: 3
producer_name: "OpenVINO ONNX Frontend"
graph {
node {
input: "A"
output: "B"
op_type: "ReduceSumSquare"
}
name: "compute_graph"
input {
name: "A"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 1
}
dim {
dim_value: 4
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "B"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 18
}
32 changes: 32 additions & 0 deletions src/frontends/onnx/tests/onnx_import.in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,38 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reduce_sum_square) {
test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reduce_sum_square_13) {
auto model = convert_model("reduce_sum_square_13.onnx");

// input data shape (1, 1, 4, 4)
Inputs inputs{
ov::test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}}).get_vector()};

// output data shape (1,)
auto expected_output = ov::test::NDArray<float, 4>({{{{16}}}}).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_sum_square_18) {
auto model = convert_model("reduce_sum_square_18.onnx");

// input data shape (1, 1, 4, 4)
Inputs inputs{
ov::test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}}).get_vector()};

// output data shape (1,)
auto expected_output = ov::test::NDArray<float, 4>({{{{16}}}}).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_sum_13_axes_as_constant) {
auto model = convert_model("reduce_sum_13_axes_as_constant.onnx");

Expand Down
7 changes: 0 additions & 7 deletions src/frontends/onnx/tests/tests_python/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,6 @@ def expect_fail(test_case_path, xfail): # type: (str) -> None
"OnnxBackendNodeModelTest.test_reduce_prod_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_prod_negative_axes_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_prod_negative_axes_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_sum_square_do_not_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_sum_square_do_not_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_sum_square_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_sum_square_keepdims_random_cpu",
"OnnxBackendNodeModelTest.test_reduce_sum_square_negative_axes_keepdims_example_cpu",
"OnnxBackendNodeModelTest.test_reduce_sum_square_negative_axes_keepdims_random_cpu",
),
(
xfail_issue_99969,
Expand Down Expand Up @@ -685,7 +679,6 @@ def expect_fail(test_case_path, xfail): # type: (str) -> None
"OnnxBackendNodeModelTest.test_reduce_l1_empty_set_cpu",
"OnnxBackendNodeModelTest.test_reduce_log_sum_exp_empty_set_cpu",
"OnnxBackendNodeModelTest.test_reduce_prod_empty_set_cpu",
"OnnxBackendNodeModelTest.test_reduce_sum_square_empty_set_cpu",
),
(
skip_misalignment,
Expand Down

0 comments on commit 9025003

Please sign in to comment.