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

5 years ago
5 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
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "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, "Failed to get pred node for switch %s, no pred anchor", switch_node->GetName().c_str());
  46. return INTERNAL_ERROR;
  47. }
  48. auto pred_node_anchor = pred_in_anchor->GetPeerOutAnchor();
  49. if (pred_node_anchor == nullptr) {
  50. REPORT_INNER_ERROR("E19999", "Node:%s(%s)'s index:%d in data anchor, its peer anchor is nullptr, check invalid",
  51. switch_node->GetName().c_str(), switch_node->GetType().c_str(), kSwitchPredIndex);
  52. GELOGE(INTERNAL_ERROR,
  53. "Failed to get pred node for switch %s, node peer out anchor",
  54. switch_node->GetName().c_str());
  55. return INTERNAL_ERROR;
  56. }
  57. auto pred_node = pred_node_anchor->GetOwnerNode();
  58. if (pred_node == nullptr) {
  59. REPORT_INNER_ERROR("E19999", "Node:%s(%s)'s index:%d in data anchor, its peer node is nullptr, check invalid",
  60. switch_node->GetName().c_str(), switch_node->GetType().c_str(), kSwitchPredIndex);
  61. GELOGE(INTERNAL_ERROR,
  62. "Failed to get pred node for switch %s, null node",
  63. switch_node->GetName().c_str());
  64. return INTERNAL_ERROR;
  65. }
  66. pred_node_index.first = pred_node;
  67. pred_node_index.second = pred_node_anchor->GetIdx();
  68. return SUCCESS;
  69. }
  70. } // namespace
  71. Status SwitchLogicRemovePass::Run(NodePtr &node) {
  72. GE_CHECK_NOTNULL(node);
  73. if (!IsSwitch(node->GetType())) {
  74. return SUCCESS;
  75. }
  76. PredNodeAndOut pred_node_and_out;
  77. auto ret = GetPredNode(node, pred_node_and_out);
  78. if (ret != SUCCESS) {
  79. GELOGE(INTERNAL_ERROR, "Failed to run switch logic remove pass, no pred node found from switch %s",
  80. node->GetName().c_str());
  81. return INTERNAL_ERROR;
  82. }
  83. for (int i = 0; i < kSwitchOutputNum; ++i) {
  84. auto out_anchor = node->GetOutDataAnchor(i);
  85. if (out_anchor == nullptr) {
  86. GELOGW("Unexpected switch node, the %d out anchor is null", i);
  87. return SUCCESS;
  88. }
  89. for (auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  90. if (in_anchor == nullptr) {
  91. REPORT_INNER_ERROR("E19999", "Node:%s(%s)'s index:%d out data anchor, its peer anchors has nullptr, "
  92. "check invalid", node->GetName().c_str(), node->GetType().c_str(), i);
  93. GELOGE(INTERNAL_ERROR, "The in-anchor from out anchor %d node %s is null", i, node->GetName().c_str());
  94. return INTERNAL_ERROR;
  95. }
  96. auto dst_node = in_anchor->GetOwnerNode();
  97. if (dst_node == nullptr) {
  98. REPORT_INNER_ERROR("E19999", "Node:%s(%s)'s index:%d out data anchor, its peer nodes has nullptr, "
  99. "check invalid", node->GetName().c_str(), node->GetType().c_str(), i);
  100. GELOGE(INTERNAL_ERROR, "The peer node from out anchor %d node %s is null", i, node->GetName().c_str());
  101. return INTERNAL_ERROR;
  102. }
  103. if (!IsSwitch(dst_node->GetType())) {
  104. continue;
  105. }
  106. PredNodeAndOut pred_node_next_switch;
  107. ret = GetPredNode(dst_node, pred_node_next_switch);
  108. if (ret != SUCCESS) {
  109. GELOGE(INTERNAL_ERROR, "Failed to run switch logic remove pass, no pred node found from switch %s",
  110. dst_node->GetName().c_str());
  111. return INTERNAL_ERROR;
  112. }
  113. if (pred_node_and_out != pred_node_next_switch) {
  114. continue;
  115. }
  116. GELOGI("The switch nodes cascaded %s and %s have the save pred node %s, the %s can be remove",
  117. node->GetName().c_str(), dst_node->GetName().c_str(),
  118. pred_node_and_out.first->GetName().c_str(), dst_node->GetName().c_str());
  119. ret = RemoveSwitchNodeLogically(i, dst_node);
  120. if (ret != SUCCESS) {
  121. return ret;
  122. }
  123. }
  124. }
  125. return SUCCESS;
  126. }
  127. Status SwitchLogicRemovePass::RemoveSwitchNodeLogically(int parent_index, NodePtr &switch_node) {
  128. std::vector<int> isolate_map({-1, -1});
  129. for (int i = 0; i < kSwitchOutputNum; ++i) {
  130. if (i == parent_index) {
  131. isolate_map[i] = 0;
  132. continue;
  133. }
  134. GE_CHECK_NOTNULL(switch_node);
  135. auto out_anchor = switch_node->GetOutDataAnchor(i);
  136. if (out_anchor == nullptr) {
  137. GELOGW("The switch removing %s does not has %d out anchor, ignore it", switch_node->GetName().c_str(), i);
  138. continue;
  139. }
  140. GELOGI("Remove inactivate branch %s(%d) from switch %s",
  141. GetOutputNameFromIndex(i), i, switch_node->GetName().c_str());
  142. std::vector<NodePtr> deleted_nodes;
  143. std::vector<NodePtr> end_nodes;
  144. auto ret = PassUtils::RemoveInactiveBranchToMerge(out_anchor, deleted_nodes, end_nodes);
  145. if (ret != SUCCESS) {
  146. REPORT_CALL_ERROR("E19999", "Remove inactive branch from node:%s(%s) to merge failed",
  147. switch_node->GetName().c_str(), switch_node->GetType().c_str());
  148. return ret;
  149. }
  150. for (auto &node : deleted_nodes) {
  151. GE_CHECK_NOTNULL(node);
  152. GELOGD("Remove node %s from inactivate branch from switch %s",
  153. node->GetName().c_str(), switch_node->GetName().c_str());
  154. AddNodeDeleted(node);
  155. }
  156. for (auto &node : end_nodes) {
  157. GE_CHECK_NOTNULL(node);
  158. GELOGD("Add end node %s to re-pass list, for inactivate branch from switch %s",
  159. node->GetName().c_str(), switch_node->GetName().c_str());
  160. AddRePassNode(node);
  161. }
  162. }
  163. GELOGI("Remove switch node cascaded %s, replace out index %d",
  164. switch_node->GetName().c_str(), parent_index);
  165. return IsolateAndDeleteNode(switch_node, isolate_map);
  166. }
  167. } // namespace ge

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