MaxPool-1 -> MaxPool-8 upgrade transformation (#8784)

* upgrade transformation

* test fix

* comments resolving

* added case with dynamic rank
This commit is contained in:
Yegor Kruglov
2021-12-10 16:31:49 +03:00
committed by GitHub
parent c85fb74efc
commit 562d388ad9
4 changed files with 152 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <string>
#include <memory>
#include <ngraph/function.hpp>
#include <ngraph/opsets/opset1.hpp>
#include <ngraph/opsets/opset8.hpp>
#include <ngraph/pass/manager.hpp>
#include <transformations/op_conversions/convert_maxpool_upgrade.hpp>
#include <transformations/init_node_info.hpp>
#include "common_test_utils/ngraph_test_utils.hpp"
using namespace testing;
TEST_F(TransformationTestsF, ConvertMaxPool1ToMaxPool8) {
{
auto data = std::make_shared<ngraph::opset1::Parameter>(ngraph::element::f32, ngraph::Shape{1, 2, 3});
ngraph::Strides strides{1};
ngraph::Shape pads_begin{0}, pads_end{0}, kernel{1};
auto maxpool_1 = std::make_shared<ngraph::opset1::MaxPool>(data, strides, pads_begin, pads_end,
kernel);
function = std::make_shared<ngraph::Function>(ngraph::OutputVector{maxpool_1->output(0)},
ngraph::ParameterVector{data});
manager.register_pass<ngraph::pass::ConvertMaxPool1ToMaxPool8>();
}
{
auto data = std::make_shared<ngraph::opset1::Parameter>(ngraph::element::f32, ngraph::Shape{1, 2, 3});
ngraph::Strides strides{1}, dilations{1};
ngraph::Shape pads_begin{0}, pads_end{0}, kernel{1};
auto maxpool_8 = std::make_shared<ngraph::opset8::MaxPool>(data, strides, dilations, pads_begin, pads_end,
kernel);
function_ref = std::make_shared<ngraph::Function>(ngraph::OutputVector{maxpool_8->output(0)},
ngraph::ParameterVector{data});
}
}
TEST_F(TransformationTestsF, negative_ConvertMaxPool1ToMaxPool8_dynamic_rank) {
{
auto data = std::make_shared<ngraph::opset1::Parameter>(ngraph::element::f32, ngraph::PartialShape::dynamic());
ngraph::Strides strides{1};
ngraph::Shape pads_begin{0}, pads_end{0}, kernel{1};
auto maxpool_1 = std::make_shared<ngraph::opset1::MaxPool>(data, strides, pads_begin, pads_end,
kernel);
function = std::make_shared<ngraph::Function>(ngraph::OutputVector{maxpool_1->output(0)},
ngraph::ParameterVector{data});
manager.register_pass<ngraph::pass::ConvertMaxPool1ToMaxPool8>();
}
{
auto data = std::make_shared<ngraph::opset1::Parameter>(ngraph::element::f32, ngraph::PartialShape::dynamic());
ngraph::Strides strides{1};
ngraph::Shape pads_begin{0}, pads_end{0}, kernel{1};
auto maxpool_1 = std::make_shared<ngraph::opset1::MaxPool>(data, strides, pads_begin, pads_end,
kernel);
function_ref = std::make_shared<ngraph::Function>(ngraph::OutputVector{maxpool_1->output(0)},
ngraph::ParameterVector{data});
}
}

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <transformations_visibility.hpp>
#include <ngraph/pass/graph_rewrite.hpp>
namespace ngraph {
namespace pass {
class TRANSFORMATIONS_API ConvertMaxPool1ToMaxPool8;
} // namespace pass
} // namespace ngraph
/**
* @ingroup ie_transformation_common_api
* @brief ConvertMaxPool1ToMaxPool8 converts v1::MaxPool into v8::MaxPool.
*/
class ngraph::pass::ConvertMaxPool1ToMaxPool8 : public ngraph::pass::MatcherPass {
public:
OPENVINO_RTTI("ConvertMaxPool1ToMaxPool8");
ConvertMaxPool1ToMaxPool8();
};

View File

@@ -85,6 +85,7 @@
#include "transformations/op_conversions/gather_normalize_negative_indices.hpp"
#include "transformations/op_conversions/convert_deformable_conv_v8_to_v1.hpp"
#include "transformations/op_conversions/convert_maxpool_downgrade.hpp"
#include "transformations/op_conversions/convert_maxpool_upgrade.hpp"
#include "transformations/disable_decompression_convert_constant_folding.hpp"
#include "transformations/op_conversions/convert_prior_box_v8_to_v0.hpp"
@@ -183,6 +184,7 @@ bool ngraph::pass::CommonOptimizations::run_on_model(const std::shared_ptr<ngrap
manager.register_pass<ngraph::pass::ConvertSoftMax8ToSoftMax1>();
manager.register_pass<ngraph::pass::ConvertSoftMax1ToSoftMax8, false>();
manager.register_pass<ngraph::pass::ConvertMaxPool8ToMaxPool1>();
manager.register_pass<ngraph::pass::ConvertMaxPool1ToMaxPool8, false>();
manager.register_pass<ngraph::pass::ConvertPriorBox8To0>(); // not plugins implemented priorbox8
manager.register_pass<ngraph::pass::ConvertDetectionOutput1ToDetectionOutput8, false>();
manager.register_pass<ngraph::pass::ConvertDetectionOutput8ToDetectionOutput1>();

View File

@@ -0,0 +1,52 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "transformations/op_conversions/convert_maxpool_upgrade.hpp"
#include <ngraph/opsets/opset1.hpp>
#include <ngraph/opsets/opset8.hpp>
#include <ngraph/rt_info.hpp>
#include <ngraph/pattern/op/wrap_type.hpp>
#include <transformations/utils/utils.hpp>
#include "itt.hpp"
ngraph::pass::ConvertMaxPool1ToMaxPool8::ConvertMaxPool1ToMaxPool8() {
MATCHER_SCOPE(ConvertMaxPool1ToMaxPool8);
// Replaces v1::MaxPool with v8::MaxPool with default dilations, axis and index_element_type attributes
auto input = pattern::any_input(pattern::has_static_rank());
auto maxpool_v1_pattern = ngraph::pattern::wrap_type<ngraph::opset1::MaxPool>({input});
ngraph::matcher_pass_callback callback = [=](ngraph::pattern::Matcher& m) {
auto maxpool_v1_node = std::dynamic_pointer_cast<ngraph::opset1::MaxPool>(m.get_match_root());
if (!maxpool_v1_node)
return false;
auto spatial_dims = maxpool_v1_node->get_input_partial_shape(0).rank().get_length() - 2;
if (spatial_dims <= 0)
return false;
ov::Strides dilations(spatial_dims, 1);
auto maxpool_v8_node = std::make_shared<ngraph::opset8::MaxPool>(maxpool_v1_node->input_value(0),
maxpool_v1_node->get_strides(),
dilations,
maxpool_v1_node->get_pads_begin(),
maxpool_v1_node->get_pads_end(),
maxpool_v1_node->get_kernel(),
maxpool_v1_node->get_rounding_type(),
maxpool_v1_node->get_auto_pad(),
ov::element::i64,
0);
maxpool_v8_node->set_friendly_name(maxpool_v1_node->get_friendly_name());
maxpool_v1_node->output(0).replace(maxpool_v8_node->output(0));
ngraph::copy_runtime_info(maxpool_v1_node, maxpool_v8_node);
maxpool_v1_node->clear_control_dependencies();
return true;
};
auto m = std::make_shared<pattern::Matcher>(maxpool_v1_pattern, matcher_name);
register_matcher(m, callback);
}