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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 <algorithm>
  18. #include <deque>
  19. #include <memory>
  20. #include <unordered_set>
  21. #include "ir/anf.h"
  22. #include "ir/manager.h"
  23. #include "optimizer/optimizer.h"
  24. #include "utils/log_adapter.h"
  25. #include "utils/ordered_set.h"
  26. namespace mindspore {
  27. /* namespace to support opt */
  28. namespace opt {
  29. SubstitutionPtr MakeSubstitution(const OptimizerCallerPtr &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 OptimizerCallerPtr &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. auto cnode = node->cast<CNodePtr>();
  41. auto inp0 = cnode->input(0);
  42. auto prim0 = GetValueNode<PrimitivePtr>(inp0);
  43. if (prim0 == nullptr) {
  44. return false;
  45. }
  46. auto hash = prim0->Hash();
  47. auto const &name = prim0->name();
  48. for (auto &prim : prims) {
  49. if (hash == prim->Hash() && name == prim->name()) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. };
  55. return std::make_shared<Substitution>(transform, name, fn, renorm_action);
  56. }
  57. SubstitutionPtr MakeSubstitution(const OptimizerCallerPtr &transform, const std::string &name,
  58. const PredicateFuncType &predicate, const RenormAction &renorm_action) {
  59. return std::make_shared<Substitution>(transform, name, predicate, renorm_action);
  60. }
  61. AnfNodePtr Substitution::operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) {
  62. #ifdef ENABLE_PROFILE
  63. double t = GetTime();
  64. #endif
  65. AnfNodePtr result = (*transform_)(optimizer, node);
  66. #ifdef ENABLE_PROFILE
  67. if (optimizer != nullptr) {
  68. auto time = GetTime();
  69. MsProfile::StatTime("substitution." + name_, time - t);
  70. if (result != nullptr) {
  71. MsProfile::StatTime("match." + name_, time - t);
  72. }
  73. }
  74. #endif
  75. if (optimizer != nullptr && optimizer->is_watch_renormalize() && result != nullptr) {
  76. if (renorm_action_ == FORCE_RENORM) {
  77. optimizer->add_node_to_renormalize(result);
  78. } else {
  79. // renorm_action_ is CHECK_RENORM
  80. if (result->abstract() == nullptr) {
  81. optimizer->add_node_to_renormalize(result);
  82. }
  83. }
  84. }
  85. return result;
  86. }
  87. static bool isTraversable(const AnfNodePtr &node) {
  88. if (node == nullptr) {
  89. return false;
  90. }
  91. if (node->isa<CNode>() || node->isa<Parameter>()) {
  92. return true;
  93. }
  94. if (IsValueNode<FuncGraph>(node) || IsValueNode<RefKey>(node)) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. bool SubstitutionList::ApplyTransform(const OptimizerPtr &optimizer, const AnfNodePtr &root_node,
  100. const SubstitutionPtr &transform) const {
  101. #ifdef ENABLE_PROFILE
  102. double start = GetTime();
  103. #endif
  104. FuncGraphManagerPtr manager = optimizer->manager();
  105. auto seen = NewSeenGeneration();
  106. // 1024 is for the initial capacity of deque
  107. std::deque<AnfNodePtr> todo(1024);
  108. todo.clear();
  109. todo.push_back(root_node);
  110. bool changes = false;
  111. auto &all_nodes = manager->all_nodes();
  112. while (!todo.empty()) {
  113. AnfNodePtr node = todo.front();
  114. todo.pop_front();
  115. // check whether this node has been matched.
  116. if (node == nullptr || node->seen_ == seen || !isTraversable(node) || !all_nodes.contains(node)) {
  117. continue;
  118. }
  119. node->seen_ = seen;
  120. // select nodes that this transform can be applied.
  121. bool is_match = transform->predicate_(node);
  122. // apply transform on this node
  123. bool change = false;
  124. if (is_match) {
  125. auto ret = (*transform)(optimizer, node);
  126. if (ret != nullptr && ret != node) {
  127. change = true;
  128. changes = true;
  129. #ifdef ENABLE_PROFILE
  130. double t = GetTime();
  131. #endif
  132. (void)manager->Replace(node, ret);
  133. #ifdef ENABLE_PROFILE
  134. MsProfile::StatTime("replace." + transform->name_, GetTime() - t);
  135. #endif
  136. node = ret;
  137. }
  138. }
  139. // find success, and add them to todo list
  140. if (IsValueNode<FuncGraph>(node)) {
  141. todo.push_back(GetValueNode<FuncGraphPtr>(node)->output());
  142. }
  143. if (node->isa<CNode>()) {
  144. auto &inputs = node->cast<CNodePtr>()->inputs();
  145. (void)std::copy(inputs.begin(), inputs.end(), std::back_inserter(todo));
  146. }
  147. auto &node_users = manager->node_users();
  148. if (change && node_users.find(node) != node_users.end()) {
  149. for (auto &use : node_users[node]) {
  150. auto use_node = use.first;
  151. if (use_node == nullptr) {
  152. continue;
  153. }
  154. todo.push_back(use_node);
  155. if (use_node->seen_ == seen) {
  156. use_node->seen_--;
  157. }
  158. }
  159. }
  160. }
  161. #ifdef ENABLE_PROFILE
  162. MsProfile::StatTime("opt.transform." + optimizer->name(), GetTime() - start);
  163. #endif
  164. return changes;
  165. }
  166. bool SubstitutionList::operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer) const {
  167. MS_EXCEPTION_IF_NULL(optimizer);
  168. MS_EXCEPTION_IF_NULL(func_graph);
  169. FuncGraphManagerPtr manager = optimizer->manager();
  170. manager->AddFuncGraph(func_graph);
  171. bool loop = false;
  172. bool changes = false;
  173. do {
  174. loop = false;
  175. for (auto const &transform : list_) {
  176. auto change = ApplyTransform(optimizer, func_graph->output(), transform);
  177. changes = changes || change;
  178. loop = loop || change;
  179. }
  180. if (is_once_) {
  181. break;
  182. }
  183. } while (loop);
  184. return changes;
  185. }
  186. } // namespace opt
  187. } // namespace mindspore