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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019-2021 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 "utils/hash_map.h"
  22. #include "abstract/abstract_function.h"
  23. #include "utils/flags.h"
  24. #include "utils/utils.h"
  25. #include "utils/anf_utils.h"
  26. namespace mindspore {
  27. /* namespace to support opt */
  28. namespace opt {
  29. using mindspore::abstract::AbstractBase;
  30. using mindspore::abstract::AbstractFunction;
  31. using mindspore::abstract::AbstractFunctionPtr;
  32. bool WithRecomputedScope(const AnfNodePtr &node) {
  33. MS_EXCEPTION_IF_NULL(node);
  34. if (!node->isa<CNode>()) {
  35. return false;
  36. }
  37. auto full_name_with_scope = node->fullname_with_scope();
  38. return full_name_with_scope.find(kAttrRecompute) == 0;
  39. }
  40. bool IsSetRecomputed(const CNodePtr &a, const CNodePtr &b) {
  41. return (WithRecomputedScope(a) && !a->HasAttr(kAttrNeedCseAfterRecompute)) ||
  42. (WithRecomputedScope(b) && !b->HasAttr(kAttrNeedCseAfterRecompute));
  43. }
  44. void UpdateDebugInfoAndDumpFlag(const AnfNodePtr &main, const AnfNodePtr &node) {
  45. if (main == nullptr || !main->isa<CNode>()) {
  46. return;
  47. }
  48. if (AnfUtils::GetDumpFlag(node) && !AnfUtils::GetDumpFlag(main)) {
  49. AnfUtils::SetDumpFlag(main);
  50. }
  51. auto main_cnode = main->cast<CNodePtr>();
  52. main_cnode->AddFusedDebugInfo(node);
  53. }
  54. BasePtr AbsOf(const AnfNodePtr &node, bool ignore_fg_abs_tracking_id) {
  55. MS_EXCEPTION_IF_NULL(node);
  56. auto node_abs = node->abstract();
  57. // In testcase: TestOptOpt.CSE, node->abstract() is null.
  58. if (node_abs == nullptr) {
  59. return kAnyValue;
  60. }
  61. if (node_abs->isa<abstract::PrimitiveAbstractClosure>()) {
  62. // Ignore the tracking_id and prim pointer hash.
  63. auto prim_abs = node_abs->cast<abstract::PrimitiveAbstractClosurePtr>();
  64. return prim_abs->prim();
  65. } else if (ignore_fg_abs_tracking_id && node_abs->isa<abstract::FuncGraphAbstractClosure>()) {
  66. // Ignore the tracking_id.
  67. auto new_fg_abs = node_abs->cast<abstract::AbstractFunctionPtr>()->Copy();
  68. new_fg_abs->set_tracking_id(nullptr);
  69. return new_fg_abs;
  70. }
  71. return node_abs;
  72. }
  73. bool CSE::BuildOrderGroupAndDoReplaceForOneGraph(const FuncGraphPtr &fg, const FuncGraphManagerPtr &manager) const {
  74. MS_EXCEPTION_IF_NULL(fg);
  75. std::vector<std::size_t> order_group;
  76. mindspore::HashMap<std::size_t, std::vector<AnfNodePtr>> groups;
  77. mindspore::HashMap<AnfNodePtr, std::size_t> hashes;
  78. std::vector<AnfNodePtr> toposet = TopoSort(fg->get_return());
  79. for (auto node : toposet) {
  80. MS_EXCEPTION_IF_NULL(node);
  81. if (hashes.find(node) != hashes.end()) {
  82. continue;
  83. }
  84. std::size_t h = 0;
  85. if (node->isa<ValueNode>()) {
  86. ValueNodePtr value_node = node->cast<ValueNodePtr>();
  87. auto value = value_node->value();
  88. MS_EXCEPTION_IF_NULL(value);
  89. h = hash_combine(value->hash(), (AbsOf(value_node, true)->hash()));
  90. } else if (node->isa<CNode>()) {
  91. auto cnode = node->cast<CNodePtr>();
  92. auto &inputs = cnode->inputs();
  93. size_t init = 0;
  94. h = std::accumulate(inputs.begin(), inputs.end(), init, [&hashes](std::size_t hash, const AnfNodePtr &node_in) {
  95. return hash_combine(hash, hashes[node_in]);
  96. });
  97. } else if (node->isa<Parameter>()) {
  98. h = node->hash();
  99. } else {
  100. MS_LOG(ERROR) << "Unknown node type";
  101. }
  102. hashes[node] = h;
  103. if (groups.find(h) == groups.end()) {
  104. std::vector<AnfNodePtr> innervec({node});
  105. groups[h] = innervec;
  106. order_group.emplace_back(h);
  107. } else {
  108. groups[h].push_back(node);
  109. }
  110. }
  111. return DoReplace(manager, order_group, &groups);
  112. }
  113. bool CSE::BuildOrderGroupAndDoReplace(const FuncGraphManagerPtr manager) const {
  114. bool changed = false;
  115. for (FuncGraphPtr fg : manager->func_graphs()) {
  116. changed = BuildOrderGroupAndDoReplaceForOneGraph(fg, manager) || changed;
  117. }
  118. return changed;
  119. }
  120. // The op like print, summary, or the op do not has true output, and always as a depend node input.
  121. static bool HasSideEffect(const AnfNodePtr &node) {
  122. auto prim = GetCNodePrimitive(node);
  123. if (prim == nullptr) {
  124. return false;
  125. }
  126. auto side_effect_v = prim->GetAttr(GRAPH_FLAG_SIDE_EFFECT);
  127. if (side_effect_v != nullptr && side_effect_v->isa<BoolImm>()) {
  128. return GetValue<bool>(side_effect_v);
  129. }
  130. return false;
  131. }
  132. // If true do not merge the node.
  133. bool CSE::CheckRandomEffect(const AnfNodePtr &main, const AnfNodePtr &node) const {
  134. auto prim_main = GetCNodePrimitive(main);
  135. auto prim_node = GetCNodePrimitive(node);
  136. // if has random effect, when generate by different op (not same object), do not merge.
  137. if (prim_main != nullptr) {
  138. auto effect_val = prim_main->GetAttr(GRAPH_FLAG_RANDOM_EFFECT);
  139. if (effect_val != nullptr && effect_val->isa<BoolImm>()) {
  140. bool has_random_effect = GetValue<bool>(effect_val);
  141. if (has_random_effect) {
  142. return true;
  143. }
  144. }
  145. if (prim_main->name() != prim_node->name()) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. bool CSE::CheckReplace(const AnfNodePtr &main, const AnfNodePtr &node, bool check_side_effect) const {
  152. MS_EXCEPTION_IF_NULL(main);
  153. MS_EXCEPTION_IF_NULL(node);
  154. if (main->isa<ValueNode>() && node->isa<ValueNode>()) {
  155. auto main_value = GetValueNode(main);
  156. auto node_value = GetValueNode(node);
  157. return (AbsOf(main, true) == AbsOf(node, true)) && (*main_value == *node_value);
  158. } else if (main->isa<CNode>() && node->isa<CNode>()) {
  159. auto c_main = main->cast<CNodePtr>();
  160. auto c_node = node->cast<CNodePtr>();
  161. // Not do cse for the node set recompute before the recompute pass.
  162. if (IsSetRecomputed(c_main, c_node)) {
  163. return false;
  164. }
  165. // When appsame is true, check if has side effect, do not merge.
  166. if (check_side_effect && HasSideEffect(main)) {
  167. return false;
  168. }
  169. const auto &inp1 = c_main->inputs();
  170. const auto &inp2 = c_node->inputs();
  171. if (inp1.size() != inp2.size()) {
  172. return false;
  173. }
  174. for (size_t j = 0; j < inp1.size(); j++) {
  175. auto inp1_j = inp1[j];
  176. auto inp2_j = inp2[j];
  177. MS_EXCEPTION_IF_NULL(inp1_j);
  178. MS_EXCEPTION_IF_NULL(inp2_j);
  179. if (!(*inp1_j == *inp2_j)) {
  180. // Handle the case of two different Tensor, but with the same value
  181. if (IsValueNode<tensor::Tensor>(inp1_j) && IsValueNode<tensor::Tensor>(inp2_j)) {
  182. auto tensor1 = GetValueNode<tensor::TensorPtr>(inp1_j);
  183. auto tensor2 = GetValueNode<tensor::TensorPtr>(inp2_j);
  184. if (tensor1->ValueEqual(*tensor2)) {
  185. continue;
  186. }
  187. } else if (HasSideEffect(inp1_j) && HasSideEffect(inp2_j)) {
  188. // When the same side effect node as another two nodes' inputs, we still merge the node.
  189. // Because the node only can be the inputs of `depend`, when the `depend` is duplicated merge the depend the
  190. // node.
  191. if (CheckReplace(inp1_j, inp2_j, false)) {
  192. continue;
  193. }
  194. }
  195. return false;
  196. }
  197. }
  198. // When appsame is true, check if has random effect do not merge
  199. if (CheckRandomEffect(c_main, c_node)) {
  200. return false;
  201. }
  202. return true;
  203. }
  204. // a parameter node.
  205. return false;
  206. }
  207. bool CSE::DoReplace(const FuncGraphManagerPtr manager, const std::vector<std::size_t> &order_group,
  208. mindspore::HashMap<std::size_t, std::vector<AnfNodePtr>> *groups) const {
  209. bool changes = false;
  210. std::set<size_t> clear_set;
  211. for (auto &h : order_group) {
  212. std::vector<AnfNodePtr> &group = (*groups)[h];
  213. // If there are more than 2 node in that group, they may be same common expression can be eliminated.
  214. if (group.size() > 1) {
  215. for (size_t k = 0; k < group.size() - 1; k++) {
  216. AnfNodePtr main = group[k];
  217. MS_EXCEPTION_IF_NULL(main);
  218. // When all node in group has been replaced
  219. // or a valuenode node, skip compare in group
  220. if ((k + 1 + clear_set.size() == group.size()) || (k > 0 && main->isa<ValueNode>())) {
  221. break;
  222. }
  223. // skip node has been replaced
  224. if (clear_set.find(k) != clear_set.end()) {
  225. continue;
  226. }
  227. // Compare with rest elements in this group.
  228. for (size_t i = k + 1; i < group.size(); i++) {
  229. auto node = group[i];
  230. MS_EXCEPTION_IF_NULL(node);
  231. if (clear_set.find(i) != clear_set.end()) {
  232. continue;
  233. }
  234. if (main->func_graph() != node->func_graph()) {
  235. continue;
  236. }
  237. if (CheckReplace(node, main)) {
  238. changes = true;
  239. UpdateDebugInfoAndDumpFlag(main, node);
  240. (void)manager->Replace(node, main);
  241. (void)clear_set.insert(i);
  242. }
  243. }
  244. }
  245. clear_set.clear();
  246. }
  247. }
  248. return changes;
  249. }
  250. bool CSE::Cse(const FuncGraphPtr root, const FuncGraphManagerPtr manager) const {
  251. MS_EXCEPTION_IF_NULL(manager);
  252. manager->AddFuncGraph(root);
  253. return BuildOrderGroupAndDoReplace(manager);
  254. }
  255. } // namespace opt
  256. } // namespace mindspore