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

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

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