Interpolate v11 usage in ONNX FE (#16463)

This commit is contained in:
Tomasz Dołbniak 2023-03-24 03:06:22 +01:00 committed by GitHub
parent abaf61d059
commit 1b89ecdbae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 73 additions and 125 deletions

View File

@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/opsets/opset10.hpp"
#include "ngraph/opsets/opset11.hpp"
namespace ngraph {
namespace onnx_import {
namespace default_opset = ngraph::opset10;
namespace default_opset = ngraph::opset11;
}
} // namespace ngraph

View File

@ -4,8 +4,6 @@
#include "op/hardmax.hpp"
#include <openvino/opsets/opset11.hpp>
#include "exceptions.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/op/one_hot.hpp"
@ -39,11 +37,11 @@ OutputVector hardmax(const Node& node) {
const auto indices_axis = 1;
const auto topk =
std::make_shared<ov::opset11::TopK>(coerced_tensor,
default_opset::Constant::create(ngraph::element::i64, Shape{}, {1}),
indices_axis,
ov::opset11::TopK::Mode::MAX,
ov::opset11::TopK::SortType::NONE);
std::make_shared<default_opset::TopK>(coerced_tensor,
default_opset::Constant::create(ngraph::element::i64, Shape{}, {1}),
indices_axis,
default_opset::TopK::Mode::MAX,
default_opset::TopK::SortType::NONE);
const auto on_value = default_opset::Constant::create(ngraph::element::i64, Shape{}, {1});
const auto off_value = default_opset::Constant::create(ngraph::element::i64, Shape{}, {0});
@ -73,11 +71,11 @@ OutputVector hardmax(const Node& node) {
row_size = ngraph::onnx_import::reshape::interpret_as_scalar(row_size);
const auto topk =
std::make_shared<ov::opset11::TopK>(input,
default_opset::Constant::create(ngraph::element::i64, Shape{}, {1}),
axis,
ov::opset11::TopK::Mode::MAX,
ov::opset11::TopK::SortType::NONE);
std::make_shared<default_opset::TopK>(input,
default_opset::Constant::create(ngraph::element::i64, Shape{}, {1}),
axis,
default_opset::TopK::Mode::MAX,
default_opset::TopK::SortType::NONE);
const auto on_value = default_opset::Constant::create(ngraph::element::i64, Shape{}, {1});
const auto off_value = default_opset::Constant::create(ngraph::element::i64, Shape{}, {0});

View File

@ -24,12 +24,12 @@ static const std::unordered_set<std::string> supported_transforms = {"half_pixel
"asymmetric",
"tf_half_pixel_for_nn"};
using InterpolateMode = ngraph::op::v4::Interpolate::InterpolateMode;
using InterpolateMode = default_opset::Interpolate::InterpolateMode;
static const std::map<std::string, int> interp_mode_map = {{"nearest", static_cast<int>(InterpolateMode::NEAREST)},
{"linear", static_cast<int>(InterpolateMode::LINEAR_ONNX)},
{"cubic", static_cast<int>(InterpolateMode::CUBIC)}};
using Transform_mode = ngraph::op::v4::Interpolate::CoordinateTransformMode;
using Transform_mode = default_opset::Interpolate::CoordinateTransformMode;
static const std::map<std::string, int> transform_mode_map = {
{"half_pixel", static_cast<int>(Transform_mode::HALF_PIXEL)},
{"pytorch_half_pixel", static_cast<int>(Transform_mode::PYTORCH_HALF_PIXEL)},
@ -37,7 +37,7 @@ static const std::map<std::string, int> transform_mode_map = {
{"asymmetric", static_cast<int>(Transform_mode::ASYMMETRIC)},
{"tf_half_pixel_for_nn", static_cast<int>(Transform_mode::TF_HALF_PIXEL_FOR_NN)}};
using Nearest_mode = ngraph::op::v4::Interpolate::NearestMode;
using Nearest_mode = default_opset::Interpolate::NearestMode;
static const std::map<std::string, int> nearest_mode_map = {
{"round_prefer_floor", static_cast<int>(Nearest_mode::ROUND_PREFER_FLOOR)},
{"round_prefer_ceil", static_cast<int>(Nearest_mode::ROUND_PREFER_CEIL)},
@ -57,9 +57,9 @@ static int mode_as_int(const std::map<std::string, int>& converting_map, const s
return result;
}
using InterpolateV4Attrs = ngraph::op::v4::Interpolate::InterpolateAttrs;
using InterpolateAttrs = default_opset::Interpolate::InterpolateAttrs;
InterpolateV4Attrs get_resize_attrs(const onnx_import::Node& node) {
InterpolateAttrs get_resize_attrs(const onnx_import::Node& node) {
auto get_str_attr = [&node](const std::string& name, const std::string& default_value) {
return node.get_attribute_value<std::string>(name, default_value);
};
@ -98,43 +98,17 @@ InterpolateV4Attrs get_resize_attrs(const onnx_import::Node& node) {
supported_modes_str);
}
InterpolateV4Attrs attrs;
InterpolateAttrs attrs;
attrs.mode = static_cast<InterpolateMode>(mode_as_int(interp_mode_map, mode));
attrs.coordinate_transformation_mode = static_cast<Transform_mode>(mode_as_int(transform_mode_map, transform_mode));
attrs.nearest_mode = static_cast<Nearest_mode>(mode_as_int(nearest_mode_map, nearest_mode));
attrs.antialias = false;
attrs.cube_coeff = node.get_attribute_value<float>("cubic_coeff_a", -0.75);
auto zero_pad = std::vector<size_t>(1, 0);
attrs.pads_begin = zero_pad;
attrs.pads_end = zero_pad;
attrs.pads_begin = {0};
attrs.pads_end = {0};
return attrs;
}
std::shared_ptr<ngraph::Node> calculate_output_shape_based_on_scales(const Output<ngraph::Node>& data,
const Output<ngraph::Node>& scales) {
const auto shape_of_data = std::make_shared<default_opset::Convert>(std::make_shared<default_opset::ShapeOf>(data),
scales.get_element_type());
const auto multiply = std::make_shared<default_opset::Multiply>(shape_of_data, scales);
const auto output_shape = std::make_shared<default_opset::Convert>(multiply, ngraph::element::i64);
return output_shape;
}
std::shared_ptr<ngraph::Node> calculate_scales_based_on_sizes(const Output<ngraph::Node>& data,
const Output<ngraph::Node>& sizes) {
const float epsilon = 1.0e-5f;
const auto shape_of_data =
std::make_shared<default_opset::Convert>(std::make_shared<default_opset::ShapeOf>(data), ngraph::element::f32);
const auto converted_sizes = std::make_shared<default_opset::Convert>(sizes, ngraph::element::f32);
const auto divide = std::make_shared<default_opset::Divide>(converted_sizes, shape_of_data);
const auto eps_node = std::make_shared<default_opset::Constant>(ngraph::element::f32, Shape{}, epsilon);
const auto scales = std::make_shared<default_opset::Add>(divide, eps_node);
return scales;
}
} // namespace
namespace set_11 {
@ -149,16 +123,12 @@ OutputVector resize(const onnx_import::Node& node) {
if (inputs.size() == 4 && !ngraph::op::is_null(inputs[3])) {
attrs.shape_calculation_mode = default_opset::Interpolate::ShapeCalcMode::SIZES;
const auto& sizes = inputs.at(3);
const auto scales = calculate_scales_based_on_sizes(data, sizes);
return {std::make_shared<default_opset::Interpolate>(data, sizes, scales, attrs)};
return {std::make_shared<default_opset::Interpolate>(data, sizes, attrs)};
} else {
attrs.shape_calculation_mode = default_opset::Interpolate::ShapeCalcMode::SCALES;
const auto& scales = inputs.at(2);
return {std::make_shared<default_opset::Interpolate>(data, scales, attrs)};
}
attrs.shape_calculation_mode = default_opset::Interpolate::ShapeCalcMode::SCALES;
const auto& scales = inputs.at(2);
const auto output_shape = calculate_output_shape_based_on_scales(data, scales);
return {std::make_shared<default_opset::Interpolate>(data, output_shape, scales, attrs)};
}
} // namespace set_11
@ -169,6 +139,7 @@ OutputVector resize(const onnx_import::Node& node) {
const auto& scales = inputs.at(1);
auto attrs = get_resize_attrs(node);
attrs.shape_calculation_mode = default_opset::Interpolate::ShapeCalcMode::SIZES;
if (attrs.mode == InterpolateMode::NEAREST) {
attrs.nearest_mode = Nearest_mode::FLOOR;
@ -177,8 +148,12 @@ OutputVector resize(const onnx_import::Node& node) {
attrs.coordinate_transformation_mode = Transform_mode::ASYMMETRIC;
}
const auto output_shape = calculate_output_shape_based_on_scales(data, scales);
return {std::make_shared<default_opset::Interpolate>(data, output_shape, scales, attrs)};
const auto shape_of_data = std::make_shared<default_opset::Convert>(std::make_shared<default_opset::ShapeOf>(data),
scales.get_element_type());
const auto multiply = std::make_shared<default_opset::Multiply>(shape_of_data, scales);
const auto output_shape = std::make_shared<default_opset::Convert>(multiply, ngraph::element::i64);
return {std::make_shared<default_opset::Interpolate>(data, output_shape, attrs)};
}
} // namespace set_1

View File

@ -6,7 +6,6 @@
#include <cstdint>
#include <memory>
#include <openvino/opsets/opset11.hpp>
#include "default_opset.hpp"
#include "ngraph/node.hpp"
@ -38,12 +37,13 @@ OutputVector topk(const Node& node) {
const auto k_node = node.get_attribute_as_constant<std::int64_t>("k");
const std::int64_t axis{node.get_attribute_value<std::int64_t>("axis", -1)};
std::shared_ptr<ngraph::Node> top_k = std::make_shared<ov::opset11::TopK>(data,
k_node,
axis,
ov::opset11::TopK::Mode::MAX,
ov::opset11::TopK::SortType::SORT_VALUES,
element::i64);
std::shared_ptr<ngraph::Node> top_k =
std::make_shared<default_opset::TopK>(data,
k_node,
axis,
default_opset::TopK::Mode::MAX,
default_opset::TopK::SortType::SORT_VALUES,
element::i64);
return {top_k->output(0), top_k->output(1)};
}
@ -55,12 +55,13 @@ OutputVector topk(const Node& node) {
auto k = get_k(node);
const std::int64_t axis{node.get_attribute_value<std::int64_t>("axis", -1)};
std::shared_ptr<ngraph::Node> top_k = std::make_shared<ov::opset11::TopK>(data,
k,
axis,
ov::opset11::TopK::Mode::MAX,
ov::opset11::TopK::SortType::SORT_VALUES,
element::i64);
std::shared_ptr<ngraph::Node> top_k =
std::make_shared<default_opset::TopK>(data,
k,
axis,
default_opset::TopK::Mode::MAX,
default_opset::TopK::SortType::SORT_VALUES,
element::i64);
return {top_k->output(0), top_k->output(1)};
}
@ -78,13 +79,13 @@ OutputVector topk(const Node& node) {
const auto sorted = node.get_attribute_value<std::int64_t>("sorted", 1);
// Map attribute values to nGraph enums
const auto sort_type = sorted ? ov::opset11::TopK::SortType::SORT_VALUES : ov::opset11::TopK::SortType::NONE;
const auto sort_type = sorted ? default_opset::TopK::SortType::SORT_VALUES : default_opset::TopK::SortType::NONE;
const auto compute_max = static_cast<bool>(largest);
const auto mode = compute_max ? ov::opset11::TopK::Mode::MAX : ov::opset11::TopK::Mode::MIN;
const auto mode = compute_max ? default_opset::TopK::Mode::MAX : default_opset::TopK::Mode::MIN;
std::shared_ptr<ngraph::Node> top_k =
std::make_shared<ov::opset11::TopK>(data, k, axis, mode, sort_type, element::i64);
std::make_shared<default_opset::TopK>(data, k, axis, mode, sort_type, element::i64);
return {top_k->output(0), top_k->output(1)};
}

View File

@ -38,34 +38,21 @@ void check_mode_support(const onnx_import::Node& node, const std::string& mode,
}
default_opset::Interpolate::InterpolateAttrs get_attributes(const std::string& mode) {
using InterpolateMode = default_opset::Interpolate::InterpolateMode;
using Transform_mode = default_opset::Interpolate::CoordinateTransformMode;
using ShapeCalcMode = default_opset::Interpolate::ShapeCalcMode;
const auto interpolate_mode = mode == "linear" || mode == "bilinear"
? default_opset::Interpolate::InterpolateMode::LINEAR_ONNX
: default_opset::Interpolate::InterpolateMode::NEAREST;
const auto interpolate_mode =
(mode == "linear" || mode == "bilinear" ? InterpolateMode::LINEAR_ONNX : InterpolateMode::NEAREST);
auto attrs = default_opset::Interpolate::InterpolateAttrs(interpolate_mode,
default_opset::Interpolate::ShapeCalcMode::SCALES,
{0},
{0});
std::vector<size_t> pad{0};
auto attrs = default_opset::Interpolate::InterpolateAttrs(interpolate_mode, ShapeCalcMode::SCALES, pad, pad);
if (attrs.mode == InterpolateMode::LINEAR_ONNX)
attrs.coordinate_transformation_mode = Transform_mode::ASYMMETRIC;
if (attrs.mode == default_opset::Interpolate::InterpolateMode::LINEAR_ONNX) {
attrs.coordinate_transformation_mode = default_opset::Interpolate::CoordinateTransformMode::ASYMMETRIC;
}
return attrs;
}
OutputVector create_upsample_subgraph(const Output<ngraph::Node>& data,
const Output<ngraph::Node>& scales,
const std::string& mode) {
const auto shape_of_data =
std::make_shared<default_opset::Convert>(std::make_shared<default_opset::ShapeOf>(data), ngraph::element::f32);
const auto multiply = std::make_shared<default_opset::Multiply>(shape_of_data, scales);
const auto output_shape = std::make_shared<default_opset::Convert>(std::make_shared<default_opset::Floor>(multiply),
ngraph::element::i64);
return {std::make_shared<default_opset::Interpolate>(data, output_shape, scales, get_attributes(mode))};
}
} // namespace
namespace set_1 {
@ -89,7 +76,7 @@ OutputVector upsample(const onnx_import::Node& node) {
const auto scales_const = default_opset::Constant::create(ngraph::element::f32, Shape({scales.size()}), scales);
return create_upsample_subgraph(data, scales_const, mode);
return std::make_shared<default_opset::Interpolate>(data, scales_const, get_attributes(mode))->outputs();
}
} // namespace set_1
@ -110,7 +97,7 @@ OutputVector upsample(const onnx_import::Node& node) {
const auto scales_const = default_opset::Constant::create(ngraph::element::f32, Shape({scales.size()}), scales);
return create_upsample_subgraph(data, scales_const, mode);
return std::make_shared<default_opset::Interpolate>(data, scales_const, get_attributes(mode))->outputs();
}
} // namespace set_7
@ -120,17 +107,8 @@ OutputVector upsample(const onnx_import::Node& node) {
const auto mode = node.get_attribute_value<std::string>("mode", "nearest");
check_mode_support(node, mode, version_9);
const auto inputs = node.get_ng_inputs();
const auto& data = inputs.at(0);
const auto& scales = inputs.at(1);
const auto& data_shape = data.get_partial_shape();
const auto& scales_shape = scales.get_partial_shape();
CHECK_VALID_NODE(node,
(scales_shape.is_static() || data_shape.rank().is_static()),
" Data rank or shape of Scales input is required to be static.");
return create_upsample_subgraph(data, scales, mode);
const auto& inputs = node.get_ng_inputs();
return std::make_shared<default_opset::Interpolate>(inputs.at(0), inputs.at(1), get_attributes(mode))->outputs();
}
} // namespace set_9

View File

@ -4,8 +4,6 @@
#include "utils/arg_min_max_factory.hpp"
#include <openvino/opsets/opset11.hpp>
#include "default_opset.hpp"
#include "ngraph/opsets/opset1.hpp"
#include "ngraph/validation_util.hpp"
@ -20,14 +18,14 @@ ArgMinMaxFactory::ArgMinMaxFactory(const Node& node)
m_select_last_index{node.get_attribute_value<std::int64_t>("select_last_index", 0)} {}
std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_arg_max() const {
return make_topk_subgraph(ov::opset11::TopK::Mode::MAX);
return make_topk_subgraph(default_opset::TopK::Mode::MAX);
}
std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_arg_min() const {
return make_topk_subgraph(ov::opset11::TopK::Mode::MIN);
return make_topk_subgraph(default_opset::TopK::Mode::MIN);
}
std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_topk_subgraph(ov::opset11::TopK::Mode mode) const {
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});
if (m_select_last_index == 1) {
@ -61,11 +59,11 @@ std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_topk_subgraph(ov::opset11::
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 topk = std::make_shared<ov::opset11::TopK>(reverse,
k_node,
normalized_axis,
mode,
ov::opset11::TopK::SortType::NONE);
const auto topk = std::make_shared<default_opset::TopK>(reverse,
k_node,
normalized_axis,
mode,
default_opset::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>(
@ -90,7 +88,7 @@ std::shared_ptr<ngraph::Node> ArgMinMaxFactory::make_topk_subgraph(ov::opset11::
}
const auto topk =
std::make_shared<ov::opset11::TopK>(m_input_node, k_node, m_axis, mode, ov::opset11::TopK::SortType::NONE);
std::make_shared<default_opset::TopK>(m_input_node, k_node, m_axis, mode, default_opset::TopK::SortType::NONE);
const auto result = std::make_shared<default_opset::Convert>(topk->output(1), element::i64);

View File

@ -433,3 +433,4 @@ IE_CPU.onnx_model_top_k_repeating_unsorted
# Accuracy regression - Ticket 105909
IE_CPU.onnx_model_attention_qkv_hidden_sizes

View File

@ -3,9 +3,6 @@ INTERPRETER.onnx_top_k_opset_10
# Temporarily disabled:
INTERPRETER.onnx_resize11_scales_nearest_asymmetric_floor_dynamic_sizes
INTERPRETER.onnx_resize11_up_sizes_cubic_half_pixel_dynamic_sizes
INTERPRETER.onnx_resize10_up_scales_const_nearest
INTERPRETER.onnx_resize10_up_scales_const_linear
INTERPRETER.onnx_resize10_down_scales_const_nearest
# Failed in MacOS:
INTERPRETER.onnx_resize11_sizes_nearest_asymmetric_floor