Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Dołbniak authored Jun 29, 2023
1 parent e43ce5a commit a4a7344
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
#include "transformations/op_conversions/convert_minimum_to_power_and_max.hpp"
#include "transformations/op_conversions/convert_mod.hpp"
#include "transformations/op_conversions/convert_multiclass_nms_upgrade.hpp"
#include "transformations/op_conversions/convert_pad12_downgrade.hpp"
#include "transformations/op_conversions/convert_pad_to_group_conv.hpp"
#include "transformations/op_conversions/convert_prior_box_v8_to_v0.hpp"
#include "transformations/op_conversions/convert_reduce_to_pooling.hpp"
Expand Down Expand Up @@ -213,6 +214,7 @@ bool ov::pass::CommonOptimizations::run_on_model(const std::shared_ptr<ov::Model
REGISTER_PASS(manager, ConvertXorToLogicalXor)
REGISTER_PASS(manager, ConvertTopK11ToTopK3)
REGISTER_PASS(manager, ConvertInterpolate11ToInterpolate4)
REGISTER_PASS(manager, ConvertPad12ToPad1)

auto fq_fusions = manager.register_pass<GraphRewrite>();
ADD_MATCHER(fq_fusions, FakeQuantizeMulFusion)
Expand Down
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);
}
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);
}

0 comments on commit a4a7344

Please sign in to comment.