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.

control_depend.cc 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "optimizer/control_depend.h"
  17. #include <vector>
  18. #include <list>
  19. #include <utility>
  20. #include <memory>
  21. #include <algorithm>
  22. #include "optimizer/optimizer.h"
  23. namespace mindspore {
  24. namespace opt {
  25. std::vector<AnfNodePtr> DoControlDepend(const FuncGraphPtr &graph, const CNodePtr &return_node,
  26. const std::vector<size_t> &effect_index, const std::vector<CNodePtr> &cnodes) {
  27. std::vector<AnfNodePtr> depend_nodes{NewValueNode(prim::kPrimDepend), return_node->input(1)};
  28. std::vector<AnfNodePtr> make_tuple{NewValueNode(prim::kPrimMakeTuple)};
  29. size_t effect_size = effect_index.size();
  30. for (size_t i = 0; i < effect_size; i++) {
  31. size_t pre_index = 0;
  32. if (i > 0) {
  33. pre_index = effect_index[i - 1] + 1;
  34. }
  35. size_t this_index = effect_index[i];
  36. size_t last_index = cnodes.size() - 2;
  37. if (i < effect_size - 1) {
  38. last_index = effect_index[i + 1];
  39. }
  40. if (this_index > pre_index) {
  41. std::vector<CNodePtr> pre_segment;
  42. for (size_t k = pre_index; k < this_index; k++) {
  43. // Skip depend, make_tuple, and tuple_get_item, because these primitives are not real operator in GE.
  44. if (IsPrimitiveCNode(cnodes[k], prim::kPrimDepend) || IsPrimitiveCNode(cnodes[k], prim::kPrimMakeTuple) ||
  45. IsPrimitiveCNode(cnodes[k], prim::kPrimTupleGetItem)) {
  46. continue;
  47. }
  48. pre_segment.push_back(cnodes[k]);
  49. }
  50. auto roots = FindRoots(pre_segment);
  51. for (auto iter = roots->begin(); iter != roots->end(); (void)iter++) {
  52. AnfNodePtr control_depend =
  53. graph->NewCNode({NewValueNode(prim::kPrimControlDepend), *iter, cnodes[this_index]});
  54. make_tuple.push_back(control_depend);
  55. }
  56. }
  57. if (last_index > this_index) {
  58. std::vector<CNodePtr> last_segment;
  59. for (size_t k = this_index + 1; k <= last_index; k++) {
  60. // Skip depend, make_tuple, and tuple_get_item, because these primitives are not real operator in GE.
  61. if (IsPrimitiveCNode(cnodes[k], prim::kPrimDepend) || IsPrimitiveCNode(cnodes[k], prim::kPrimMakeTuple) ||
  62. IsPrimitiveCNode(cnodes[k], prim::kPrimTupleGetItem)) {
  63. continue;
  64. }
  65. last_segment.push_back(cnodes[k]);
  66. }
  67. auto leaves = FindLeaves(last_segment);
  68. for (auto iter = leaves->begin(); iter != leaves->end(); (void)iter++) {
  69. AnfNodePtr control_depend =
  70. graph->NewCNode({NewValueNode(prim::kPrimControlDepend), cnodes[this_index], *iter});
  71. make_tuple.push_back(control_depend);
  72. }
  73. }
  74. }
  75. depend_nodes.push_back(graph->NewCNode(make_tuple));
  76. return depend_nodes;
  77. }
  78. void AddControlDepend(const FuncGraphPtr &graph) {
  79. MS_EXCEPTION_IF_NULL(graph);
  80. std::list<CNodePtr> orders = graph->GetOrderedCnodes();
  81. std::vector<CNodePtr> cnodes(orders.begin(), orders.end());
  82. size_t cnodes_size = cnodes.size();
  83. // get effect index of cnodes
  84. std::vector<size_t> effect_index{};
  85. for (size_t i = 0; i < cnodes_size; i++) {
  86. if (graph->HasEffect(cnodes[i])) {
  87. effect_index.push_back(i);
  88. }
  89. }
  90. if (effect_index.empty()) {
  91. return;
  92. }
  93. AnfNodePtr last_node = cnodes[cnodes_size - 1];
  94. CNodePtr return_node;
  95. if (last_node->isa<CNode>()) {
  96. return_node = last_node->cast<CNodePtr>();
  97. }
  98. MS_EXCEPTION_IF_NULL(return_node);
  99. if (!IsPrimitiveCNode(return_node, prim::kPrimReturn)) {
  100. MS_LOG(EXCEPTION) << "The last cnode after sorting, not return cnode.";
  101. }
  102. if (return_node->inputs().size() < 2) {
  103. MS_LOG(EXCEPTION) << "Number of return node inputs should be great than or equal to 2.";
  104. }
  105. auto depend_node_inputs = DoControlDepend(graph, return_node, effect_index, cnodes);
  106. auto depend_cnode = graph->NewCNode(depend_node_inputs);
  107. depend_cnode->set_abstract(depend_cnode->input(1)->abstract());
  108. auto manager = graph->manager();
  109. MS_EXCEPTION_IF_NULL(manager);
  110. if (!manager->Replace(return_node->input(1), depend_cnode)) {
  111. MS_LOG(EXCEPTION) << "Depend replace node failed";
  112. }
  113. }
  114. } // namespace opt
  115. } // namespace mindspore