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.

unused_args_clean_pass.cc 8.5 kB

5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "unused_args_clean_pass.h"
  17. #include "graph/utils/node_utils.h"
  18. namespace ge {
  19. Status UnusedArgsCleanPass::Run(ComputeGraphPtr graph) {
  20. GE_CHECK_NOTNULL(graph);
  21. if (graph->GetParentGraph() != nullptr) {
  22. GELOGD("Subgraph %s skip the UnusedArgsCleanPass", graph->GetName().c_str());
  23. return SUCCESS;
  24. }
  25. GELOGD("Begin to run Unused args clean on graph: %s", graph->GetName().c_str());
  26. for (const auto &node : graph->GetDirectNode()) {
  27. if (node->GetType() != CASE) {
  28. continue;
  29. }
  30. const auto &func_desc = node->GetOpDesc();
  31. map<ComputeGraphPtr, map<uint32_t, NodePtr>> graph_nodes;
  32. if (ClassifyDataNodes(graph, func_desc, graph_nodes) != SUCCESS) {
  33. return FAILED;
  34. }
  35. // {subgraph0, {{0, Data}, {1, Data}, {2, Data}, {3, Data}, ..., {n, Data}}}
  36. // {subgraph1, {{0, Data}, {1, Data}, {2, Data}, {3, Data}, ..., {n, Data}}}
  37. // {subgraph2, {{0, Data}, {1, Data}, {2, Data}, {3, Data}, ..., {n, Data}}}
  38. uint32_t unused_args_num = 0;
  39. uint32_t inputs_args_num = func_desc->GetInputsSize();
  40. for (size_t i = 1; i < inputs_args_num; ++i) {
  41. if (UnusedInputTensor(graph_nodes, node, i)) {
  42. unused_args_num++;
  43. } else {
  44. (void)UpdateInputTensor(graph_nodes, node, i, unused_args_num);
  45. }
  46. }
  47. (void)NodeUtils::RemoveInputAnchor(node, inputs_args_num - unused_args_num);
  48. }
  49. return SUCCESS;
  50. }
  51. ///
  52. /// @ingroup ge
  53. /// @brief Create nodes for root graph.
  54. /// @param [in] graph_nodes: Data groups of subgraph.
  55. /// @param [in] func_node: functional Node of Case.
  56. /// @param [in] parent_index: parent index for check.
  57. /// @return true: unused / false: used
  58. ///
  59. bool UnusedArgsCleanPass::UnusedInputTensor(const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  60. const NodePtr &func_node, uint32_t parent_index) {
  61. for (const auto &item : graph_nodes) {
  62. const auto &nodes = item.second;
  63. const auto it = nodes.find(parent_index);
  64. if (it == nodes.end()) { // not used.
  65. continue;
  66. }
  67. const auto &data = it->second;
  68. for (const auto out_anchor : data->GetAllOutAnchors()) {
  69. for (const auto in_anchor : out_anchor->GetPeerAnchors()) {
  70. if (in_anchor == nullptr) {
  71. continue;
  72. }
  73. return false;
  74. }
  75. }
  76. }
  77. return RemoveInputTensor(graph_nodes, func_node, parent_index) == SUCCESS;
  78. }
  79. ///
  80. /// @ingroup ge
  81. /// @brief Get all Data nodes for all subgraph.
  82. /// @param [in] graph: Root compute graph.
  83. /// @param [in] func_desc: functional OpDesc of Case.
  84. /// @param [out] graph_nodes: Data groups of subgraph.
  85. /// @return 0: SUCCESS / others: FAILED
  86. ///
  87. Status UnusedArgsCleanPass::ClassifyDataNodes(const ComputeGraphPtr &graph, const OpDescPtr &func_desc,
  88. map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes) {
  89. for (const auto &name : func_desc->GetSubgraphInstanceNames()) {
  90. const auto &subgraph = graph->GetSubgraph(name);
  91. if (subgraph == nullptr) {
  92. REPORT_CALL_ERROR("E19999", "Get subgraph from graph:%s by name:%s failed",
  93. graph->GetName().c_str(), name.c_str());
  94. GELOGE(GE_GRAPH_EMPTY_SUBGRAPH, "Subgraph not found, name: %s", name.c_str());
  95. return GE_GRAPH_EMPTY_SUBGRAPH;
  96. }
  97. auto &data_nodes = graph_nodes[subgraph];
  98. for (auto &data : subgraph->GetDirectNode()) {
  99. if (data->GetType() != DATA) {
  100. continue;
  101. }
  102. uint32_t parent_index = 0;
  103. if (!AttrUtils::GetInt(data->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  104. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  105. data->GetName().c_str(), data->GetType().c_str());
  106. GELOGE(FAILED, "Parent index not found, name: %s", data->GetName().c_str());
  107. return FAILED;
  108. }
  109. data_nodes[parent_index] = data;
  110. GELOGD("%s, Parent index: %u, Data: %s", subgraph->GetName().c_str(), parent_index, data->GetName().c_str());
  111. }
  112. }
  113. return SUCCESS;
  114. }
  115. ///
  116. /// @ingroup ge
  117. /// @brief Update Case input Tensor.
  118. /// @param [in] graph_nodes: Data groups of subgraph.
  119. /// @param [in] func_node: functional Node of Case.
  120. /// @param [in] parent_index: parent index for update.
  121. /// @param [in] unused_num: unused args num.
  122. /// @return 0: SUCCESS / others: FAILED
  123. ///
  124. Status UnusedArgsCleanPass::UpdateInputTensor(const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  125. const NodePtr &func_node, uint32_t parent_index, uint32_t unused_num) {
  126. if (unused_num == 0) {
  127. return SUCCESS;
  128. }
  129. uint32_t update_index = parent_index - unused_num;
  130. for (const auto &item : graph_nodes) {
  131. const auto &nodes = item.second;
  132. const auto it = nodes.find(parent_index);
  133. if (it == nodes.end()) { // not used.
  134. continue;
  135. }
  136. const auto data = it->second;
  137. if (!AttrUtils::SetInt(data->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, update_index)) {
  138. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  139. data->GetName().c_str(), data->GetType().c_str());
  140. GELOGE(FAILED, "Set parent index failed, name: %s", data->GetName().c_str());
  141. return FAILED;
  142. }
  143. }
  144. const auto &new_anchor = func_node->GetInDataAnchor(update_index);
  145. const auto &old_anchor = func_node->GetInDataAnchor(parent_index);
  146. const auto &out_anchor = old_anchor->GetPeerOutAnchor();
  147. const auto &out_node = out_anchor->GetOwnerNode();
  148. const auto &func_desc = func_node->GetOpDesc();
  149. const auto &old_desc = func_desc->GetInputDesc(parent_index);
  150. (void)func_desc->UpdateInputDesc(update_index, old_desc);
  151. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(out_anchor, new_anchor), "Add edge failed");
  152. GELOGI("Add edge success, func node: %s, node: %s, parent index: %u, update index: %u",
  153. func_node->GetName().c_str(), out_node->GetName().c_str(), parent_index, update_index);
  154. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, old_anchor), "Remove edge failed");
  155. GELOGI("Remove edge success, func node: %s, node: %s", func_node->GetName().c_str(), out_node->GetName().c_str());
  156. return SUCCESS;
  157. }
  158. ///
  159. /// @ingroup ge
  160. /// @brief Remove Case input Tensor.
  161. /// @param [in] graph_nodes: Data groups of subgraph.
  162. /// @param [in] func_node: functional Node of Case.
  163. /// @param [in] parent_index: parent index for remove.
  164. /// @return 0: SUCCESS / others: FAILED
  165. ///
  166. Status UnusedArgsCleanPass::RemoveInputTensor(const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  167. const NodePtr &func_node, uint32_t parent_index) {
  168. for (const auto &item : graph_nodes) {
  169. const auto &graph = item.first;
  170. const auto &nodes = item.second;
  171. const auto it = nodes.find(parent_index);
  172. if (it == nodes.end()) { // not used.
  173. continue;
  174. }
  175. const auto &data = it->second;
  176. GE_CHK_GRAPH_STATUS_RET(graph->RemoveNode(data), "Remove node failed: %s", data->GetName().c_str());
  177. GELOGI("Remove Node: %s %s", graph->GetName().c_str(), data->GetName().c_str());
  178. }
  179. const auto &old_anchor = func_node->GetInDataAnchor(parent_index);
  180. const auto &out_anchor = old_anchor->GetPeerOutAnchor();
  181. const auto &out_node = out_anchor->GetOwnerNode();
  182. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, old_anchor), "Remove edge failed");
  183. GELOGI("Remove edge: %s %s", out_node->GetName().c_str(), func_node->GetName().c_str());
  184. if (out_node->GetInDataNodes().size() == 0 && out_node->GetOutAllNodes().size() == 0) {
  185. GE_CHK_GRAPH_STATUS_RET(out_node->GetOwnerComputeGraph()->RemoveNode(out_node), "Remove node failed: %s",
  186. out_node->GetName().c_str());
  187. }
  188. return SUCCESS;
  189. }
  190. } // namespace ge

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