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

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