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.

opt.cc 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "optimizer/opt.h"
  17. #include <memory>
  18. #include <unordered_set>
  19. #include <deque>
  20. #include <algorithm>
  21. #include "ir/anf.h"
  22. #include "ir/manager.h"
  23. #include "utils/ordered_set.h"
  24. #include "utils/log_adapter.h"
  25. #include "optimizer/optimizer.h"
  26. namespace mindspore {
  27. /* namespace to support opt */
  28. namespace opt {
  29. SubstitutionPtr MakeSubstitution(const TransformFuncType &transform, const std::string &name, const PrimitivePtr &prim,
  30. const RenormAction &renorm_action) {
  31. auto fn = [prim](const AnfNodePtr &node) -> bool { return IsPrimitiveCNode(node, prim); };
  32. return std::make_shared<Substitution>(transform, name, fn, renorm_action);
  33. }
  34. SubstitutionPtr MakeSubstitution(const TransformFuncType &transform, const std::string &name,
  35. const std::vector<PrimitivePtr> &prims, const RenormAction &renorm_action) {
  36. auto fn = [prims](const AnfNodePtr &node) -> bool {
  37. if (!node->isa<CNode>()) {
  38. return false;
  39. }
  40. for (auto &prim : prims) {
  41. if (IsPrimitiveCNode(node, prim)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. };
  47. return std::make_shared<Substitution>(transform, name, fn, renorm_action);
  48. }
  49. SubstitutionPtr MakeSubstitution(const TransformFuncType &transform, const std::string &name,
  50. const PredicateFuncType &predicate, const RenormAction &renorm_action) {
  51. return std::make_shared<Substitution>(transform, name, predicate, renorm_action);
  52. }
  53. AnfNodePtr Substitution::operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) const {
  54. #ifdef ENABLE_PROFILE
  55. double t = GetTime();
  56. #endif
  57. AnfNodePtr result = transform_(optimizer, node);
  58. #ifdef ENABLE_PROFILE
  59. if (optimizer != nullptr) {
  60. auto time = GetTime();
  61. MsProfile::StatTime("substitution." + name_, time - t);
  62. if (result != nullptr) {
  63. MsProfile::StatTime("match." + name_, time - t);
  64. }
  65. }
  66. #endif
  67. if (optimizer != nullptr && optimizer->is_watch_renormalize() && result != nullptr) {
  68. if (renorm_action_ == FORCE_RENORM) {
  69. optimizer->add_node_to_renormalize(result);
  70. } else {
  71. // renorm_action_ is CHECK_RENORM
  72. if (result->abstract() == nullptr) {
  73. optimizer->add_node_to_renormalize(result);
  74. }
  75. }
  76. }
  77. return result;
  78. }
  79. bool SubstitutionList::ApplyTransform(const OptimizerPtr &optimizer, const AnfNodePtr &root_node,
  80. const SubstitutionPtr &transform) const {
  81. FuncGraphManagerPtr manager = optimizer->manager();
  82. std::unordered_set<AnfNodePtr> seen_node;
  83. std::deque<AnfNodePtr> todo{root_node};
  84. bool changes = false;
  85. while (!todo.empty()) {
  86. AnfNodePtr node = todo.front();
  87. todo.pop_front();
  88. // check whether this node has been matched.
  89. if (seen_node.find(node) != seen_node.end() || !manager->all_nodes().contains(node)) {
  90. continue;
  91. }
  92. (void)seen_node.insert(node);
  93. // select nodes that this transform can be applied.
  94. bool is_match = transform->predicate_(node);
  95. // apply transform on this node
  96. bool change = false;
  97. if (is_match) {
  98. auto ret = (*transform)(optimizer, node);
  99. if (ret != nullptr && ret != node) {
  100. change = true;
  101. #ifdef ENABLE_PROFILE
  102. double t = GetTime();
  103. #endif
  104. (void)manager->Replace(node, ret);
  105. #ifdef ENABLE_PROFILE
  106. MsProfile::StatTime("replace." + transform->name_, GetTime() - t);
  107. #endif
  108. node = ret;
  109. }
  110. }
  111. // find success, and add them to todo list
  112. if (IsValueNode<FuncGraph>(node)) {
  113. todo.push_back(GetValueNode<FuncGraphPtr>(node)->output());
  114. }
  115. if (node->isa<CNode>()) {
  116. auto &inputs = node->cast<CNodePtr>()->inputs();
  117. (void)std::copy(inputs.begin(), inputs.end(), std::back_inserter(todo));
  118. }
  119. auto &node_users = manager->node_users();
  120. if (change && node_users.find(node) != node_users.end()) {
  121. for (auto &use : node_users[node]) {
  122. auto use_node = use.first;
  123. todo.push_back(use_node);
  124. if (seen_node.find(use_node) != seen_node.end()) {
  125. (void)seen_node.erase(use_node);
  126. }
  127. }
  128. }
  129. changes = changes || change;
  130. }
  131. return changes;
  132. }
  133. bool SubstitutionList::operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer) const {
  134. MS_EXCEPTION_IF_NULL(optimizer);
  135. MS_EXCEPTION_IF_NULL(func_graph);
  136. FuncGraphManagerPtr manager = optimizer->manager();
  137. manager->AddFuncGraph(func_graph);
  138. bool loop = false;
  139. bool changes = false;
  140. do {
  141. loop = false;
  142. for (auto const &transform : list_) {
  143. auto change = ApplyTransform(optimizer, func_graph->output(), transform);
  144. changes = changes || change;
  145. loop = loop || change;
  146. }
  147. if (is_once_) {
  148. break;
  149. }
  150. } while (loop);
  151. return changes;
  152. }
  153. } // namespace opt
  154. } // namespace mindspore