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

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