Removed StridedSlice to StridedSliceIE transformation (#661)

This commit is contained in:
Gleb Kazantaev
2020-05-28 18:27:54 +03:00
committed by GitHub
parent 33aca7d2c4
commit bb41994f56
9 changed files with 1 additions and 315 deletions

View File

@@ -34,7 +34,6 @@
#include "ngraph_ops/nms_ie.hpp"
#include "ngraph_ops/crop_ie.hpp"
#include "ngraph_ops/selu_ie.hpp"
#include "ngraph_ops/strided_slice_ie.hpp"
#include "ngraph_ops/rnn_cell_ie.hpp"
#include "ngraph_ops/topk_ie.hpp"
#include "generic_ie.hpp"
@@ -555,7 +554,6 @@ std::shared_ptr<CNNNetworkImpl> convertFunctionToICNNNetwork(const std::shared_p
std::make_shared<Builder::NodeConverter<::ngraph::op::v1::Split>>(),
std::make_shared<Builder::NodeConverter<::ngraph::op::VariadicSplit>>(),
std::make_shared<Builder::NodeConverter<::ngraph::op::v1::StridedSlice>>(),
std::make_shared<Builder::NodeConverter<::ngraph::op::StridedSliceIE>>(),
std::make_shared<Builder::NodeConverter<::ngraph::op::Squeeze>>(),
std::make_shared<Builder::NodeConverter<::ngraph::op::Sqrt>>(),
std::make_shared<Builder::NodeConverter<::ngraph::op::Subtract>>(),

View File

@@ -42,7 +42,6 @@
#include "ngraph_ops/scaleshift.hpp"
#include "ngraph_ops/tile_ie.hpp"
#include "ngraph_ops/topk_ie.hpp"
#include "ngraph_ops/strided_slice_ie.hpp"
#include "ngraph_ops/rnn_cell_ie.hpp"
#include "ngraph_ops/hard_sigmoid_ie.hpp"
#include "generic_ie.hpp"
@@ -2114,17 +2113,10 @@ CNNLayer::Ptr NodeConverter<ngraph::op::Sqrt>::createLayer(const std::shared_ptr
template <>
CNNLayer::Ptr NodeConverter<ngraph::op::v1::StridedSlice>::createLayer(
const std::shared_ptr<ngraph::Node>& layer) const {
THROW_IE_EXCEPTION << "StridedSlice operation has a form that is not supported." << layer->get_friendly_name()
<< " should be converted to StridedSliceIE operation";
}
template <>
CNNLayer::Ptr NodeConverter<ngraph::op::StridedSliceIE>::createLayer(
const std::shared_ptr<ngraph::Node>& layer) const {
LayerParams params = {layer->get_friendly_name(), "StridedSlice",
details::convertPrecision(layer->get_output_element_type(0))};
auto res = std::make_shared<InferenceEngine::StridedSliceLayer>(params);
auto castedLayer = std::dynamic_pointer_cast<ngraph::op::StridedSliceIE>(layer);
auto castedLayer = std::dynamic_pointer_cast<ngraph::op::v1::StridedSlice>(layer);
if (castedLayer == nullptr) THROW_IE_EXCEPTION << "Cannot get " << params.type << " layer " << params.name;
std::string value;

View File

@@ -1,51 +0,0 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <memory>
#include <vector>
#include <ie_api.h>
#include "ngraph/op/op.hpp"
namespace ngraph {
namespace op {
class INFERENCE_ENGINE_API_CLASS(StridedSliceIE) : public Op {
public:
static constexpr NodeTypeInfo type_info{"StridedSliceIE", 1};
const NodeTypeInfo& get_type_info() const override { return type_info; }
StridedSliceIE(const Output<Node>& data,
const Output<Node>& begin,
const Output<Node>& end,
const Output<Node>& strides,
const std::vector<int64_t>& begin_mask,
const std::vector<int64_t>& end_mask,
const std::vector<int64_t>& new_axis_mask,
const std::vector<int64_t>& shrink_axis_mask,
const std::vector<int64_t>& ellipsis_mask);
void validate_and_infer_types() override;
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector & new_args) const override;
const std::vector<int64_t>& get_begin_mask() const { return m_begin_mask; }
const std::vector<int64_t>& get_end_mask() const { return m_end_mask; }
const std::vector<int64_t>& get_new_axis_mask() const { return m_new_axis_mask; }
const std::vector<int64_t>& get_shrink_axis_mask() const { return m_shrink_axis_mask; }
const std::vector<int64_t>& get_ellipsis_mask() const { return m_ellipsis_mask; }
protected:
const std::vector<int64_t> m_begin_mask;
const std::vector<int64_t> m_end_mask;
const std::vector<int64_t> m_new_axis_mask;
const std::vector<int64_t> m_shrink_axis_mask;
const std::vector<int64_t> m_ellipsis_mask;
};
} // namespace op
} // namespace ngraph

View File

@@ -53,7 +53,6 @@ NGRAPH_PASS(ConvertHardSigmoidToHardSigmoidIE, ::ngraph::pass)
NGRAPH_PASS(ConvertCellsToCellsIE, ::ngraph::pass)
NGRAPH_PASS(ConvertInterpolateToInterpOrResample, ::ngraph::pass)
NGRAPH_PASS(ConvertStridedSliceToCrop, ::ngraph::pass)
NGRAPH_PASS(ConvertStridedSliceToStridedSliceIE, ::ngraph::pass)
NGRAPH_PASS(ConvertPowerToPowerIE, ::ngraph::pass)
NGRAPH_PASS(ConvertSqrtToPowerIE, ::ngraph::pass)
NGRAPH_PASS(ConvertPReLUToReLUIE, ::ngraph::pass)

View File

@@ -1,37 +0,0 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <vector>
#include <memory>
#include <ie_api.h>
#include <ngraph/pass/graph_rewrite.hpp>
namespace ngraph {
namespace pass {
class INFERENCE_ENGINE_API_CLASS(ConvertStridedSliceToStridedSliceIE);
} // namespace pass
} // namespace ngraph
/*
* Description:
* This transformation converts opset1::StridedSlice to legacy StridedSliceIE
* StridedSliceIE takes begin, end and strides inputs ony in i32 precision.
* Inputs with precision != i32 are converted with Convert operation.
*/
class ngraph::pass::ConvertStridedSliceToStridedSliceIE: public ngraph::pass::GraphRewrite {
public:
ConvertStridedSliceToStridedSliceIE() : GraphRewrite() {
convert_strided_slice_to_strided_slice_ie();
}
private:
void convert_strided_slice_to_strided_slice_ie();
};

View File

@@ -1,62 +0,0 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph_ops/strided_slice_ie.hpp"
#include <algorithm>
#include <vector>
#include <memory>
#include <ngraph/ops.hpp>
#include <ngraph/opsets/opset1.hpp>
using namespace std;
using namespace ngraph;
constexpr NodeTypeInfo op::StridedSliceIE::type_info;
op::StridedSliceIE::StridedSliceIE(const Output <Node> &data, const Output <Node> &begin, const Output <Node> &end,
const Output <Node> &strides, const std::vector<int64_t> &begin_mask,
const std::vector<int64_t> &end_mask, const std::vector<int64_t> &new_axis_mask,
const std::vector<int64_t> &shrink_axis_mask,
const std::vector<int64_t> &ellipsis_mask)
: Op({data, begin, end, strides})
, m_begin_mask(begin_mask)
, m_end_mask(end_mask)
, m_new_axis_mask(new_axis_mask)
, m_shrink_axis_mask(shrink_axis_mask)
, m_ellipsis_mask(ellipsis_mask) {
constructor_validate_and_infer_types();
}
std::shared_ptr<Node> op::StridedSliceIE::clone_with_new_inputs(const ngraph::OutputVector &new_args) const {
check_new_args_count(this, new_args);
return std::make_shared<op::StridedSliceIE>(new_args.at(0), new_args.at(1), new_args.at(2), new_args.at(3), m_begin_mask,
m_end_mask, m_new_axis_mask, m_shrink_axis_mask, m_ellipsis_mask);
}
void op::StridedSliceIE::validate_and_infer_types() {
const auto& begin_mask_et = input_value(1).get_element_type();
const auto& end_mask_et = input_value(2).get_element_type();
const auto& strides_et = input_value(3).get_element_type();
NODE_VALIDATION_CHECK(this,
begin_mask_et.is_integral_number(),
"Begin mask must have i32 type, but its: ",
begin_mask_et);
NODE_VALIDATION_CHECK(this,
end_mask_et == element::i32,
"End mask must have i32 type, but its: ",
end_mask_et);
NODE_VALIDATION_CHECK(this,
strides_et.is_integral_number(),
"Strides must have i32 type, but its: ",
strides_et);
// Calculate output shape via opset1::StridedSlice operation
auto slice = std::make_shared<opset1::StridedSlice>(input_value(0), input_value(1), input_value(2), input_value(3),
m_begin_mask, m_end_mask, m_new_axis_mask, m_shrink_axis_mask, m_ellipsis_mask);
set_output_type(0, slice->output(0).get_element_type(), slice->output(0).get_partial_shape());
}

View File

@@ -44,7 +44,6 @@
#include <transformations/convert_opset1_to_legacy/reshape_1d_ops.hpp>
#include <transformations/convert_opset1_to_legacy/reshape_fully_connected.hpp>
#include <transformations/pull_transpose_through_fq.hpp>
#include <transformations/convert_opset1_to_legacy/convert_strided_slice_to_strided_slice_ie.hpp>
#include <transformations/convert_opset1_to_legacy/convert_hard_sigmoid_to_hard_sigmoid_ie.hpp>
#include <ngraph/pass/constant_folding.hpp>

View File

@@ -1,55 +0,0 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "transformations/convert_opset1_to_legacy/convert_strided_slice_to_strided_slice_ie.hpp"
#include <memory>
#include <vector>
#include <ngraph/opsets/opset1.hpp>
#include <ngraph_ops/strided_slice_ie.hpp>
#include <ngraph/rt_info.hpp>
void ngraph::pass::ConvertStridedSliceToStridedSliceIE::convert_strided_slice_to_strided_slice_ie() {
auto slice = std::make_shared<pattern::op::Label>(element::f32, Shape{}, pattern::has_class<opset1::StridedSlice>());
ngraph::graph_rewrite_callback callback = [](pattern::Matcher& m) {
auto slice = std::dynamic_pointer_cast<opset1::StridedSlice> (m.get_match_root());
if (!slice) {
return false;
}
auto data_node = slice->input_value(0);
auto begin_node = std::dynamic_pointer_cast<opset1::Constant>(slice->input_value(1).get_node_shared_ptr());
auto end_node = std::dynamic_pointer_cast<opset1::Constant>(slice->input_value(2).get_node_shared_ptr());
auto stride_node = std::dynamic_pointer_cast<opset1::Constant>(slice->input_value(3).get_node_shared_ptr());
if (!begin_node || !end_node || !stride_node) {
return false;
}
auto converted_begin = std::make_shared<opset1::Convert>(begin_node, element::i32);
auto converted_end = std::make_shared<opset1::Convert>(end_node, element::i32);
auto converted_stride = std::make_shared<opset1::Convert>(stride_node, element::i32);
auto slice_ie = std::make_shared<ngraph::op::StridedSliceIE>(data_node,
converted_begin,
converted_end,
converted_stride,
slice->get_begin_mask(),
slice->get_end_mask(),
slice->get_new_axis_mask(),
slice->get_shrink_axis_mask(),
slice->get_ellipsis_mask());
slice_ie->set_friendly_name(slice->get_friendly_name());
ngraph::copy_runtime_info(slice, {converted_begin, converted_end, converted_stride, slice_ie});
ngraph::replace_node(slice, slice_ie);
return true;
};
auto m = std::make_shared<ngraph::pattern::Matcher>(slice, "ConvertStridedSliceToStridedSliceIE");
this->add_matcher(m, callback, PassProperty::CHANGE_DYNAMIC_STATE);
}

View File

@@ -1,97 +0,0 @@
// Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <string>
#include <memory>
#include <queue>
#include <ngraph/function.hpp>
#include <ngraph/opsets/opset1.hpp>
#include <transformations/convert_opset1_to_legacy/convert_strided_slice_to_strided_slice_ie.hpp>
#include <transformations/init_node_info.hpp>
#include <transformations/utils/utils.hpp>
#include <ngraph/pass/constant_folding.hpp>
#include <ngraph_ops/strided_slice_ie.hpp>
#include "ngraph_test_utils.hpp"
using namespace testing;
TEST(TransformationTests, ConvertStridedSliceToStridedSliceIEStatic) {
std::shared_ptr<ngraph::Function> f(nullptr), f_ref(nullptr);
{
auto data = std::make_shared<ngraph::opset1::Parameter>(ngraph::element::f32, ngraph::Shape{1, 2, 3, 4});
auto begin = ngraph::opset1::Constant::create(ngraph::element::i64, ngraph::Shape{4}, {0, 0, 0, 0});
auto end = ngraph::opset1::Constant::create(ngraph::element::i64, ngraph::Shape{4}, {-1, -1, -1, -1});
auto stride = ngraph::opset1::Constant::create(ngraph::element::i64, ngraph::Shape{4}, {1});
std::vector<int64_t> begin_mask{0, 0, 0, 0};
std::vector<int64_t> end_mask{1, 1, 1, 1};
auto ss = std::make_shared<ngraph::opset1::StridedSlice>(data, begin, end, stride, begin_mask, end_mask);
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{ss}, ngraph::ParameterVector{data});
ngraph::pass::InitNodeInfo().run_on_function(f);
ngraph::pass::ConvertStridedSliceToStridedSliceIE().run_on_function(f);
ASSERT_NO_THROW(check_rt_info(f));
ngraph::pass::ConstantFolding().run_on_function(f);
}
{
auto data = std::make_shared<ngraph::opset1::Parameter>(ngraph::element::f32, ngraph::Shape{1, 2, 3, 4});
auto begin = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{4}, {0, 0, 0, 0});
auto end = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{4}, {-1, -1, -1, -1});
auto stride = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{4}, {1});
std::vector<int64_t> begin_mask{0, 0, 0, 0}, end_mask{1, 1, 1, 1}, new_axis_mask{}, shrink_axis_mask{}, ellipsis_mask{};
auto ss = std::make_shared<ngraph::op::StridedSliceIE>(data, begin, end, stride,
begin_mask, end_mask, new_axis_mask, shrink_axis_mask, ellipsis_mask);
f_ref = std::make_shared<ngraph::Function>(ngraph::NodeVector{ss}, ngraph::ParameterVector{data});
}
auto res = compare_functions(f, f_ref);
ASSERT_TRUE(res.first) << res.second;
}
TEST(TransformationTests, ConvertStridedSliceToStridedSliceIEDynamic) {
std::shared_ptr<ngraph::Function> f(nullptr), f_ref(nullptr);
{
auto data = std::make_shared<ngraph::opset1::Parameter>(ngraph::element::f32, ngraph::PartialShape::dynamic(4));
auto begin = ngraph::opset1::Constant::create(ngraph::element::i64, ngraph::Shape{4}, {0, 0, 0, 0});
auto end = ngraph::opset1::Constant::create(ngraph::element::i64, ngraph::Shape{4}, {-1, -1, -1, -1});
auto stride = ngraph::opset1::Constant::create(ngraph::element::i64, ngraph::Shape{4}, {1});
std::vector<int64_t> begin_mask{0, 0, 0, 0};
std::vector<int64_t> end_mask{1, 1, 1, 1};
auto ss = std::make_shared<ngraph::opset1::StridedSlice>(data, begin, end, stride, begin_mask, end_mask);
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{ss}, ngraph::ParameterVector{data});
ngraph::pass::InitNodeInfo().run_on_function(f);
ngraph::pass::ConvertStridedSliceToStridedSliceIE().run_on_function(f);
ASSERT_NO_THROW(check_rt_info(f));
ngraph::pass::ConstantFolding().run_on_function(f);
}
{
auto data = std::make_shared<ngraph::opset1::Parameter>(ngraph::element::f32, ngraph::PartialShape::dynamic(4));
auto begin = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{4}, {0, 0, 0, 0});
auto end = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{4}, {-1, -1, -1, -1});
auto stride = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{4}, {1});
std::vector<int64_t> begin_mask{0, 0, 0, 0}, end_mask{1, 1, 1, 1}, new_axis_mask{}, shrink_axis_mask{}, ellipsis_mask{};
auto ss = std::make_shared<ngraph::op::StridedSliceIE>(data, begin, end, stride,
begin_mask, end_mask, new_axis_mask, shrink_axis_mask, ellipsis_mask);
f_ref = std::make_shared<ngraph::Function>(ngraph::NodeVector{ss}, ngraph::ParameterVector{data});
}
auto res = compare_functions(f, f_ref);
ASSERT_TRUE(res.first) << res.second;
}