Skip to content

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

[ONNX] Fix issue with reading optional types (#33649)

Maxim Vafine2269de1a4a5
diff --git a/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp b/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp
index 788f4d3e72c..2f4065e3069 100644
--- a/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp
+++ b/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp
@@ -68,6 +68,19 @@ const ov::element::Type& get_ov_element_type(int64_t onnx_type) {
throw std::runtime_error("Unsupported type");
}
+const ::ONNX_NAMESPACE::TypeProto_Tensor* get_tensor_type(const ::ONNX_NAMESPACE::TypeProto& type_proto) {
+ if (type_proto.has_tensor_type()) {
+ return &type_proto.tensor_type();
+ }
+ if (type_proto.has_optional_type() && type_proto.optional_type().has_elem_type()) {
+ return get_tensor_type(type_proto.optional_type().elem_type());
+ }
+ if (type_proto.has_sequence_type() && type_proto.sequence_type().has_elem_type()) {
+ return get_tensor_type(type_proto.sequence_type().elem_type());
+ }
+ return nullptr;
+}
+
void fixup_legacy_nodes(::ONNX_NAMESPACE::ModelProto& model_proto) {
auto* graph_proto = model_proto.mutable_graph();
if (!graph_proto) {
@@ -213,13 +226,16 @@ ov::frontend::onnx::TensorMetaInfo extract_tensor_meta_info(const TensorProto* t
}
if (value_info != nullptr) {
tensor_meta_info.m_tensor_name = value_info->has_name() ? &value_info->name() : &empty_name;
- if (value_info->has_type() && !value_info->type().has_tensor_type()) {
+ const auto* value_type = value_info->has_type() ? get_tensor_type(value_info->type()) : nullptr;
+ const auto value_case =
+ value_info->has_type() ? value_info->type().value_case() : ::ONNX_NAMESPACE::TypeProto::VALUE_NOT_SET;
+ if (value_info->has_type() && value_type == nullptr &&
+ value_case != ::ONNX_NAMESPACE::TypeProto::VALUE_NOT_SET) {
throw std::runtime_error("Unsupported value_info type: " + (*tensor_meta_info.m_tensor_name));
}
- const auto& value_type = value_info->type().tensor_type();
- if (value_type.has_shape()) {
+ if (value_type != nullptr && value_type->has_shape()) {
std::vector<int64_t> dims{};
- for (const auto& dim : value_type.shape().dim()) {
+ for (const auto& dim : value_type->shape().dim()) {
if (dim.has_dim_value()) {
dims.push_back(dim.dim_value());
} else {
@@ -230,8 +246,8 @@ ov::frontend::onnx::TensorMetaInfo extract_tensor_meta_info(const TensorProto* t
} else {
tensor_meta_info.m_partial_shape = ov::PartialShape::dynamic();
}
- if (value_type.has_elem_type()) {
- tensor_meta_info.m_element_type = get_ov_element_type(value_type.elem_type());
+ if (value_type != nullptr && value_type->has_elem_type()) {
+ tensor_meta_info.m_element_type = get_ov_element_type(value_type->elem_type());
} else {
tensor_meta_info.m_element_type = ov::element::dynamic;
}
diff --git a/src/frontends/onnx/tests/graph_iterator.cpp b/src/frontends/onnx/tests/graph_iterator.cpp
index 42ba4539aeb..b6ee2372442 100644
--- a/src/frontends/onnx/tests/graph_iterator.cpp
+++ b/src/frontends/onnx/tests/graph_iterator.cpp
@@ -7,16 +7,15 @@
#include <algorithm>
#include <filesystem>
-#include <fstream>
-#include <iostream>
#include <map>
#include <openvino/frontend/exception.hpp>
#include <openvino/frontend/graph_iterator.hpp>
#include <openvino/frontend/input_model.hpp>
#include <openvino/openvino.hpp>
-#include <system_error>
#include <unordered_map>
+#include <vector>
+#include "../frontend/src/core/decoder_proto.hpp"
#include "../frontend/src/core/graph_iterator_proto.hpp"
#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/test_case.hpp"
@@ -24,9 +23,6 @@
#include "onnx_utils.hpp"
#include "utils.hpp"
-using ::ONNX_NAMESPACE::ModelProto;
-using ::ONNX_NAMESPACE::Version;
-
class SimpleIterator : public ov::frontend::onnx::GraphIterator {
public:
mutable size_t get_model_dir_call_count = 0;
@@ -65,6 +61,20 @@ public:
~SimpleIterator() override {};
};
+namespace {
+
+class GraphIteratorProtoAccessor : public ov::frontend::onnx::GraphIteratorProto {
+public:
+ using ov::frontend::onnx::GraphIteratorProto::GraphIteratorProto;
+
+ std::shared_ptr<ov::frontend::onnx::DecoderProtoTensor> get_tensor_by_name(const std::string& name) {
+ GraphIteratorProto* owner = nullptr;
+ return GraphIteratorProto::get_tensor(name, &owner);
+ }
+};
+
+} // namespace
+
TEST_P(FrontEndLoadFromTest, testLoadUsingSimpleGraphIterator) {
ov::frontend::FrontEnd::Ptr fe;
@@ -237,3 +247,47 @@ TEST(FrontEndGraphIteratorTest, loads_bfloat16_raw_initializer_via_iterator) {
test_case.add_expected_output<ov::bfloat16>(ov::Shape{2, 2}, expected_data);
test_case.run();
}
+
+TEST(FrontEndGraphIteratorTest, handles_optional_value_info) {
+ const std::string model_name = "graph_iterator/optional_value_info.onnx";
+ const auto model_path =
+ ov::util::path_join({ov::test::utils::getExecutableDirectory(), TEST_ONNX_MODELS_DIRNAME, model_name});
+
+ GraphIteratorProtoAccessor iterator(ov::frontend::onnx::GraphIteratorProtoMemoryManagementMode::Internal_Stream);
+ ASSERT_NO_THROW(iterator.initialize(model_path));
+ ASSERT_NO_THROW(iterator.reset());
+
+ auto optional_input = iterator.get_tensor_by_name("optional_input");
+ ASSERT_NE(optional_input, nullptr);
+ const auto& input_info = optional_input->get_tensor_info();
+ EXPECT_EQ(input_info.m_element_type, ov::element::f32);
+ EXPECT_EQ(input_info.m_partial_shape, ov::PartialShape({2, 3}));
+
+ auto optional_output = iterator.get_tensor_by_name("optional_output");
+ ASSERT_NE(optional_output, nullptr);
+ const auto& output_info = optional_output->get_tensor_info();
+ EXPECT_EQ(output_info.m_element_type, ov::element::f32);
+ EXPECT_EQ(output_info.m_partial_shape, ov::PartialShape({2, 3}));
+}
+
+TEST(FrontEndGraphIteratorTest, handles_sequence_value_info) {
+ const std::string model_name = "graph_iterator/sequence_value_info.onnx";
+ const auto model_path =
+ ov::util::path_join({ov::test::utils::getExecutableDirectory(), TEST_ONNX_MODELS_DIRNAME, model_name});
+
+ GraphIteratorProtoAccessor iterator(ov::frontend::onnx::GraphIteratorProtoMemoryManagementMode::Internal_Stream);
+ ASSERT_NO_THROW(iterator.initialize(model_path));
+ ASSERT_NO_THROW(iterator.reset());
+
+ auto sequence_input = iterator.get_tensor_by_name("sequence_input");
+ ASSERT_NE(sequence_input, nullptr);
+ const auto& input_info = sequence_input->get_tensor_info();
+ EXPECT_EQ(input_info.m_element_type, ov::element::i64);
+ EXPECT_EQ(input_info.m_partial_shape, ov::PartialShape({4}));
+
+ auto sequence_output = iterator.get_tensor_by_name("sequence_output");
+ ASSERT_NE(sequence_output, nullptr);
+ const auto& output_info = sequence_output->get_tensor_info();
+ EXPECT_EQ(output_info.m_element_type, ov::element::i64);
+ EXPECT_EQ(output_info.m_partial_shape, ov::PartialShape({4}));
+}
diff --git a/src/frontends/onnx/tests/models/graph_iterator/optional_value_info.prototxt b/src/frontends/onnx/tests/models/graph_iterator/optional_value_info.prototxt
new file mode 100644
index 00000000000..a50bef4072a
--- /dev/null
+++ b/src/frontends/onnx/tests/models/graph_iterator/optional_value_info.prototxt
@@ -0,0 +1,53 @@
+ir_version: 3
+producer_name: "OpenVINO GraphIterator Tests"
+graph {
+ node {
+ input: "optional_input"
+ output: "optional_output"
+ op_type: "Identity"
+ }
+ name: "graph_iterator_optional"
+ input {
+ name: "optional_input"
+ type {
+ optional_type {
+ elem_type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "optional_output"
+ type {
+ optional_type {
+ elem_type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 17
+}
diff --git a/src/frontends/onnx/tests/models/graph_iterator/sequence_value_info.prototxt b/src/frontends/onnx/tests/models/graph_iterator/sequence_value_info.prototxt
new file mode 100644
index 00000000000..d068868c50d
--- /dev/null
+++ b/src/frontends/onnx/tests/models/graph_iterator/sequence_value_info.prototxt
@@ -0,0 +1,47 @@
+ir_version: 3
+producer_name: "OpenVINO GraphIterator Tests"
+graph {
+ node {
+ input: "sequence_input"
+ output: "sequence_output"
+ op_type: "Identity"
+ }
+ name: "graph_iterator_sequence"
+ input {
+ name: "sequence_input"
+ type {
+ sequence_type {
+ elem_type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "sequence_output"
+ type {
+ sequence_type {
+ elem_type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 17
+}