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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. GELOGE(FAILED, "Ref node of RefIdentity[%s] not found", node->GetName().c_str());
  28. return FAILED);
  29. CHECK_FALSE_EXEC(DealNoOutputRef(ref_node, node, input_index, graph) == SUCCESS,
  30. GELOGE(FAILED, "Ref identity [%s] delete failed", node->GetName().c_str());
  31. return FAILED);
  32. }
  33. return SUCCESS;
  34. }
  35. NodePtr RefIdentityDeleteOpPass::GetRefNode(const NodePtr &node, int &input_index) {
  36. OutDataAnchorPtr out_anchor = node->GetOutDataAnchor(0);
  37. CHECK_FALSE_EXEC(out_anchor != nullptr, return nullptr);
  38. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  39. CHECK_FALSE_EXEC(peer_in_anchor != nullptr, continue);
  40. auto peer_node = peer_in_anchor->GetOwnerNode();
  41. CHECK_FALSE_EXEC(peer_node != nullptr, continue);
  42. const auto &peer_op_desc = peer_node->GetOpDesc();
  43. CHECK_FALSE_EXEC(peer_op_desc != nullptr, return nullptr);
  44. const auto &peer_input_desc = peer_op_desc->GetInputDescPtr(static_cast<uint32_t>(peer_in_anchor->GetIdx()));
  45. if (!peer_input_desc->GetRefPortIndex().empty()) {
  46. input_index = peer_in_anchor->GetIdx();
  47. return peer_node;
  48. }
  49. }
  50. return nullptr;
  51. }
  52. Status RefIdentityDeleteOpPass::DealNoOutputRef(const NodePtr &node, const NodePtr &ref_identity, int input_index,
  53. const ComputeGraphPtr &graph) {
  54. NodePtr first_node = nullptr;
  55. NodePtr variable_ref = GetVariableRef(node, ref_identity, first_node);
  56. if (variable_ref == nullptr) {
  57. GELOGE(FAILED, "[RefIdentityDeleteOpPass]Can not find variable ref for %s:%d", node->GetName().c_str(),
  58. input_index);
  59. return FAILED;
  60. }
  61. if (first_node->GetName() != variable_ref->GetName()) {
  62. // Remove the control edge between ref node and variable ref
  63. // Add a control edge between ref node and trans node
  64. // +-----------+ +-----------+
  65. // +---------+RefIdentity| +-----------+RefIdentity|
  66. // | +-----+-----+ | +-----+-----+
  67. // | | | |
  68. // | v | v
  69. // +-----v-----+ +----+----+ +-----v-----+ +----+----+
  70. // | TransNode | | RefNode | ==> | TransNode +<--C--+ RefNode |
  71. // +-----+-----+ +----+----+ +-----+-----+ +---------+
  72. // | | |
  73. // v C v
  74. // +-----+-----+ | +-----+-----+
  75. // |VariableRef+<--------+ |VariableRef|
  76. // +-----------+ +-----------+
  77. auto ret = ge::GraphUtils::AddEdge(node->GetOutControlAnchor(), first_node->GetInControlAnchor());
  78. if (ret != SUCCESS) {
  79. GELOGE(FAILED, "Add control edge between ref node and trans node failed");
  80. return FAILED;
  81. }
  82. ret = ge::GraphUtils::RemoveEdge(node->GetOutControlAnchor(), variable_ref->GetInControlAnchor());
  83. if (ret != SUCCESS) {
  84. GELOGE(FAILED, "Remove control edge between ref node and its peer node failed");
  85. return FAILED;
  86. }
  87. } else {
  88. // +-----------+ +-----------+
  89. // +-----------+RefIdentity| +-----------+RefIdentity|
  90. // | +-----+-----+ | +-----+-----+
  91. // | | | |
  92. // | v | v
  93. // +-----v-----+ +----+----+ +-----v-----+ +----+----+
  94. // |VariableRef+<--C--+ RefNode | ==> |VariableRef+<--C--+ RefNode |
  95. // +-----+-----+ +----+----+ +-----------+ +----+----+
  96. // | | |
  97. // | v v
  98. // | +---+----+ +---+----+
  99. // +-----C------>+ | | |
  100. // +--------+ +--------+
  101. auto ret = RemoveUselessControlEdge(node, variable_ref);
  102. if (ret != SUCCESS) {
  103. GELOGE(FAILED, "Remove useless control edge failed.");
  104. return FAILED;
  105. }
  106. }
  107. // remove ref identity
  108. if (GraphUtils::IsolateNode(ref_identity, {0}) != GRAPH_SUCCESS) {
  109. GELOGE(INTERNAL_ERROR, "Isolate removed node: %s, type: %s failed", ref_identity->GetName().c_str(),
  110. variable_ref->GetType().c_str());
  111. return FAILED;
  112. }
  113. if (GraphUtils::RemoveNodeWithoutRelink(graph, ref_identity) != GRAPH_SUCCESS) {
  114. GELOGE(INTERNAL_ERROR, "Remove node: %s, type: %s without relink failed", ref_identity->GetName().c_str(),
  115. ref_identity->GetType().c_str());
  116. return FAILED;
  117. }
  118. return SUCCESS;
  119. }
  120. ge::NodePtr RefIdentityDeleteOpPass::GetVariableRef(const NodePtr &ref, const NodePtr &ref_identity,
  121. NodePtr &first_node) {
  122. const auto &ref_identity_out_anchor = ref_identity->GetOutDataAnchor(0);
  123. if (ref_identity_out_anchor == nullptr) {
  124. return nullptr;
  125. }
  126. for (auto &peer_in_anchor : ref_identity_out_anchor->GetPeerInDataAnchors()) {
  127. const auto &peer_node = peer_in_anchor->GetOwnerNode();
  128. if (peer_node == nullptr || peer_node->GetName() == ref->GetName()) {
  129. continue;
  130. }
  131. // DFS to find variable ref node.
  132. std::stack<NodePtr> nodes_to_check;
  133. nodes_to_check.push(peer_node);
  134. GELOGI("[RefIdentityDeleteOpPass]Start to search variable ref node from %s.", peer_node->GetName().c_str());
  135. NodePtr cur_node = nullptr;
  136. while (!nodes_to_check.empty()) {
  137. cur_node = nodes_to_check.top();
  138. nodes_to_check.pop();
  139. const auto &type = cur_node->GetType();
  140. if (type == VARIABLE && CheckControlEdge(ref, cur_node)) {
  141. // Target variable ref node found.
  142. GELOGI("[RefIdentityDeleteOpPass]variable ref node[%s] found.", cur_node->GetName().c_str());
  143. first_node = peer_node;
  144. return cur_node;
  145. }
  146. int data_index = TransOpUtil::GetTransOpDataIndex(type);
  147. if (data_index < 0) {
  148. GELOGI("[RefIdentityDeleteOpPass]Find node[%s] that is not trans op[%s], stop to search its output.",
  149. cur_node->GetName().c_str(), type.c_str());
  150. continue;
  151. }
  152. const auto &cur_out_anchor = cur_node->GetOutDataAnchor(0);
  153. if (cur_out_anchor == nullptr) {
  154. GELOGI("[RefIdentityDeleteOpPass]Get out anchor of [%s] failed, stop to search its output.",
  155. cur_node->GetName().c_str());
  156. continue;
  157. }
  158. for (const auto &cur_peer_in_anchor : cur_out_anchor->GetPeerInDataAnchors()) {
  159. const auto &cur_peer_node = cur_peer_in_anchor->GetOwnerNode();
  160. if (cur_peer_node == nullptr) {
  161. continue;
  162. }
  163. nodes_to_check.push(cur_peer_node);
  164. }
  165. }
  166. GELOGI("[RefIdentityDeleteOpPass]Can not find variable ref node from %s.", peer_node->GetName().c_str());
  167. }
  168. GELOGI("[RefIdentityDeleteOpPass]Can not find variable ref node, return nullptr.");
  169. return nullptr;
  170. }
  171. bool RefIdentityDeleteOpPass::CheckControlEdge(const NodePtr &ref, const NodePtr &variable_ref) {
  172. const auto &control_out_anchor = ref->GetOutControlAnchor();
  173. if (control_out_anchor == nullptr) {
  174. return false;
  175. }
  176. const string &variable_ref_name = variable_ref->GetName();
  177. for (const auto &peer_in_control_anchor : control_out_anchor->GetPeerInControlAnchors()) {
  178. const auto &node = peer_in_control_anchor->GetOwnerNode();
  179. if (node != nullptr && node->GetName() == variable_ref_name) {
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. Status RefIdentityDeleteOpPass::RemoveUselessControlEdge(const NodePtr &ref, const NodePtr &variable_ref) {
  186. map<string, NodePtr> out_nodes_map;
  187. for (const auto &out_anchor : ref->GetAllOutDataAnchors()) {
  188. for (const auto &peer_in_anchor : out_anchor->GetPeerAnchors()) {
  189. const auto &peer_node = peer_in_anchor->GetOwnerNode();
  190. if (peer_node == nullptr) {
  191. continue;
  192. }
  193. out_nodes_map[peer_node->GetName()] = peer_node;
  194. }
  195. }
  196. const auto &out_control_anchor = variable_ref->GetOutControlAnchor();
  197. GE_CHECK_NOTNULL(out_control_anchor);
  198. for (const auto &peer_in_control_anchor : out_control_anchor->GetPeerInControlAnchors()) {
  199. const auto &peer_node = peer_in_control_anchor->GetOwnerNode();
  200. if (peer_node == nullptr) {
  201. continue;
  202. }
  203. if (out_nodes_map.find(peer_node->GetName()) != out_nodes_map.end()) {
  204. auto ret = ge::GraphUtils::RemoveEdge(out_control_anchor, peer_in_control_anchor);
  205. if (ret != SUCCESS) {
  206. GELOGE(FAILED, "Remove control edge between variable ref node[%s] and ref node's peer node[%s] failed",
  207. variable_ref->GetName().c_str(), peer_node->GetName().c_str());
  208. return FAILED;
  209. }
  210. }
  211. }
  212. return SUCCESS;
  213. }
  214. } // namespace ge

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