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

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