2023-01-16 11:02:17 +04:00
|
|
|
// Copyright (C) 2018-2023 Intel Corporation
|
2020-06-30 18:02:26 +03:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
//
|
|
|
|
|
|
2023-03-08 11:12:05 +04:00
|
|
|
#include "template_pattern_transformation.hpp"
|
2020-06-30 18:02:26 +03:00
|
|
|
|
2022-03-01 09:03:59 +03:00
|
|
|
#include "openvino/cc/pass/itt.hpp"
|
|
|
|
|
#include "openvino/core/rt_info.hpp"
|
|
|
|
|
#include "openvino/opsets/opset3.hpp"
|
|
|
|
|
#include "openvino/pass/manager.hpp"
|
|
|
|
|
#include "openvino/pass/pattern/op/wrap_type.hpp"
|
2023-03-08 11:12:05 +04:00
|
|
|
#include "template_model_transformation.hpp"
|
2020-06-30 18:02:26 +03:00
|
|
|
|
|
|
|
|
// ! [graph_rewrite:template_transformation_cpp]
|
|
|
|
|
// template_pattern_transformation.cpp
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::pass::DecomposeDivideMatcher::DecomposeDivideMatcher() {
|
2021-06-01 10:05:17 +03:00
|
|
|
MATCHER_SCOPE(DecomposeDivideMatcher);
|
2020-06-30 18:02:26 +03:00
|
|
|
// Pattern example
|
2020-09-23 17:26:12 +03:00
|
|
|
auto input0 = pattern::any_input();
|
|
|
|
|
auto input1 = pattern::any_input();
|
2022-02-23 06:29:03 +03:00
|
|
|
auto div = std::make_shared<ov::opset3::Divide>(input0, input1);
|
2020-06-30 18:02:26 +03:00
|
|
|
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::matcher_pass_callback callback = [](pattern::Matcher& m) {
|
|
|
|
|
auto div = std::dynamic_pointer_cast<ov::opset3::Divide>(m.get_match_root());
|
2020-06-30 18:02:26 +03:00
|
|
|
// We can not apply this transformation in case with integer input data type
|
|
|
|
|
if (!div || div->input(0).get_element_type().is_integral()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decompose Divide into Multiply with Power operations
|
2022-02-23 06:29:03 +03:00
|
|
|
auto pow = std::make_shared<ov::opset3::Power>(
|
2021-08-11 09:38:43 +03:00
|
|
|
div->input_value(1),
|
|
|
|
|
opset3::Constant::create(div->get_input_element_type(1), Shape{1}, {-1}));
|
2020-06-30 18:02:26 +03:00
|
|
|
|
2022-02-23 06:29:03 +03:00
|
|
|
auto mul = std::make_shared<ov::opset3::Multiply>(div->input_value(0), pow);
|
2020-06-30 18:02:26 +03:00
|
|
|
|
|
|
|
|
// Save original name to last operation in replacement sub-graph
|
|
|
|
|
mul->set_friendly_name(div->get_friendly_name());
|
|
|
|
|
|
|
|
|
|
// Copy runtime info attributes to newly created operation
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::copy_runtime_info(div, {pow, mul});
|
2020-06-30 18:02:26 +03:00
|
|
|
|
|
|
|
|
// Replace Divide operation with Multiply
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::replace_node(div, mul);
|
2020-06-30 18:02:26 +03:00
|
|
|
|
|
|
|
|
// Return true as the root node was changed
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-27 19:47:37 +03:00
|
|
|
// Register pattern with Divide operation as a pattern root node
|
2022-02-23 06:29:03 +03:00
|
|
|
auto m = std::make_shared<ov::pass::pattern::Matcher>(div, "ConvertDivide");
|
2020-06-30 18:02:26 +03:00
|
|
|
// Register Matcher
|
2020-09-23 17:26:12 +03:00
|
|
|
register_matcher(m, callback);
|
2020-06-30 18:02:26 +03:00
|
|
|
}
|
2020-07-09 06:09:28 +03:00
|
|
|
// ! [graph_rewrite:template_transformation_cpp]
|
2020-07-27 19:47:37 +03:00
|
|
|
|
|
|
|
|
// ! [matcher_pass:relu_fusion]
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::pass::ReluReluFusionMatcher::ReluReluFusionMatcher() {
|
2021-06-01 10:05:17 +03:00
|
|
|
MATCHER_SCOPE(ReluReluFusionMatcher);
|
2022-02-23 06:29:03 +03:00
|
|
|
auto m_relu1 = ov::pass::pattern::wrap_type<ov::opset3::Relu>(pattern::consumers_count(1));
|
|
|
|
|
auto m_relu2 = ov::pass::pattern::wrap_type<ov::opset3::Relu>({m_relu1});
|
2020-07-27 19:47:37 +03:00
|
|
|
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::matcher_pass_callback callback = [=](pattern::Matcher& m) {
|
2020-07-27 19:47:37 +03:00
|
|
|
// Map that helps to connect labels with matched outputs
|
|
|
|
|
auto& node_to_output = m.get_pattern_value_map();
|
|
|
|
|
|
|
|
|
|
// Create new Relu operation and add register it for additional execution
|
2021-08-11 09:38:43 +03:00
|
|
|
auto new_relu =
|
2022-02-23 06:29:03 +03:00
|
|
|
register_new_node<ov::opset3::Relu>(node_to_output.at(m_relu1).get_node_shared_ptr()->input_value(0));
|
2020-07-27 19:47:37 +03:00
|
|
|
|
|
|
|
|
// Copy runtime info attributes to newly created operation
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::copy_runtime_info(m.get_matched_nodes(), new_relu);
|
2020-07-27 19:47:37 +03:00
|
|
|
|
|
|
|
|
// Save last Relu name to new Relu operation
|
|
|
|
|
new_relu->set_friendly_name(m.get_match_root()->get_friendly_name());
|
|
|
|
|
|
|
|
|
|
// Replace Relu->Relu with Relu
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::replace_node(m.get_match_root(), new_relu);
|
2020-07-27 19:47:37 +03:00
|
|
|
|
|
|
|
|
// Return true as the root node was changed
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Register pattern with Relu operation as a pattern root node
|
2022-02-23 06:29:03 +03:00
|
|
|
auto m = std::make_shared<ov::pass::pattern::Matcher>(m_relu2, "ReluReluFusion");
|
2020-07-27 19:47:37 +03:00
|
|
|
// Register Matcher
|
2020-09-23 17:26:12 +03:00
|
|
|
register_matcher(m, callback);
|
2020-07-27 19:47:37 +03:00
|
|
|
}
|
|
|
|
|
// ! [matcher_pass:relu_fusion]
|
|
|
|
|
|
2022-02-23 06:29:03 +03:00
|
|
|
void run_matcher_on_node(std::shared_ptr<ov::Node> node) {
|
2021-05-25 10:32:48 +03:00
|
|
|
// ! [matcher_pass:run_on_node]
|
2022-02-23 06:29:03 +03:00
|
|
|
if (ov::pass::DecomposeDivideMatcher().apply(node)) {
|
2021-05-25 10:32:48 +03:00
|
|
|
// successful execution (root node was replaced)
|
|
|
|
|
}
|
|
|
|
|
// ! [matcher_pass:run_on_node]
|
2020-07-27 19:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 06:29:03 +03:00
|
|
|
void run_matcher_with_manager(std::shared_ptr<ov::Model> f) {
|
2021-05-25 10:32:48 +03:00
|
|
|
// ! [matcher_pass:manager]
|
|
|
|
|
// Two matchers will run independently (two independent graph traversals)
|
|
|
|
|
// pass::Manager automatically creates GraphRewrite container for each MatcherPass
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::pass::Manager manager;
|
|
|
|
|
manager.register_pass<ov::pass::DecomposeDivideMatcher>();
|
|
|
|
|
manager.register_pass<ov::pass::ReluReluFusionMatcher>();
|
2021-05-25 10:32:48 +03:00
|
|
|
manager.run_passes(f);
|
|
|
|
|
// ! [matcher_pass:manager]
|
2020-07-27 19:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 06:29:03 +03:00
|
|
|
void run_matcher_with_manager2(std::shared_ptr<ov::Model> f) {
|
2021-05-25 10:32:48 +03:00
|
|
|
// ! [matcher_pass:manager2]
|
|
|
|
|
// Register anchor GraphRewrite pass inside manager that will execute two matchers simultaneously
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::pass::Manager manager;
|
|
|
|
|
auto anchor = manager.register_pass<ov::pass::GraphRewrite>();
|
[Core/CC]Verify add_matcher scope for Conditinal Compilation. (#13148)
* My verification:
libopenvino.so original size: 10799560
After adding this patch, new size: 10684744
Reduce: (10799560 - 10684744)/1024.0 = 112.125K.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* try to fix clang issue
* Merge xuejun/cc_test
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Test
Signed-off-by: xuejun <Xuejun.Zhai@intel.com>
* TEST1
Signed-off-by: xuejun <Xuejun.Zhai@intel.com>
* TEST2
Signed-off-by: xuejun <Xuejun.Zhai@intel.com>
* Complete Add_matcher wraper.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* register_pass rebase
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* fix clang issue.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* fix some replace error.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* 1: Rename ADD_MATCHER_SCOPE_WITH_OBJ -> ADD_MATCHER_SCOPE;
2: Remove debug info;
3: Fix bug: REGISTER_PASS_WITH_FALSE_
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Add ADD_MATCHER_SCOPE_WITHOUT_NSPACE macro
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* fix macro define issue.
* Add register_pass for cnn_network_ngraph_impl.cpp
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Try to fix vpux plugin fail issue.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Fix clang issue
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Merge macro "REGISTER_PASS_MODEL_SCOPE" and "REGISTER_PASS_FUNCTION_SCOPE" to "REGISTER_PASS_SCOPE"
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Rename "REGISTER_PASS_SCOPE_WITH_FALSE" to "REGISTER_DISABLED_PASS_SCOPE_WITH"
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Rename ADD_MATCHER_SCOPE_WITHOUT_OBJ to ADD_MATCHER_SCOPE_FOR_THIS
ADD_MATCHER_SCOPE_WITHOUT_NSPACE to ADD_MATCHER_SCOPE_FOR_THIS_WITHOUT_NSPACE
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* In order to implement CC, I have to move "StridesOptimization()" constructor to cpp file.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Remove "SCOPE" in macro.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Recover changes to fix clang issue.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Remove "REGISTER_PASS_MODEL_IF"
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* 1: Remove "ADD_MATCHER_FOR_THIS_WITHOUT_NSPACE";
2: Remove param "nspace" in Macro, replace with "using namespace " in local.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* fix missing update.
* For MACRO REGISTER_PASS:
1: Only keep a external macro define;
2: Judge 3 possibilities, if one of them is true, it will be disable;
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Optimize ADD_MATCHER_FOR_THIS.
-# define ADD_MATCHER_FOR_THIS(region, ...) add_matcher<region>(__VA_ARGS__);
+# define ADD_MATCHER_FOR_THIS(region, ...) ADD_MATCHER(this, region, __VA_ARGS__)
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Update src/common/conditional_compilation/include/openvino/cc/pass/itt.hpp
Co-authored-by: Ilya Churaev <ilyachur@gmail.com>
* Update src/common/conditional_compilation/include/openvino/cc/pass/itt.hpp
Co-authored-by: Ilya Churaev <ilyachur@gmail.com>
* Update src/common/conditional_compilation/include/openvino/cc/pass/itt.hpp
Co-authored-by: Ilya Churaev <ilyachur@gmail.com>
* Update src/common/conditional_compilation/include/openvino/cc/pass/itt.hpp
Co-authored-by: Ilya Churaev <ilyachur@gmail.com>
* clang format
* Update OR defiation in MACRO, increasing code scalability.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
* Fix window compiles error.
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
Signed-off-by: Yan, Xiping <xiping.yan@intel.com>
Signed-off-by: xuejun <Xuejun.Zhai@intel.com>
Co-authored-by: xuejun <Xuejun.Zhai@intel.com>
Co-authored-by: Ilya Churaev <ilyachur@gmail.com>
2022-11-21 00:02:42 +08:00
|
|
|
using namespace ov::pass;
|
|
|
|
|
ADD_MATCHER(anchor, DecomposeDivideMatcher)
|
|
|
|
|
ADD_MATCHER(anchor, ReluReluFusionMatcher)
|
2021-05-25 10:32:48 +03:00
|
|
|
manager.run_passes(f);
|
|
|
|
|
// ! [matcher_pass:manager2]
|
2020-07-27 19:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 06:29:03 +03:00
|
|
|
void run_matcher_with_manager3(std::shared_ptr<ov::Model> f) {
|
2021-05-25 10:32:48 +03:00
|
|
|
// ! [matcher_pass:manager3]
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::pass::Manager manager;
|
2022-03-01 09:03:59 +03:00
|
|
|
manager.register_pass<ov::pass::MyModelTransformation>();
|
2021-05-25 10:32:48 +03:00
|
|
|
// Two matchers will run independently (two independent graph traversals)
|
|
|
|
|
// pass::Manager automatically creates GraphRewrite container for each MatcherPass
|
2022-02-23 06:29:03 +03:00
|
|
|
manager.register_pass<ov::pass::DecomposeDivideMatcher>();
|
|
|
|
|
manager.register_pass<ov::pass::ReluReluFusionMatcher>();
|
2021-05-25 10:32:48 +03:00
|
|
|
manager.run_passes(f);
|
|
|
|
|
// ! [matcher_pass:manager3]
|
2020-07-27 19:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 06:29:03 +03:00
|
|
|
void run_matcher_with_gr(std::shared_ptr<ov::Model> f) {
|
2021-05-25 10:32:48 +03:00
|
|
|
// ! [matcher_pass:graph_rewrite]
|
|
|
|
|
// Two matcher passes will run simultaneously in a single graph traversal
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::pass::GraphRewrite pass;
|
|
|
|
|
pass.add_matcher<ov::pass::DecomposeDivideMatcher>();
|
|
|
|
|
pass.add_matcher<ov::pass::ReluReluFusionMatcher>();
|
2021-12-10 13:08:38 +03:00
|
|
|
pass.run_on_model(f);
|
2021-05-25 10:32:48 +03:00
|
|
|
// ! [matcher_pass:graph_rewrite]
|
2020-07-27 19:47:37 +03:00
|
|
|
}
|
2020-09-23 17:26:12 +03:00
|
|
|
|
|
|
|
|
// ! [manual_constant_folding]
|
|
|
|
|
template <class T>
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::Output<ov::Node> eltwise_fold(const ov::Output<ov::Node>& input0, const ov::Output<ov::Node>& input1) {
|
2020-09-23 17:26:12 +03:00
|
|
|
auto eltwise = std::make_shared<T>(input0, input1);
|
2022-02-23 06:29:03 +03:00
|
|
|
ov::OutputVector output(eltwise->get_output_size());
|
2020-09-23 17:26:12 +03:00
|
|
|
// If constant folding wasn't successful return eltwise output
|
|
|
|
|
if (!eltwise->constant_fold(output, {input0, input1})) {
|
|
|
|
|
return eltwise->output(0);
|
|
|
|
|
}
|
|
|
|
|
return output[0];
|
|
|
|
|
}
|
|
|
|
|
// ! [manual_constant_folding]
|