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.

input_output_connection_identify_pass.cc 8.9 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/input_output_connection_identify_pass.h"
  17. #include <map>
  18. #include <memory>
  19. #include <sstream>
  20. #include <string>
  21. #include <vector>
  22. #include "common/ge/ge_util.h"
  23. #include "common/ge_inner_error_codes.h"
  24. #include "framework/common/debug/ge_log.h"
  25. #include "graph/debug/ge_attr_define.h"
  26. #include "graph/utils/graph_utils.h"
  27. #include "graph/utils/node_utils.h"
  28. using std::map;
  29. using std::string;
  30. using std::vector;
  31. namespace ge {
  32. namespace {
  33. inline bool IsDataOp(const std::string &node_type) {
  34. return (node_type == DATA_TYPE) || (node_type == AIPP_DATA_TYPE) || (node_type == ANN_DATA_TYPE);
  35. }
  36. } // namespace
  37. Status InputOutputConnectionIdentifyPass::Run(ComputeGraphPtr graph) {
  38. if (graph == nullptr) {
  39. REPORT_INNER_ERROR("E19999", "Param graph is nullptr, check invalid");
  40. GELOGE(PARAM_INVALID, "Input param graph is null, skip identification of nodes that connect to input and output.");
  41. return PARAM_INVALID;
  42. }
  43. if (graph->GetParentGraph() != nullptr) {
  44. REPORT_INNER_ERROR("E19999", "Param graph's parent graph is nullptr, "
  45. "check invalid");
  46. GELOGD("Current graph %s is a subgraph, skip identification of nodes that connect to input and output.",
  47. graph->GetName().c_str());
  48. return SUCCESS;
  49. }
  50. GELOGD("Start to identify nodes that connect to input and output.");
  51. if (graph->TopologicalSorting() != GRAPH_SUCCESS) {
  52. REPORT_CALL_ERROR("E19999", "Topological Sorting graph:%s failed",
  53. graph->GetName().c_str());
  54. GELOGE(INTERNAL_ERROR, "Graph topological sort failed.");
  55. return INTERNAL_ERROR;
  56. }
  57. if (GraphUtils::GetRefMapping(graph, symbol_to_anchors_, anchor_to_symbol_) != GRAPH_SUCCESS) {
  58. REPORT_CALL_ERROR("E19999", "Get ref mapping from graph:%s failed",
  59. graph->GetName().c_str());
  60. GELOGE(INTERNAL_ERROR, "Get ref-mapping for graph %s failed.", graph->GetName().c_str());
  61. return INTERNAL_ERROR;
  62. }
  63. map<NodePtr, vector<uint32_t>> connect_input_node_idx_map;
  64. map<NodePtr, vector<uint32_t>> connect_output_node_idx_map;
  65. Status status = SUCCESS;
  66. for (const NodePtr &node : graph->GetDirectNode()) {
  67. // Not only node type Data is determined.
  68. if (IsDataOp(node->GetType())) {
  69. GELOGD("Find nodes that connect to root graph input node: %s.", node->GetName().c_str());
  70. status = ProcessInputNode(node, connect_input_node_idx_map, connect_output_node_idx_map);
  71. if (status != SUCCESS) {
  72. GELOGE(status, "Failed to process nodes that connect to input node: %s.", node->GetName().c_str());
  73. return status;
  74. }
  75. }
  76. if (node->GetType() == NETOUTPUT) {
  77. GELOGD("Find nodes that connect to root graph output node: %s.", node->GetName().c_str());
  78. status = ProcessOutputNode(node, connect_input_node_idx_map, connect_output_node_idx_map);
  79. if (status != SUCCESS) {
  80. GELOGE(status, "Failed to process nodes that connect to output node: %s.", node->GetName().c_str());
  81. return status;
  82. }
  83. }
  84. }
  85. status = SetNodeAttrOfConnectingInputOutput(connect_input_node_idx_map, connect_output_node_idx_map);
  86. if (status != SUCCESS) {
  87. GELOGE(status, "Failed to set attr for nodes that connect to input and output.");
  88. return status;
  89. }
  90. GELOGD("Success to identify nodes that connect to input and output.");
  91. return SUCCESS;
  92. }
  93. Status InputOutputConnectionIdentifyPass::ProcessInputNode(const NodePtr &node,
  94. map<NodePtr, vector<uint32_t>> &connect_input_node_idx,
  95. map<NodePtr, vector<uint32_t>> &connect_output_node_idx) {
  96. GE_CHECK_NOTNULL(node);
  97. for (const auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  98. // The return ptr of GetAllOutDataAnchors is always valid.
  99. auto anchor_iter = anchor_to_symbol_.find(NodeIndexIO(node, out_data_anchor->GetIdx(), kOut).ToString());
  100. if (anchor_iter == anchor_to_symbol_.end()) {
  101. GELOGW("Current node: %s out_data_anchor: %d is invalid, can not find related symbol.", node->GetName().c_str(),
  102. out_data_anchor->GetIdx());
  103. continue;
  104. }
  105. const string &symbol = anchor_iter->second;
  106. auto status = UpdateNodeIdxMap(symbol, connect_input_node_idx, connect_output_node_idx);
  107. if (status != SUCCESS) {
  108. GELOGE(status, "Failed to update node anchor_index map.");
  109. return status;
  110. }
  111. }
  112. return SUCCESS;
  113. }
  114. Status InputOutputConnectionIdentifyPass::UpdateNodeIdxMap(const string &symbol_string,
  115. map<NodePtr, vector<uint32_t>> &connect_input_node_idx,
  116. map<NodePtr, vector<uint32_t>> &connect_output_node_idx) {
  117. auto symbol_iter = symbol_to_anchors_.find(symbol_string);
  118. if (symbol_iter == symbol_to_anchors_.end()) {
  119. REPORT_CALL_ERROR("E19999", "Can't find symbol:%s in symbol_to_anchors map, check invalid",
  120. symbol_string.c_str());
  121. GELOGE(PARAM_INVALID, "Input param symbol string: %s is invalid.", symbol_string.c_str());
  122. return PARAM_INVALID;
  123. }
  124. const auto &node_index_io_list = symbol_iter->second;
  125. for (const auto &node_index_io : node_index_io_list) {
  126. if (node_index_io.io_type_ == kOut) {
  127. // find node that has shared output memory.
  128. connect_output_node_idx[node_index_io.node_].emplace_back(node_index_io.index_);
  129. } else {
  130. // find node that has shared input memory.
  131. connect_input_node_idx[node_index_io.node_].emplace_back(node_index_io.index_);
  132. }
  133. }
  134. return SUCCESS;
  135. }
  136. Status InputOutputConnectionIdentifyPass::ProcessOutputNode(const NodePtr &node,
  137. map<NodePtr, vector<uint32_t>> &connect_input_node_idx,
  138. map<NodePtr, vector<uint32_t>> &connect_output_node_idx) {
  139. GE_CHECK_NOTNULL(node);
  140. for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
  141. // The return ptr of GetAllInDataAnchors is always valid.
  142. auto anchor_iter = anchor_to_symbol_.find(NodeIndexIO(node, in_data_anchor->GetIdx(), kIn).ToString());
  143. if (anchor_iter == anchor_to_symbol_.end()) {
  144. GELOGW("Current node: %s in_data_anchor: %d is invalid, can not find related symbol.", node->GetName().c_str(),
  145. in_data_anchor->GetIdx());
  146. continue;
  147. }
  148. const string &symbol = anchor_iter->second;
  149. auto status = UpdateNodeIdxMap(symbol, connect_input_node_idx, connect_output_node_idx);
  150. if (status != SUCCESS) {
  151. GELOGE(status, "Failed to update node anchor_index map.");
  152. return status;
  153. }
  154. }
  155. return SUCCESS;
  156. }
  157. Status InputOutputConnectionIdentifyPass::SetNodeAttrOfConnectingInputOutput(
  158. const map<NodePtr, vector<uint32_t>> &connect_input_node_idx,
  159. const map<NodePtr, vector<uint32_t>> &connect_output_node_idx) {
  160. for (const auto &iter : connect_input_node_idx) {
  161. GE_CHECK_NOTNULL(iter.first);
  162. if (iter.first->GetOpDesc() != nullptr) {
  163. if (!AttrUtils::SetListInt(iter.first->GetOpDesc(), ATTR_NAME_NODE_CONNECT_INPUT, iter.second)) {
  164. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  165. ATTR_NAME_NODE_CONNECT_INPUT.c_str(),
  166. iter.first->GetName().c_str(), iter.first->GetType().c_str());
  167. GELOGE(INTERNAL_ERROR, "Failed to set attr %s for node %s.", ATTR_NAME_NODE_CONNECT_INPUT.c_str(),
  168. iter.first->GetName().c_str());
  169. return INTERNAL_ERROR;
  170. }
  171. }
  172. }
  173. for (const auto &iter : connect_output_node_idx) {
  174. GE_CHECK_NOTNULL(iter.first);
  175. if (iter.first->GetOpDesc() != nullptr) {
  176. if (!AttrUtils::SetListInt(iter.first->GetOpDesc(), ATTR_NAME_NODE_CONNECT_OUTPUT, iter.second)) {
  177. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  178. ATTR_NAME_NODE_CONNECT_OUTPUT.c_str(),
  179. iter.first->GetName().c_str(), iter.first->GetType().c_str());
  180. GELOGE(INTERNAL_ERROR, "Failed to set attr %s for node %s.", ATTR_NAME_NODE_CONNECT_OUTPUT.c_str(),
  181. iter.first->GetName().c_str());
  182. return INTERNAL_ERROR;
  183. }
  184. }
  185. }
  186. return SUCCESS;
  187. }
  188. } // namespace ge

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