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

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

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