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 6.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. static bool isTraversable(const AnfNodePtr &node) {
  80. if (node == nullptr) {
  81. return false;
  82. }
  83. if (node->isa<CNode>() || node->isa<Parameter>()) {
  84. return true;
  85. }
  86. if (IsValueNode<FuncGraph>(node) || IsValueNode<RefKey>(node)) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. bool SubstitutionList::ApplyTransform(const OptimizerPtr &optimizer, const AnfNodePtr &root_node,
  92. const SubstitutionPtr &transform) const {
  93. #ifdef ENABLE_PROFILE
  94. double start = GetTime();
  95. #endif
  96. FuncGraphManagerPtr manager = optimizer->manager();
  97. auto seen = NewSeenGeneration();
  98. // 1024 is for the initial capacity of deque
  99. std::deque<AnfNodePtr> todo(1024);
  100. todo.push_back(root_node);
  101. bool changes = false;
  102. auto &all_nodes = manager->all_nodes();
  103. while (!todo.empty()) {
  104. AnfNodePtr node = todo.front();
  105. todo.pop_front();
  106. // check whether this node has been matched.
  107. if (node == nullptr || node->seen_ == seen || !isTraversable(node) || !all_nodes.contains(node)) {
  108. continue;
  109. }
  110. node->seen_ = seen;
  111. // select nodes that this transform can be applied.
  112. bool is_match = transform->predicate_(node);
  113. // apply transform on this node
  114. bool change = false;
  115. if (is_match) {
  116. auto ret = (*transform)(optimizer, node);
  117. if (ret != nullptr && ret != node) {
  118. change = true;
  119. changes = true;
  120. #ifdef ENABLE_PROFILE
  121. double t = GetTime();
  122. #endif
  123. (void)manager->Replace(node, ret);
  124. #ifdef ENABLE_PROFILE
  125. MsProfile::StatTime("replace." + transform->name_, GetTime() - t);
  126. #endif
  127. node = ret;
  128. }
  129. }
  130. // find success, and add them to todo list
  131. if (IsValueNode<FuncGraph>(node)) {
  132. todo.push_back(GetValueNode<FuncGraphPtr>(node)->output());
  133. }
  134. if (node->isa<CNode>()) {
  135. auto &inputs = node->cast<CNodePtr>()->inputs();
  136. (void)std::copy(inputs.begin(), inputs.end(), std::back_inserter(todo));
  137. }
  138. auto &node_users = manager->node_users();
  139. if (change && node_users.find(node) != node_users.end()) {
  140. for (auto &use : node_users[node]) {
  141. auto use_node = use.first;
  142. if (use_node == nullptr) {
  143. continue;
  144. }
  145. todo.push_back(use_node);
  146. if (use_node->seen_ == seen) {
  147. use_node->seen_--;
  148. }
  149. }
  150. }
  151. }
  152. #ifdef ENABLE_PROFILE
  153. MsProfile::StatTime("opt.transform", GetTime() - start);
  154. #endif
  155. return changes;
  156. }
  157. bool SubstitutionList::operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer) const {
  158. MS_EXCEPTION_IF_NULL(optimizer);
  159. MS_EXCEPTION_IF_NULL(func_graph);
  160. FuncGraphManagerPtr manager = optimizer->manager();
  161. manager->AddFuncGraph(func_graph);
  162. bool loop = false;
  163. bool changes = false;
  164. do {
  165. loop = false;
  166. for (auto const &transform : list_) {
  167. auto change = ApplyTransform(optimizer, func_graph->output(), transform);
  168. changes = changes || change;
  169. loop = loop || change;
  170. }
  171. if (is_once_) {
  172. break;
  173. }
  174. } while (loop);
  175. return changes;
  176. }
  177. } // namespace opt
  178. } // namespace mindspore