Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[ONNX FE] Support allowzero in Reshape (#36046)

Maxim Vafin66db6ac8c7ad
diff --git a/src/frontends/onnx/frontend/src/op/reshape.cpp b/src/frontends/onnx/frontend/src/op/reshape.cpp
index 3975b02559b..c04f5c374db 100644
--- a/src/frontends/onnx/frontend/src/op/reshape.cpp
+++ b/src/frontends/onnx/frontend/src/op/reshape.cpp
@@ -6,6 +6,7 @@
#include "core/operator_set.hpp"
#include "exceptions.hpp"
+#include "openvino/op/constant.hpp"
#include "utils/common.hpp"
#include "utils/reshape.hpp"
using namespace ov::op;
@@ -20,7 +21,8 @@ ov::OutputVector reshape(const ov::frontend::onnx::Node& node) {
const auto data = ov_inputs.at(0);
ov::Output<ov::Node> pattern;
- bool special_zero = true;
+ // ONNX `allowzero` is the inverse of OpenVINO's `special_zero`.
+ const bool special_zero = !node.get_attribute_value<int64_t>("allowzero", 0);
// Since opset 5 the target shape is provided as input
if (ov_inputs.size() == 2) {
pattern = ov_inputs.at(1);
@@ -31,9 +33,6 @@ ov::OutputVector reshape(const ov::frontend::onnx::Node& node) {
pattern = v0::Constant::create(ov::element::i64, {0}, {});
}
} else {
- // Added in onnx reshape version 14
- special_zero = !node.get_attribute_value<int64_t>("allowzero", 0);
-
pattern = node.get_attribute_as_constant<std::vector<int64_t>>("shape", {});
}
diff --git a/src/frontends/onnx/tests/__init__.py b/src/frontends/onnx/tests/__init__.py
index 1b7ccd8403b..25aeb8191c8 100644
--- a/src/frontends/onnx/tests/__init__.py
+++ b/src/frontends/onnx/tests/__init__.py
@@ -66,7 +66,6 @@ xfail_issue_99969 = xfail_test(reason="Resize - Results mismatch / "
xfail_issue_99970 = xfail_test(reason="Scatter and ScatterND - RuntimeError: Check '(reduction == none)' failed at "
"src/frontends/onnx/frontend/src/op/scatter_elements.cpp OR at "
"src/frontends/onnx/frontend/src/op/scatter_nd")
-xfail_issue_38710 = xfail_test(reason="RuntimeError: data has zero dimension which is not allowed")
xfail_issue_38713 = xfail_test(reason="RuntimeError: OV does not support the following ONNX operations: "
"ai.onnx.preview.training.Momentum")
xfail_issue_38724 = xfail_test(reason="RuntimeError: While validating ONNX node '<Node(Resize): Y>': "
diff --git a/src/frontends/onnx/tests/models/reshape_allowzero.prototxt b/src/frontends/onnx/tests/models/reshape_allowzero.prototxt
new file mode 100644
index 00000000000..3afc66fc03b
--- /dev/null
+++ b/src/frontends/onnx/tests/models/reshape_allowzero.prototxt
@@ -0,0 +1,64 @@
+ir_version: 7
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ output: "shape_const"
+ op_type: "Constant"
+ attribute {
+ name: "value"
+ t {
+ dims: 3
+ data_type: 7
+ int64_data: 3
+ int64_data: 4
+ int64_data: 0
+ name: "shape_tensor"
+ }
+ type: TENSOR
+ }
+ }
+ node {
+ input: "data"
+ input: "shape_const"
+ output: "reshaped"
+ op_type: "Reshape"
+ attribute {
+ name: "allowzero"
+ i: 1
+ type: INT
+ }
+ }
+ name: "test_reshape_allowzero"
+ input {
+ name: "data"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 0
+ }
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "reshaped"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 14
+}
diff --git a/src/frontends/onnx/tests/onnx_import_reshape.in.cpp b/src/frontends/onnx/tests/onnx_import_reshape.in.cpp
index bea93f087c0..1206895f0df 100644
--- a/src/frontends/onnx/tests/onnx_import_reshape.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_reshape.in.cpp
@@ -183,6 +183,25 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reshape_output_shape_as_input) {
test_case.run();
}
+// Regression test: with the `allowzero=1` attribute (opset 14+), a `0` in the
+// target shape must be taken literally instead of being mapped to OpenVINO's
+// `special_zero=true` (copy-dim) semantics. Previously, when the shape was
+// provided as an input (opset 5+ form), `allowzero` was ignored and `0` was
+// always interpreted as "copy from input".
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reshape_allowzero) {
+ auto model = convert_model("reshape_allowzero.onnx");
+
+ // input shape (0, 3, 4), zero-element tensor
+ std::vector<float> input;
+ // target shape literally [3, 4, 0] (must NOT be reinterpreted as [3, 4, 4])
+ std::vector<float> expected_output;
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input(Shape{0, 3, 4}, input);
+ test_case.add_expected_output(Shape{3, 4, 0}, expected_output);
+ test_case.run();
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_depth_to_space) {
auto model = convert_model("depth_to_space.onnx");
diff --git a/src/frontends/onnx/tests/tests_python/test_backend.py b/src/frontends/onnx/tests/tests_python/test_backend.py
index 30d134ce8be..c63e917eae2 100644
--- a/src/frontends/onnx/tests/tests_python/test_backend.py
+++ b/src/frontends/onnx/tests/tests_python/test_backend.py
@@ -17,7 +17,6 @@ from tests import (
xfail_issue_38699,
xfail_issue_38701,
xfail_issue_38706,
- xfail_issue_38710,
xfail_issue_38713,
xfail_issue_38724,
xfail_issue_38734,
@@ -287,10 +286,6 @@ tests_expected_to_fail = [
"OnnxBackendNodeModelTest.test_lstm_batchwise_cpu",
"OnnxBackendNodeModelTest.test_simple_rnn_batchwise_cpu",
),
- (
- xfail_issue_38710,
- "OnnxBackendNodeModelTest.test_reshape_allowzero_reordered_cpu",
- ),
(
skip_dynamic_model,
"OnnxBackendNodeModelTest.test_triu_one_row_cpu",