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

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

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