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

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