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.

data_pass.cc 8.8 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "graph/passes/data_pass.h"
  17. #include <string>
  18. #include "framework/common/debug/ge_log.h"
  19. #include "graph/utils/graph_utils.h"
  20. #include "register/op_registry.h"
  21. namespace ge {
  22. namespace {
  23. Status MappingSubgraphInput(const ComputeGraphPtr &graph,
  24. const std::function<Status(int data_index, int &parent_index)> &input) {
  25. for (const auto &node : graph->GetDirectNode()) {
  26. if (node->GetType() != DATA) {
  27. continue;
  28. }
  29. int index = -1;
  30. if (!AttrUtils::GetInt(node->GetOpDesc(), "index", index)) {
  31. GELOGE(FAILED, "Failed to get index from data[%s]", node->GetName().c_str());
  32. return FAILED;
  33. }
  34. GELOGI("Get index %d from data[%s]", index, node->GetName().c_str());
  35. int parent_index = -1;
  36. auto ret = input(index, parent_index);
  37. if (ret != SUCCESS) {
  38. GELOGE(FAILED, "Failed to get parent index from data index %d, error code %u", index, ret);
  39. return FAILED;
  40. }
  41. GELOGI("Generate subgraph input map for subgraph %s, data index %d, parent index %d",
  42. graph->GetName().c_str(), index, parent_index);
  43. if (!AttrUtils::SetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  44. GELOGE(FAILED, "Failed to set parent index for node %s", node->GetName().c_str());
  45. return FAILED;
  46. }
  47. }
  48. return SUCCESS;
  49. }
  50. Status MappingSubgraphOutput(const ComputeGraphPtr &graph,
  51. const std::function<Status(int retval_index, int &parent_index)> &output) {
  52. const auto &output_node = graph->FindFirstNodeMatchType(NETOUTPUT);
  53. if (output_node == nullptr) {
  54. return SUCCESS;
  55. }
  56. const auto &op_desc = output_node->GetOpDesc();
  57. GE_CHECK_NOTNULL(op_desc);
  58. for (auto index = 0; index < op_desc->GetInputsSize(); ++index) {
  59. int parent_index = -1;
  60. auto ret = output(index, parent_index);
  61. if (ret != SUCCESS) {
  62. GELOGE(FAILED, "Failed to get parent index for net output index %d, error code %u", index, ret);
  63. return FAILED;
  64. }
  65. GELOGI("Generate subgraph output map for subgraph %s, index %d, parent index %d",
  66. graph->GetName().c_str(), index, parent_index);
  67. if (parent_index == -1) {
  68. continue;
  69. }
  70. GeTensorDescPtr tensor = op_desc->MutableInputDesc(index);
  71. GE_CHECK_NOTNULL(tensor);
  72. if (!AttrUtils::SetInt(tensor, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  73. GELOGE(FAILED, "Failed to set parent index for graph %s", graph->GetName().c_str());
  74. return FAILED;
  75. }
  76. }
  77. return SUCCESS;
  78. }
  79. Status MappingSubgraphIndex(const ComputeGraphPtr &graph,
  80. const std::function<Status(int data_index, int &parent_index)> &input,
  81. const std::function<Status(int retval_index, int &parent_index)> &output) {
  82. if (MappingSubgraphInput(graph, input) != SUCCESS) {
  83. GELOGE(FAILED, "Failed to mapping subgraph input for graph: %s", graph->GetName().c_str());
  84. return FAILED;
  85. }
  86. if (MappingSubgraphOutput(graph, output) != SUCCESS) {
  87. GELOGE(FAILED, "Failed to mapping subgraph output for graph: %s", graph->GetName().c_str());
  88. return FAILED;
  89. }
  90. return SUCCESS;
  91. }
  92. Status MappingSubgraphIndex(const ComputeGraphPtr &graph,
  93. const std::function<Status(int data_index)> &input,
  94. const std::function<Status(int retval_index)> &output) {
  95. GE_CHECK_NOTNULL(graph);
  96. GE_CHECK_NOTNULL(input);
  97. GE_CHECK_NOTNULL(output);
  98. return MappingSubgraphIndex(graph,
  99. [&](int i, int &o) -> Status {
  100. o = input(i);
  101. return SUCCESS;
  102. },
  103. [&](int i, int &o) -> Status {
  104. o = output(i);
  105. return SUCCESS;
  106. });
  107. }
  108. Status ParseSubgraphPostFnCase(const string &subgraph_name, const ComputeGraphPtr &graph) {
  109. return MappingSubgraphIndex(graph,
  110. [](int data_index) { return data_index + 1; },
  111. [](int retval_index) { return retval_index; });
  112. }
  113. Status ParseSubgraphPostFnIf(const string &subgraph_name, const ComputeGraphPtr &graph) {
  114. return MappingSubgraphIndex(graph,
  115. [](int data_index) { return data_index + 1; },
  116. [](int retval_index) { return retval_index; });
  117. }
  118. Status ParseSubgraphPostFnWhile(const string &subgraph_name, const ComputeGraphPtr &graph) {
  119. return MappingSubgraphIndex(graph,
  120. [](int data_index) { return data_index; },
  121. [&](int retval_index) { return (subgraph_name == "cond") ? -1 : retval_index; });
  122. }
  123. Status ParseSubgraphPostFnFor(const string &subgraph_name, const ComputeGraphPtr &graph) {
  124. return MappingSubgraphIndex(graph,
  125. [](int data_index) { return (data_index == 0) ? 0 : data_index + 2; },
  126. [](int retval_index) { return retval_index; });
  127. }
  128. Status ParseSubgraphPostFnPartitionedCall(const string &subgraph_name, const ComputeGraphPtr &graph) {
  129. return MappingSubgraphIndex(graph,
  130. [](int data_index) { return data_index; },
  131. [](int retval_index) { return retval_index; });
  132. }
  133. }
  134. Status DataPass::PostParseSubgraph(const ComputeGraphPtr &graph, const string &ir_name, const NodePtr &parent_node) {
  135. using ParseSubgraphFunc = std::function<Status(const string &subgraph_name, const ComputeGraphPtr &graph)>;
  136. const static std::map<string, ParseSubgraphFunc> subgraph_handle = {
  137. {FOR, ParseSubgraphPostFnFor},
  138. {CASE, ParseSubgraphPostFnCase},
  139. {IF, ParseSubgraphPostFnIf},
  140. {_IF, ParseSubgraphPostFnIf},
  141. {STATELESSIF, ParseSubgraphPostFnIf},
  142. {WHILE, ParseSubgraphPostFnWhile},
  143. {_WHILE, ParseSubgraphPostFnWhile},
  144. {STATELESSWHILE, ParseSubgraphPostFnWhile},
  145. {PARTITIONEDCALL, ParseSubgraphPostFnPartitionedCall},
  146. {STATEFULPARTITIONEDCALL, ParseSubgraphPostFnPartitionedCall}
  147. };
  148. auto post_func_it = subgraph_handle.find(parent_node->GetType());
  149. if (post_func_it == subgraph_handle.end()) {
  150. GELOGE(FAILED, "The subgraph post func for node %s type %s is null.",
  151. parent_node->GetName().c_str(), parent_node->GetType().c_str());
  152. return FAILED;
  153. }
  154. if (post_func_it->second(ir_name, graph) != SUCCESS) {
  155. GELOGE(FAILED, "Failed to post process subgraph %s on node %s type %s",
  156. graph->GetName().c_str(), parent_node->GetName().c_str(), parent_node->GetType().c_str());
  157. return FAILED;
  158. }
  159. return SUCCESS;
  160. }
  161. Status DataPass::Run(ComputeGraphPtr compute_graph) {
  162. GE_CHECK_NOTNULL(compute_graph);
  163. if (compute_graph->GetParentNode() == nullptr) { // for subgraph post process.
  164. return SUCCESS;
  165. }
  166. for (const NodePtr &node : compute_graph->GetDirectNode()) {
  167. GE_CHECK_NOTNULL(node->GetOpDesc());
  168. if (node->GetType() == DATA) {
  169. uint32_t parent_index = 0;
  170. if (!AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  171. break; // parent_index not set, Graph from IR.
  172. }
  173. return SUCCESS; // Graph from Parser.
  174. }
  175. }
  176. std::string subgraph_name;
  177. const auto &parent_node = compute_graph->GetParentNode();
  178. GE_CHECK_NOTNULL(parent_node->GetOpDesc());
  179. auto func_desc = parent_node->GetOpDesc();
  180. GE_CHK_STATUS_RET(func_desc->GetSubgraphNameByInstanceName(compute_graph->GetName(), subgraph_name),
  181. "Subgraph: %s get subgraph name failed.", compute_graph->GetName().c_str());
  182. GELOGI("Post process for subgraph %s, Subgraph name: %s, Parent name: %s, Parent type: %s.",
  183. compute_graph->GetName().c_str(), subgraph_name.c_str(), parent_node->GetName().c_str(),
  184. parent_node->GetType().c_str());
  185. const auto &parent_graph = compute_graph->GetParentGraph();
  186. GE_CHECK_NOTNULL(parent_graph);
  187. for (const NodePtr &node : compute_graph->GetDirectNode()) {
  188. GE_CHECK_NOTNULL(node->GetOpDesc());
  189. if ((node->GetType() == VARIABLE) || (node->GetType() == VARIABLEV2) || (node->GetType() == NETOUTPUT)) {
  190. continue;
  191. }
  192. node->GetOpDesc()->SetName(parent_node->GetName() + "_" + compute_graph->GetName() + "/" + node->GetName());
  193. }
  194. return PostParseSubgraph(compute_graph, subgraph_name, parent_node);
  195. }
  196. } // namespace ge

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