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.

cse.cc 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "frontend/optimizer/cse.h"
  19. #include <vector>
  20. #include <set>
  21. #include <unordered_map>
  22. #include "abstract/abstract_function.h"
  23. #include "utils/flags.h"
  24. namespace mindspore {
  25. /* namespace to support opt */
  26. namespace opt {
  27. using mindspore::abstract::AbstractBase;
  28. using mindspore::abstract::AbstractFunction;
  29. using mindspore::abstract::AbstractFunctionPtr;
  30. BasePtr AbsOf(const AnfNodePtr &node, bool ignore_fg_abs_tracking_id) {
  31. MS_EXCEPTION_IF_NULL(node);
  32. auto node_abs = node->abstract();
  33. // In testcase: TestOptOpt.CSE, node->abstract() is null.
  34. if (node_abs == nullptr) {
  35. return kAnyValue;
  36. }
  37. if (node_abs->isa<abstract::PrimitiveAbstractClosure>()) {
  38. // Ignore the tracking_id and prim pointer hash.
  39. auto prim_abs = node_abs->cast<abstract::PrimitiveAbstractClosurePtr>();
  40. return prim_abs->prim();
  41. } else if (ignore_fg_abs_tracking_id && node_abs->isa<abstract::FuncGraphAbstractClosure>()) {
  42. // Ignore the tracking_id.
  43. auto new_fg_abs = node_abs->cast<abstract::AbstractFunctionPtr>()->Copy();
  44. new_fg_abs->set_tracking_id(nullptr);
  45. return new_fg_abs;
  46. }
  47. return node_abs;
  48. }
  49. bool CSE::BuildOrderGroupAndDoReplace(const FuncGraphManagerPtr manager) const {
  50. bool changed = false;
  51. for (FuncGraphPtr fg : manager->func_graphs()) {
  52. MS_EXCEPTION_IF_NULL(fg);
  53. std::vector<std::size_t> order_group;
  54. std::unordered_map<std::size_t, std::vector<AnfNodePtr>> groups;
  55. std::unordered_map<AnfNodePtr, std::size_t> hashes;
  56. std::vector<AnfNodePtr> toposet = TopoSort(fg->get_return());
  57. for (auto node : toposet) {
  58. MS_EXCEPTION_IF_NULL(node);
  59. if (hashes.find(node) != hashes.end()) {
  60. continue;
  61. }
  62. std::size_t h = 0;
  63. if (node->isa<ValueNode>()) {
  64. ValueNodePtr value_node = node->cast<ValueNodePtr>();
  65. auto value = value_node->value();
  66. MS_EXCEPTION_IF_NULL(value);
  67. h = hash_combine(value->hash(), (AbsOf(value_node, true)->hash()));
  68. } else if (node->isa<CNode>()) {
  69. auto cnode = node->cast<CNodePtr>();
  70. auto &inputs = cnode->inputs();
  71. size_t init = 0;
  72. h = std::accumulate(inputs.begin(), inputs.end(), init, [&hashes](std::size_t hash, const AnfNodePtr &node_in) {
  73. return hash_combine(hash, hashes[node_in]);
  74. });
  75. } else if (node->isa<Parameter>()) {
  76. h = node->hash();
  77. } else {
  78. MS_LOG(ERROR) << "Unknow node type";
  79. }
  80. hashes[node] = h;
  81. if (groups.find(h) == groups.end()) {
  82. std::vector<AnfNodePtr> innervec({node});
  83. groups[h] = innervec;
  84. order_group.emplace_back(h);
  85. } else {
  86. groups[h].push_back(node);
  87. }
  88. }
  89. changed = DoReplace(manager, order_group, &groups) || changed;
  90. }
  91. return changed;
  92. }
  93. // The op like print, summary, or the op do not has true output, and always as a depend node input.
  94. static bool HasSideEffect(const AnfNodePtr &node) {
  95. auto prim = GetCNodePrimitive(node);
  96. if (prim == nullptr) {
  97. return false;
  98. }
  99. auto side_effect_v = prim->GetAttr(GRAPH_FLAG_SIDE_EFFECT);
  100. if (side_effect_v != nullptr && side_effect_v->isa<BoolImm>()) {
  101. return GetValue<bool>(side_effect_v);
  102. }
  103. return false;
  104. }
  105. // If true do not merge the node.
  106. bool CSE::CheckRandomEffect(const AnfNodePtr &main, const AnfNodePtr &node) const {
  107. bool has_random_effect = false;
  108. auto prim_main = GetCNodePrimitive(main);
  109. auto prim_node = GetCNodePrimitive(node);
  110. // if has random effect, when generate by different op (not same object), do not merge.
  111. if (prim_main != nullptr) {
  112. if (prim_main == prim_node) {
  113. return false;
  114. }
  115. auto effect_val = prim_main->GetAttr(GRAPH_FLAG_RANDOM_EFFECT);
  116. if (effect_val != nullptr && effect_val->isa<BoolImm>()) {
  117. has_random_effect = GetValue<bool>(effect_val);
  118. }
  119. }
  120. return has_random_effect;
  121. }
  122. bool CSE::CheckReplace(const AnfNodePtr &main, const AnfNodePtr &node, bool check_side_effect) const {
  123. MS_EXCEPTION_IF_NULL(main);
  124. MS_EXCEPTION_IF_NULL(node);
  125. if (main->isa<ValueNode>() && node->isa<ValueNode>()) {
  126. auto main_value = GetValueNode(main);
  127. auto node_value = GetValueNode(node);
  128. return (AbsOf(main, true) == AbsOf(node, true)) && (*main_value == *node_value);
  129. } else if (main->isa<CNode>() && node->isa<CNode>()) {
  130. auto c_main = main->cast<CNodePtr>();
  131. auto c_node = node->cast<CNodePtr>();
  132. // When appsame is true, check if has side effect, do not merge.
  133. if (check_side_effect && HasSideEffect(main)) {
  134. return false;
  135. }
  136. const auto &inp1 = c_main->inputs();
  137. const auto &inp2 = c_node->inputs();
  138. if (inp1.size() != inp2.size()) {
  139. return false;
  140. }
  141. for (size_t j = 0; j < inp1.size(); j++) {
  142. auto inp1_j = inp1[j];
  143. auto inp2_j = inp2[j];
  144. MS_EXCEPTION_IF_NULL(inp1_j);
  145. MS_EXCEPTION_IF_NULL(inp2_j);
  146. if (!(*inp1_j == *inp2_j)) {
  147. // Handle the case of two different Tensor, but with the same value
  148. if (IsValueNode<tensor::Tensor>(inp1_j) && IsValueNode<tensor::Tensor>(inp2_j)) {
  149. auto tensor1 = GetValueNode<tensor::TensorPtr>(inp1_j);
  150. auto tensor2 = GetValueNode<tensor::TensorPtr>(inp2_j);
  151. if (tensor1->ValueEqual(*tensor2)) {
  152. continue;
  153. }
  154. } else if (HasSideEffect(inp1_j) && HasSideEffect(inp2_j)) {
  155. // When the same side effect node as another two nodes' inputs, we still merge the node.
  156. // Because the node only can be the inputs of `depend`, when the `depend` is duplicated merge the depend the
  157. // node.
  158. if (CheckReplace(inp1_j, inp2_j, false)) {
  159. continue;
  160. }
  161. }
  162. return false;
  163. }
  164. }
  165. // When appsame is true, check if has random effect do not merge
  166. if (CheckRandomEffect(c_main, c_node)) {
  167. return false;
  168. }
  169. return true;
  170. }
  171. // a parameter node.
  172. return false;
  173. }
  174. bool CSE::DoReplace(const FuncGraphManagerPtr manager, const std::vector<std::size_t> &order_group,
  175. std::unordered_map<std::size_t, std::vector<AnfNodePtr>> *groups) const {
  176. bool changes = false;
  177. std::set<size_t> clear_set;
  178. for (auto &h : order_group) {
  179. std::vector<AnfNodePtr> &group = (*groups)[h];
  180. // If there are more than 2 node in that group, they may be same common expression can be eliminated.
  181. if (group.size() > 1) {
  182. for (size_t k = 0; k < group.size() - 1; k++) {
  183. AnfNodePtr main = group[k];
  184. MS_EXCEPTION_IF_NULL(main);
  185. // When all node in group has been replaced
  186. // or a valuenode node, skip compare in group
  187. if ((k + 1 + clear_set.size() == group.size()) || (k > 0 && main->isa<ValueNode>())) {
  188. break;
  189. }
  190. // skip node has been replaced
  191. if (clear_set.find(k) != clear_set.end()) {
  192. continue;
  193. }
  194. // Compare with rest elements in this group.
  195. for (size_t i = k + 1; i < group.size(); i++) {
  196. auto node = group[i];
  197. MS_EXCEPTION_IF_NULL(node);
  198. if (clear_set.find(i) != clear_set.end()) {
  199. continue;
  200. }
  201. if (main->func_graph() != node->func_graph()) {
  202. continue;
  203. }
  204. if (CheckReplace(node, main)) {
  205. changes = true;
  206. (void)manager->Replace(node, main);
  207. (void)clear_set.insert(i);
  208. }
  209. }
  210. }
  211. clear_set.clear();
  212. }
  213. }
  214. return changes;
  215. }
  216. bool CSE::Cse(const FuncGraphPtr root, const FuncGraphManagerPtr manager) const {
  217. MS_EXCEPTION_IF_NULL(manager);
  218. manager->AddFuncGraph(root);
  219. return BuildOrderGroupAndDoReplace(manager);
  220. }
  221. } // namespace opt
  222. } // namespace mindspore