forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tomdol
committed
Jun 28, 2023
1 parent
c2afa2a
commit ee486e8
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...common/transformations/include/transformations/op_conversions/convert_pad12_downgrade.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <openvino/pass/graph_rewrite.hpp> | ||
#include <transformations_visibility.hpp> | ||
|
||
namespace ov { | ||
namespace pass { | ||
/** | ||
* @ingroup ie_transformation_common_api | ||
* @brief Converts Pad v12 to Pad v1 | ||
*/ | ||
class TRANSFORMATIONS_API ConvertPad12ToPad1 : public MatcherPass { | ||
public: | ||
OPENVINO_RTTI("ConvertPad12ToPad1", "0"); | ||
ConvertPad12ToPad1(); | ||
}; | ||
|
||
} // namespace pass | ||
} // namespace ov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/common/transformations/src/transformations/op_conversions/convert_pad12_downgrade.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "transformations/op_conversions/convert_pad12_downgrade.hpp" | ||
|
||
#include <ngraph/pattern/op/wrap_type.hpp> | ||
#include <ngraph/rt_info.hpp> | ||
#include <openvino/op/pad.hpp> | ||
|
||
#include "itt.hpp" | ||
|
||
ov::pass::ConvertPad12ToPad1::ConvertPad12ToPad1() { | ||
MATCHER_SCOPE(ConvertPad12ToPad1); | ||
|
||
const auto pad_v12_pattern = pattern::wrap_type<ov::op::v12::Pad>(); | ||
|
||
const matcher_pass_callback callback = [=](pattern::Matcher& m) { | ||
const auto pad_v12 = std::dynamic_pointer_cast<ov::op::v12::Pad>(m.get_match_root()); | ||
if (!pad_v12 || transformation_callback(pad_v12)) { | ||
return false; | ||
} | ||
|
||
std::shared_ptr<ov::Node> pad_v1; | ||
if (pad_v12->get_input_size() == 4) { | ||
pad_v1 = std::make_shared<ov::op::v1::Pad>(pad_v12->input_value(0), | ||
pad_v12->input_value(1), | ||
pad_v12->input_value(2), | ||
pad_v12->input_value(3), | ||
pad_v12->get_pad_mode()); | ||
} else { | ||
const auto pad_value = | ||
ov::op::v0::Constant::create(pad_v12->input_value(0).get_element_type(), ov::Shape{}, {0}); | ||
|
||
pad_v1 = std::make_shared<ov::op::v1::Pad>(pad_v12->input_value(0), | ||
pad_v12->input_value(1), | ||
pad_v12->input_value(2), | ||
pad_value, | ||
pad_v12->get_pad_mode()); | ||
} | ||
pad_v1->set_friendly_name(pad_v12->get_friendly_name()); | ||
copy_runtime_info(pad_v12, pad_v1); | ||
replace_node(pad_v12, pad_v1); | ||
|
||
return true; | ||
}; | ||
|
||
auto m = std::make_shared<pattern::Matcher>(pad_v12_pattern, matcher_name); | ||
register_matcher(m, callback); | ||
} |
86 changes: 86 additions & 0 deletions
86
src/common/transformations/tests/op_conversions/convert_pad12_downgrade_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <memory> | ||
#include <openvino/opsets/opset1.hpp> | ||
#include <openvino/opsets/opset12.hpp> | ||
#include <openvino/pass/manager.hpp> | ||
#include <transformations/op_conversions/convert_pad12_downgrade.hpp> | ||
#include <transformations/utils/utils.hpp> | ||
|
||
#include "common_test_utils/ngraph_test_utils.hpp" | ||
|
||
using namespace testing; | ||
|
||
namespace { | ||
std::shared_ptr<ov::Model> create_v12_model(const ov::op::PadMode pad_mode, const int16_t pad_v = -1) { | ||
const auto input = std::make_shared<ov::opset12::Parameter>(ov::element::i16, ov::Shape{1, 3, 100, 100}); | ||
const auto pads_begin = | ||
std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{0, 2, 1, 0}); | ||
const auto pads_end = | ||
std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{0, 1, 1, 0}); | ||
|
||
std::shared_ptr<ov::opset12::Pad> pad; | ||
if (pad_v != -1) { | ||
const auto pad_value = | ||
std::make_shared<ov::op::v0::Constant>(ov::element::i16, ov::Shape{}, std::vector<int16_t>{pad_v}); | ||
pad = std::make_shared<ov::opset12::Pad>(input, pads_begin, pads_end, pad_value, pad_mode); | ||
} else { | ||
pad = std::make_shared<ov::opset12::Pad>(input, pads_begin, pads_end, pad_mode); | ||
} | ||
pad->set_friendly_name("pad12"); | ||
|
||
return std::make_shared<ov::Model>(pad->outputs(), ov::ParameterVector{input}); | ||
} | ||
|
||
std::shared_ptr<ov::Model> create_v1_model(const ov::op::PadMode pad_mode, const int16_t pad_v) { | ||
const auto input = std::make_shared<ov::opset1::Parameter>(ov::element::i16, ov::Shape{1, 3, 100, 100}); | ||
const auto pads_begin = | ||
std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{0, 2, 1, 0}); | ||
const auto pads_end = | ||
std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{0, 1, 1, 0}); | ||
const auto pad_value = | ||
std::make_shared<ov::op::v0::Constant>(ov::element::i16, ov::Shape{}, std::vector<int16_t>{pad_v}); | ||
|
||
const auto pad = std::make_shared<ov::opset1::Pad>(input, pads_begin, pads_end, pad_value, pad_mode); | ||
pad->set_friendly_name("pad1"); | ||
|
||
return std::make_shared<ov::Model>(pad->outputs(), ov::ParameterVector{input}); | ||
} | ||
|
||
} // namespace | ||
|
||
TEST_F(TransformationTestsF, ConvertPad12ToPad1) { | ||
manager.register_pass<ov::pass::ConvertPad12ToPad1>(); | ||
function = create_v12_model(ov::op::PadMode::CONSTANT); | ||
function_ref = create_v1_model(ov::op::PadMode::CONSTANT, 0); | ||
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); | ||
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); | ||
} | ||
|
||
TEST_F(TransformationTestsF, ConvertPad12ToPad1_explicit_pad_value) { | ||
manager.register_pass<ov::pass::ConvertPad12ToPad1>(); | ||
function = create_v12_model(ov::op::PadMode::CONSTANT, 5); | ||
function_ref = create_v1_model(ov::op::PadMode::CONSTANT, 5); | ||
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); | ||
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); | ||
} | ||
|
||
TEST_F(TransformationTestsF, ConvertPad12ToPad1_symmetric) { | ||
manager.register_pass<ov::pass::ConvertPad12ToPad1>(); | ||
function = create_v12_model(ov::op::PadMode::SYMMETRIC); | ||
function_ref = create_v1_model(ov::op::PadMode::SYMMETRIC, 0); | ||
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); | ||
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); | ||
} | ||
|
||
TEST_F(TransformationTestsF, ConvertPad12ToPad1_symmetric_explicit_pad_value) { | ||
manager.register_pass<ov::pass::ConvertPad12ToPad1>(); | ||
function = create_v12_model(ov::op::PadMode::SYMMETRIC, 5); | ||
function_ref = create_v1_model(ov::op::PadMode::SYMMETRIC, 5); | ||
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); | ||
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); | ||
} |