You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

optimizer.cc 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "backend/common/optimizer/optimizer.h"
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include <utility>
  21. #include "backend/common/optimizer/pass_manager.h"
  22. #include "backend/common/session/anf_runtime_algorithm.h"
  23. #include "include/common/utils/anfalgo.h"
  24. #include "ir/manager.h"
  25. namespace mindspore {
  26. namespace opt {
  27. PatternProcessPass::PatternProcessPass(const std::string &name, bool multigraph)
  28. : NodePass(name),
  29. multigraph_(multigraph),
  30. pattern_engine_(PatternEngine(std::make_shared<Visitor>())),
  31. primitive_vars_(std::make_shared<PrimitiveVarMap>()),
  32. equiv_(std::make_shared<Equiv>()) {}
  33. const BaseRef PatternProcessPass::DefinePattern() const {
  34. VarPtr X = std::make_shared<Var>();
  35. return BaseRef({X});
  36. }
  37. void PatternProcessPass::Build() {
  38. VarPtr fg = std::make_shared<Var>("RootG");
  39. pattern_ = SexpToNode(DefinePattern(), fg, primitive_vars_.get(), multigraph_);
  40. }
  41. AnfNodePtr PatternProcessPass::Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node) {
  42. if (pattern_ == nullptr) {
  43. Build();
  44. }
  45. auto primitive = GetCNodePrimitive(pattern_);
  46. if (IsPrimitiveCNode(node, primitive)) {
  47. MS_EXCEPTION_IF_NULL(primitive_vars_);
  48. MS_EXCEPTION_IF_NULL(equiv_);
  49. equiv_->clear();
  50. EquivPtr equiv = pattern_engine_.Match(pattern_, node, *primitive_vars_, equiv_);
  51. if (equiv != nullptr && !equiv->empty()) {
  52. return Process(func_graph, node, equiv);
  53. }
  54. }
  55. return nullptr;
  56. }
  57. std::vector<AnfNodePtr> PatternProcessPass::GetOrigNodes() const {
  58. std::vector<AnfNodePtr> orig_nodes;
  59. for (auto &prim_var : *primitive_vars_) {
  60. if (equiv_->find(prim_var.second) == equiv_->end()) {
  61. continue;
  62. }
  63. auto baseref = (*equiv_)[prim_var.second];
  64. if (!utils::isa<CNode>(baseref)) {
  65. continue;
  66. }
  67. auto node = utils::cast<AnfNodePtr>(baseref);
  68. orig_nodes.push_back(node);
  69. }
  70. return orig_nodes;
  71. }
  72. CNodePtr PatternProcessPass::NewCNode(const std::vector<AnfNodePtr> &inputs, const FuncGraphPtr &fg) const {
  73. MS_EXCEPTION_IF_NULL(fg);
  74. auto orig_nodes = GetOrigNodes();
  75. return opt::NewCNode(inputs, fg, orig_nodes);
  76. }
  77. CNodePtr PatternProcessPass::NewCNode(const CNodePtr &cnode, const KernelGraphPtr &fg) const {
  78. MS_EXCEPTION_IF_NULL(fg);
  79. auto orig_nodes = GetOrigNodes();
  80. return opt::NewCNode(cnode, fg, orig_nodes);
  81. }
  82. bool MultipleOutputPatternProcessPass::MatchAnotherPattern(const AnfNodePtr &node, const EquivPtr &equiv) const {
  83. MS_EXCEPTION_IF_NULL(node);
  84. MS_EXCEPTION_IF_NULL(equiv);
  85. VarPtr fg = std::make_shared<Var>("RootG");
  86. MS_EXCEPTION_IF_NULL(child_primitive_vars_);
  87. MS_EXCEPTION_IF_NULL(child_equiv_);
  88. EquivPtr another_equiv =
  89. child_pattern_engine_.Match(SexpToNode(DefineAnotherPattern(), fg, child_primitive_vars_.get(), true), node,
  90. *child_primitive_vars_, child_equiv_);
  91. if (another_equiv != nullptr && !another_equiv->empty()) {
  92. return IsShareNodes(equiv, another_equiv);
  93. }
  94. return false;
  95. }
  96. std::vector<AnfNodePtr> MultipleOutputPatternProcessPass::GetOrigNodes() const {
  97. std::vector<AnfNodePtr> orig_nodes = PatternProcessPass::GetOrigNodes();
  98. for (auto &prim_var : *child_primitive_vars_) {
  99. auto baseref = (*child_equiv_)[prim_var.second];
  100. if (!utils::isa<CNode>(baseref)) {
  101. continue;
  102. }
  103. auto node = utils::cast<AnfNodePtr>(baseref);
  104. orig_nodes.push_back(node);
  105. }
  106. return orig_nodes;
  107. }
  108. void GraphOptimizer::AddPassManager(const PassManagerPtr &pass_manager) {
  109. if (pass_manager != nullptr) {
  110. pass_managers_.push_back(pass_manager);
  111. }
  112. }
  113. FuncGraphPtr GraphOptimizer::Optimize(const FuncGraphPtr &func_graph, bool run_only_once) {
  114. MS_EXCEPTION_IF_NULL(func_graph);
  115. run_only_once_ = (pass_managers_.size() == 1) ? true : run_only_once;
  116. // cppcheck-suppress *
  117. auto manager = Manage(func_graph, true);
  118. bool changed = true;
  119. while (changed) {
  120. changed = false;
  121. for (size_t i = 0; i < pass_managers_.size(); ++i) {
  122. const PassManagerPtr &pm = pass_managers_[i];
  123. if (pm != nullptr && pm->Run(func_graph)) {
  124. changed = true;
  125. }
  126. }
  127. if (run_only_once_) {
  128. break;
  129. }
  130. }
  131. std::vector<FuncGraphPtr> func_graphs;
  132. func_graphs.push_back(func_graph);
  133. (void)TopoSort(func_graph->get_return());
  134. return func_graph;
  135. }
  136. } // namespace opt
  137. } // namespace mindspore