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.5 kB

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

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