Skip to content

Commit

Permalink
[ONNX] Frontend refactoring (openvinotoolkit#22178)
Browse files Browse the repository at this point in the history
* Cleanup ngraph headers

* Updates of editor.hpp/cpp

* Replaced Function by ov::Model in graph.cpp/hpp

* Fixed code style

* Refactored operations which are needed to build on master
  • Loading branch information
gkrivor authored Jan 17, 2024
1 parent 14183ae commit 388d2bb
Show file tree
Hide file tree
Showing 29 changed files with 152 additions and 172 deletions.
8 changes: 4 additions & 4 deletions src/frontends/onnx/frontend/include/onnx_import/core/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
#include <cstddef>
#include <string>

#include "ngraph/deprecated.hpp"
#include "ngraph/except.hpp"
#include "ngraph/node.hpp"
#include "ngraph/op/constant.hpp"
#include "onnx_import/onnx_importer_visibility.hpp"
#include "openvino/core/deprecated.hpp"
#include "openvino/core/except.hpp"
#include "openvino/core/node.hpp"
#include "openvino/op/constant.hpp"

namespace ONNX_NAMESPACE {
// forward declaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <string>
#include <unordered_map>

#include "ngraph/node.hpp"
#include "onnx_import/core/node.hpp"

namespace ngraph {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#include <cstdint>
#include <string>

#include "ngraph/deprecated.hpp"
#include "onnx_import/core/operator_set.hpp"
#include "onnx_importer_visibility.hpp"
#include "openvino/core/deprecated.hpp"

namespace ngraph {
namespace onnx_import {
Expand Down
2 changes: 1 addition & 1 deletion src/frontends/onnx/frontend/src/core/attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "core/sparse_tensor.hpp"
#include "core/tensor.hpp"
#include "ngraph/except.hpp"
#include "openvino/core/except.hpp"

namespace ngraph {
namespace onnx_import {
Expand Down
37 changes: 18 additions & 19 deletions src/frontends/onnx/frontend/src/core/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "core/transform.hpp"
#include "core/value_info.hpp"
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "onnx_framework_node.hpp"
#include "onnx_import/core/node.hpp"
Expand Down Expand Up @@ -148,7 +147,7 @@ Graph::Graph(const std::string& model_dir,
for (const auto& initializer_tensor : m_model->get_graph().initializer()) {
if (initializer_tensor.has_name()) {
Tensor tensor = Tensor{initializer_tensor, m_model_dir, m_mmap_cache};
std::shared_ptr<default_opset::Constant> ov_constant;
std::shared_ptr<ov::op::v0::Constant> ov_constant;
// For each initializer create a Constant node and store it in cache
try {
ov_constant = tensor.get_ov_constant();
Expand All @@ -165,7 +164,7 @@ Graph::Graph(const std::string& model_dir,
}
}

// Process all ONNX graph inputs, convert them to nGraph nodes and store in cache
// Process all ONNX graph inputs, convert them to OV nodes and store in cache
for (const auto& input : m_model->get_graph().input()) {
// Check if a Constant node was already created from an initializer
if (m_cache->contains(input.name())) {
Expand All @@ -183,7 +182,7 @@ void Graph::convert_to_ov_nodes() {
const float total = static_cast<float>(m_model->get_graph().node().size());
unsigned int completed = 0u;
std::map<std::string, uint64_t> op_statistics;
// Process ONNX graph nodes, convert to nGraph nodes
// Process ONNX graph nodes, convert to OV nodes
for (const auto& node_proto : m_model->get_graph().node()) {
if (m_extensions.telemetry) {
op_statistics[node_proto.op_type()]++;
Expand Down Expand Up @@ -249,10 +248,10 @@ void Graph::set_metadata(std::shared_ptr<ov::Model>& model) const {
}
}

std::shared_ptr<Function> Graph::convert() {
std::shared_ptr<ov::Model> Graph::convert() {
convert_to_ov_nodes();
remove_dangling_parameters();
auto function = create_function();
auto function = create_model();
set_metadata(function);
return function;
}
Expand All @@ -263,10 +262,10 @@ OutputVector Graph::make_framework_nodes(const Node& onnx_node) {
if (onnx_node.has_subgraphs()) {
const auto& subgraphs = onnx_node.get_subgraphs();
auto inputs = onnx_node.get_ng_inputs();
std::vector<std::shared_ptr<Function>> functions;
std::vector<std::shared_ptr<ov::Model>> models;
for (const auto& kv : subgraphs) {
auto& subgraph = kv.second;
functions.push_back(subgraph->decode());
models.push_back(subgraph->decode());
for (const auto& input : subgraph->get_inputs_from_parent()) {
const auto& name = input.get_node()->get_friendly_name();
if (std::find_if(inputs.begin(), inputs.end(), [&name](const Output<ov::Node>& n) -> bool {
Expand All @@ -276,7 +275,7 @@ OutputVector Graph::make_framework_nodes(const Node& onnx_node) {
}
}
}
framework_node = std::make_shared<frontend::ONNXSubgraphFrameworkNode>(onnx_node, functions, inputs);
framework_node = std::make_shared<frontend::ONNXSubgraphFrameworkNode>(onnx_node, models, inputs);
} else {
framework_node = std::make_shared<frontend::ONNXFrameworkNode>(onnx_node);
}
Expand All @@ -287,7 +286,7 @@ void Graph::decode_to_framework_nodes() {
const float total = static_cast<float>(m_model->get_graph().node().size());
unsigned int completed = 0u;
std::map<std::string, uint64_t> op_statistics;
// Process ONNX graph nodes, convert to nGraph nodes
// Process ONNX graph nodes, convert to OV nodes
for (const auto& node_proto : m_model->get_graph().node()) {
if (m_extensions.telemetry) {
op_statistics[node_proto.op_type()]++;
Expand All @@ -312,22 +311,22 @@ void Graph::decode_to_framework_nodes() {
}
OPENVINO_SUPPRESS_DEPRECATED_END

std::shared_ptr<Function> Graph::create_function() {
auto function = std::make_shared<Function>(get_ov_outputs(), m_parameters, get_name());
std::shared_ptr<ov::Model> Graph::create_model() {
auto model = std::make_shared<ov::Model>(get_ov_outputs(), m_parameters, get_name());
const auto& onnx_outputs = m_model->get_graph().output();
for (std::size_t i{0}; i < function->get_output_size(); ++i) {
const auto& result_node = function->get_output_op(i);
for (std::size_t i{0}; i < model->get_output_size(); ++i) {
const auto& result_node = model->get_output_op(i);
const std::string onnx_output_name = onnx_outputs.Get(static_cast<int>(i)).name();
result_node->set_friendly_name(onnx_output_name + "/sink_port_0");
const auto& previous_operation = result_node->get_input_node_shared_ptr(0);
previous_operation->set_friendly_name(onnx_output_name);
}
return function;
return model;
}

std::shared_ptr<Function> Graph::decode() {
std::shared_ptr<ov::Model> Graph::decode() {
decode_to_framework_nodes();
auto function = create_function();
auto function = create_model();
auto& rt_info = function->get_rt_info();
rt_info[ONNX_GRAPH_RT_ATTRIBUTE] = shared_from_this();
return function;
Expand Down Expand Up @@ -486,9 +485,9 @@ Output<ov::Node> Subgraph::get_ov_node_from_cache(const std::string& name) {
return new_param;
}

std::shared_ptr<Function> Subgraph::convert() {
std::shared_ptr<ov::Model> Subgraph::convert() {
convert_to_ov_nodes();
return create_function();
return create_model();
}

const std::vector<Output<ov::Node>> Subgraph::get_inputs_from_parent() const {
Expand Down
8 changes: 4 additions & 4 deletions src/frontends/onnx/frontend/src/core/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Graph : public std::enable_shared_from_this<Graph> {

Graph& operator=(const Graph&) = delete;
Graph& operator=(Graph&&) = default;
std::shared_ptr<Function> decode();
virtual std::shared_ptr<Function> convert();
std::shared_ptr<ov::Model> decode();
virtual std::shared_ptr<ov::Model> convert();
OutputVector get_ov_outputs();
const std::string& get_name() const {
return m_model->get_graph().name();
Expand Down Expand Up @@ -80,7 +80,7 @@ class Graph : public std::enable_shared_from_this<Graph> {
void convert_to_ov_nodes();
void remove_dangling_parameters();
void set_metadata(std::shared_ptr<ov::Model>& model) const;
std::shared_ptr<Function> create_function();
std::shared_ptr<ov::Model> create_model();

ParameterVector m_parameters;
std::unique_ptr<Model> m_model;
Expand Down Expand Up @@ -111,7 +111,7 @@ class Subgraph : public Graph {
/// \return Vector of edge nodes from parent scope.
const std::vector<Output<ov::Node>> get_inputs_from_parent() const;

std::shared_ptr<Function> convert() override;
std::shared_ptr<ov::Model> convert() override;

Subgraph() = delete;

Expand Down
6 changes: 3 additions & 3 deletions src/frontends/onnx/frontend/src/core/sparse_tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include <vector>

#include "ngraph/shape.hpp"
#include "ngraph/type/element_type.hpp"
#include "openvino/core/shape.hpp"
#include "openvino/core/type/element_type.hpp"
#include "tensor.hpp"

namespace ngraph {
Expand All @@ -26,7 +26,7 @@ class SparseTensor {
if (m_shape == Shape{0}) {
// It's possible to construct a sparse tensor in ONNX with "dims: 0" property
// Such tensor contains a scalar. This results in a Shape{0} stored in m_shape.
// In nGraph a scalar is represented with Shape{} and thus this replacement.
// In OpenVINO a scalar is represented with Shape{} and thus this replacement.
m_shape = Shape{};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/frontends/onnx/frontend/src/core/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static const std::vector<std::string> legacy_ops_to_fixup = {"DeformableConv2D",
/// Some legacy models use custom operators (listed in legacy_ops_to_fixup vector) which
/// were registered in the default ONNX domain. This function updates nodes with these
/// operations to use OPENVINO_ONNX_DOMAIN in order to process them correctly
/// in the nGraph ONNX Importer.
/// in the OpenVINO ONNX Frontend.
///
/// \param model_proto Protobuf message with ONNX model to transform.
void fixup_legacy_operators(ONNX_NAMESPACE::ModelProto& model_proto);
Expand Down
17 changes: 8 additions & 9 deletions src/frontends/onnx/frontend/src/core/value_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
#include <onnx/onnx_pb.h>

#include "core/tensor.hpp"
#include "default_opset.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/op/parameter.hpp"
#include "ngraph/partial_shape.hpp"
#include "ngraph/type/element_type.hpp"
#include "onnx_common/utils.hpp"
#include "onnx_import/core/node.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/parameter.hpp"
#include "utils/common.hpp"

using namespace ov::frontend::onnx::common;

using namespace ov::op;

namespace ngraph {
namespace onnx_import {
class ValueInfo {
Expand Down Expand Up @@ -49,7 +48,7 @@ class ValueInfo {
if (m_value_info_proto->type().tensor_type().has_elem_type()) {
return common::get_ov_element_type(m_value_info_proto->type().tensor_type().elem_type());
}
return ngraph::element::dynamic;
return ov::element::dynamic;
}

std::shared_ptr<ov::Node> get_ov_node(ParameterVector& parameters,
Expand All @@ -63,14 +62,14 @@ class ValueInfo {
}

protected:
std::shared_ptr<ngraph::op::Parameter> get_ov_parameter() const {
auto parameter = std::make_shared<ngraph::op::Parameter>(get_element_type(), get_shape());
std::shared_ptr<v0::Parameter> get_ov_parameter() const {
auto parameter = std::make_shared<v0::Parameter>(get_element_type(), get_shape());
parameter->set_friendly_name(get_name());
parameter->get_output_tensor(0).set_names({get_name()});
return parameter;
}

std::shared_ptr<ngraph::op::Constant> get_ov_constant(const Tensor& tensor) const {
std::shared_ptr<v0::Constant> get_ov_constant(const Tensor& tensor) const {
return tensor.get_ov_constant();
}

Expand Down
2 changes: 1 addition & 1 deletion src/frontends/onnx/frontend/src/edge_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <algorithm>

#include "ngraph/except.hpp"
#include "openvino/core/except.hpp"
#include "openvino/frontend/exception.hpp"

using namespace ov;
Expand Down
8 changes: 4 additions & 4 deletions src/frontends/onnx/frontend/src/edge_mapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class EdgeMapper {
/// In such a case the algorthim tries to match the given node name
/// with the input name (providing an input index is not enough).
/// If a unique edge is found, it will be returned.
/// If InputEdge cannot be determined based on parameter values an ngraph_error
/// exception will be thrown.
/// If InputEdge cannot be determined based on parameter values an
/// ov:Exception will be thrown.
///
/// \param node An EditorNode helper structure created based on a node name
/// or a node output name.
Expand All @@ -56,8 +56,8 @@ class EdgeMapper {
/// In such a case the algorthim will try to match the given node name
/// with the output name (providing an output index is not enough).
/// If after such operation a found edge is unique, it is returned.
/// If OutputEdge cannot be determined based on given params the ngraph_error
/// exception is thrown.
/// If OutputEdge cannot be determined based on given params an
/// ov::Exception is thrown.
///
/// \param node An EditorNode helper structure created based on a node name
/// or a node output name.
Expand Down
10 changes: 5 additions & 5 deletions src/frontends/onnx/frontend/src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace ov;
using namespace ov::onnx_editor;
using namespace ov::frontend::onnx::common;

NGRAPH_SUPPRESS_DEPRECATED_START
OPENVINO_SUPPRESS_DEPRECATED_START

namespace {
using namespace ONNX_NAMESPACE;
Expand Down Expand Up @@ -97,7 +97,7 @@ void add_dim_to_onnx_shape(const Dimension& dim, ONNX_NAMESPACE::TensorShapeProt
if (dim.is_static()) {
new_dim->set_dim_value(dim.get_length());
} else {
// nGraph Dimension is also considered dynamic if it represents a constrained range
// Dimension is also considered dynamic if it represents a constrained range
// of allowed values as well as if it's unconstrained at all. ONNX cannot represent
// ranged dimensions so this might not be 100% accurate. The modified ONNX model will
// always have a fully dynamic dimension in this case.
Expand Down Expand Up @@ -140,7 +140,7 @@ std::string extract_name(const T& input_or_initializer) {

void modify_initializer(TensorProto& initializer,
const std::string& name,
const std::shared_ptr<ngraph::op::Constant> values,
const std::shared_ptr<ov::op::v0::Constant> values,
ValueInfoProto* input) {
const auto elem_type = values->get_element_type();
OPENVINO_ASSERT(is_supported_ov_type(elem_type),
Expand Down Expand Up @@ -392,7 +392,7 @@ element::Type_t onnx_editor::ONNXModelEditor::get_input_type(const std::string&
return ngraph::onnx_import::common::get_ov_element_type(type);
}

void onnx_editor::ONNXModelEditor::set_input_shapes(const std::map<std::string, ngraph::PartialShape>& input_shapes) {
void onnx_editor::ONNXModelEditor::set_input_shapes(const std::map<std::string, ov::PartialShape>& input_shapes) {
auto* onnx_graph = m_pimpl->m_model_proto->mutable_graph();

for (const auto& input_desc : input_shapes) {
Expand Down Expand Up @@ -540,7 +540,7 @@ std::shared_ptr<Model> onnx_editor::ONNXModelEditor::get_function() const {
}

void onnx_editor::ONNXModelEditor::set_input_values(
const std::map<std::string, std::shared_ptr<ngraph::op::Constant>>& input_values) {
const std::map<std::string, std::shared_ptr<ov::op::v0::Constant>>& input_values) {
auto onnx_graph = m_pimpl->m_model_proto->mutable_graph();

for (const auto& input : input_values) {
Expand Down
Loading

0 comments on commit 388d2bb

Please sign in to comment.