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

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

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