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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. GELOGE(GE_GRAPH_EMPTY_SUBGRAPH, "Subgraph not found, name: %s", name.c_str());
  93. return GE_GRAPH_EMPTY_SUBGRAPH;
  94. }
  95. auto &data_nodes = graph_nodes[subgraph];
  96. for (auto &data : subgraph->GetDirectNode()) {
  97. if (data->GetType() != DATA) {
  98. continue;
  99. }
  100. uint32_t parent_index = 0;
  101. if (!AttrUtils::GetInt(data->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  102. GELOGE(FAILED, "Parent index not found, name: %s", data->GetName().c_str());
  103. return FAILED;
  104. }
  105. data_nodes[parent_index] = data;
  106. GELOGD("%s, Parent index: %u, Data: %s", subgraph->GetName().c_str(), parent_index, data->GetName().c_str());
  107. }
  108. }
  109. return SUCCESS;
  110. }
  111. ///
  112. /// @ingroup ge
  113. /// @brief Update Case input Tensor.
  114. /// @param [in] graph_nodes: Data groups of subgraph.
  115. /// @param [in] func_node: functional Node of Case.
  116. /// @param [in] parent_index: parent index for update.
  117. /// @param [in] unused_num: unused args num.
  118. /// @return 0: SUCCESS / others: FAILED
  119. ///
  120. Status UnusedArgsCleanPass::UpdateInputTensor(const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  121. const NodePtr &func_node, uint32_t parent_index, uint32_t unused_num) {
  122. if (unused_num == 0) {
  123. return SUCCESS;
  124. }
  125. uint32_t update_index = parent_index - unused_num;
  126. for (const auto &item : graph_nodes) {
  127. const auto &nodes = item.second;
  128. const auto it = nodes.find(parent_index);
  129. if (it == nodes.end()) { // not used.
  130. continue;
  131. }
  132. const auto data = it->second;
  133. if (!AttrUtils::SetInt(data->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, update_index)) {
  134. GELOGE(FAILED, "Set parent index failed, name: %s", data->GetName().c_str());
  135. return FAILED;
  136. }
  137. }
  138. const auto &new_anchor = func_node->GetInDataAnchor(update_index);
  139. const auto &old_anchor = func_node->GetInDataAnchor(parent_index);
  140. const auto &out_anchor = old_anchor->GetPeerOutAnchor();
  141. const auto &out_node = out_anchor->GetOwnerNode();
  142. const auto &func_desc = func_node->GetOpDesc();
  143. const auto &old_desc = func_desc->GetInputDesc(parent_index);
  144. (void)func_desc->UpdateInputDesc(update_index, old_desc);
  145. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(out_anchor, new_anchor), "Add edge failed");
  146. GELOGI("Add edge success, func node: %s, node: %s, parent index: %u, update index: %u",
  147. func_node->GetName().c_str(), out_node->GetName().c_str(), parent_index, update_index);
  148. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, old_anchor), "Remove edge failed");
  149. GELOGI("Remove edge success, func node: %s, node: %s", func_node->GetName().c_str(), out_node->GetName().c_str());
  150. return SUCCESS;
  151. }
  152. ///
  153. /// @ingroup ge
  154. /// @brief Remove Case input Tensor.
  155. /// @param [in] graph_nodes: Data groups of subgraph.
  156. /// @param [in] func_node: functional Node of Case.
  157. /// @param [in] parent_index: parent index for remove.
  158. /// @return 0: SUCCESS / others: FAILED
  159. ///
  160. Status UnusedArgsCleanPass::RemoveInputTensor(const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  161. const NodePtr &func_node, uint32_t parent_index) {
  162. for (const auto &item : graph_nodes) {
  163. const auto &graph = item.first;
  164. const auto &nodes = item.second;
  165. const auto it = nodes.find(parent_index);
  166. if (it == nodes.end()) { // not used.
  167. continue;
  168. }
  169. const auto &data = it->second;
  170. GE_CHK_GRAPH_STATUS_RET(graph->RemoveNode(data), "Remove node failed: %s", data->GetName().c_str());
  171. GELOGI("Remove Node: %s %s", graph->GetName().c_str(), data->GetName().c_str());
  172. }
  173. const auto &old_anchor = func_node->GetInDataAnchor(parent_index);
  174. const auto &out_anchor = old_anchor->GetPeerOutAnchor();
  175. const auto &out_node = out_anchor->GetOwnerNode();
  176. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, old_anchor), "Remove edge failed");
  177. GELOGI("Remove edge: %s %s", out_node->GetName().c_str(), func_node->GetName().c_str());
  178. return SUCCESS;
  179. }
  180. } // namespace ge

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