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.

ref_identity_delete_op_pass.cc 12 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. #include "ref_identity_delete_op_pass.h"
  14. #include <map>
  15. #include <stack>
  16. #include "graph/common/transop_util.h"
  17. namespace ge {
  18. Status RefIdentityDeleteOpPass::Run(ComputeGraphPtr graph) {
  19. GE_CHECK_NOTNULL(graph);
  20. for (auto &node : graph->GetAllNodes()) {
  21. if (node->GetType() != REFIDENTITY) {
  22. continue;
  23. }
  24. int input_index = 0;
  25. NodePtr ref_node = GetRefNode(node, input_index);
  26. CHECK_FALSE_EXEC(GetRefNode(node, input_index) != nullptr,
  27. REPORT_CALL_ERROR("E19999", "Get Ref node of node:%s(%s) failed",
  28. node->GetName().c_str(), node->GetType().c_str());
  29. GELOGE(FAILED, "Ref node of RefIdentity[%s] not found", node->GetName().c_str());
  30. return FAILED);
  31. CHECK_FALSE_EXEC(DealNoOutputRef(ref_node, node, input_index, graph) == SUCCESS,
  32. GELOGE(FAILED, "Ref identity [%s] delete failed", node->GetName().c_str());
  33. return FAILED);
  34. }
  35. return SUCCESS;
  36. }
  37. NodePtr RefIdentityDeleteOpPass::GetRefNode(const NodePtr &node, int &input_index) {
  38. OutDataAnchorPtr out_anchor = node->GetOutDataAnchor(0);
  39. CHECK_FALSE_EXEC(out_anchor != nullptr, return nullptr);
  40. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  41. CHECK_FALSE_EXEC(peer_in_anchor != nullptr, continue);
  42. auto peer_node = peer_in_anchor->GetOwnerNode();
  43. CHECK_FALSE_EXEC(peer_node != nullptr, continue);
  44. const auto &peer_op_desc = peer_node->GetOpDesc();
  45. CHECK_FALSE_EXEC(peer_op_desc != nullptr, return nullptr);
  46. const auto &peer_input_desc = peer_op_desc->GetInputDescPtr(static_cast<uint32_t>(peer_in_anchor->GetIdx()));
  47. if (!peer_input_desc->GetRefPortIndex().empty()) {
  48. input_index = peer_in_anchor->GetIdx();
  49. return peer_node;
  50. }
  51. }
  52. return nullptr;
  53. }
  54. Status RefIdentityDeleteOpPass::DealNoOutputRef(const NodePtr &node, const NodePtr &ref_identity, int input_index,
  55. const ComputeGraphPtr &graph) {
  56. NodePtr first_node = nullptr;
  57. NodePtr variable_ref = GetVariableRef(node, ref_identity, first_node);
  58. if (variable_ref == nullptr) {
  59. REPORT_CALL_ERROR("E19999", "Get variable ref of node:%s(%s) failed",
  60. node->GetName().c_str(), node->GetType().c_str());
  61. GELOGE(FAILED, "[RefIdentityDeleteOpPass]Can not find variable ref for %s:%d", node->GetName().c_str(),
  62. input_index);
  63. return FAILED;
  64. }
  65. if (first_node->GetName() != variable_ref->GetName()) {
  66. // Remove the control edge between ref node and variable ref
  67. // Add a control edge between ref node and trans node
  68. // +-----------+ +-----------+
  69. // +---------+RefIdentity| +-----------+RefIdentity|
  70. // | +-----+-----+ | +-----+-----+
  71. // | | | |
  72. // | v | v
  73. // +-----v-----+ +----+----+ +-----v-----+ +----+----+
  74. // | TransNode | | RefNode | ==> | TransNode +<--C--+ RefNode |
  75. // +-----+-----+ +----+----+ +-----+-----+ +---------+
  76. // | | |
  77. // v C v
  78. // +-----+-----+ | +-----+-----+
  79. // |VariableRef+<--------+ |VariableRef|
  80. // +-----------+ +-----------+
  81. auto ret = ge::GraphUtils::AddEdge(node->GetOutControlAnchor(), first_node->GetInControlAnchor());
  82. if (ret != SUCCESS) {
  83. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  84. node->GetName().c_str(), node->GetType().c_str(),
  85. first_node->GetName().c_str(), first_node->GetType().c_str());
  86. GELOGE(FAILED, "Add control edge between ref node and trans node failed");
  87. return FAILED;
  88. }
  89. ret = ge::GraphUtils::RemoveEdge(node->GetOutControlAnchor(), variable_ref->GetInControlAnchor());
  90. if (ret != SUCCESS) {
  91. REPORT_CALL_ERROR("E19999", "Remove control edge between op:%s(%s) and op:%s(%s) failed",
  92. node->GetName().c_str(), node->GetType().c_str(),
  93. first_node->GetName().c_str(), first_node->GetType().c_str());
  94. GELOGE(FAILED, "Remove control edge between ref node and its peer node failed");
  95. return FAILED;
  96. }
  97. } else {
  98. // +-----------+ +-----------+
  99. // +-----------+RefIdentity| +-----------+RefIdentity|
  100. // | +-----+-----+ | +-----+-----+
  101. // | | | |
  102. // | v | v
  103. // +-----v-----+ +----+----+ +-----v-----+ +----+----+
  104. // |VariableRef+<--C--+ RefNode | ==> |VariableRef+<--C--+ RefNode |
  105. // +-----+-----+ +----+----+ +-----------+ +----+----+
  106. // | | |
  107. // | v v
  108. // | +---+----+ +---+----+
  109. // +-----C------>+ | | |
  110. // +--------+ +--------+
  111. auto ret = RemoveUselessControlEdge(node, variable_ref);
  112. if (ret != SUCCESS) {
  113. GELOGE(FAILED, "Remove useless control edge failed.");
  114. return FAILED;
  115. }
  116. }
  117. // remove ref identity
  118. if (GraphUtils::IsolateNode(ref_identity, {0}) != GRAPH_SUCCESS) {
  119. REPORT_CALL_ERROR("E19999", "Isolate op:%s(%s) failed",
  120. ref_identity->GetName().c_str(), ref_identity->GetType().c_str());
  121. GELOGE(INTERNAL_ERROR, "Isolate removed node: %s, type: %s failed", ref_identity->GetName().c_str(),
  122. variable_ref->GetType().c_str());
  123. return FAILED;
  124. }
  125. if (GraphUtils::RemoveNodeWithoutRelink(graph, ref_identity) != GRAPH_SUCCESS) {
  126. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) without relink in graph:%s failed",
  127. ref_identity->GetName().c_str(), ref_identity->GetType().c_str(), graph->GetName().c_str());
  128. GELOGE(INTERNAL_ERROR, "Remove node: %s, type: %s without relink failed", ref_identity->GetName().c_str(),
  129. ref_identity->GetType().c_str());
  130. return FAILED;
  131. }
  132. return SUCCESS;
  133. }
  134. ge::NodePtr RefIdentityDeleteOpPass::GetVariableRef(const NodePtr &ref, const NodePtr &ref_identity,
  135. NodePtr &first_node) {
  136. const auto &ref_identity_out_anchor = ref_identity->GetOutDataAnchor(0);
  137. if (ref_identity_out_anchor == nullptr) {
  138. return nullptr;
  139. }
  140. for (auto &peer_in_anchor : ref_identity_out_anchor->GetPeerInDataAnchors()) {
  141. const auto &peer_node = peer_in_anchor->GetOwnerNode();
  142. if (peer_node == nullptr || peer_node->GetName() == ref->GetName()) {
  143. continue;
  144. }
  145. // DFS to find variable ref node.
  146. std::stack<NodePtr> nodes_to_check;
  147. nodes_to_check.push(peer_node);
  148. GELOGI("[RefIdentityDeleteOpPass]Start to search variable ref node from %s.", peer_node->GetName().c_str());
  149. NodePtr cur_node = nullptr;
  150. while (!nodes_to_check.empty()) {
  151. cur_node = nodes_to_check.top();
  152. nodes_to_check.pop();
  153. const auto &type = cur_node->GetType();
  154. if (type == VARIABLE && CheckControlEdge(ref, cur_node)) {
  155. // Target variable ref node found.
  156. GELOGI("[RefIdentityDeleteOpPass]variable ref node[%s] found.", cur_node->GetName().c_str());
  157. first_node = peer_node;
  158. return cur_node;
  159. }
  160. int data_index = TransOpUtil::GetTransOpDataIndex(type);
  161. if (data_index < 0) {
  162. GELOGI("[RefIdentityDeleteOpPass]Find node[%s] that is not trans op[%s], stop to search its output.",
  163. cur_node->GetName().c_str(), type.c_str());
  164. continue;
  165. }
  166. const auto &cur_out_anchor = cur_node->GetOutDataAnchor(0);
  167. if (cur_out_anchor == nullptr) {
  168. GELOGI("[RefIdentityDeleteOpPass]Get out anchor of [%s] failed, stop to search its output.",
  169. cur_node->GetName().c_str());
  170. continue;
  171. }
  172. for (const auto &cur_peer_in_anchor : cur_out_anchor->GetPeerInDataAnchors()) {
  173. const auto &cur_peer_node = cur_peer_in_anchor->GetOwnerNode();
  174. if (cur_peer_node == nullptr) {
  175. continue;
  176. }
  177. nodes_to_check.push(cur_peer_node);
  178. }
  179. }
  180. GELOGI("[RefIdentityDeleteOpPass]Can not find variable ref node from %s.", peer_node->GetName().c_str());
  181. }
  182. GELOGI("[RefIdentityDeleteOpPass]Can not find variable ref node, return nullptr.");
  183. return nullptr;
  184. }
  185. bool RefIdentityDeleteOpPass::CheckControlEdge(const NodePtr &ref, const NodePtr &variable_ref) {
  186. const auto &control_out_anchor = ref->GetOutControlAnchor();
  187. if (control_out_anchor == nullptr) {
  188. return false;
  189. }
  190. const string &variable_ref_name = variable_ref->GetName();
  191. for (const auto &peer_in_control_anchor : control_out_anchor->GetPeerInControlAnchors()) {
  192. const auto &node = peer_in_control_anchor->GetOwnerNode();
  193. if (node != nullptr && node->GetName() == variable_ref_name) {
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. Status RefIdentityDeleteOpPass::RemoveUselessControlEdge(const NodePtr &ref, const NodePtr &variable_ref) {
  200. map<string, NodePtr> out_nodes_map;
  201. for (const auto &out_anchor : ref->GetAllOutDataAnchors()) {
  202. for (const auto &peer_in_anchor : out_anchor->GetPeerAnchors()) {
  203. const auto &peer_node = peer_in_anchor->GetOwnerNode();
  204. if (peer_node == nullptr) {
  205. continue;
  206. }
  207. out_nodes_map[peer_node->GetName()] = peer_node;
  208. }
  209. }
  210. const auto &out_control_anchor = variable_ref->GetOutControlAnchor();
  211. GE_CHECK_NOTNULL(out_control_anchor);
  212. for (const auto &peer_in_control_anchor : out_control_anchor->GetPeerInControlAnchors()) {
  213. const auto &peer_node = peer_in_control_anchor->GetOwnerNode();
  214. if (peer_node == nullptr) {
  215. continue;
  216. }
  217. if (out_nodes_map.find(peer_node->GetName()) != out_nodes_map.end()) {
  218. auto ret = ge::GraphUtils::RemoveEdge(out_control_anchor, peer_in_control_anchor);
  219. if (ret != SUCCESS) {
  220. REPORT_CALL_ERROR("E19999", "Remove control edge between op:%s(%s) and op:%s(%s) failed",
  221. variable_ref->GetName().c_str(), variable_ref->GetType().c_str(),
  222. peer_node->GetName().c_str(), peer_node->GetType().c_str());
  223. GELOGE(FAILED, "Remove control edge between variable ref node[%s] and ref node's peer node[%s] failed",
  224. variable_ref->GetName().c_str(), peer_node->GetName().c_str());
  225. return FAILED;
  226. }
  227. }
  228. }
  229. return SUCCESS;
  230. }
  231. } // namespace ge

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