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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no index:%d out data anchor, check invalid",
  51. node->GetName().c_str(), node->GetType().c_str(), index);
  52. GELOGE(PARAM_INVALID, "[Get][OutDataNodes] of index %d from node %s failed, the anchor does not exists",
  53. index, node->GetName().c_str());
  54. return {};
  55. }
  56. std::vector<std::pair<NodePtr, InDataAnchorPtr>> nodes_and_anchors;
  57. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  58. auto out_node = in_anchor->GetOwnerNode();
  59. if (out_node != nullptr) {
  60. nodes_and_anchors.emplace_back(out_node, in_anchor);
  61. }
  62. }
  63. return nodes_and_anchors;
  64. }
  65. std::pair<NodePtr, OutDataAnchorPtr> GetInDataNodeByIndex(const NodePtr &node, int index) {
  66. auto in_anchor = node->GetInDataAnchor(index);
  67. if (in_anchor == nullptr) {
  68. GELOGD("Failed to get in data node of index %d from node %s, the anchor does not exists", index,
  69. node->GetName().c_str());
  70. return {};
  71. }
  72. auto out_anchor = in_anchor->GetPeerOutAnchor();
  73. if (out_anchor == nullptr) {
  74. GELOGD("Failed to get in data node of index %d from node %s, the data input does not exists", index,
  75. node->GetName().c_str());
  76. return {};
  77. }
  78. return {out_anchor->GetOwnerNode(), out_anchor};
  79. }
  80. NodePtr AddIdentityAfterNode(const NodePtr &node, int index) {
  81. static std::atomic_long atomic_identity_counter(0);
  82. auto identity_counter = atomic_identity_counter.fetch_add(1);
  83. auto node_desc = node->GetOpDesc();
  84. if (node_desc == nullptr) {
  85. REPORT_INNER_ERROR("E19999", "OpDesc in node is nullptr, check invalid");
  86. GELOGE(INTERNAL_ERROR, "[Get][OpDesc] failed, the op desc is nullptr");
  87. return nullptr;
  88. }
  89. auto tensor = node_desc->GetOutputDescPtr(index);
  90. if (tensor == nullptr) {
  91. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no index:%d output tensor, check invalid",
  92. node_desc->GetName().c_str(), node_desc->GetType().c_str(), index);
  93. GELOGE(INTERNAL_ERROR, "[Get][OutputDescPtr] failed, Node:%s(%s) has no index:%d output tensor",
  94. node_desc->GetName().c_str(), node_desc->GetType().c_str(), index);
  95. return nullptr;
  96. }
  97. auto anchor = node->GetOutDataAnchor(index);
  98. if (anchor == nullptr) {
  99. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no index:%d out data anchor, check invalid",
  100. node->GetName().c_str(), node->GetType().c_str(), index);
  101. GELOGE(OUT_OF_MEMORY, "[Get][OutDataAnchor] failed, Node:%s(%s) has no index:%d out data anchor",
  102. node->GetName().c_str(), node->GetType().c_str(), index);
  103. return nullptr;
  104. }
  105. auto identity_opdesc =
  106. MakeShared<OpDesc>("SwitchDataEdgesByPass_Identity_" + std::to_string(identity_counter), IDENTITY);
  107. if (identity_opdesc == nullptr) {
  108. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  109. GELOGE(OUT_OF_MEMORY, "[New][OpDesc] failed");
  110. return nullptr;
  111. }
  112. auto ret1 = identity_opdesc->AddInputDesc("x", *tensor);
  113. auto ret2 = identity_opdesc->AddOutputDesc("y", *tensor);
  114. auto identity = node->GetOwnerComputeGraph()->AddNode(identity_opdesc);
  115. if (ret1 != GRAPH_SUCCESS || ret2 != GRAPH_SUCCESS || identity == nullptr) {
  116. REPORT_CALL_ERROR("E19999", "Add input ouput desc to op:%s(%s) failed or add it to graph:%s failed",
  117. identity_opdesc->GetName().c_str(), identity_opdesc->GetType().c_str(),
  118. node->GetOwnerComputeGraph()->GetName().c_str());
  119. GELOGE(OUT_OF_MEMORY, "[Check][Param] Add input ouput desc to op:%s(%s) failed or add it to graph:%s failed",
  120. identity_opdesc->GetName().c_str(), identity_opdesc->GetType().c_str(),
  121. node->GetOwnerComputeGraph()->GetName().c_str());
  122. return nullptr;
  123. }
  124. (void)anchor->LinkTo(identity->GetInDataAnchor(0));
  125. return identity;
  126. }
  127. NodePtr AddMemcpyBeforeNode(const NodePtr &node, int index) {
  128. static std::atomic_long atomic_counter(0);
  129. auto counter = atomic_counter.fetch_add(1);
  130. auto node_desc = node->GetOpDesc();
  131. if (node_desc == nullptr) {
  132. REPORT_INNER_ERROR("E19999", "OpDesc in node is nullptr, check invalid");
  133. GELOGE(INTERNAL_ERROR, "[Get][OpDesc] failed, OpDesc in node is nullptr");
  134. return nullptr;
  135. }
  136. auto tensor = node_desc->GetInputDescPtr(index);
  137. if (tensor == nullptr) {
  138. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no index:%d input tensor, check invalid",
  139. node_desc->GetName().c_str(), node_desc->GetType().c_str(), index);
  140. GELOGE(INTERNAL_ERROR, "[Get][InputDescPtr] failed, Node:%s(%s) has no index:%d input tensor",
  141. node_desc->GetName().c_str(), node_desc->GetType().c_str(), index);
  142. return nullptr;
  143. }
  144. auto anchor = node->GetInDataAnchor(index);
  145. if (anchor == nullptr) {
  146. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no index:%d in data anchor, check invalid",
  147. node->GetName().c_str(), node->GetType().c_str(), index);
  148. GELOGE(INTERNAL_ERROR, "[Get][InDataAnchor] failed, Node:%s(%s) has no index:%d in data anchor",
  149. node->GetName().c_str(), node->GetType().c_str(), index);
  150. return nullptr;
  151. }
  152. auto memcpy_opdesc = MakeShared<OpDesc>("SwitchDataEdgesByPass_Memcpy_" + std::to_string(counter), MEMCPYASYNC);
  153. if (memcpy_opdesc == nullptr) {
  154. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  155. GELOGE(OUT_OF_MEMORY, "[New][OpDesc] failed");
  156. return nullptr;
  157. }
  158. auto ret1 = memcpy_opdesc->AddInputDesc(*tensor);
  159. auto ret2 = memcpy_opdesc->AddOutputDesc(*tensor);
  160. auto memcpy_node = node->GetOwnerComputeGraph()->AddNode(memcpy_opdesc);
  161. if (ret1 != GRAPH_SUCCESS || ret2 != GRAPH_SUCCESS || memcpy_node == nullptr) {
  162. REPORT_CALL_ERROR("E19999", "Add input ouput desc to op:%s(%s) failed or add it to graph:%s failed",
  163. memcpy_opdesc->GetName().c_str(), memcpy_opdesc->GetType().c_str(),
  164. node->GetOwnerComputeGraph()->GetName().c_str());
  165. GELOGE(OUT_OF_MEMORY, "[Check][Param] Add input ouput desc to op:%s(%s) failed or add it to graph:%s failed",
  166. memcpy_opdesc->GetName().c_str(), memcpy_opdesc->GetType().c_str(),
  167. node->GetOwnerComputeGraph()->GetName().c_str());
  168. return nullptr;
  169. }
  170. (void)memcpy_node->GetOutDataAnchor(0)->LinkTo(anchor);
  171. return memcpy_node;
  172. }
  173. Status BypassSwitchOut(const NodePtr &switch_node, int out_index) {
  174. auto nodes_and_anchors = GetOutDataNodesByIndex(switch_node, out_index);
  175. if (nodes_and_anchors.empty()) {
  176. GELOGD("The switch node %s does not has out branch %d, skip the bypass process", switch_node->GetName().c_str(),
  177. out_index);
  178. return SUCCESS;
  179. }
  180. auto data_node_and_anchor = GetInDataNodeByIndex(switch_node, SWITCH_DATA_INPUT);
  181. if (data_node_and_anchor.first == nullptr) {
  182. GELOGW("Can not bypass switch node %s, the node does not has a data input", switch_node->GetName().c_str());
  183. return SUCCESS;
  184. }
  185. auto identity = AddIdentityAfterNode(switch_node, out_index);
  186. GE_CHECK_NOTNULL(identity);
  187. std::set<Node *> connected_nodes;
  188. for (const auto &node_and_anchor : nodes_and_anchors) {
  189. auto head_anchor = node_and_anchor.second;
  190. head_anchor->UnlinkAll();
  191. auto head_node = node_and_anchor.first;
  192. auto head_node_type = NodeUtils::GetNodeType(*head_node);
  193. if (head_node_type == MEMCPYASYNC) {
  194. // if the switch connect to the merge directly, insert memcpy before merge
  195. auto memcpy_node = AddMemcpyBeforeNode(head_node, head_anchor->GetIdx());
  196. GE_CHECK_NOTNULL(memcpy_node);
  197. GELOGD("Add memcpy %s before merge node %s", memcpy_node->GetName().c_str(), head_node->GetName().c_str());
  198. head_node = memcpy_node;
  199. head_anchor = memcpy_node->GetInDataAnchor(0);
  200. }
  201. (void)data_node_and_anchor.second->LinkTo(head_anchor);
  202. if (connected_nodes.insert(head_node.get()).second) {
  203. (void)identity->GetOutControlAnchor()->LinkTo(head_node->GetInControlAnchor());
  204. }
  205. }
  206. GELOGI("Bypass switch %s out index %d success", switch_node->GetName().c_str(), out_index);
  207. return SUCCESS;
  208. }
  209. } // namespace
  210. Status SwitchDataEdgesBypass::Run(ComputeGraphPtr graph) {
  211. for (const auto &node : graph->GetDirectNode()) {
  212. auto ret = BypassSwitch(node);
  213. GE_CHK_STATUS_RET(ret, "[Bypass][Switch] node %s failed", node->GetName().c_str())
  214. }
  215. return SUCCESS;
  216. }
  217. Status SwitchDataEdgesBypass::BypassSwitch(const NodePtr &node) {
  218. auto node_type = NodeUtils::GetNodeType(*node);
  219. if ((node_type != SWITCH) && (node_type != REFSWITCH)) {
  220. return SUCCESS;
  221. }
  222. if (IsSwitchInWhileLoop(node)) {
  223. return SUCCESS;
  224. }
  225. auto ret = BypassSwitchOut(node, SWITCH_FALSE_OUTPUT);
  226. GE_CHK_STATUS_RET(ret, "[Bypass][Switch] node %s false output failed", node->GetName().c_str())
  227. ret = BypassSwitchOut(node, SWITCH_TRUE_OUTPUT);
  228. GE_CHK_STATUS_RET(ret, "[Bypass][Switch] node %s true output failed", node->GetName().c_str())
  229. return SUCCESS;
  230. }
  231. } // namespace ge

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