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
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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, "Failed to get out data nodes of index %d from node %s, the anchor does not exists", index,
  53. 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, "Failed to add identity after node %s index %d, the op desc is null",
  87. node->GetName().c_str(), index);
  88. return nullptr;
  89. }
  90. auto tensor = node_desc->GetOutputDescPtr(index);
  91. if (tensor == nullptr) {
  92. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no index:%d output tensor, check invalid",
  93. node_desc->GetName().c_str(), node_desc->GetType().c_str(), index);
  94. GELOGE(INTERNAL_ERROR, "Failed to find the tensor by index %d from node %s, can not add the identity node", index,
  95. node->GetName().c_str());
  96. return nullptr;
  97. }
  98. auto anchor = node->GetOutDataAnchor(index);
  99. if (anchor == nullptr) {
  100. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no index:%d out data anchor, check invalid",
  101. node->GetName().c_str(), node->GetType().c_str(), index);
  102. GELOGE(OUT_OF_MEMORY, "Failed to add identity after node %s index %d, the out anchor does not exists",
  103. node->GetName().c_str(), index);
  104. return nullptr;
  105. }
  106. auto identity_opdesc =
  107. MakeShared<OpDesc>("SwitchDataEdgesByPass_Identity_" + std::to_string(identity_counter), IDENTITY);
  108. if (identity_opdesc == nullptr) {
  109. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  110. GELOGE(OUT_OF_MEMORY, "Failed to add identity after node %s index %d", node->GetName().c_str(), index);
  111. return nullptr;
  112. }
  113. auto ret1 = identity_opdesc->AddInputDesc("x", *tensor);
  114. auto ret2 = identity_opdesc->AddOutputDesc("y", *tensor);
  115. auto identity = node->GetOwnerComputeGraph()->AddNode(identity_opdesc);
  116. if (ret1 != GRAPH_SUCCESS || ret2 != GRAPH_SUCCESS || identity == nullptr) {
  117. REPORT_CALL_ERROR("E19999", "Add input ouput desc to op:%s(%s) failed or add it to graph:%s failed",
  118. identity_opdesc->GetName().c_str(), identity_opdesc->GetType().c_str(),
  119. node->GetOwnerComputeGraph()->GetName().c_str());
  120. GELOGE(OUT_OF_MEMORY, "Failed to add identity after node %s index %d", node->GetName().c_str(), index);
  121. return nullptr;
  122. }
  123. (void)anchor->LinkTo(identity->GetInDataAnchor(0));
  124. return identity;
  125. }
  126. NodePtr AddMemcpyBeforeNode(const NodePtr &node, int index) {
  127. static std::atomic_long atomic_counter(0);
  128. auto counter = atomic_counter.fetch_add(1);
  129. auto node_desc = node->GetOpDesc();
  130. if (node_desc == nullptr) {
  131. REPORT_INNER_ERROR("E19999", "OpDesc in node is nullptr, check invalid");
  132. GELOGE(INTERNAL_ERROR, "Failed to add memcpy before node %s index %d, null op desc", node->GetName().c_str(),
  133. index);
  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, "Failed to find the tensor by index %d from node %s, can not add the memcpy node", index,
  141. node->GetName().c_str());
  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, "Failed to add memcpy before node %s index %d, the in anchor does not exists",
  149. node->GetName().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, "Failed to add memcpy before node %s index %d", node->GetName().c_str(), index);
  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, "Failed to add memcpy before node %s index %d", node->GetName().c_str(), index);
  166. return nullptr;
  167. }
  168. (void)memcpy_node->GetOutDataAnchor(0)->LinkTo(anchor);
  169. return memcpy_node;
  170. }
  171. Status BypassSwitchOut(const NodePtr &switch_node, int out_index) {
  172. auto nodes_and_anchors = GetOutDataNodesByIndex(switch_node, out_index);
  173. if (nodes_and_anchors.empty()) {
  174. GELOGD("The switch node %s does not has out branch %d, skip the bypass process", switch_node->GetName().c_str(),
  175. out_index);
  176. return SUCCESS;
  177. }
  178. auto data_node_and_anchor = GetInDataNodeByIndex(switch_node, SWITCH_DATA_INPUT);
  179. if (data_node_and_anchor.first == nullptr) {
  180. GELOGW("Can not bypass switch node %s, the node does not has a data input", switch_node->GetName().c_str());
  181. return SUCCESS;
  182. }
  183. auto identity = AddIdentityAfterNode(switch_node, out_index);
  184. GE_CHECK_NOTNULL(identity);
  185. std::set<Node *> connected_nodes;
  186. for (const auto &node_and_anchor : nodes_and_anchors) {
  187. auto head_anchor = node_and_anchor.second;
  188. head_anchor->UnlinkAll();
  189. auto head_node = node_and_anchor.first;
  190. auto head_node_type = NodeUtils::GetNodeType(*head_node);
  191. if (head_node_type == MEMCPYASYNC) {
  192. // if the switch connect to the merge directly, insert memcpy before merge
  193. auto memcpy_node = AddMemcpyBeforeNode(head_node, head_anchor->GetIdx());
  194. GE_CHECK_NOTNULL(memcpy_node);
  195. GELOGD("Add memcpy %s before merge node %s", memcpy_node->GetName().c_str(), head_node->GetName().c_str());
  196. head_node = memcpy_node;
  197. head_anchor = memcpy_node->GetInDataAnchor(0);
  198. }
  199. (void)data_node_and_anchor.second->LinkTo(head_anchor);
  200. if (connected_nodes.insert(head_node.get()).second) {
  201. (void)identity->GetOutControlAnchor()->LinkTo(head_node->GetInControlAnchor());
  202. }
  203. }
  204. GELOGI("Bypass switch %s out index %d success", switch_node->GetName().c_str(), out_index);
  205. return SUCCESS;
  206. }
  207. } // namespace
  208. Status SwitchDataEdgesBypass::Run(ComputeGraphPtr graph) {
  209. for (const auto &node : graph->GetDirectNode()) {
  210. auto ret = BypassSwitch(node);
  211. GE_CHK_STATUS_RET(ret, "By pass switch node %s failed", node->GetName().c_str())
  212. }
  213. return SUCCESS;
  214. }
  215. Status SwitchDataEdgesBypass::BypassSwitch(const NodePtr &node) {
  216. auto node_type = NodeUtils::GetNodeType(*node);
  217. if ((node_type != SWITCH) && (node_type != REFSWITCH)) {
  218. return SUCCESS;
  219. }
  220. if (IsSwitchInWhileLoop(node)) {
  221. return SUCCESS;
  222. }
  223. auto ret = BypassSwitchOut(node, SWITCH_FALSE_OUTPUT);
  224. GE_CHK_STATUS_RET(ret, "By pass switch node %s false output failed", node->GetName().c_str())
  225. ret = BypassSwitchOut(node, SWITCH_TRUE_OUTPUT);
  226. GE_CHK_STATUS_RET(ret, "By pass switch node %s true output failed", node->GetName().c_str())
  227. return SUCCESS;
  228. }
  229. } // namespace ge

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