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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. bool CSE::CheckRandomEffect(const AnfNodePtr &main, const AnfNodePtr &node) const {
  83. bool has_random_effect = false;
  84. auto prim_main = GetCNodePrimitive(main);
  85. auto prim_node = GetCNodePrimitive(node);
  86. if (prim_main == prim_node) {
  87. return false;
  88. }
  89. if (prim_main != nullptr) {
  90. auto effect_val = prim_main->GetAttr(GRAPH_FLAG_RANDOM_EFFECT);
  91. if (effect_val != nullptr && effect_val->isa<BoolImm>()) {
  92. has_random_effect = GetValue<bool>(effect_val);
  93. }
  94. }
  95. return has_random_effect;
  96. }
  97. bool CSE::CheckReplace(const AnfNodePtr &main, const AnfNodePtr &node) const {
  98. MS_EXCEPTION_IF_NULL(main);
  99. MS_EXCEPTION_IF_NULL(node);
  100. bool replace = false;
  101. if (main->isa<ValueNode>() && node->isa<ValueNode>()) {
  102. auto main_value = GetValueNode(main);
  103. auto node_value = GetValueNode(node);
  104. replace = (AbsOf(main) == AbsOf(node)) && (*main_value == *node_value);
  105. } else if (main->isa<CNode>() && node->isa<CNode>()) {
  106. auto c_main = main->cast<CNodePtr>();
  107. auto c_node = node->cast<CNodePtr>();
  108. const auto &inp1 = c_main->inputs();
  109. const auto &inp2 = c_node->inputs();
  110. if (inp1.size() == inp2.size()) {
  111. bool appsame = true;
  112. for (size_t j = 0; j < inp1.size(); j++) {
  113. MS_EXCEPTION_IF_NULL(inp1[j]);
  114. MS_EXCEPTION_IF_NULL(inp2[j]);
  115. if (!(*inp1[j] == *inp2[j])) {
  116. // Handle the case of two different Tensor, but with the same value
  117. if (IsValueNode<tensor::Tensor>(inp1[j]) && IsValueNode<tensor::Tensor>(inp2[j])) {
  118. auto tensor1 = GetValueNode<tensor::TensorPtr>(inp1[j]);
  119. auto tensor2 = GetValueNode<tensor::TensorPtr>(inp2[j]);
  120. if (tensor1->ValueEqual(*tensor2)) {
  121. continue;
  122. }
  123. }
  124. appsame = false;
  125. break;
  126. }
  127. }
  128. if (CheckRandomEffect(c_main, c_node)) {
  129. appsame = false;
  130. }
  131. replace = appsame;
  132. }
  133. }
  134. return replace;
  135. }
  136. bool CSE::DoReplace(const FuncGraphManagerPtr manager, const std::vector<std::size_t> &order_group,
  137. std::unordered_map<std::size_t, std::vector<AnfNodePtr>> *groups) const {
  138. bool changes = false;
  139. std::set<size_t> clear_set;
  140. for (auto &h : order_group) {
  141. std::vector<AnfNodePtr> &group = (*groups)[h];
  142. // If there are more than 2 node in that group, they may be same common expression can be eliminated.
  143. if (group.size() > 1) {
  144. for (size_t k = 0; k < group.size() - 1; k++) {
  145. AnfNodePtr main = group[k];
  146. MS_EXCEPTION_IF_NULL(main);
  147. // When all node in group has been replaced
  148. // or a valuenode node, skip compare in group
  149. if ((k + 1 + clear_set.size() == group.size()) || (k > 0 && main->isa<ValueNode>())) {
  150. break;
  151. }
  152. // skip node has been replaced
  153. if (clear_set.find(k) != clear_set.end()) {
  154. continue;
  155. }
  156. // Compare with rest elements in this group.
  157. for (size_t i = k + 1; i < group.size(); i++) {
  158. auto node = group[i];
  159. MS_EXCEPTION_IF_NULL(node);
  160. if (clear_set.find(i) != clear_set.end()) {
  161. continue;
  162. }
  163. if (main->func_graph() != node->func_graph()) {
  164. continue;
  165. }
  166. if (CheckReplace(node, main)) {
  167. changes = true;
  168. (void)manager->Replace(node, main);
  169. (void)clear_set.insert(i);
  170. }
  171. }
  172. }
  173. clear_set.clear();
  174. }
  175. }
  176. return changes;
  177. }
  178. bool CSE::Cse(const FuncGraphPtr root, const FuncGraphManagerPtr manager) const {
  179. MS_EXCEPTION_IF_NULL(manager);
  180. manager->AddFuncGraph(root);
  181. return BuildOrderGroupAndDoReplace(manager);
  182. }
  183. } // namespace opt
  184. } // namespace mindspore