Skip to content

Commit

Permalink
[ONNX] Frontend refactoring (#22064)
Browse files Browse the repository at this point in the history
* Cleanup in frontend.cpp

* Cleanup input_model.hpp/cpp

* Refactored onnx_framework_node.hpp/cpp

* Updated arg_min_max_factory.cpp/hpp

* Updated conv_factory.cpp/hpp
  • Loading branch information
gkrivor authored Jan 10, 2024
1 parent 2a4ae7c commit 0703ac2
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 104 deletions.
6 changes: 3 additions & 3 deletions src/frontends/onnx/frontend/src/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const
return nullptr;
}

std::shared_ptr<ngraph::Function> FrontEnd::convert_partially(const InputModel::Ptr& model) const {
std::shared_ptr<ov::Model> FrontEnd::convert_partially(const InputModel::Ptr& model) const {
auto model_onnx = std::dynamic_pointer_cast<InputModel>(model);
FRONT_END_GENERAL_CHECK(model_onnx != nullptr, "Invalid input model");

Expand Down Expand Up @@ -118,7 +118,7 @@ void FrontEnd::normalize(const std::shared_ptr<ov::Model>& model) const {
manager.run_passes(model);
}

std::shared_ptr<ngraph::Function> FrontEnd::convert(const InputModel::Ptr& model) const {
std::shared_ptr<ov::Model> FrontEnd::convert(const InputModel::Ptr& model) const {
const auto partially_converted = convert_partially(model);

const auto error_message = ngraph::onnx_import::common::collect_translation_exceptions(partially_converted);
Expand All @@ -134,7 +134,7 @@ void FrontEnd::convert(const std::shared_ptr<ov::Model>& partially_converted) co
normalize(partially_converted);
}

std::shared_ptr<ngraph::Function> FrontEnd::decode(const InputModel::Ptr& model) const {
std::shared_ptr<ov::Model> FrontEnd::decode(const InputModel::Ptr& model) const {
auto model_onnx = std::dynamic_pointer_cast<InputModel>(model);
FRONT_END_GENERAL_CHECK(model_onnx != nullptr, "Invalid input model");
return model_onnx->decode();
Expand Down
10 changes: 5 additions & 5 deletions src/frontends/onnx/frontend/src/input_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using namespace ov;
using namespace ov::frontend::onnx;

NGRAPH_SUPPRESS_DEPRECATED_START
OPENVINO_SUPPRESS_DEPRECATED_START

InputModel::InputModel(const std::string& path, const bool enable_mmap, frontend::ExtensionHolder extensions)
: m_editor{std::make_shared<onnx_editor::ONNXModelEditor>(path, enable_mmap, std::move(extensions))} {}
Expand Down Expand Up @@ -152,7 +152,7 @@ void InputModel::free_name_for_tensor(const std::string&) {
FRONT_END_THROW("Method free_name_for_tensor is not applicable for ONNX model. ONNX tensor name is an identifier.");
}

void InputModel::set_partial_shape(const ov::frontend::Place::Ptr& place, const ngraph::PartialShape& shape) {
void InputModel::set_partial_shape(const ov::frontend::Place::Ptr& place, const ov::PartialShape& shape) {
std::string input_name; // name of the model input which should be reshaped
const auto input_edge = std::dynamic_pointer_cast<PlaceInputEdge>(place);
if (input_edge) {
Expand All @@ -172,7 +172,7 @@ void InputModel::set_partial_shape(const ov::frontend::Place::Ptr& place, const
m_inputs_to_reshape[input_name] = shape;
}

ngraph::PartialShape InputModel::get_partial_shape(const ov::frontend::Place::Ptr& place) const {
ov::PartialShape InputModel::get_partial_shape(const ov::frontend::Place::Ptr& place) const {
std::string tensor_name; // name of the model input which should be reshaped
const auto input_edge = std::dynamic_pointer_cast<PlaceInputEdge>(place);
const auto output_edge = std::dynamic_pointer_cast<PlaceOutputEdge>(place);
Expand All @@ -193,8 +193,8 @@ ngraph::PartialShape InputModel::get_partial_shape(const ov::frontend::Place::Pt
return m_editor->get_tensor_shape(tensor_name);
}

void InputModel::set_element_type(const ov::frontend::Place::Ptr& place, const ngraph::element::Type& type) {
std::map<std::string, ngraph::element::Type_t> m;
void InputModel::set_element_type(const ov::frontend::Place::Ptr& place, const ov::element::Type& type) {
std::map<std::string, ov::element::Type_t> m;
m[place->get_names().at(0)] = type;
m_editor->set_input_types(m);
}
Expand Down
6 changes: 3 additions & 3 deletions src/frontends/onnx/frontend/src/input_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class InputModel : public ov::frontend::InputModel {
/// \brief Not applicable for ONNX model. Throws immediately
void free_name_for_tensor(const std::string& name) override;

void set_partial_shape(const ov::frontend::Place::Ptr& place, const ngraph::PartialShape& shape) override;
ngraph::PartialShape get_partial_shape(const ov::frontend::Place::Ptr& place) const override;
void set_element_type(const ov::frontend::Place::Ptr& place, const ngraph::element::Type& type) override;
void set_partial_shape(const ov::frontend::Place::Ptr& place, const ov::PartialShape& shape) override;
ov::PartialShape get_partial_shape(const ov::frontend::Place::Ptr& place) const override;
void set_element_type(const ov::frontend::Place::Ptr& place, const ov::element::Type& type) override;
ov::element::Type get_element_type(const ov::frontend::Place::Ptr& place) const override;
ov::frontend::Place::Ptr add_output(const ov::frontend::Place::Ptr& place) override;
void remove_output(const ov::frontend::Place::Ptr& place) override;
Expand Down
2 changes: 1 addition & 1 deletion src/frontends/onnx/frontend/src/onnx_framework_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ std::shared_ptr<Node> ONNXFrameworkNode::clone_with_new_inputs(const OutputVecto
}

std::shared_ptr<Node> ONNXSubgraphFrameworkNode::clone_with_new_inputs(const OutputVector& inputs) const {
return std::make_shared<ONNXSubgraphFrameworkNode>(m_node, m_functions, inputs);
return std::make_shared<ONNXSubgraphFrameworkNode>(m_node, m_models, inputs);
}

std::shared_ptr<Node> NotSupportedONNXNode::clone_with_new_inputs(const OutputVector& inputs) const {
Expand Down
29 changes: 14 additions & 15 deletions src/frontends/onnx/frontend/src/onnx_framework_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

#pragma once

#include <core/graph.hpp>
#include <ngraph/function.hpp>
#include <ngraph/graph_util.hpp>
#include <onnx_import/core/node.hpp>
#include <openvino/op/util/framework_node.hpp>
#include "core/graph.hpp"
#include "onnx_import/core/node.hpp"
#include "openvino/core/model.hpp"
#include "openvino/op/util/framework_node.hpp"

namespace ONNX_NAMESPACE {
// forward declaration
Expand Down Expand Up @@ -49,12 +48,12 @@ class ONNXFrameworkNode : public ov::op::util::FrameworkNode {
set_attrs(attrs);
}

OutputVector get_ng_nodes(const std::shared_ptr<onnx_import::Graph>& graph) const {
OutputVector ng_nodes{graph->make_ov_nodes(m_node)};
if (ng_nodes.size() > get_output_size()) {
ng_nodes.resize(get_output_size());
OutputVector get_ov_nodes(const std::shared_ptr<onnx_import::Graph>& graph) const {
OutputVector ov_nodes{graph->make_ov_nodes(m_node)};
if (ov_nodes.size() > get_output_size()) {
ov_nodes.resize(get_output_size());
}
return ng_nodes;
return ov_nodes;
}

virtual std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& inputs) const override;
Expand All @@ -77,24 +76,24 @@ class ONNXSubgraphFrameworkNode : public ONNXFrameworkNode {
OPENVINO_OP("ONNXSubgraphFrameworkNode", "util", ONNXFrameworkNode);

ONNXSubgraphFrameworkNode(const onnx_import::Node& node,
const std::vector<std::shared_ptr<Function>>& functions,
const std::vector<std::shared_ptr<ov::Model>>& models,
const OutputVector& inputs)
: ONNXFrameworkNode(node, inputs),
m_functions(functions) {}
m_models(models) {}

void infer_inputs_from_parent() {
for (auto& subgraph : m_node.get_subgraphs())
subgraph.second->infer_inputs_from_parent();
}

const std::vector<std::shared_ptr<Function>>& get_subgraph_functions() const {
return m_functions;
const std::vector<std::shared_ptr<ov::Model>>& get_subgraph_models() const {
return m_models;
}

virtual std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& inputs) const override;

private:
std::vector<std::shared_ptr<Function>> m_functions;
std::vector<std::shared_ptr<ov::Model>> m_models;
};
OPENVINO_SUPPRESS_DEPRECATED_END

Expand Down
70 changes: 35 additions & 35 deletions src/frontends/onnx/frontend/src/utils/arg_min_max_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

#include "utils/arg_min_max_factory.hpp"

#include "default_opset.hpp"
#include "ngraph/opsets/opset1.hpp"
#include "ngraph/validation_util.hpp"
#include "openvino/core/validation_util.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/reverse.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/topk.hpp"

using namespace ov;
using namespace ov::op;

namespace ngraph {
namespace onnx_import {
Expand All @@ -19,16 +28,16 @@ ArgMinMaxFactory::ArgMinMaxFactory(const Node& node)
m_select_last_index{node.get_attribute_value<std::int64_t>("select_last_index", 0)} {}
OPENVINO_SUPPRESS_DEPRECATED_END

std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_arg_max() const {
return make_topk_subgraph(default_opset::TopK::Mode::MAX);
std::shared_ptr<ov::Node> ArgMinMaxFactory::make_arg_max() const {
return make_topk_subgraph(v11::TopK::Mode::MAX);
}

std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_arg_min() const {
return make_topk_subgraph(default_opset::TopK::Mode::MIN);
std::shared_ptr<ov::Node> ArgMinMaxFactory::make_arg_min() const {
return make_topk_subgraph(v11::TopK::Mode::MIN);
}

std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_topk_subgraph(default_opset::TopK::Mode mode) const {
const auto k_node = default_opset::Constant::create(ngraph::element::i64, Shape{}, {1});
std::shared_ptr<ov::Node> ArgMinMaxFactory::make_topk_subgraph(v11::TopK::Mode mode) const {
const auto k_node = v0::Constant::create(element::i64, Shape{}, {1});

if (m_select_last_index == 1) {
// Example (ArgMin):
Expand Down Expand Up @@ -57,49 +66,40 @@ std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_topk_subgraph(default_opset

OPENVINO_SUPPRESS_DEPRECATED_START
const int64_t normalized_axis =
normalize_axis(m_input_node.get_node(), m_axis, m_input_node.get_partial_shape().rank());
ov::normalize_axis(m_input_node.get_node(), m_axis, m_input_node.get_partial_shape().rank());
OPENVINO_SUPPRESS_DEPRECATED_END

const auto axis_node = default_opset::Constant::create(ngraph::element::i64, Shape{1}, {normalized_axis});
const auto reverse = std::make_shared<opset1::Reverse>(m_input_node, axis_node, opset1::Reverse::Mode::INDEX);
const auto axis_node = v0::Constant::create(element::i64, Shape{1}, {normalized_axis});
const auto reverse = std::make_shared<v1::Reverse>(m_input_node, axis_node, v1::Reverse::Mode::INDEX);

const auto topk = std::make_shared<default_opset::TopK>(reverse,
k_node,
normalized_axis,
mode,
default_opset::TopK::SortType::NONE);
const auto topk = std::make_shared<v11::TopK>(reverse, k_node, normalized_axis, mode, v1::TopK::SortType::NONE);

const auto data_shape = std::make_shared<default_opset::ShapeOf>(m_input_node);
const auto dims_on_axis = std::make_shared<default_opset::Gather>(
data_shape,
axis_node,
default_opset::Constant::create(ngraph::element::i64, Shape{}, {0}));
const auto data_shape = std::make_shared<v0::ShapeOf>(m_input_node);
const auto dims_on_axis =
std::make_shared<v1::Gather>(data_shape, axis_node, v0::Constant::create(element::i64, Shape{}, {0}));

const auto res_index = std::make_shared<default_opset::Subtract>(
dims_on_axis,
std::make_shared<default_opset::Convert>(topk->output(1), element::i64));
const auto result = std::make_shared<default_opset::Subtract>(
res_index,
default_opset::Constant::create(ngraph::element::i64, Shape{1}, {1}));
const auto res_index =
std::make_shared<v1::Subtract>(dims_on_axis, std::make_shared<v0::Convert>(topk->output(1), element::i64));
const auto result =
std::make_shared<v1::Subtract>(res_index, v0::Constant::create(element::i64, Shape{1}, {1}));

if (m_keep_dims == 0) {
const auto axis_to_remove = default_opset::Constant::create(element::u64, Shape{}, {topk->get_axis()});
const auto axis_to_remove = v0::Constant::create(element::u64, Shape{}, {topk->get_axis()});

return std::make_shared<default_opset::Squeeze>(result, axis_to_remove);
return std::make_shared<v0::Squeeze>(result, axis_to_remove);
}

return result;
}

const auto topk =
std::make_shared<default_opset::TopK>(m_input_node, k_node, m_axis, mode, default_opset::TopK::SortType::NONE);
const auto topk = std::make_shared<v11::TopK>(m_input_node, k_node, m_axis, mode, v11::TopK::SortType::NONE);

const auto result = std::make_shared<default_opset::Convert>(topk->output(1), element::i64);
const auto result = std::make_shared<v0::Convert>(topk->output(1), element::i64);

if (m_keep_dims == 0) {
const auto axis_to_remove = default_opset::Constant::create(element::u64, Shape{}, {topk->get_axis()});
const auto axis_to_remove = v0::Constant::create(element::u64, Shape{}, {topk->get_axis()});

return std::make_shared<default_opset::Squeeze>(result, axis_to_remove);
return std::make_shared<v0::Squeeze>(result, axis_to_remove);
}

return result;
Expand Down
12 changes: 6 additions & 6 deletions src/frontends/onnx/frontend/src/utils/arg_min_max_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <cstdint>
#include <memory>

#include "default_opset.hpp"
#include "ngraph/node.hpp"
#include "onnx_import/core/node.hpp"
#include "openvino/core/deprecated.hpp"
#include "openvino/core/node.hpp"
#include "openvino/op/topk.hpp"

namespace ngraph {
namespace onnx_import {
Expand All @@ -25,17 +25,17 @@ class ArgMinMaxFactory {

/// \brief Creates ArgMax ONNX operation.
/// \return Sub-graph representing ArgMax op.
std::shared_ptr<ngraph::Node> make_arg_max() const;
std::shared_ptr<ov::Node> make_arg_max() const;

/// \brief Creates ArgMin ONNX operation.
/// \return Sub-graph representing ArgMin op.
std::shared_ptr<ngraph::Node> make_arg_min() const;
std::shared_ptr<ov::Node> make_arg_min() const;

private:
std::shared_ptr<ngraph::Node> make_topk_subgraph(default_opset::TopK::Mode mode) const;
std::shared_ptr<ov::Node> make_topk_subgraph(ov::op::v11::TopK::Mode mode) const;

const std::int64_t m_keep_dims;
Output<ngraph::Node> m_input_node;
Output<ov::Node> m_input_node;
std::int64_t m_axis;
std::int64_t m_select_last_index;
};
Expand Down
46 changes: 23 additions & 23 deletions src/frontends/onnx/frontend/src/utils/conv_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/op/group_conv.hpp"
#include "ngraph/op/util/attr_types.hpp"
#include "onnx_import/core/null_node.hpp"
#include "openvino/op/group_conv.hpp"
#include "openvino/op/util/attr_types.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "utils/conv_factory.hpp"
#include "utils/convpool.hpp"
Expand All @@ -17,32 +17,32 @@
namespace ngraph {
namespace onnx_import {
namespace conv_factory {
std::shared_ptr<ov::op::Op> make_ng_convolution(const Output<ngraph::Node>& data,
const Output<ngraph::Node>& filters,
const ngraph::Strides& strides,
const ngraph::Strides& dilations,
const ngraph::CoordinateDiff& padding_below,
const ngraph::CoordinateDiff& padding_above,
std::shared_ptr<ov::op::Op> make_ng_convolution(const Output<ov::Node>& data,
const Output<ov::Node>& filters,
const ov::Strides& strides,
const ov::Strides& dilations,
const ov::CoordinateDiff& padding_below,
const ov::CoordinateDiff& padding_above,
int64_t groups,
const ngraph::op::PadType& auto_pad) {
const ov::op::PadType& auto_pad) {
if (groups > 1) {
const auto reshaped_filters = convpool::get_reshaped_filters(filters, groups);

return std::make_shared<default_opset::GroupConvolution>(data,
reshaped_filters,
strides,
padding_below,
padding_above,
dilations,
auto_pad);
return std::make_shared<ov::op::v1::GroupConvolution>(data,
reshaped_filters,
strides,
padding_below,
padding_above,
dilations,
auto_pad);
} else {
return std::make_shared<default_opset::Convolution>(data,
filters,
strides,
padding_below,
padding_above,
dilations,
auto_pad);
return std::make_shared<ov::op::v1::Convolution>(data,
filters,
strides,
padding_below,
padding_above,
dilations,
auto_pad);
}
}
} // namespace conv_factory
Expand Down
18 changes: 9 additions & 9 deletions src/frontends/onnx/frontend/src/utils/conv_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

#pragma once

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

namespace ngraph {
namespace onnx_import {
namespace conv_factory {
std::shared_ptr<ov::op::Op> make_ng_convolution(const Output<ngraph::Node>& data,
const Output<ngraph::Node>& filters,
const ngraph::Strides& strides,
const ngraph::Strides& dilations,
const ngraph::CoordinateDiff& padding_below,
const ngraph::CoordinateDiff& padding_above,
std::shared_ptr<ov::op::Op> make_ng_convolution(const Output<ov::Node>& data,
const Output<ov::Node>& filters,
const ov::Strides& strides,
const ov::Strides& dilations,
const ov::CoordinateDiff& padding_below,
const ov::CoordinateDiff& padding_above,
int64_t groups,
const ngraph::op::PadType& auto_pad);
const ov::op::PadType& auto_pad);
} // namespace conv_factory
} // namespace onnx_import
} // namespace ngraph
Loading

0 comments on commit 0703ac2

Please sign in to comment.