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

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