Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ONNX] Frontend refactoring: operations #22383

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/frontends/onnx/frontend/src/op/qlinear_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

#include "op/qlinear_conv.hpp"

#include <cstddef>
#include <memory>
#include <vector>

#include "conv.hpp"
#include "dequantize_linear.hpp"
#include "exceptions.hpp"
Expand All @@ -19,6 +15,8 @@
#include "openvino/op/multiply.hpp"
#include "quantize_linear.hpp"

using namespace ov::op;

OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
namespace onnx_import {
Expand All @@ -35,22 +33,22 @@ OutputVector qlinear_conv(const Node& node) {
auto w_zero_point = inputs.at(5);
auto y_scale = inputs.at(6);
auto y_zero_point = inputs.at(7);
Output<ngraph::Node> B = inputs.size() > 8 ? inputs.at(8) : std::make_shared<NullNode>()->output(0);
Output<ov::Node> B = inputs.size() > 8 ? inputs.at(8) : std::make_shared<NullNode>()->output(0);

x = set_13::detail::dequantize_linear(x,
x_scale,
std::make_shared<ov::op::v0::Convert>(x_zero_point, element::f32),
std::make_shared<v0::Convert>(x_zero_point, element::f32),
1,
node)[0];
w = set_13::detail::dequantize_linear(w,
w_scale,
std::make_shared<ov::op::v0::Convert>(w_zero_point, element::f32),
std::make_shared<v0::Convert>(w_zero_point, element::f32),
1,
node)[0];

if (!ov::op::util::is_null(B)) {
B = std::make_shared<ov::op::v1::Multiply>(std::make_shared<ov::op::v0::Convert>(B, x_scale.get_element_type()),
std::make_shared<ov::op::v1::Multiply>(x_scale, w_scale))
B = std::make_shared<v1::Multiply>(std::make_shared<v0::Convert>(B, x_scale.get_element_type()),
std::make_shared<v1::Multiply>(x_scale, w_scale))
->output(0);
}

Expand Down
5 changes: 1 addition & 4 deletions src/frontends/onnx/frontend/src/op/qlinear_conv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "openvino/core/deprecated.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START

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

namespace ngraph {
Expand All @@ -21,14 +20,12 @@ namespace set_1 {
///
/// \param node The ONNX node object representing this operation.
///
/// \return The vector containing Ngraph nodes producing output of ONNX quantizied
/// \return The vector containing OV nodes producing output of ONNX quantizied
/// convolution operation.
OutputVector qlinear_conv(const Node& node);

} // namespace set_1

} // namespace op

} // namespace onnx_import

} // namespace ngraph
Expand Down
10 changes: 4 additions & 6 deletions src/frontends/onnx/frontend/src/op/qlinear_matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

#include "op/qlinear_matmul.hpp"

#include <cstddef>
#include <memory>
#include <vector>

#include "dequantize_linear.hpp"
#include "matmul.hpp"
#include "openvino/op/convert.hpp"
#include "quantize_linear.hpp"
#include "utils/reshape.hpp"

using namespace ov::op;

OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
namespace onnx_import {
Expand All @@ -34,13 +32,13 @@ OutputVector qlinear_matmul(const Node& node) {
const auto& dequnatize_a =
set_13::detail::dequantize_linear(a,
a_scale,
std::make_shared<ov::op::v0::Convert>(a_zero_point, element::f32),
std::make_shared<v0::Convert>(a_zero_point, element::f32),
1,
node);
const auto& dequnatize_b =
set_13::detail::dequantize_linear(b,
b_scale,
std::make_shared<ov::op::v0::Convert>(b_zero_point, element::f32),
std::make_shared<v0::Convert>(b_zero_point, element::f32),
1,
node);

Expand Down
3 changes: 1 addition & 2 deletions src/frontends/onnx/frontend/src/op/qlinear_matmul.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "openvino/core/deprecated.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START

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

namespace ngraph {
Expand All @@ -18,7 +17,7 @@ namespace set_1 {
///
/// \param node The ONNX node object representing this operation.
///
/// \return The vector containing Ngraph nodes producing output of ONNX quantizied
/// \return The vector containing OV nodes producing output of ONNX quantizied
/// matrix multiplication operation.
OutputVector qlinear_matmul(const Node& node);
} // namespace set_1
Expand Down
1 change: 0 additions & 1 deletion src/frontends/onnx/frontend/src/op/quant_conv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "openvino/core/deprecated.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START

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

namespace ngraph {
Expand Down
105 changes: 49 additions & 56 deletions src/frontends/onnx/frontend/src/op/quantize_linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,34 @@

#include "op/quantize_linear.hpp"

#include <cstdint>
#include <memory>
#include <numeric>
#include <tuple>

#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/axis_set.hpp"
#include "ngraph/shape.hpp"
#include "ngraph/type/element_type.hpp"
#include "ngraph/validation_util.hpp"
#include "openvino/core/validation_util.hpp"
#include "openvino/frontend/exception.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/fake_quantize.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/subtract.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "utils/reshape.hpp"

using namespace ov::op;

OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
namespace onnx_import {
namespace op {
namespace detail {
namespace {
Output<ngraph::Node> get_zero_point(const OutputVector& inputs) {
Output<ov::Node> get_zero_point(const OutputVector& inputs) {
if (inputs.size() > 2) {
return inputs.at(2);
} else {
return std::make_shared<default_opset::Constant>(element::u8, Shape{1}, std::uint8_t(0));
return std::make_shared<v0::Constant>(element::u8, Shape{1}, std::uint8_t(0));
}
}

void validate_zero_point_type(const Node& onnx_node, const Output<ngraph::Node>& y_zero_point) {
void validate_zero_point_type(const Node& onnx_node, const Output<ov::Node>& y_zero_point) {
const auto& y_zero_point_et = y_zero_point.get_element_type();
CHECK_VALID_NODE(
onnx_node,
Expand All @@ -43,49 +41,48 @@ void validate_zero_point_type(const Node& onnx_node, const Output<ngraph::Node>&
"integer type.");
}

Output<ngraph::Node> validate_scale(const Node& onnx_node, const Output<ngraph::Node>& y_scale) {
Output<ov::Node> validate_scale(const Node& onnx_node, const Output<ov::Node>& y_scale) {
const auto& y_scale_et = y_scale.get_element_type();
CHECK_VALID_NODE(onnx_node, y_scale_et.is_static(), "\"y_scale\" input data type must be static.");
if (y_scale_et != element::f32) {
return std::make_shared<default_opset::Convert>(y_scale, element::f32);
return std::make_shared<v0::Convert>(y_scale, element::f32);
}
return y_scale;
}

Output<ngraph::Node> validate_data(const Node& onnx_node, const Output<ngraph::Node>& data) {
Output<ov::Node> validate_data(const Node& onnx_node, const Output<ov::Node>& data) {
const auto& data_et = data.get_element_type();
CHECK_VALID_NODE(onnx_node, data_et.is_static(), "\"x\" input data type must be static.");

if (data_et != element::f32) {
return std::make_shared<default_opset::Convert>(data, element::f32);
return std::make_shared<v0::Convert>(data, element::f32);
}
return data;
}

std::tuple<std::shared_ptr<ngraph::Node>, std::shared_ptr<ngraph::Node>> get_output_bands(
const element::Type& destination_type,
const element::Type& data_type) {
std::shared_ptr<ngraph::Node> output_low;
std::shared_ptr<ngraph::Node> output_high;
std::tuple<std::shared_ptr<ov::Node>, std::shared_ptr<ov::Node>> get_output_bands(const element::Type& destination_type,
const element::Type& data_type) {
std::shared_ptr<ov::Node> output_low;
std::shared_ptr<ov::Node> output_high;

// These values could be used in a ConvertQuantizeDequantize transformation and
// should be aligned
switch (destination_type) {
case element::i8:
output_low = std::make_shared<default_opset::Constant>(data_type, Shape{1}, -128);
output_high = std::make_shared<default_opset::Constant>(data_type, Shape{1}, 127);
output_low = std::make_shared<v0::Constant>(data_type, Shape{1}, -128);
output_high = std::make_shared<v0::Constant>(data_type, Shape{1}, 127);
break;
case element::u8:
output_low = std::make_shared<default_opset::Constant>(data_type, Shape{1}, 0);
output_high = std::make_shared<default_opset::Constant>(data_type, Shape{1}, 255);
output_low = std::make_shared<v0::Constant>(data_type, Shape{1}, 0);
output_high = std::make_shared<v0::Constant>(data_type, Shape{1}, 255);
break;
case element::i16:
output_low = std::make_shared<default_opset::Constant>(data_type, Shape{1}, -32768);
output_high = std::make_shared<default_opset::Constant>(data_type, Shape{1}, 32767);
output_low = std::make_shared<v0::Constant>(data_type, Shape{1}, -32768);
output_high = std::make_shared<v0::Constant>(data_type, Shape{1}, 32767);
break;
case element::u16:
output_low = std::make_shared<default_opset::Constant>(data_type, Shape{1}, 0);
output_high = std::make_shared<default_opset::Constant>(data_type, Shape{1}, 65535);
output_low = std::make_shared<v0::Constant>(data_type, Shape{1}, 0);
output_high = std::make_shared<v0::Constant>(data_type, Shape{1}, 65535);
break;
default:
OPENVINO_THROW("Unsupported element type for QuantizeLinear");
Expand All @@ -95,27 +92,23 @@ std::tuple<std::shared_ptr<ngraph::Node>, std::shared_ptr<ngraph::Node>> get_out
return std::make_tuple(output_low, output_high);
}

std::tuple<std::shared_ptr<ngraph::Node>, std::shared_ptr<ngraph::Node>> get_input_bands(
const Output<ngraph::Node>& y_scale,
const Output<ngraph::Node>& y_zero_point,
const std::shared_ptr<ngraph::Node>& output_low,
const std::shared_ptr<ngraph::Node>& output_high,
std::tuple<std::shared_ptr<ov::Node>, std::shared_ptr<ov::Node>> get_input_bands(
const Output<ov::Node>& y_scale,
const Output<ov::Node>& y_zero_point,
const std::shared_ptr<ov::Node>& output_low,
const std::shared_ptr<ov::Node>& output_high,
const element::Type& data_type) {
std::shared_ptr<ngraph::Node> input_low;
std::shared_ptr<ngraph::Node> input_high;
const auto& zero_point = std::make_shared<default_opset::Convert>(y_zero_point, data_type);
std::shared_ptr<ov::Node> input_low;
std::shared_ptr<ov::Node> input_high;
const auto& zero_point = std::make_shared<v0::Convert>(y_zero_point, data_type);

input_low =
std::make_shared<default_opset::Multiply>(y_scale,
std::make_shared<default_opset::Subtract>(output_low, zero_point));
input_low = std::make_shared<v1::Multiply>(y_scale, std::make_shared<v1::Subtract>(output_low, zero_point));
OPENVINO_SUPPRESS_DEPRECATED_START
if (auto constant = ov::get_constant_from_source(input_low)) {
OPENVINO_SUPPRESS_DEPRECATED_END
input_low = constant;
}
input_high =
std::make_shared<default_opset::Multiply>(y_scale,
std::make_shared<default_opset::Subtract>(output_high, zero_point));
input_high = std::make_shared<v1::Multiply>(y_scale, std::make_shared<v1::Subtract>(output_high, zero_point));
OPENVINO_SUPPRESS_DEPRECATED_START
if (auto constant = ov::get_constant_from_source(input_high)) {
OPENVINO_SUPPRESS_DEPRECATED_END
Expand All @@ -125,25 +118,25 @@ std::tuple<std::shared_ptr<ngraph::Node>, std::shared_ptr<ngraph::Node>> get_inp
return std::make_tuple(input_low, input_high);
}
} // namespace
std::shared_ptr<ngraph::Node> make_fake_quantize(const Output<ngraph::Node>& y_scale,
const Output<ngraph::Node>& y_zero_point,
const Output<ngraph::Node>& data) {
std::shared_ptr<ov::Node> make_fake_quantize(const Output<ov::Node>& y_scale,
const Output<ov::Node>& y_zero_point,
const Output<ov::Node>& data) {
const element::Type& destination_type = y_zero_point.get_element_type();
const element::Type& data_type = data.get_element_type();

std::shared_ptr<ngraph::Node> output_low;
std::shared_ptr<ngraph::Node> output_high;
std::shared_ptr<ov::Node> output_low;
std::shared_ptr<ov::Node> output_high;
std::tie(output_low, output_high) = detail::get_output_bands(destination_type, data_type);

std::shared_ptr<ngraph::Node> input_low;
std::shared_ptr<ngraph::Node> input_high;
std::shared_ptr<ov::Node> input_low;
std::shared_ptr<ov::Node> input_high;
std::tie(input_low, input_high) =
detail::get_input_bands(y_scale, y_zero_point, output_low, output_high, data_type);

const std::size_t levels = static_cast<size_t>(1) << destination_type.bitwidth();

return std::make_shared<default_opset::Convert>(
std::make_shared<default_opset::FakeQuantize>(data, input_low, input_high, output_low, output_high, levels),
return std::make_shared<v0::Convert>(
std::make_shared<v0::FakeQuantize>(data, input_low, input_high, output_low, output_high, levels),
destination_type);
}
} // namespace detail
Expand All @@ -165,9 +158,9 @@ OutputVector quantize_linear(const Node& node) {

namespace set_13 {
namespace {
OutputVector quantize_linear(Output<ngraph::Node> x,
Output<ngraph::Node> y_scale,
Output<ngraph::Node> y_zero_point,
OutputVector quantize_linear(Output<ov::Node> x,
Output<ov::Node> y_scale,
Output<ov::Node> y_zero_point,
int64_t axis,
Node node) {
namespace detail = ngraph::onnx_import::op::detail;
Expand Down
8 changes: 4 additions & 4 deletions src/frontends/onnx/frontend/src/op/quantize_linear.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
#include "openvino/core/deprecated.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START

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

namespace ngraph {
namespace onnx_import {
namespace op {
namespace detail {
std::shared_ptr<ngraph::Node> make_fake_quantize(const Output<ngraph::Node>& y_scale,
const Output<ngraph::Node>& y_zero_point,
const Output<ngraph::Node>& data);
std::shared_ptr<ov::Node> make_fake_quantize(const Output<ov::Node>& y_scale,
const Output<ov::Node>& y_zero_point,
const Output<ov::Node>& data);
}
namespace set_1 {
OutputVector quantize_linear(const Node& node);
Expand Down
9 changes: 6 additions & 3 deletions src/frontends/onnx/frontend/src/op/random_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "op/random_normal.hpp"

#include "exceptions.hpp"
#include "ngraph/shape.hpp"
#include "openvino/frontend/common/random_normal_helper.hpp"
#include "openvino/op/constant.hpp"
#include "utils/common.hpp"

using namespace ov::op;

OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
namespace onnx_import {
Expand All @@ -23,8 +26,8 @@ OutputVector random_normal(const Node& node) {

const auto mean = node.get_attribute_value<float>("mean", 0.0f);
const auto scale = node.get_attribute_value<float>("scale", 1.0f);
auto scale_node = ov::op::v0::Constant::create(target_type, Shape{1}, {scale});
auto mean_node = ov::op::v0::Constant::create(target_type, Shape{1}, {mean});
auto scale_node = v0::Constant::create(target_type, Shape{1}, {scale});
auto mean_node = v0::Constant::create(target_type, Shape{1}, {mean});

const auto seed = node.get_attribute_value<float>("seed", 0);
const auto shape = node.get_attribute_as_constant<std::vector<int64_t>>("shape");
Expand Down
1 change: 0 additions & 1 deletion src/frontends/onnx/frontend/src/op/random_normal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "openvino/core/deprecated.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START

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

namespace ngraph {
Expand Down
Loading
Loading