OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[ONNX] Refactor how delegate work with initializers (#33099)
Maxim Vafinf11d78893eda
diff --git a/src/frontends/onnx/frontend/src/core/decoder_proto.cpp b/src/frontends/onnx/frontend/src/core/decoder_proto.cppindex c71d5a3488a..7c9f1b2e8ed 100644--- a/src/frontends/onnx/frontend/src/core/decoder_proto.cpp+++ b/src/frontends/onnx/frontend/src/core/decoder_proto.cpp@@ -54,7 +54,7 @@ ov::Any DecoderProto::get_attribute(const std::string& name) const {break;case AttributeProto_AttributeType::AttributeProto_AttributeType_TENSOR:return static_cast<ov::frontend::onnx::DecoderBase::Ptr>(- std::make_shared<DecoderProtoTensor>(&attr.t(), m_parent, 0, 0));+ std::make_shared<DecoderProtoTensor>(&attr.t(), m_parent));case AttributeProto_AttributeType::AttributeProto_AttributeType_SPARSE_TENSOR: {ov::frontend::onnx::SparseTensorInfo sparse_tensor_info{};auto& sparse_tensor = attr.sparse_tensor();@@ -62,11 +62,11 @@ ov::Any DecoderProto::get_attribute(const std::string& name) const {ov::PartialShape{std::vector<int64_t>(sparse_tensor.dims().begin(), sparse_tensor.dims().end())};if (sparse_tensor.has_values()) {sparse_tensor_info.m_values = static_cast<ov::frontend::onnx::DecoderBase::Ptr>(- std::make_shared<DecoderProtoTensor>(&sparse_tensor.values(), m_parent, 0, 0));+ std::make_shared<DecoderProtoTensor>(&sparse_tensor.values(), m_parent));}if (sparse_tensor.has_indices()) {sparse_tensor_info.m_indices = static_cast<ov::frontend::onnx::DecoderBase::Ptr>(- std::make_shared<DecoderProtoTensor>(&sparse_tensor.indices(), m_parent, 0, 0));+ std::make_shared<DecoderProtoTensor>(&sparse_tensor.indices(), m_parent));}return sparse_tensor_info;}diff --git a/src/frontends/onnx/frontend/src/core/decoder_proto.hpp b/src/frontends/onnx/frontend/src/core/decoder_proto.hppindex d902b9a7abc..86aa489d271 100644--- a/src/frontends/onnx/frontend/src/core/decoder_proto.hpp+++ b/src/frontends/onnx/frontend/src/core/decoder_proto.hpp@@ -37,30 +37,30 @@ extern const std::string EMPTY_OP_TYPE;class DecoderProtoTensor : public ov::frontend::onnx::DecoderBaseTensor {ov::frontend::onnx::TensorMetaInfo m_tensor_meta_info;- int64_t m_input_idx, m_output_idx;+ int64_t m_input_idx = -1;+ int64_t m_output_idx = -1;public:DecoderProtoTensor(const TensorProto* tensor_info,GraphIteratorProto* parent,- const int64_t input_idx,- const int64_t output_idx)- // Probably, we may need to force it to 0/0+ const int64_t input_idx = -1,+ const int64_t output_idx = -1): m_input_idx(input_idx),m_output_idx(output_idx) {m_tensor_meta_info = extract_tensor_meta_info(tensor_info, nullptr, parent);}DecoderProtoTensor(const ValueInfoProto* value_info,GraphIteratorProto* parent,- const int64_t input_idx,- const int64_t output_idx)+ const int64_t input_idx = -1,+ const int64_t output_idx = -1): m_input_idx(input_idx),m_output_idx(output_idx) {m_tensor_meta_info = extract_tensor_meta_info(nullptr, value_info, parent);}DecoderProtoTensor(const std::string& name,GraphIteratorProto* parent,- const int64_t input_idx,- const int64_t output_idx)+ const int64_t input_idx = -1,+ const int64_t output_idx = -1): m_input_idx(input_idx),m_output_idx(output_idx) {m_tensor_meta_info.m_tensor_name = &name;diff --git a/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp b/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cppindex ea0e19fc18e..c0fd62f2e57 100644--- a/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp+++ b/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp@@ -348,7 +348,7 @@ std::shared_ptr<DecoderProtoTensor> GraphIteratorProto::get_tensor(const std::stif (m_tensors.count(name) == 0) {if (name == empty_name) {*owner = this;- const auto& tensor_decoder = std::make_shared<DecoderProtoTensor>(empty_name, this, -1, -1);+ const auto& tensor_decoder = std::make_shared<DecoderProtoTensor>(empty_name, this);m_tensors[empty_name] = tensor_decoder;return tensor_decoder;}@@ -374,8 +374,19 @@ void GraphIteratorProto::reset() {return;m_decoders.reserve(m_graph->initializer_size() + m_graph->input_size() + m_graph->output_size() +m_graph->node_size());+ int64_t index = 0;for (const auto& value : m_graph->input()) {- auto tensor = std::make_shared<DecoderProtoTensor>(&value, this, 0, -1);+ const auto& initializer = std::find_if(m_graph->initializer().begin(),+ m_graph->initializer().end(),+ [&value](const TensorProto& tensor) {+ return tensor.has_name() && tensor.name() == value.name();+ });+ std::shared_ptr<DecoderProtoTensor> tensor;+ if (initializer == m_graph->initializer().end()) {+ tensor = std::make_shared<DecoderProtoTensor>(&value, this, index++, -1);+ } else {+ tensor = std::make_shared<DecoderProtoTensor>(&*initializer, this);+ }m_decoders.push_back(tensor);const auto& t_name = *tensor->get_tensor_info().m_tensor_name;if (m_tensors.count(t_name) > 0) {@@ -383,8 +394,9 @@ void GraphIteratorProto::reset() {}m_tensors.emplace(t_name, tensor);}+ index = 0;for (const auto& value : m_graph->output()) {- auto tensor = std::make_shared<DecoderProtoTensor>(&value, this, -1, 0);+ auto tensor = std::make_shared<DecoderProtoTensor>(&value, this, -1, index++);m_decoders.push_back(tensor);const auto& t_name = *tensor->get_tensor_info().m_tensor_name;if (m_tensors.count(t_name) == 0) {diff --git a/src/frontends/onnx/frontend/src/input_model.cpp b/src/frontends/onnx/frontend/src/input_model.cppindex 72375520254..b6cf45812e7 100644--- a/src/frontends/onnx/frontend/src/input_model.cpp+++ b/src/frontends/onnx/frontend/src/input_model.cpp@@ -4,6 +4,8 @@#include "input_model.hpp"+#include <utility>+#include "openvino/frontend/exception.hpp"#include "openvino/op/constant.hpp"#include "openvino/util/file_util.hpp"@@ -645,6 +647,13 @@ private:detail::MappedMemoryHandles m_mmap_cache;// This is used for keeping a readed external data without MMAPdetail::LocalStreamHandles m_stream_cache;++ std::shared_ptr<TensorONNXPlace> register_tensor_place(const std::shared_ptr<TensorONNXPlace>& tensor_place);+ std::shared_ptr<TensorONNXPlace> find_tensor_place(const TensorMetaInfo& tensor_meta_info) const;+ std::shared_ptr<TensorONNXPlace> ensure_tensor_place(const TensorMetaInfo& tensor_meta_info);+ void connect_inputs(const std::shared_ptr<OpPlace>& op_place, const std::shared_ptr<DecoderBaseOperation>& decoder);+ void connect_outputs(const std::shared_ptr<OpPlace>& op_place,+ const std::shared_ptr<DecoderBaseOperation>& decoder);};namespace {@@ -664,21 +673,6 @@ std::shared_ptr<ov::frontend::onnx::TensorONNXPlace> decode_tensor_place(return tensor_place;}-std::shared_ptr<ov::frontend::onnx::TensorONNXPlace> decode_input_tensor(- const std::shared_ptr<ov::frontend::onnx::DecoderBaseOperation>& decoder,- size_t idx,- const ov::frontend::InputModel& model) {- const auto& tensor_meta_info = decoder->get_input_tensor_info(idx);- return decode_tensor_place(tensor_meta_info, model);-}--std::shared_ptr<ov::frontend::onnx::TensorONNXPlace> decode_output_tensor(- const std::shared_ptr<ov::frontend::onnx::DecoderBaseOperation>& decoder,- size_t idx,- const ov::frontend::InputModel& model) {- const auto& tensor_meta_info = decoder->get_output_tensor_info(idx);- return decode_tensor_place(tensor_meta_info, model);-}} // namespacevoid InputModel::InputModelONNXImpl::load_model() {@@ -692,43 +686,40 @@ void InputModel::InputModelONNXImpl::load_model() {auto tensor_place = decode_tensor_place(tensor_decoder->get_tensor_info(), m_input_model);tensor_place->set_input_index(tensor_decoder->get_input_idx());tensor_place->set_output_index(tensor_decoder->get_output_idx());+// Constant with data has been foundif (tensor_place->get_data() != nullptr)continue;- auto name = tensor_place->get_names()[0];- if (m_tensor_places.count(name) == 0) {- m_tensor_places[name] = tensor_place;- }++ tensor_place = register_tensor_place(tensor_place);+ if (!tensor_place)+ continue;+if (tensor_place->is_input())m_inputs.push_back(tensor_place);if (tensor_place->is_output())m_outputs.push_back(tensor_place);- continue;- }- m_op_places.push_back(std::make_shared<OpPlace>(m_input_model, decoder));+ } else {+ auto op_place = std::make_shared<OpPlace>(m_input_model, decoder);+ m_op_places.push_back(op_place);- if (m_telemetry) {- if (auto op_decoder = std::dynamic_pointer_cast<DecoderBaseOperation>(decoder)) {- std::string op_name = decoder->get_op_type() + "-" +- std::to_string(m_graph_iterator->get_opset_version(op_decoder->get_domain()));+ auto operation_decoder = std::dynamic_pointer_cast<DecoderBaseOperation>(decoder);+ FRONT_END_GENERAL_CHECK(operation_decoder, "Operation decoder is expected");++ if (m_telemetry) {+ std::string op_name =+ operation_decoder->get_op_type() + "-" ++ std::to_string(m_graph_iterator->get_opset_version(operation_decoder->get_domain()));op_statistics[op_name]++;}- }- auto operation_decoder = std::dynamic_pointer_cast<DecoderBaseOperation>(decoder);- FRONT_END_GENERAL_CHECK(operation_decoder, "Operation decoder is expected");- for (size_t i = 0; i < operation_decoder->get_input_size(); ++i) {- auto place = decode_input_tensor(operation_decoder, i, m_input_model);- auto name = place->get_names()[0];- if (m_tensor_places.count(name) == 0) {- m_tensor_places[name] = place;+ const auto& operation_name = operation_decoder->get_op_name();+ if (!operation_name.empty()) {+ m_op_places_map[operation_name] = op_place;}- }- for (size_t i = 0; i < operation_decoder->get_output_size(); ++i) {- auto place = decode_output_tensor(operation_decoder, i, m_input_model);- auto name = place->get_names()[0];- if (m_tensor_places.count(name) == 0)- m_tensor_places[name] = place;++ connect_inputs(op_place, operation_decoder);+ connect_outputs(op_place, operation_decoder);}}@@ -751,7 +742,6 @@ void InputModel::InputModelONNXImpl::load_model() {};};std::sort(m_inputs.begin(), m_inputs.end(), sorting_places_by_idx(true));- std::sort(m_outputs.begin(), m_outputs.end(), sorting_places_by_idx(false));if (m_telemetry) {for (const auto& op : op_statistics) {@@ -762,6 +752,82 @@ void InputModel::InputModelONNXImpl::load_model() {m_metadata = m_graph_iterator->get_metadata();}+std::shared_ptr<TensorONNXPlace> InputModel::InputModelONNXImpl::register_tensor_place(+ const std::shared_ptr<TensorONNXPlace>& tensor_place) {+ if (!tensor_place) {+ return nullptr;+ }++ const auto& names = tensor_place->get_names();+ if (names.empty()) {+ return nullptr;+ }++ const auto& tensor_name = names.front();+ const auto it = m_tensor_places.emplace(tensor_name, tensor_place).first;+ return it->second;+}++std::shared_ptr<TensorONNXPlace> InputModel::InputModelONNXImpl::find_tensor_place(+ const TensorMetaInfo& tensor_meta_info) const {+ if (!tensor_meta_info.m_tensor_name || tensor_meta_info.m_tensor_name->empty()) {+ return nullptr;+ }++ const auto it = m_tensor_places.find(*tensor_meta_info.m_tensor_name);+ if (it == m_tensor_places.end()) {+ return nullptr;+ }+ return it->second;+}++std::shared_ptr<TensorONNXPlace> InputModel::InputModelONNXImpl::ensure_tensor_place(+ const TensorMetaInfo& tensor_meta_info) {+ if (auto existing = find_tensor_place(tensor_meta_info)) {+ return existing;+ }+ return register_tensor_place(decode_tensor_place(tensor_meta_info, m_input_model));+}++void InputModel::InputModelONNXImpl::connect_inputs(const std::shared_ptr<OpPlace>& op_place,+ const std::shared_ptr<DecoderBaseOperation>& decoder) {+ const auto input_count = decoder->get_input_size();+ for (size_t i = 0; i < input_count; ++i) {+ auto tensor_place = ensure_tensor_place(decoder->get_input_tensor_info(i));+ if (!tensor_place) {+ continue;+ }++ auto in_port = std::make_shared<InPortPlace>(m_input_model);+ tensor_place->add_consuming_port(in_port);+ in_port->set_source_tensor(tensor_place);+ in_port->set_op(op_place);++ std::string port_name = decoder->get_input_tensor_name(i);+ if (port_name.empty()) {+ port_name = "input_" + std::to_string(i);+ }+ op_place->add_in_port(in_port, port_name);+ }+}++void InputModel::InputModelONNXImpl::connect_outputs(const std::shared_ptr<OpPlace>& op_place,+ const std::shared_ptr<DecoderBaseOperation>& decoder) {+ const auto output_count = decoder->get_output_size();+ for (size_t i = 0; i < output_count; ++i) {+ auto tensor_place = ensure_tensor_place(decoder->get_output_tensor_info(i));+ if (!tensor_place) {+ continue;+ }++ auto out_port = std::make_shared<OutPortPlace>(m_input_model);+ tensor_place->add_producing_port(out_port);+ out_port->set_target_tensor(tensor_place);+ out_port->set_op(op_place);+ op_place->add_out_port(out_port, static_cast<int>(i));+ }+}+InputModel::InputModelONNXImpl::InputModelONNXImpl(const GraphIterator::Ptr& graph_iterator,const ov::frontend::InputModel& input_model,const std::shared_ptr<TelemetryExtension>& telemetry,@@ -862,6 +928,19 @@ void InputModel::InputModelONNXImpl::set_name_for_operation(const Place::Ptr& op}void InputModel::InputModelONNXImpl::override_all_inputs(const std::vector<ov::frontend::Place::Ptr>& inputs) {+ // Only support the case when new inputs are same as before+ bool is_same_as_existing_inputs = true;+ for (const auto& input : inputs) {+ if (!std::any_of(m_inputs.begin(), m_inputs.end(), [&input](const auto& existing_input) {+ return input->is_equal(existing_input);+ })) {+ is_same_as_existing_inputs = false;+ break;+ }+ }+ if (is_same_as_existing_inputs) {+ return;+ }FRONT_END_NOT_IMPLEMENTED(override_all_inputs);}diff --git a/src/frontends/onnx/tests/load_from.cpp b/src/frontends/onnx/tests/load_from.cppindex 6ad0c775692..b81a504cb74 100644--- a/src/frontends/onnx/tests/load_from.cpp+++ b/src/frontends/onnx/tests/load_from.cpp@@ -7,6 +7,7 @@#include <onnx/onnx_pb.h>#include <fstream>+#include <vector>#include "common_test_utils/test_assertions.hpp"#include "onnx_utils.hpp"@@ -159,3 +160,21 @@ TEST_P(FrontEndLoadFromTest, testLoadFromModelProtoUint64_Negative) {testing::HasSubstr("unsupported IR version"));}// !!! End of Experimental feature !!!++TEST(FrontEndInputModel, InitializersAreNotModelInputs) {+ ov::frontend::FrontEnd::Ptr fe;+ auto input_model = ov::frontend::onnx::tests::load_model("regression/initializer_shares_input_name.onnx", &fe);+ ASSERT_NE(input_model, nullptr);++ auto inputs = input_model->get_inputs();+ ASSERT_EQ(inputs.size(), 1);++ std::vector<std::string> input_names;+ input_names.reserve(inputs.size());+ for (const auto& place : inputs) {+ ASSERT_FALSE(place->get_names().empty());+ input_names.push_back(place->get_names().front());+ }++ EXPECT_THAT(input_names, ElementsAre("data"));+}diff --git a/src/frontends/onnx/tests/models/regression/initializer_shares_input_name.prototxt b/src/frontends/onnx/tests/models/regression/initializer_shares_input_name.prototxtnew file mode 100644index 00000000000..cfce1c271e0--- /dev/null+++ b/src/frontends/onnx/tests/models/regression/initializer_shares_input_name.prototxt@@ -0,0 +1,65 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+doc_string: "Model where initializer shares name with a declared input"+graph {+ name: "Initializer matches input"+ node {+ input: "data"+ input: "weight"+ output: "sum"+ op_type: "Add"+ }++ input {+ name: "data"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }++ input {+ name: "weight"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }++ initializer {+ dims: 1+ data_type: 1+ name: "weight"+ float_data: 0.5+ }++ output {+ name: "sum"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+}++opset_import {+ version: 8+}