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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "frontend/optimizer/control_depend.h"
  17. #include <vector>
  18. #include <list>
  19. #include <utility>
  20. #include <memory>
  21. #include "frontend/optimizer/optimizer.h"
  22. namespace mindspore {
  23. namespace opt {
  24. std::vector<AnfNodePtr> DoControlDepend(const FuncGraphPtr &graph, const CNodePtr &return_node,
  25. const std::vector<size_t> &effect_index, const std::vector<CNodePtr> &cnodes) {
  26. std::vector<AnfNodePtr> depend_nodes{NewValueNode(prim::kPrimDepend), return_node->input(1)};
  27. std::vector<AnfNodePtr> make_tuple{NewValueNode(prim::kPrimMakeTuple)};
  28. size_t effect_size = effect_index.size();
  29. for (size_t i = 0; i < effect_size; i++) {
  30. size_t pre_index = 0;
  31. if (i > 0) {
  32. pre_index = effect_index[i - 1] + 1;
  33. }
  34. size_t this_index = effect_index[i];
  35. size_t last_index = cnodes.size() - 2;
  36. if (i < effect_size - 1) {
  37. last_index = effect_index[i + 1];
  38. }
  39. if (this_index > pre_index) {
  40. std::vector<CNodePtr> pre_segment;
  41. for (size_t k = pre_index; k < this_index; k++) {
  42. // Skip depend, make_tuple, and tuple_get_item, because these primitives are not real operator in GE.
  43. if (IsPrimitiveCNode(cnodes[k], prim::kPrimDepend) || IsPrimitiveCNode(cnodes[k], prim::kPrimMakeTuple) ||
  44. IsPrimitiveCNode(cnodes[k], prim::kPrimTupleGetItem)) {
  45. continue;
  46. }
  47. pre_segment.push_back(cnodes[k]);
  48. }
  49. auto roots = FindRoots(pre_segment);
  50. for (auto iter = roots->begin(); iter != roots->end(); (void)iter++) {
  51. AnfNodePtr control_depend =
  52. graph->NewCNode({NewValueNode(prim::kPrimControlDepend), *iter, cnodes[this_index]});
  53. make_tuple.push_back(control_depend);
  54. }
  55. }
  56. if (last_index > this_index) {
  57. std::vector<CNodePtr> last_segment;
  58. for (size_t k = this_index + 1; k <= last_index; k++) {
  59. // Skip depend, make_tuple, and tuple_get_item, because these primitives are not real operator in GE.
  60. if (IsPrimitiveCNode(cnodes[k], prim::kPrimDepend) || IsPrimitiveCNode(cnodes[k], prim::kPrimMakeTuple) ||
  61. IsPrimitiveCNode(cnodes[k], prim::kPrimTupleGetItem)) {
  62. continue;
  63. }
  64. last_segment.push_back(cnodes[k]);
  65. }
  66. auto leaves = FindLeaves(last_segment);
  67. for (auto iter = leaves->begin(); iter != leaves->end(); (void)iter++) {
  68. AnfNodePtr control_depend =
  69. graph->NewCNode({NewValueNode(prim::kPrimControlDepend), cnodes[this_index], *iter});
  70. make_tuple.push_back(control_depend);
  71. }
  72. }
  73. }
  74. depend_nodes.push_back(graph->NewCNode(make_tuple));
  75. return depend_nodes;
  76. }
  77. void AddControlDepend(const FuncGraphPtr &graph) {
  78. MS_EXCEPTION_IF_NULL(graph);
  79. std::list<CNodePtr> orders = graph->GetOrderedCnodes();
  80. std::vector<CNodePtr> cnodes(orders.begin(), orders.end());
  81. size_t cnodes_size = cnodes.size();
  82. // get effect index of cnodes
  83. std::vector<size_t> effect_index{};
  84. for (size_t i = 0; i < cnodes_size; i++) {
  85. if (graph->HasEffect(cnodes[i])) {
  86. effect_index.push_back(i);
  87. }
  88. }
  89. if (effect_index.empty()) {
  90. return;
  91. }
  92. AnfNodePtr last_node = cnodes[cnodes_size - 1];
  93. CNodePtr return_node;
  94. if (last_node->isa<CNode>()) {
  95. return_node = last_node->cast<CNodePtr>();
  96. }
  97. MS_EXCEPTION_IF_NULL(return_node);
  98. if (!IsPrimitiveCNode(return_node, prim::kPrimReturn)) {
  99. MS_LOG(EXCEPTION) << "The last cnode after sorting, not return cnode.";
  100. }
  101. if (return_node->inputs().size() < 2) {
  102. MS_LOG(EXCEPTION) << "Number of return node inputs should be great than or equal to 2.";
  103. }
  104. auto depend_node_inputs = DoControlDepend(graph, return_node, effect_index, cnodes);
  105. auto depend_cnode = graph->NewCNode(depend_node_inputs);
  106. depend_cnode->set_abstract(depend_cnode->input(1)->abstract());
  107. auto manager = graph->manager();
  108. MS_EXCEPTION_IF_NULL(manager);
  109. if (!manager->Replace(return_node->input(1), depend_cnode)) {
  110. MS_LOG(EXCEPTION) << "Depend replace node failed";
  111. }
  112. }
  113. } // namespace opt
  114. } // namespace mindspore