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_data_edges_bypass.cc 9.0 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "switch_data_edges_bypass.h"
  17. #include <atomic>
  18. #include "common/debug/log.h"
  19. #include "common/ge/ge_util.h"
  20. #include "common/op/ge_op_utils.h"
  21. #include "common/util.h"
  22. #include "graph/utils/node_utils.h"
  23. namespace ge {
  24. namespace {
  25. bool IsSwitchInWhileLoop(const NodePtr &node) {
  26. auto pred_anchor = node->GetInDataAnchor(SWITCH_PRED_INPUT);
  27. if (pred_anchor == nullptr) {
  28. GELOGW("The switch node %s does not have a pred in anchor, the node may be invalid", node->GetName().c_str());
  29. return true;
  30. }
  31. auto pred_node_anchor = pred_anchor->GetPeerOutAnchor();
  32. if (pred_node_anchor == nullptr) {
  33. GELOGW("The switch node %s does not have a pred in node, the graph may be invalid", node->GetName().c_str());
  34. return true;
  35. }
  36. auto pred_node = pred_node_anchor->GetOwnerNode();
  37. if (pred_node == nullptr) {
  38. GELOGW("The switch node %s does not have a pred in node, the pred-anchor may be invalid", node->GetName().c_str());
  39. return true;
  40. }
  41. if (pred_node->GetType() == LOOPCOND) {
  42. GELOGD("The switch node %s is in a while loop, skip the bypass process", node->GetName().c_str());
  43. return true;
  44. }
  45. return false;
  46. }
  47. std::vector<std::pair<NodePtr, InDataAnchorPtr>> GetOutDataNodesByIndex(const NodePtr &node, int index) {
  48. auto out_anchor = node->GetOutDataAnchor(index);
  49. if (out_anchor == nullptr) {
  50. GELOGE(PARAM_INVALID, "Failed to get out data nodes of index %d from node %s, the anchor does not exists", index,
  51. node->GetName().c_str());
  52. return {};
  53. }
  54. std::vector<std::pair<NodePtr, InDataAnchorPtr>> nodes_and_anchors;
  55. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  56. auto out_node = in_anchor->GetOwnerNode();
  57. if (out_node != nullptr) {
  58. nodes_and_anchors.emplace_back(out_node, in_anchor);
  59. }
  60. }
  61. return nodes_and_anchors;
  62. }
  63. std::pair<NodePtr, OutDataAnchorPtr> GetInDataNodeByIndex(const NodePtr &node, int index) {
  64. auto in_anchor = node->GetInDataAnchor(index);
  65. if (in_anchor == nullptr) {
  66. GELOGD("Failed to get in data node of index %d from node %s, the anchor does not exists", index,
  67. node->GetName().c_str());
  68. return {};
  69. }
  70. auto out_anchor = in_anchor->GetPeerOutAnchor();
  71. if (out_anchor == nullptr) {
  72. GELOGD("Failed to get in data node of index %d from node %s, the data input does not exists", index,
  73. node->GetName().c_str());
  74. return {};
  75. }
  76. return {out_anchor->GetOwnerNode(), out_anchor};
  77. }
  78. NodePtr AddIdentityAfterNode(const NodePtr &node, int index) {
  79. static std::atomic_long atomic_identity_counter(0);
  80. auto identity_counter = atomic_identity_counter.fetch_add(1);
  81. auto node_desc = node->GetOpDesc();
  82. if (node_desc == nullptr) {
  83. GELOGE(INTERNAL_ERROR, "Failed to add identity after node %s index %d, the op desc is null",
  84. node->GetName().c_str(), index);
  85. return nullptr;
  86. }
  87. auto tensor = node_desc->GetOutputDescPtr(index);
  88. if (tensor == nullptr) {
  89. GELOGE(INTERNAL_ERROR, "Failed to find the tensor by index %d from node %s, can not add the identity node", index,
  90. node->GetName().c_str());
  91. return nullptr;
  92. }
  93. auto anchor = node->GetOutDataAnchor(index);
  94. if (anchor == nullptr) {
  95. GELOGE(OUT_OF_MEMORY, "Failed to add identity after node %s index %d, the out anchor does not exists",
  96. node->GetName().c_str(), index);
  97. return nullptr;
  98. }
  99. auto identity_opdesc =
  100. MakeShared<OpDesc>("SwitchDataEdgesByPass_Identity_" + std::to_string(identity_counter), IDENTITY);
  101. if (identity_opdesc == nullptr) {
  102. GELOGE(OUT_OF_MEMORY, "Failed to add identity after node %s index %d", node->GetName().c_str(), index);
  103. return nullptr;
  104. }
  105. auto ret1 = identity_opdesc->AddInputDesc("x", *tensor);
  106. auto ret2 = identity_opdesc->AddOutputDesc("y", *tensor);
  107. auto identity = node->GetOwnerComputeGraph()->AddNode(identity_opdesc);
  108. if (ret1 != GRAPH_SUCCESS || ret2 != GRAPH_SUCCESS || identity == nullptr) {
  109. GELOGE(OUT_OF_MEMORY, "Failed to add identity after node %s index %d", node->GetName().c_str(), index);
  110. return nullptr;
  111. }
  112. (void)anchor->LinkTo(identity->GetInDataAnchor(0));
  113. return identity;
  114. }
  115. NodePtr AddMemcpyBeforeNode(const NodePtr &node, int index) {
  116. static std::atomic_long atomic_counter(0);
  117. auto counter = atomic_counter.fetch_add(1);
  118. auto node_desc = node->GetOpDesc();
  119. if (node_desc == nullptr) {
  120. GELOGE(INTERNAL_ERROR, "Failed to add memcpy before node %s index %d, null op desc", node->GetName().c_str(),
  121. index);
  122. return nullptr;
  123. }
  124. auto tensor = node_desc->GetInputDescPtr(index);
  125. if (tensor == nullptr) {
  126. GELOGE(INTERNAL_ERROR, "Failed to find the tensor by index %d from node %s, can not add the memcpy node", index,
  127. node->GetName().c_str());
  128. return nullptr;
  129. }
  130. auto anchor = node->GetInDataAnchor(index);
  131. if (anchor == nullptr) {
  132. GELOGE(INTERNAL_ERROR, "Failed to add memcpy before node %s index %d, the in anchor does not exists",
  133. node->GetName().c_str(), index);
  134. return nullptr;
  135. }
  136. auto memcpy_opdesc = MakeShared<OpDesc>("SwitchDataEdgesByPass_Memcpy_" + std::to_string(counter), MEMCPYASYNC);
  137. if (memcpy_opdesc == nullptr) {
  138. GELOGE(OUT_OF_MEMORY, "Failed to add memcpy before node %s index %d", node->GetName().c_str(), index);
  139. return nullptr;
  140. }
  141. auto ret1 = memcpy_opdesc->AddInputDesc(*tensor);
  142. auto ret2 = memcpy_opdesc->AddOutputDesc(*tensor);
  143. auto memcpy_node = node->GetOwnerComputeGraph()->AddNode(memcpy_opdesc);
  144. if (ret1 != GRAPH_SUCCESS || ret2 != GRAPH_SUCCESS || memcpy_node == nullptr) {
  145. GELOGE(OUT_OF_MEMORY, "Failed to add memcpy before node %s index %d", node->GetName().c_str(), index);
  146. return nullptr;
  147. }
  148. (void)memcpy_node->GetOutDataAnchor(0)->LinkTo(anchor);
  149. return memcpy_node;
  150. }
  151. Status BypassSwitchOut(const NodePtr &switch_node, int out_index) {
  152. auto nodes_and_anchors = GetOutDataNodesByIndex(switch_node, out_index);
  153. if (nodes_and_anchors.empty()) {
  154. GELOGD("The switch node %s does not has out branch %d, skip the bypass process", switch_node->GetName().c_str(),
  155. out_index);
  156. return SUCCESS;
  157. }
  158. auto data_node_and_anchor = GetInDataNodeByIndex(switch_node, SWITCH_DATA_INPUT);
  159. if (data_node_and_anchor.first == nullptr) {
  160. GELOGW("Can not bypass switch node %s, the node does not has a data input", switch_node->GetName().c_str());
  161. return SUCCESS;
  162. }
  163. auto identity = AddIdentityAfterNode(switch_node, out_index);
  164. GE_CHECK_NOTNULL(identity);
  165. std::set<Node *> connected_nodes;
  166. for (const auto &node_and_anchor : nodes_and_anchors) {
  167. auto head_anchor = node_and_anchor.second;
  168. head_anchor->UnlinkAll();
  169. auto head_node = node_and_anchor.first;
  170. auto head_node_type = NodeUtils::GetNodeType(*head_node);
  171. if (head_node_type == MEMCPYASYNC) {
  172. // if the switch connect to the merge directly, insert memcpy before merge
  173. auto memcpy_node = AddMemcpyBeforeNode(head_node, head_anchor->GetIdx());
  174. GE_CHECK_NOTNULL(memcpy_node);
  175. GELOGD("Add memcpy %s before merge node %s", memcpy_node->GetName().c_str(), head_node->GetName().c_str());
  176. head_node = memcpy_node;
  177. head_anchor = memcpy_node->GetInDataAnchor(0);
  178. }
  179. (void)data_node_and_anchor.second->LinkTo(head_anchor);
  180. if (connected_nodes.insert(head_node.get()).second) {
  181. (void)identity->GetOutControlAnchor()->LinkTo(head_node->GetInControlAnchor());
  182. }
  183. }
  184. GELOGI("Bypass switch %s out index %d success", switch_node->GetName().c_str(), out_index);
  185. return SUCCESS;
  186. }
  187. } // namespace
  188. Status SwitchDataEdgesBypass::Run(ComputeGraphPtr graph) {
  189. for (const auto &node : graph->GetDirectNode()) {
  190. auto ret = BypassSwitch(node);
  191. GE_CHK_STATUS_RET(ret, "By pass switch node %s failed", node->GetName().c_str())
  192. }
  193. return SUCCESS;
  194. }
  195. Status SwitchDataEdgesBypass::BypassSwitch(const NodePtr &node) {
  196. auto node_type = NodeUtils::GetNodeType(*node);
  197. if ((node_type != SWITCH) && (node_type != REFSWITCH)) {
  198. return SUCCESS;
  199. }
  200. if (IsSwitchInWhileLoop(node)) {
  201. return SUCCESS;
  202. }
  203. auto ret = BypassSwitchOut(node, SWITCH_FALSE_OUTPUT);
  204. GE_CHK_STATUS_RET(ret, "By pass switch node %s false output failed", node->GetName().c_str())
  205. ret = BypassSwitchOut(node, SWITCH_TRUE_OUTPUT);
  206. GE_CHK_STATUS_RET(ret, "By pass switch node %s true output failed", node->GetName().c_str())
  207. return SUCCESS;
  208. }
  209. } // namespace ge

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