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.

switch_logic_remove_pass.cc 7.8 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright 2020 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 "graph/passes/switch_logic_remove_pass.h"
  17. #include <string>
  18. #include <vector>
  19. #include <utility>
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/utils/graph_utils.h"
  22. #include "graph/passes/pass_utils.h"
  23. #include "framework/common/util.h"
  24. namespace ge {
  25. namespace {
  26. using PredNodeAndOut = std::pair<NodePtr, int>;
  27. constexpr int kSwitchOutputNum = 2;
  28. constexpr int kSwitchPredIndex = 1;
  29. char const *GetOutputNameFromIndex(int index) {
  30. if ((index >= 0) && (index < kSwitchOutputNum)) {
  31. static char const *name[kSwitchOutputNum] = {"false", "true"};
  32. return name[index];
  33. }
  34. return "UNKNOWN";
  35. }
  36. inline bool IsSwitch(const std::string &type) {
  37. return type == SWITCH || type == REFSWITCH;
  38. }
  39. Status GetPredNode(const NodePtr &switch_node, PredNodeAndOut &pred_node_index) {
  40. GE_CHECK_NOTNULL(switch_node);
  41. auto pred_in_anchor = switch_node->GetInDataAnchor(kSwitchPredIndex);
  42. if (pred_in_anchor == nullptr) {
  43. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no index:%d in data anchor, check invalid",
  44. switch_node->GetName().c_str(), switch_node->GetType().c_str(), kSwitchPredIndex);
  45. GELOGE(INTERNAL_ERROR, "[Get][InDataAnchor] failed, Node:%s(%s) has no index:%d in data anchor",
  46. switch_node->GetName().c_str(), switch_node->GetType().c_str(), kSwitchPredIndex);
  47. return INTERNAL_ERROR;
  48. }
  49. auto pred_node_anchor = pred_in_anchor->GetPeerOutAnchor();
  50. if (pred_node_anchor == nullptr) {
  51. REPORT_INNER_ERROR("E19999", "Node:%s(%s)'s index:%d in data anchor, its peer anchor is nullptr, check invalid",
  52. switch_node->GetName().c_str(), switch_node->GetType().c_str(), kSwitchPredIndex);
  53. GELOGE(INTERNAL_ERROR,
  54. "[Get][PeerOutAnchor] failed, Node:%s(%s)'s index:%d in data anchor, its peer anchor is nullptr",
  55. switch_node->GetName().c_str(), switch_node->GetType().c_str(), kSwitchPredIndex);
  56. return INTERNAL_ERROR;
  57. }
  58. auto pred_node = pred_node_anchor->GetOwnerNode();
  59. if (pred_node == nullptr) {
  60. REPORT_INNER_ERROR("E19999", "Node:%s(%s)'s index:%d in data anchor, its peer node is nullptr, check invalid",
  61. switch_node->GetName().c_str(), switch_node->GetType().c_str(), kSwitchPredIndex);
  62. GELOGE(INTERNAL_ERROR,
  63. "[Get][OwnerNode] failed, Node:%s(%s)'s index:%d in data anchor, its peer node is nullptr",
  64. switch_node->GetName().c_str(), switch_node->GetType().c_str(), kSwitchPredIndex);
  65. return INTERNAL_ERROR;
  66. }
  67. pred_node_index.first = pred_node;
  68. pred_node_index.second = pred_node_anchor->GetIdx();
  69. return SUCCESS;
  70. }
  71. } // namespace
  72. Status SwitchLogicRemovePass::Run(NodePtr &node) {
  73. GE_CHECK_NOTNULL(node);
  74. if (!IsSwitch(node->GetType())) {
  75. return SUCCESS;
  76. }
  77. PredNodeAndOut pred_node_and_out;
  78. auto ret = GetPredNode(node, pred_node_and_out);
  79. if (ret != SUCCESS) {
  80. GELOGE(INTERNAL_ERROR, "[Check][Param] Failed to run switch logic remove pass, no pred node found from switch %s",
  81. node->GetName().c_str());
  82. return INTERNAL_ERROR;
  83. }
  84. for (int i = 0; i < kSwitchOutputNum; ++i) {
  85. auto out_anchor = node->GetOutDataAnchor(i);
  86. if (out_anchor == nullptr) {
  87. GELOGW("Unexpected switch node, the %d out anchor is null", i);
  88. return SUCCESS;
  89. }
  90. for (auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  91. if (in_anchor == nullptr) {
  92. REPORT_INNER_ERROR("E19999", "Node:%s(%s)'s index:%d out data anchor, its peer anchors has nullptr, "
  93. "check invalid", node->GetName().c_str(), node->GetType().c_str(), i);
  94. GELOGE(INTERNAL_ERROR, "[Check][Param] Node:%s(%s)'s index:%d out data anchor, its peer anchors has nullptr",
  95. node->GetName().c_str(), node->GetType().c_str(), i);
  96. return INTERNAL_ERROR;
  97. }
  98. auto dst_node = in_anchor->GetOwnerNode();
  99. if (dst_node == nullptr) {
  100. REPORT_INNER_ERROR("E19999", "Node:%s(%s)'s index:%d out data anchor, its peer nodes has nullptr, "
  101. "check invalid", node->GetName().c_str(), node->GetType().c_str(), i);
  102. GELOGE(INTERNAL_ERROR, "[Check][Param] Node:%s(%s)'s index:%d out data anchor, its peer nodes has nullptr",
  103. node->GetName().c_str(), node->GetType().c_str(), i);
  104. return INTERNAL_ERROR;
  105. }
  106. if (!IsSwitch(dst_node->GetType())) {
  107. continue;
  108. }
  109. PredNodeAndOut pred_node_next_switch;
  110. ret = GetPredNode(dst_node, pred_node_next_switch);
  111. if (ret != SUCCESS) {
  112. GELOGE(INTERNAL_ERROR,
  113. "[Check][Param] Failed to run switch logic remove pass, no pred node found from switch %s",
  114. dst_node->GetName().c_str());
  115. return INTERNAL_ERROR;
  116. }
  117. if (pred_node_and_out != pred_node_next_switch) {
  118. continue;
  119. }
  120. GELOGI("The switch nodes cascaded %s and %s have the save pred node %s, the %s can be remove",
  121. node->GetName().c_str(), dst_node->GetName().c_str(),
  122. pred_node_and_out.first->GetName().c_str(), dst_node->GetName().c_str());
  123. ret = RemoveSwitchNodeLogically(i, dst_node);
  124. if (ret != SUCCESS) {
  125. return ret;
  126. }
  127. }
  128. }
  129. return SUCCESS;
  130. }
  131. Status SwitchLogicRemovePass::RemoveSwitchNodeLogically(int parent_index, NodePtr &switch_node) {
  132. std::vector<int> isolate_map({-1, -1});
  133. for (int i = 0; i < kSwitchOutputNum; ++i) {
  134. if (i == parent_index) {
  135. isolate_map[i] = 0;
  136. continue;
  137. }
  138. GE_CHECK_NOTNULL(switch_node);
  139. auto out_anchor = switch_node->GetOutDataAnchor(i);
  140. if (out_anchor == nullptr) {
  141. GELOGW("The switch removing %s does not has %d out anchor, ignore it", switch_node->GetName().c_str(), i);
  142. continue;
  143. }
  144. GELOGI("Remove inactivate branch %s(%d) from switch %s",
  145. GetOutputNameFromIndex(i), i, switch_node->GetName().c_str());
  146. std::vector<NodePtr> deleted_nodes;
  147. std::vector<NodePtr> end_nodes;
  148. auto ret = PassUtils::RemoveInactiveBranchToMerge(out_anchor, deleted_nodes, end_nodes);
  149. if (ret != SUCCESS) {
  150. REPORT_CALL_ERROR("E19999", "Remove inactivate branch from node:%s(%s) to merge failed",
  151. switch_node->GetName().c_str(), switch_node->GetType().c_str());
  152. GELOGE(FAILED, "[Remove][InactiveBranch] from node:%s(%s) to merge failed",
  153. switch_node->GetName().c_str(), switch_node->GetType().c_str());
  154. return ret;
  155. }
  156. for (auto &node : deleted_nodes) {
  157. GE_CHECK_NOTNULL(node);
  158. GELOGD("Remove node %s from inactivate branch from switch %s",
  159. node->GetName().c_str(), switch_node->GetName().c_str());
  160. AddNodeDeleted(node);
  161. }
  162. for (auto &node : end_nodes) {
  163. GE_CHECK_NOTNULL(node);
  164. GELOGD("Add end node %s to re-pass list, for inactivate branch from switch %s",
  165. node->GetName().c_str(), switch_node->GetName().c_str());
  166. AddRePassNode(node);
  167. }
  168. }
  169. GELOGI("Remove switch node cascaded %s, replace out index %d",
  170. switch_node->GetName().c_str(), parent_index);
  171. return IsolateAndDeleteNode(switch_node, isolate_map);
  172. }
  173. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示