Files
openvino/docs/snippets/template_pattern_transformation.cpp

156 lines
6.0 KiB
C++
Raw Normal View History

// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "template_pattern_transformation.hpp"
#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"
#include "template_model_transformation.hpp"
// ! [graph_rewrite:template_transformation_cpp]
// template_pattern_transformation.cpp
ov::pass::DecomposeDivideMatcher::DecomposeDivideMatcher() {
MATCHER_SCOPE(DecomposeDivideMatcher);
// Pattern example
auto input0 = pattern::any_input();
auto input1 = pattern::any_input();
auto div = std::make_shared<ov::opset3::Divide>(input0, input1);
ov::matcher_pass_callback callback = [](pattern::Matcher& m) {
auto div = std::dynamic_pointer_cast<ov::opset3::Divide>(m.get_match_root());
// 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
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}));
auto mul = std::make_shared<ov::opset3::Multiply>(div->input_value(0), pow);
// 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
ov::copy_runtime_info(div, {pow, mul});
// Replace Divide operation with Multiply
ov::replace_node(div, mul);
// Return true as the root node was changed
return true;
};
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
// Register pattern with Divide operation as a pattern root node
auto m = std::make_shared<ov::pass::pattern::Matcher>(div, "ConvertDivide");
// Register Matcher
register_matcher(m, callback);
}
// ! [graph_rewrite:template_transformation_cpp]
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
// ! [matcher_pass:relu_fusion]
ov::pass::ReluReluFusionMatcher::ReluReluFusionMatcher() {
MATCHER_SCOPE(ReluReluFusionMatcher);
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});
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
ov::matcher_pass_callback callback = [=](pattern::Matcher& m) {
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
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 =
register_new_node<ov::opset3::Relu>(node_to_output.at(m_relu1).get_node_shared_ptr()->input_value(0));
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
// Copy runtime info attributes to newly created operation
ov::copy_runtime_info(m.get_matched_nodes(), new_relu);
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
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
ov::replace_node(m.get_match_root(), new_relu);
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
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
auto m = std::make_shared<ov::pass::pattern::Matcher>(m_relu2, "ReluReluFusion");
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
// Register Matcher
register_matcher(m, callback);
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
}
// ! [matcher_pass:relu_fusion]
void run_matcher_on_node(std::shared_ptr<ov::Node> node) {
// ! [matcher_pass:run_on_node]
if (ov::pass::DecomposeDivideMatcher().apply(node)) {
// successful execution (root node was replaced)
}
// ! [matcher_pass:run_on_node]
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
}
void run_matcher_with_manager(std::shared_ptr<ov::Model> f) {
// ! [matcher_pass:manager]
// Two matchers will run independently (two independent graph traversals)
// pass::Manager automatically creates GraphRewrite container for each MatcherPass
ov::pass::Manager manager;
manager.register_pass<ov::pass::DecomposeDivideMatcher>();
manager.register_pass<ov::pass::ReluReluFusionMatcher>();
manager.run_passes(f);
// ! [matcher_pass:manager]
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
}
void run_matcher_with_manager2(std::shared_ptr<ov::Model> f) {
// ! [matcher_pass:manager2]
// Register anchor GraphRewrite pass inside manager that will execute two matchers simultaneously
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)
manager.run_passes(f);
// ! [matcher_pass:manager2]
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
}
void run_matcher_with_manager3(std::shared_ptr<ov::Model> f) {
// ! [matcher_pass:manager3]
ov::pass::Manager manager;
manager.register_pass<ov::pass::MyModelTransformation>();
// Two matchers will run independently (two independent graph traversals)
// pass::Manager automatically creates GraphRewrite container for each MatcherPass
manager.register_pass<ov::pass::DecomposeDivideMatcher>();
manager.register_pass<ov::pass::ReluReluFusionMatcher>();
manager.run_passes(f);
// ! [matcher_pass:manager3]
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
}
void run_matcher_with_gr(std::shared_ptr<ov::Model> f) {
// ! [matcher_pass:graph_rewrite]
// Two matcher passes will run simultaneously in a single graph traversal
ov::pass::GraphRewrite pass;
pass.add_matcher<ov::pass::DecomposeDivideMatcher>();
pass.add_matcher<ov::pass::ReluReluFusionMatcher>();
pass.run_on_model(f);
// ! [matcher_pass:graph_rewrite]
nGraph Transformations refactoring (#931) This PR introduces next changes: 1. Transformations *_tbl.hpp files were replaced with direct registration in cpp files. 2. Plugins use pass::Manager to call conversion passes. 3. Transformations callback was moved to PassBase class as there is no more need to keep it in separate class 4. All pattern based transformations must be inherited from MatcherPass class. GraphRewrite class will be used only for matchers registration and execution on function. MatcherPass class adds new features to pattern-based transformations approach: * Allows to run matcher pass on a single node. * Operations that were created inside transformation callback can be added to execution list to be available for pattern matching within single GraphRewrite. 5. GraphRewrite MatchClosure was replaced with MatcherPass. So all matchers will be registered as a MatcherPass. 6. Added pass::Manager::clear_state() method to avoid dependency with nodes that no longer belongs to function after replacement. 7. Some representative transformations were updated to use MatcherPass as an example. 8. Mul->Add sequence fusion transformation was replaced with LinOpSequenceFusion. 9. Pattern and callback registration code was moved to class c-tors (will be finished for remaining passes in other PR) . 10. Updated pass::Manager to get pass names only when NGRAPH_PROFILE_PASS_ENABLE enabled. 11. Moving towards removing PassProperty. 12. Added ngraph::pattern::wrap_type<T>(inputs, pred) to simplify pattern creation. 13. GraphRewrite was updated to execute MatcherPass more efficient.
2020-07-27 19:47:37 +03:00
}
// ! [manual_constant_folding]
template <class T>
ov::Output<ov::Node> eltwise_fold(const ov::Output<ov::Node>& input0, const ov::Output<ov::Node>& input1) {
auto eltwise = std::make_shared<T>(input0, input1);
ov::OutputVector output(eltwise->get_output_size());
// 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]