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

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

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