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

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