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

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

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