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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "optimizer/cse.h"
  19. #include <vector>
  20. #include <set>
  21. #include <unordered_map>
  22. #include "./common.h"
  23. namespace mindspore {
  24. /* namespace to support opt */
  25. namespace opt {
  26. using mindspore::abstract::AbstractBase;
  27. using mindspore::abstract::AbstractFunction;
  28. using mindspore::abstract::AbstractFunctionPtr;
  29. BasePtr AbsOf(const AnfNodePtr &node) {
  30. MS_EXCEPTION_IF_NULL(node);
  31. auto node_abs = node->abstract();
  32. // in testcase: TestOptOpt.CSE, node->abstract() is null;
  33. if (node_abs == nullptr) {
  34. return kAnyValue;
  35. }
  36. return node_abs;
  37. }
  38. bool CSE::BuildOrderGroupAndDoReplace(const FuncGraphManagerPtr manager) const {
  39. bool changed = false;
  40. for (FuncGraphPtr fg : manager->func_graphs()) {
  41. MS_EXCEPTION_IF_NULL(fg);
  42. std::vector<std::size_t> order_group;
  43. std::unordered_map<std::size_t, std::vector<AnfNodePtr>> groups;
  44. std::unordered_map<AnfNodePtr, std::size_t> hashes;
  45. std::vector<AnfNodePtr> toposet = TopoSort(fg->get_return());
  46. for (auto node : toposet) {
  47. MS_EXCEPTION_IF_NULL(node);
  48. if (hashes.find(node) != hashes.end()) {
  49. continue;
  50. }
  51. std::size_t h = 0;
  52. if (node->isa<ValueNode>()) {
  53. ValueNodePtr value_node = node->cast<ValueNodePtr>();
  54. auto value = value_node->value();
  55. MS_EXCEPTION_IF_NULL(value);
  56. h = hash_combine(value->hash(), (AbsOf(value_node)->hash()));
  57. } else if (node->isa<CNode>()) {
  58. auto cnode = node->cast<CNodePtr>();
  59. auto &inputs = cnode->inputs();
  60. size_t init = 0;
  61. h = std::accumulate(inputs.begin(), inputs.end(), init, [&hashes](std::size_t hash, const AnfNodePtr &node_in) {
  62. return hash_combine(hash, hashes[node_in]);
  63. });
  64. } else if (node->isa<Parameter>()) {
  65. h = node->hash();
  66. } else {
  67. MS_LOG(ERROR) << "Unknow node type";
  68. }
  69. hashes[node] = h;
  70. if (groups.find(h) == groups.end()) {
  71. std::vector<AnfNodePtr> innervec({node});
  72. groups[h] = innervec;
  73. order_group.emplace_back(h);
  74. } else {
  75. groups[h].push_back(node);
  76. }
  77. }
  78. changed = DoReplace(manager, order_group, &groups) || changed;
  79. }
  80. return changed;
  81. }
  82. // The op like print, summary, or the op do not has true output, and always as a depend node input.
  83. static bool HasSideEffect(const AnfNodePtr &node) {
  84. auto prim = GetCNodePrimitive(node);
  85. if (prim == nullptr) {
  86. return false;
  87. }
  88. auto side_effect_v = prim->GetAttr(GRAPH_FLAG_SIDE_EFFECT);
  89. if (side_effect_v != nullptr && side_effect_v->isa<BoolImm>()) {
  90. return GetValue<bool>(side_effect_v);
  91. }
  92. return false;
  93. }
  94. // If true do not merge the node.
  95. bool CSE::CheckRandomEffect(const AnfNodePtr &main, const AnfNodePtr &node) const {
  96. bool has_random_effect = false;
  97. auto prim_main = GetCNodePrimitive(main);
  98. auto prim_node = GetCNodePrimitive(node);
  99. // if has random effect, when generate by different op (not same object), do not merge.
  100. if (prim_main != nullptr) {
  101. if (prim_main == prim_node) {
  102. return false;
  103. }
  104. auto effect_val = prim_main->GetAttr(GRAPH_FLAG_RANDOM_EFFECT);
  105. if (effect_val != nullptr && effect_val->isa<BoolImm>()) {
  106. has_random_effect = GetValue<bool>(effect_val);
  107. }
  108. }
  109. return has_random_effect;
  110. }
  111. bool CSE::CheckReplace(const AnfNodePtr &main, const AnfNodePtr &node, bool check_side_effect) const {
  112. MS_EXCEPTION_IF_NULL(main);
  113. MS_EXCEPTION_IF_NULL(node);
  114. if (main->isa<ValueNode>() && node->isa<ValueNode>()) {
  115. auto main_value = GetValueNode(main);
  116. auto node_value = GetValueNode(node);
  117. return (AbsOf(main) == AbsOf(node)) && (*main_value == *node_value);
  118. } else if (main->isa<CNode>() && node->isa<CNode>()) {
  119. auto c_main = main->cast<CNodePtr>();
  120. auto c_node = node->cast<CNodePtr>();
  121. // When appsame is true, check if has side effect, do not merge.
  122. if (check_side_effect && HasSideEffect(main)) {
  123. return false;
  124. }
  125. const auto &inp1 = c_main->inputs();
  126. const auto &inp2 = c_node->inputs();
  127. if (inp1.size() != inp2.size()) {
  128. return false;
  129. }
  130. for (size_t j = 0; j < inp1.size(); j++) {
  131. auto inp1_j = inp1[j];
  132. auto inp2_j = inp2[j];
  133. MS_EXCEPTION_IF_NULL(inp1_j);
  134. MS_EXCEPTION_IF_NULL(inp2_j);
  135. if (!(*inp1_j == *inp2_j)) {
  136. // Handle the case of two different Tensor, but with the same value
  137. if (IsValueNode<tensor::Tensor>(inp1_j) && IsValueNode<tensor::Tensor>(inp2_j)) {
  138. auto tensor1 = GetValueNode<tensor::TensorPtr>(inp1_j);
  139. auto tensor2 = GetValueNode<tensor::TensorPtr>(inp2_j);
  140. if (tensor1->ValueEqual(*tensor2)) {
  141. continue;
  142. }
  143. } else if (HasSideEffect(inp1_j) && HasSideEffect(inp2_j)) {
  144. // When the same side effect node as another two nodes' inputs, we still merge the node.
  145. // Because the node only can be the inputs of `depend`, when the `depend` is duplicated merge the depend the
  146. // node.
  147. if (CheckReplace(inp1_j, inp2_j, false)) {
  148. continue;
  149. }
  150. }
  151. return false;
  152. }
  153. }
  154. // When appsame is true, check if has random effect do not merge
  155. if (CheckRandomEffect(c_main, c_node)) {
  156. return false;
  157. }
  158. return true;
  159. }
  160. // a parameter node.
  161. return false;
  162. }
  163. bool CSE::DoReplace(const FuncGraphManagerPtr manager, const std::vector<std::size_t> &order_group,
  164. std::unordered_map<std::size_t, std::vector<AnfNodePtr>> *groups) const {
  165. bool changes = false;
  166. std::set<size_t> clear_set;
  167. for (auto &h : order_group) {
  168. std::vector<AnfNodePtr> &group = (*groups)[h];
  169. // If there are more than 2 node in that group, they may be same common expression can be eliminated.
  170. if (group.size() > 1) {
  171. for (size_t k = 0; k < group.size() - 1; k++) {
  172. AnfNodePtr main = group[k];
  173. MS_EXCEPTION_IF_NULL(main);
  174. // When all node in group has been replaced
  175. // or a valuenode node, skip compare in group
  176. if ((k + 1 + clear_set.size() == group.size()) || (k > 0 && main->isa<ValueNode>())) {
  177. break;
  178. }
  179. // skip node has been replaced
  180. if (clear_set.find(k) != clear_set.end()) {
  181. continue;
  182. }
  183. // Compare with rest elements in this group.
  184. for (size_t i = k + 1; i < group.size(); i++) {
  185. auto node = group[i];
  186. MS_EXCEPTION_IF_NULL(node);
  187. if (clear_set.find(i) != clear_set.end()) {
  188. continue;
  189. }
  190. if (main->func_graph() != node->func_graph()) {
  191. continue;
  192. }
  193. if (CheckReplace(node, main)) {
  194. changes = true;
  195. (void)manager->Replace(node, main);
  196. (void)clear_set.insert(i);
  197. }
  198. }
  199. }
  200. clear_set.clear();
  201. }
  202. }
  203. return changes;
  204. }
  205. bool CSE::Cse(const FuncGraphPtr root, const FuncGraphManagerPtr manager) const {
  206. MS_EXCEPTION_IF_NULL(manager);
  207. manager->AddFuncGraph(root);
  208. return BuildOrderGroupAndDoReplace(manager);
  209. }
  210. } // namespace opt
  211. } // namespace mindspore