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.

infer_base_pass.cc 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * Copyright 2021 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 "infer_base_pass.h"
  17. #include "common/ge/ge_util.h"
  18. #include "common/util/error_manager/error_manager.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "framework/common/util.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/utils/graph_utils.h"
  23. #include "graph/utils/node_utils.h"
  24. #include "graph/utils/tensor_utils.h"
  25. #include "graph/utils/type_utils.h"
  26. namespace ge {
  27. namespace {
  28. graphStatus FindValidSubgraphNetoutput(const ConstNodePtr &node, const ComputeGraphPtr &sub_graph, NodePtr &netoutput) {
  29. auto sub_nodes = sub_graph->GetDirectNode();
  30. for (size_t i = sub_nodes.size(); i > 0; --i) {
  31. auto sub_node = sub_nodes.at(i - 1);
  32. if (sub_node->GetType() == NETOUTPUT) {
  33. if (sub_node == nullptr) {
  34. REPORT_INNER_ERROR("E19999", "NetOutput node is null in subgraph %s, parent node %s.",
  35. sub_graph->GetName().c_str(), node->GetName().c_str());
  36. GELOGE(GRAPH_FAILED, "[Check][Param] NetOutput node is null on sub graph %s, parent node %s",
  37. sub_graph->GetName().c_str(), node->GetName().c_str());
  38. return GRAPH_FAILED;
  39. }
  40. auto sub_node_opdesc = sub_node->GetOpDesc();
  41. if (sub_node_opdesc == nullptr) {
  42. REPORT_INNER_ERROR("E19999", "Invalid NetOutput node in subgraph %s, parent node %s, no OpDesc on it",
  43. sub_graph->GetName().c_str(), node->GetName().c_str());
  44. GELOGE(GRAPH_FAILED, "[Check][Param] Invalid NetOutput node on sub graph %s, parent node %s, no OpDesc on it",
  45. sub_graph->GetName().c_str(), node->GetName().c_str());
  46. return GRAPH_FAILED;
  47. }
  48. netoutput = sub_node;
  49. return GRAPH_SUCCESS;
  50. }
  51. }
  52. REPORT_INNER_ERROR("E19999", "Can not find the NetOutput node in subgraph %s, parent node %s",
  53. sub_graph->GetName().c_str(), node->GetName().c_str());
  54. GELOGE(GRAPH_FAILED, "[Check][Param] Can not find the NetOutput node in subgraph %s, parent node %s",
  55. sub_graph->GetName().c_str(), node->GetName().c_str());
  56. return GRAPH_FAILED;
  57. }
  58. } // namespace
  59. Status InferBasePass::Run(NodePtr &node) {
  60. GE_CHECK_NOTNULL(node);
  61. GE_CHECK_NOTNULL(node->GetOpDesc());
  62. bool need_infer = NeedInfer(node);
  63. if (!need_infer) {
  64. GELOGD("Node %s does not need to infer.", node->GetName().c_str());
  65. return SUCCESS;
  66. }
  67. std::set<NodePtr> changed_nodes;
  68. auto ret = InferAndUpdate(node, !OptionExists(kOptimizeAfterSubGraph), changed_nodes);
  69. if (ret != GRAPH_SUCCESS) {
  70. GELOGE(ret, "Infer and update for node %s failed! ret: %u", node->GetName().c_str(), ret);
  71. return GRAPH_FAILED;
  72. }
  73. AddChangedNodesImmediateRepass(changed_nodes);
  74. return SUCCESS;
  75. }
  76. bool InferBasePass::NeedInfer(const NodePtr &node) const { return true; }
  77. void InferBasePass::AddChangedNodesImmediateRepass(const std::set<NodePtr> &changed_nodes) {
  78. for (const auto &node_ele : changed_nodes) {
  79. AddImmediateRePassNode(node_ele);
  80. }
  81. }
  82. graphStatus InferBasePass::InferAndUpdate(NodePtr &node, bool before_subgraph, std::set<NodePtr> &changed_nodes) {
  83. graphStatus ret;
  84. if (ContainsSubgraph(node)) {
  85. if (before_subgraph) {
  86. ret = UpdateTensorDescToSubgraphData(node);
  87. } else {
  88. ret = UpdateTensorDescToParentNodeOutput(node);
  89. }
  90. if (ret != GRAPH_SUCCESS) {
  91. GELOGE(ret, "Update tensor desc failed between parent node %s and subgraphs. ret: %u", node->GetName().c_str(),
  92. ret);
  93. return ret;
  94. }
  95. }
  96. PrintInOutTensors(node, "before_infer");
  97. ret = Infer(node);
  98. PrintInOutTensors(node, "after_infer");
  99. if (ret == GRAPH_NODE_NEED_REPASS) {
  100. // if a node need re_pass, it is not necessary to update peer node input.
  101. changed_nodes.insert(node);
  102. return GRAPH_SUCCESS;
  103. } else if (ret != GRAPH_SUCCESS && ret != GRAPH_NOT_CHANGED) {
  104. GELOGE(ret, "Infer failed for node %s, ret: %u", node->GetName().c_str(), ret);
  105. return ret;
  106. }
  107. ret = UpdateTensorDescToPeerInputs(node, changed_nodes);
  108. if (ret != GRAPH_SUCCESS) {
  109. GELOGE(ret, "Node %s updates tensor desc to peer input nodes failed! ret: %u", node->GetName().c_str(), ret);
  110. }
  111. GELOGD("Node %s infer and update succeeded .", node->GetName().c_str());
  112. return ret;
  113. }
  114. bool InferBasePass::ContainsSubgraph(const NodePtr &node) {
  115. auto sub_graph_names = node->GetOpDesc()->GetSubgraphInstanceNames();
  116. return !sub_graph_names.empty();
  117. }
  118. graphStatus InferBasePass::UpdateTensorDescToPeerInputs(NodePtr &node, std::set<NodePtr> &changed_nodes) {
  119. auto op_desc = node->GetOpDesc();
  120. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  121. auto output_tensor = op_desc->MutableOutputDesc(out_anchor->GetIdx());
  122. for (const auto &peer_anchor : out_anchor->GetPeerInDataAnchors()) {
  123. auto peer_anchor_opdesc = peer_anchor->GetOwnerNode()->GetOpDesc();
  124. if (peer_anchor_opdesc == nullptr) {
  125. continue;
  126. }
  127. auto peer_input_desc = peer_anchor_opdesc->MutableInputDesc(peer_anchor->GetIdx());
  128. if (peer_input_desc == nullptr) {
  129. continue;
  130. }
  131. bool changed = false;
  132. auto ret = UpdateTensorDesc(output_tensor, peer_input_desc, changed);
  133. if (ret != GRAPH_SUCCESS) {
  134. REPORT_CALL_ERROR("E19999", "Update peer input desc failed, node %s.", node->GetName().c_str());
  135. GELOGE(ret, "Update peer input desc failed, node %s.", node->GetName().c_str());
  136. return ret;
  137. }
  138. if (changed) {
  139. changed_nodes.insert(peer_anchor->GetOwnerNode());
  140. GELOGD("Node %s update peer node succeeded, peer node %s is changed.", node->GetName().c_str(),
  141. peer_anchor->GetOwnerNode()->GetName().c_str());
  142. }
  143. }
  144. }
  145. return GRAPH_SUCCESS;
  146. }
  147. std::vector<ComputeGraphPtr> InferBasePass::GetCurNodeSubgraphs(const NodePtr &node) {
  148. std::vector<ComputeGraphPtr> cur_node_subgraph;
  149. auto op_desc = node->GetOpDesc();
  150. auto sub_graph_names = op_desc->GetSubgraphInstanceNames();
  151. if (sub_graph_names.empty()) {
  152. return cur_node_subgraph;
  153. }
  154. auto root_graph = GraphUtils::FindRootGraph(node->GetOwnerComputeGraph());
  155. for (const auto &name : sub_graph_names) {
  156. if (name.empty()) {
  157. GELOGW("The node %s contains empty subgraph instance name", node->GetName().c_str());
  158. continue;
  159. }
  160. auto sub_graph = root_graph->GetSubgraph(name);
  161. if (sub_graph == nullptr) {
  162. GELOGW("The subgrpah %s for node %s is null.", name.c_str(), node->GetName().c_str());
  163. continue;
  164. }
  165. cur_node_subgraph.emplace_back(sub_graph);
  166. }
  167. return cur_node_subgraph;
  168. }
  169. graphStatus InferBasePass::UpdateTensorDescToSubgraphData(NodePtr &node) {
  170. auto op_desc = node->GetOpDesc();
  171. for (const auto &sub_graph : GetCurNodeSubgraphs(node)) {
  172. for (const auto &node_sub : sub_graph->GetDirectNode()) {
  173. if (node_sub->GetType() != DATA) {
  174. continue;
  175. }
  176. auto data_opdesc = node_sub->GetOpDesc();
  177. if (data_opdesc == nullptr) {
  178. REPORT_INNER_ERROR("E19999", "Invalid data node on the sub graph %s parent node %s, no OpDesc",
  179. sub_graph->GetName().c_str(), node->GetName().c_str());
  180. GELOGE(GRAPH_FAILED, "[Get][OpDesc] Invalid data node on the sub graph %s parent node %s, no OpDesc",
  181. sub_graph->GetName().c_str(), node->GetName().c_str());
  182. return GRAPH_FAILED;
  183. }
  184. int ref_i;
  185. if (!AttrUtils::GetInt(data_opdesc, ATTR_NAME_PARENT_NODE_INDEX, ref_i)) {
  186. REPORT_INNER_ERROR("E19999", "Invalid data node on the sub graph %s parent node %s, no ref-index attribute",
  187. sub_graph->GetName().c_str(), node->GetName().c_str());
  188. GELOGE(GRAPH_FAILED, "[Get][Int] Invalid data node on the sub graph %s parent node %s, no ref-index attribute",
  189. sub_graph->GetName().c_str(), node->GetName().c_str());
  190. return GRAPH_FAILED;
  191. }
  192. GELOGD("Subgraph Data node ref_index is %d, parent node is %s.", ref_i, node->GetName().c_str());
  193. // In multi-batch, data shape of subgraph is different, no need to refresh.
  194. if (data_opdesc->HasAttr(ATTR_MBATCH_ORIGIN_INPUT_DIMS)) {
  195. GELOGD("While updating subgraph data node, ignore node %s which is created by multi-dims",
  196. data_opdesc->GetName().c_str());
  197. continue;
  198. }
  199. auto input_desc = op_desc->MutableInputDesc(ref_i);
  200. if (input_desc == nullptr) {
  201. REPORT_INNER_ERROR("E19999",
  202. "The ref index(%d) on the data %s on the sub graph %s "
  203. "parent node %s are incompatible, inputs num %u",
  204. ref_i, node_sub->GetName().c_str(), sub_graph->GetName().c_str(), node->GetName().c_str(),
  205. node->GetAllInDataAnchorsSize());
  206. GELOGE(GRAPH_FAILED,
  207. "[Call][MutableInputDesc] The ref index(%d) on the data %s on the sub graph %s "
  208. "parent node %s are incompatible, inputs num %u",
  209. ref_i, node_sub->GetName().c_str(), sub_graph->GetName().c_str(), node->GetName().c_str(),
  210. node->GetAllInDataAnchorsSize());
  211. return GRAPH_FAILED;
  212. }
  213. GELOGI("Ref index is %d, input_desc dtype is %d, node name is %s", ref_i, input_desc->GetDataType(),
  214. node->GetName().c_str());
  215. bool has_tensor_desc_changed = false;
  216. auto data_input_td = data_opdesc->MutableInputDesc(0);
  217. auto ret = UpdateTensorDesc(input_desc, data_input_td, has_tensor_desc_changed);
  218. if (ret != GRAPH_SUCCESS) {
  219. REPORT_CALL_ERROR("E19999", "Failed to update input desc of data %s on the sub graph %s parent node %s",
  220. node_sub->GetName().c_str(), sub_graph->GetName().c_str(), node->GetName().c_str());
  221. GELOGE(GRAPH_FAILED, "[Update][InputDesc] of data %s on the sub graph %s parent node %s failed",
  222. node_sub->GetName().c_str(), sub_graph->GetName().c_str(), node->GetName().c_str());
  223. return ret;
  224. }
  225. auto data_output_td = data_opdesc->MutableOutputDesc(0);
  226. ret = UpdateTensorDesc(input_desc, data_output_td, has_tensor_desc_changed);
  227. if (ret != GRAPH_SUCCESS) {
  228. REPORT_CALL_ERROR("E19999", "Failed to update output desc of data %s on the sub graph %s parent node %s",
  229. node_sub->GetName().c_str(), sub_graph->GetName().c_str(), node->GetName().c_str());
  230. GELOGE(GRAPH_FAILED, "[Update][OutputDesc] of data %s on the sub graph %s parent node %s failed",
  231. node_sub->GetName().c_str(), sub_graph->GetName().c_str(), node->GetName().c_str());
  232. return ret;
  233. }
  234. GELOGD("Parent node %s update subgraph data %s input and output succeed.", node->GetName().c_str(),
  235. data_opdesc->GetName().c_str());
  236. }
  237. }
  238. return GRAPH_SUCCESS;
  239. }
  240. graphStatus InferBasePass::UpdateTensorDescToParentNodeOutput(NodePtr &node) {
  241. std::vector<std::vector<GeTensorDescPtr>> ref_out_tensors(node->GetAllOutDataAnchorsSize());
  242. for (const auto &sub_graph : GetCurNodeSubgraphs(node)) {
  243. NodePtr netoutput;
  244. auto ret = FindValidSubgraphNetoutput(node, sub_graph, netoutput);
  245. if (ret != GRAPH_SUCCESS) {
  246. return ret;
  247. }
  248. auto netoutput_opdesc = netoutput->GetOpDesc();
  249. for (auto &netoutput_in_anchor : netoutput->GetAllInDataAnchors()) {
  250. auto netoutput_in_desc = netoutput_opdesc->MutableInputDesc(netoutput_in_anchor->GetIdx());
  251. if (netoutput_in_desc == nullptr) {
  252. REPORT_INNER_ERROR("E19999",
  253. "Invalid NetOutput node on sub graph %s, parent node %s, can not find input tensor %d",
  254. sub_graph->GetName().c_str(), node->GetName().c_str(), netoutput_in_anchor->GetIdx());
  255. GELOGE(GRAPH_FAILED,
  256. "[Get][Tensor] Invalid NetOutput node on sub graph %s, parent node %s, can not find input tensor %d",
  257. sub_graph->GetName().c_str(), node->GetName().c_str(), netoutput_in_anchor->GetIdx());
  258. return GRAPH_FAILED;
  259. }
  260. GELOGI("Netoutput in anchor index is %d, input tensor dim is %zu", netoutput_in_anchor->GetIdx(),
  261. netoutput_in_desc->GetShape().GetDimNum());
  262. int ref_i;
  263. if (!AttrUtils::GetInt(netoutput_in_desc, ATTR_NAME_PARENT_NODE_INDEX, ref_i)) {
  264. // if there is no ref index on the TensorDesc, it means the output data will be ignored outer.
  265. continue;
  266. }
  267. GELOGI("Parent node index of edge desc is %d", ref_i);
  268. if (ref_i < 0 || static_cast<uint32_t>(ref_i) >= node->GetAllOutDataAnchorsSize()) {
  269. REPORT_INNER_ERROR("E19999",
  270. "Invalid ref_index %d of parent node %s, ref_index should less than %u.", ref_i,
  271. node->GetName().c_str(), node->GetAllOutDataAnchorsSize());
  272. GELOGE(GRAPH_FAILED,
  273. "[Get][Ref_index] Invalid ref_index %d of parent node %s, ref_index should less than %u.", ref_i,
  274. node->GetName().c_str(), node->GetAllOutDataAnchorsSize());
  275. return GRAPH_FAILED;
  276. }
  277. ref_out_tensors[ref_i].emplace_back(netoutput_in_desc);
  278. }
  279. }
  280. return UpdateParentNodeContainsSubgraphs(node, ref_out_tensors);
  281. }
  282. graphStatus InferBasePass::UpdateParentNodeContainsSubgraphs(
  283. NodePtr &node, const std::vector<std::vector<GeTensorDescPtr>> &ref_out_tensors) {
  284. for (size_t i = 0; i < ref_out_tensors.size(); i++) {
  285. if (ref_out_tensors[i].empty()) {
  286. REPORT_CALL_ERROR("E19999", "Parent node %s ref_index %zu subgraph output tensor list is empty.",
  287. node->GetName().c_str(), i);
  288. GELOGE(GRAPH_FAILED, "[Param][check] Parent node %s ref_index %zu subgraph output tensor list is empty.",
  289. node->GetName().c_str(), i);
  290. return GRAPH_FAILED;
  291. }
  292. auto node_op_desc = node->GetOpDesc();
  293. auto node_output_td = node_op_desc->MutableOutputDesc(i);
  294. if (node_output_td == nullptr) {
  295. REPORT_CALL_ERROR("E19999", "Node %s output %zu tensor desc is null.", node->GetName().c_str(), i);
  296. GELOGE(GRAPH_FAILED, "[Param][check] Node %s output %zu tensor desc is null.", node->GetName().c_str(), i);
  297. return GRAPH_FAILED;
  298. }
  299. graphStatus ret;
  300. if (node_op_desc->HasAttr(ATTR_NAME_BATCH_NUM)) {
  301. ret = UpdateOutputFromSubgraphsForMultiDims(ref_out_tensors[i], node_output_td);
  302. } else {
  303. ret = UpdateOutputFromSubgraphs(ref_out_tensors[i], node_output_td);
  304. }
  305. if (ret != GRAPH_SUCCESS) {
  306. REPORT_CALL_ERROR("E19999", "Node %s update output %zu tensor desc failed. ret: %u", node->GetName().c_str(), i,
  307. ret);
  308. GELOGE(GRAPH_FAILED, "[Param][check] Node %s update output %zu tensor desc failed. ret: %u",
  309. node->GetName().c_str(), i, ret);
  310. return ret;
  311. }
  312. GELOGD("Parent node %s successfully updated the output tensors from subgraphs.", node->GetName().c_str());
  313. }
  314. return GRAPH_SUCCESS;
  315. }
  316. void InferBasePass::PrintInOutTensors(const NodePtr &node, const std::string &phase) {
  317. if (!IsLogEnable(GE, DLOG_DEBUG)) {
  318. return;
  319. }
  320. if (node == nullptr) {
  321. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  322. GELOGE(GRAPH_FAILED, "[Check][Param] node is null");
  323. return;
  324. }
  325. ge::OpDescPtr op_desc = node->GetOpDesc();
  326. GE_IF_BOOL_EXEC(op_desc == nullptr, REPORT_INNER_ERROR("E19999", "Node has no opdesc, check invalid");
  327. GELOGE(GRAPH_FAILED, "[Get][OpDesc] op_desc is null."); return );
  328. std::stringstream ss;
  329. ss << "{";
  330. int32_t in_idx = 0;
  331. for (const auto &input_desc : op_desc->GetAllInputsDescPtr()) {
  332. if (input_desc == nullptr) {
  333. in_idx++;
  334. continue;
  335. }
  336. if (in_idx > 0) {
  337. ss << " ";
  338. }
  339. ss << "input_" << in_idx << " tensor: ";
  340. ss << SerialTensorInfo(input_desc);
  341. in_idx++;
  342. }
  343. int32_t out_idx = 0;
  344. for (const auto &output_desc : op_desc->GetAllOutputsDescPtr()) {
  345. if (output_desc == nullptr) {
  346. out_idx++;
  347. continue;
  348. }
  349. ss << " ";
  350. ss << "output_" << out_idx << " tensor: ";
  351. ss << SerialTensorInfo(output_desc);
  352. out_idx++;
  353. }
  354. ss << "}";
  355. GELOGD("Infer tensor dump [%s], Node name: [%s]. %s", phase.c_str(), node->GetName().c_str(), ss.str().c_str());
  356. }
  357. } // namespace ge

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