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 7.9 kB

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

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