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.

net_output_pass.cc 28 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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/net_output_pass.h"
  17. #include <map>
  18. #include <memory>
  19. #include <string>
  20. #include <utility>
  21. #include <vector>
  22. #include "common/ge/ge_util.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "framework/common/ge_inner_error_codes.h"
  25. #include "framework/omg/omg_inner_types.h"
  26. #include "graph/debug/ge_attr_define.h"
  27. #include "graph/common/local_context.h"
  28. #include "graph/passes/pass_utils.h"
  29. #include "graph/utils/tensor_utils.h"
  30. #include "graph/utils/type_utils.h"
  31. namespace ge {
  32. static std::map<std::string, ge::DataType> output_type_str_to_datatype = {
  33. {"FP32", ge::DT_FLOAT}, {"FP16", ge::DT_FLOAT16}, {"INT8", ge::DT_INT8}, {"INT16", ge::DT_INT16},
  34. {"UINT16", ge::DT_UINT16}, {"UINT8", ge::DT_UINT8}, {"INT32", ge::DT_INT32}, {"INT64", ge::DT_INT64},
  35. {"UINT32", ge::DT_UINT32}, {"UINT64", ge::DT_UINT64}, {"DOUBLE", ge::DT_DOUBLE}};
  36. // the size of user defined output datatype or format string after split by ":".
  37. const size_t kUserDefinedElementCount = 2;
  38. const size_t kNodesCount = 2;
  39. Status NetOutputPass::GetRetvalOutputInfo(const ge::NodePtr &node,
  40. std::map<int32_t, RetvalInfo> &retval_node_index_map) {
  41. GE_CHECK_NOTNULL(node);
  42. GE_CHECK_NOTNULL(node->GetOpDesc());
  43. int64_t output_index = 0;
  44. if (!AttrUtils::GetInt(node->GetOpDesc(), RETVAL_ATTR_NAME_INDEX, output_index)) {
  45. GELOGE(PARAM_INVALID, "Get output index failed.");
  46. return PARAM_INVALID;
  47. }
  48. if (retval_node_index_map.count(output_index) > 0) {
  49. GELOGE(PARAM_INVALID, "Retval has duplicate index.");
  50. return PARAM_INVALID;
  51. }
  52. int parent_node_index = -1;
  53. (void)AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_node_index);
  54. InDataAnchorPtr in_data_anchor = node->GetInDataAnchor(0);
  55. GE_CHECK_NOTNULL(in_data_anchor);
  56. GE_CHECK_NOTNULL(in_data_anchor->GetPeerOutAnchor());
  57. int32_t src_node_index = in_data_anchor->GetPeerOutAnchor()->GetIdx();
  58. NodePtr src_node_ptr = in_data_anchor->GetPeerOutAnchor()->GetOwnerNode();
  59. retval_node_index_map[output_index] = {src_node_ptr, src_node_index, parent_node_index};
  60. // if user targets include retval node,delete it from set and insert its input node instead
  61. // better to GetInNodes here
  62. auto iter = targets_.find(node);
  63. if (iter != targets_.end()) {
  64. targets_.erase(iter);
  65. targets_.insert(src_node_ptr);
  66. GELOGI("node [%s] is in user def targets, do not output result to user!", node->GetName().c_str());
  67. }
  68. is_include_special_node_ = true;
  69. return SUCCESS;
  70. }
  71. Status NetOutputPass::GetOutputNode(const ge::ComputeGraphPtr &graph, std::vector<RetvalInfo> &output_nodes_info) {
  72. std::map<int32_t, RetvalInfo> retval_node_index_map;
  73. for (NodePtr &node : graph->GetDirectNode()) {
  74. Status ret = SUCCESS;
  75. if ((node->GetOpDesc() != nullptr) && (node->GetOpDesc()->HasAttr(RETVAL_ATTR_NAME_INDEX))) {
  76. /// Set the output according to the Retval operator,
  77. /// identify by whether there is an index parameter
  78. ret = GetRetvalOutputInfo(node, retval_node_index_map);
  79. }
  80. if (ret != SUCCESS) {
  81. GELOGE(ret, "GetRetvalOutputInfo failed");
  82. return ret;
  83. }
  84. }
  85. GELOGI("Get retval node size:%zu.", retval_node_index_map.size());
  86. std::vector<RetvalInfo> out_nodes_tmp;
  87. /// The Netoutput output is determined by Retval, and the input order
  88. /// of Netoutput is sorted according to the index value of Retval.
  89. for (auto &it : retval_node_index_map) {
  90. out_nodes_tmp.push_back(it.second);
  91. }
  92. // when user set targets, mean that no output result
  93. for (auto &ele : graph->GetGraphOutNodesInfo()) {
  94. auto iter = targets_.find(ele.first);
  95. if (iter != targets_.end()) {
  96. GELOGI("user set out node [%s] is found in user def targets, out node is prio!", ele.first->GetName().c_str());
  97. targets_.erase(iter);
  98. }
  99. auto op_desc = ele.first->GetOpDesc();
  100. GE_CHECK_NOTNULL(op_desc);
  101. if (op_desc->HasAttr(ATTR_ATC_USER_DEFINE_OUTPUT_NODES)) {
  102. is_user_define_ouput_nodes = true;
  103. }
  104. output_nodes_info.push_back({ele.first, ele.second, -1});
  105. }
  106. GELOGI("Output node set by user or leaf node, size:%zu.", output_nodes_info.size());
  107. for (auto &ele : out_nodes_tmp) {
  108. // add member, no need to remove duplicated because we need to keep all edges
  109. output_nodes_info.push_back(ele);
  110. }
  111. GELOGI("Get output node, size:%zu.", output_nodes_info.size());
  112. Status check_ret = CheckOutputNodeInfo(graph, output_nodes_info);
  113. if (check_ret != SUCCESS) {
  114. return check_ret;
  115. }
  116. return SUCCESS;
  117. }
  118. Status NetOutputPass::CheckOutputNodeInfo(const ComputeGraphPtr &graph, const std::vector<RetvalInfo> &outputs) {
  119. for (auto &item : outputs) {
  120. NodePtr node = item.output_node;
  121. if (node == nullptr) {
  122. GELOGE(PARAM_INVALID, "Node in outputs is null.");
  123. return PARAM_INVALID;
  124. } else {
  125. if (graph->FindNode(node->GetName()) == nullptr) {
  126. GELOGE(INTERNAL_ERROR, "Out node (%s) is not in graph.", node->GetName().c_str());
  127. return INTERNAL_ERROR;
  128. }
  129. GE_CHECK_NOTNULL(node->GetOpDesc());
  130. int32_t out_size = node->GetOpDesc()->GetOutputsSize();
  131. int32_t index = item.node_output_index;
  132. if (index < 0 || index >= out_size) {
  133. GELOGE(PARAM_INVALID,
  134. "User declared out node (%s) output index:%d must be smaller "
  135. "than node ouput size:%d and cann't be negative!",
  136. node->GetName().c_str(), index, out_size);
  137. return PARAM_INVALID;
  138. }
  139. }
  140. }
  141. return SUCCESS;
  142. }
  143. Status NetOutputPass::RemoveUnusedNode(const ge::ComputeGraphPtr &graph) {
  144. std::vector<ge::NodePtr> node_to_delete;
  145. // Delete _Retval operator.
  146. for (auto &node : graph->GetDirectNode()) {
  147. GE_IF_BOOL_EXEC(node->GetOpDesc() == nullptr, GELOGW("Node OpDesc is nullptr"); continue);
  148. bool need_be_deleted = node->GetInDataNodes().size() != 0 && node->GetOutDataNodesSize() == 0 &&
  149. (node->GetOpDesc()->HasAttr(RETVAL_ATTR_NAME_INDEX));
  150. if (need_be_deleted) {
  151. node_to_delete.push_back(node);
  152. }
  153. }
  154. for (NodePtr &node : node_to_delete) {
  155. auto iter = targets_.find(node);
  156. if (iter != targets_.end()) {
  157. GELOGI("[Net output pass] node[%s] is in user set targets.so do not remove!", node->GetName().c_str());
  158. continue;
  159. }
  160. if (graph->RemoveNode(node) != GRAPH_SUCCESS) {
  161. GELOGE(INTERNAL_ERROR, "Remove node failed, node name:%s.", node->GetName().c_str());
  162. return INTERNAL_ERROR;
  163. }
  164. }
  165. return SUCCESS;
  166. }
  167. Status NetOutputPass::UpdateNetOutputDesc(const ge::NodePtr &net_output) {
  168. OpDescPtr net_output_desc = net_output->GetOpDesc();
  169. if (net_output_desc == nullptr) {
  170. GELOGE(INTERNAL_ERROR, "Opdesc of net output node is nullptr.");
  171. return INTERNAL_ERROR;
  172. }
  173. if (net_output_desc->GetInputsSize() == 0) {
  174. GELOGE(INTERNAL_ERROR, "Net output node input is empty.");
  175. return INTERNAL_ERROR;
  176. }
  177. std::vector<bool> is_input_const;
  178. for (const auto &in_anchor : net_output->GetAllInDataAnchors()) {
  179. GE_CHECK_NOTNULL(in_anchor);
  180. uint32_t index = static_cast<uint32_t>(in_anchor->GetIdx());
  181. if (index >= net_output_desc->GetAllInputsDesc().size()) {
  182. GELOGE(INTERNAL_ERROR, "Index is invalid, index:%u, size:%zu.", index,
  183. net_output_desc->GetAllInputsDesc().size());
  184. return INTERNAL_ERROR;
  185. }
  186. GE_CHECK_NOTNULL(in_anchor->GetPeerOutAnchor());
  187. is_input_const.push_back(PassUtils::IsConstant(in_anchor->GetPeerOutAnchor()->GetOwnerNode()));
  188. OpDescPtr src_op_desc = in_anchor->GetPeerOutAnchor()->GetOwnerNode()->GetOpDesc();
  189. GE_CHECK_NOTNULL(src_op_desc);
  190. uint32_t peer_index = static_cast<uint32_t>(in_anchor->GetPeerOutAnchor()->GetIdx());
  191. ge::GeTensorDesc output_in_desc = src_op_desc->GetOutputDesc(peer_index);
  192. if (net_output_desc->UpdateInputDesc(index, output_in_desc) != GRAPH_SUCCESS) {
  193. GELOGE(INTERNAL_ERROR, "Update input desc failed, index:%u.", index);
  194. return INTERNAL_ERROR;
  195. }
  196. GELOGD("Update desc, format:%s, data type:%s, index:%u.",
  197. TypeUtils::FormatToSerialString(output_in_desc.GetFormat()).c_str(),
  198. TypeUtils::DataTypeToSerialString(output_in_desc.GetDataType()).c_str(), index);
  199. }
  200. net_output_desc->SetIsInputConst(is_input_const);
  201. return SUCCESS;
  202. }
  203. Status NetOutputPass::AddCtrlEdgeForTargets(const ge::NodePtr &net_out_node) {
  204. if (net_out_node == nullptr) {
  205. GELOGE(PARAM_INVALID, "net out node is null.");
  206. return PARAM_INVALID;
  207. }
  208. // Add ctrl edge for targets
  209. for (auto &node : targets_) {
  210. if (node == nullptr) {
  211. continue;
  212. }
  213. // no need to check null because have handled it in run SaveAndRemoveTargets function
  214. graphStatus status = GraphUtils::AddEdge(node->GetOutControlAnchor(), net_out_node->GetInControlAnchor());
  215. if (status != GRAPH_SUCCESS) {
  216. GELOGE(INTERNAL_ERROR, "Add ctrl edge to netoutput node[%s] for target node [%s] failed!",
  217. net_out_node->GetName().c_str(), node->GetName().c_str());
  218. return INTERNAL_ERROR;
  219. }
  220. GELOGD("Add ctrl edge to netoutput node[%s] for target node [%s] success!", net_out_node->GetName().c_str(),
  221. node->GetName().c_str());
  222. }
  223. return SUCCESS;
  224. }
  225. void NetOutputPass::SaveAndRemoveTargets(const ge::ComputeGraphPtr &graph) {
  226. // save user targets node
  227. for (auto &node : graph->GetGraphTargetNodesInfo()) {
  228. if (node == nullptr) {
  229. GELOGW("User pointed targets contains null node.ignore it !");
  230. continue;
  231. }
  232. targets_.insert(node);
  233. }
  234. GELOGI("User pointed targets size is %zu !", targets_.size());
  235. }
  236. Status NetOutputPass::AddEdgesForNetOutput(const ge::ComputeGraphPtr &graph, const ge::NodePtr &net_out_node,
  237. const std::vector<RetvalInfo> &output_nodes_info) {
  238. int32_t net_input_index = 0;
  239. for (auto &item : output_nodes_info) {
  240. NodePtr src_node = item.output_node;
  241. GE_CHECK_NOTNULL(src_node);
  242. graphStatus status = GraphUtils::AddEdge(src_node->GetOutDataAnchor(item.node_output_index),
  243. net_out_node->GetInDataAnchor(net_input_index));
  244. if (status != GRAPH_SUCCESS) {
  245. GELOGE(INTERNAL_ERROR, "AddEdge failed, src name:%s, src index:%d, dst index:%d.", src_node->GetName().c_str(),
  246. item.node_output_index, net_input_index);
  247. return INTERNAL_ERROR;
  248. }
  249. GELOGD("AddEdge to output node, src name:%s, src index:%d, dst index:%d.", src_node->GetName().c_str(),
  250. item.node_output_index, net_input_index);
  251. if (item.parent_node_index >= 0) {
  252. GELOGI("Add parent node index %d for the netoutput input %d on graph %s", item.parent_node_index, net_input_index,
  253. graph->GetName().c_str());
  254. auto input_desc = net_out_node->GetOpDesc()->MutableInputDesc(net_input_index);
  255. if (input_desc == nullptr) {
  256. GELOGE(INTERNAL_ERROR, "Can not find intput tensor desc from NetOutput, index %d", net_input_index);
  257. return INTERNAL_ERROR;
  258. }
  259. if (!AttrUtils::SetInt(input_desc, ATTR_NAME_PARENT_NODE_INDEX, item.parent_node_index)) {
  260. GELOGE(INTERNAL_ERROR, "Failed to add parent index to NetOutput, index %d", net_input_index);
  261. return INTERNAL_ERROR;
  262. }
  263. }
  264. net_input_index++;
  265. }
  266. if (RemoveUnusedNode(graph) != SUCCESS) {
  267. GELOGE(INTERNAL_ERROR, "Remove unused nodes failed.");
  268. return INTERNAL_ERROR;
  269. }
  270. if (AddCtrlEdgeForTargets(net_out_node) != SUCCESS) {
  271. GELOGE(INTERNAL_ERROR, "Add ctrl edge for targets failed.");
  272. return INTERNAL_ERROR;
  273. }
  274. // Add true stream, netoutput is 0
  275. GE_IF_BOOL_EXEC(!ge::AttrUtils::SetInt(net_out_node->GetOpDesc(), ATTR_NAME_TRUE_BRANCH_STREAM, 0),
  276. GELOGE(INTERNAL_ERROR, "set ATTR_NAME_TRUE_BRANCH_STREAM failed");
  277. return INTERNAL_ERROR);
  278. return SUCCESS;
  279. }
  280. bool NetOutputPass::CheckNodeIsInOutputNodes(const ge::ComputeGraphPtr &graph, const ge::NodePtr &node) {
  281. for (auto &ele : graph->GetGraphOutNodesInfo()) {
  282. auto out_node = ele.first;
  283. if (node == out_node) {
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. Status NetOutputPass::UnLinkDataAnchorOfNetoutput(const ge::ComputeGraphPtr &graph, const ge::NodePtr &net_out_node) {
  290. if (net_out_node == nullptr) {
  291. GELOGE(PARAM_INVALID, "net out node is null.");
  292. return PARAM_INVALID;
  293. }
  294. Status ret = SUCCESS;
  295. // unlink all anchor to data anchor of netoutput
  296. for (auto &in_data_anchor : net_out_node->GetAllInDataAnchors()) {
  297. if (in_data_anchor == nullptr) {
  298. continue;
  299. }
  300. auto peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  301. if (peer_out_anchor == nullptr) {
  302. GELOGI("PeerOutAnchor is null!");
  303. continue;
  304. }
  305. auto node = peer_out_anchor->GetOwnerNode();
  306. auto iter = targets_.find(node);
  307. if (iter != targets_.end()) {
  308. if (!CheckNodeIsInOutputNodes(graph, node)) {
  309. ret = in_data_anchor->Unlink(peer_out_anchor);
  310. if (ret != SUCCESS) {
  311. GELOGE(INTERNAL_ERROR, "Unlink peer_out_anchor fail!");
  312. return ret;
  313. }
  314. } else {
  315. targets_.erase(iter);
  316. }
  317. }
  318. }
  319. return ret;
  320. }
  321. Status NetOutputPass::UnLinkControlAnchorOfNetoutput(const ge::ComputeGraphPtr &graph,
  322. const ge::NodePtr &net_out_node) {
  323. if (net_out_node == nullptr) {
  324. GELOGE(PARAM_INVALID, "net out node is null.");
  325. return PARAM_INVALID;
  326. }
  327. Status ret = SUCCESS;
  328. auto in_control_anchor = net_out_node->GetInControlAnchor();
  329. if (in_control_anchor == nullptr) {
  330. GELOGE(PARAM_INVALID, "in control anchor is null.");
  331. return PARAM_INVALID;
  332. }
  333. // unlink all data anchor to control anchor of netoutput
  334. for (auto &peer_out_data_anchor : in_control_anchor->GetPeerOutDataAnchors()) {
  335. if (peer_out_data_anchor == nullptr) {
  336. GELOGI("PeerOutControlAnchor is null!");
  337. } else {
  338. auto node = peer_out_data_anchor->GetOwnerNode();
  339. auto iter = targets_.find(node);
  340. if (iter != targets_.end()) {
  341. if (CheckNodeIsInOutputNodes(graph, node) == false) {
  342. ret = in_control_anchor->Unlink(peer_out_data_anchor);
  343. if (ret != SUCCESS) {
  344. GELOGE(INTERNAL_ERROR, "Unlink peer_out_anchor fail!");
  345. return ret;
  346. }
  347. } else {
  348. targets_.erase(iter);
  349. }
  350. }
  351. }
  352. }
  353. /// check all control anchor to control anchor of netoutput and delete it from targets
  354. /// to avoid duplicated add control edge;
  355. for (auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  356. if (peer_out_control_anchor == nullptr) {
  357. GELOGI("PeerOutControlAnchor is null");
  358. } else {
  359. auto node = peer_out_control_anchor->GetOwnerNode();
  360. auto iter = targets_.find(node);
  361. if (iter != targets_.end()) {
  362. targets_.erase(iter);
  363. }
  364. }
  365. }
  366. return ret;
  367. }
  368. Status NetOutputPass::UnLink(const ge::ComputeGraphPtr &graph, const ge::NodePtr &net_out_node) {
  369. GELOGI("[NetOutputPass] Enter Unlink process.");
  370. Status ret = UnLinkDataAnchorOfNetoutput(graph, net_out_node);
  371. if (ret != SUCCESS) {
  372. GELOGI("[NetOutputPass] UnLinkDataAnchorOfNetoutput process fail.");
  373. return ret;
  374. }
  375. ret = UnLinkControlAnchorOfNetoutput(graph, net_out_node);
  376. if (ret != SUCCESS) {
  377. GELOGI("[NetOutputPass] UnLinkControlAnchorOfNetoutput process fail.");
  378. return ret;
  379. }
  380. return ret;
  381. }
  382. Status NetOutputPass::ProcessWithNetoutput(const ge::ComputeGraphPtr &graph, const ge::NodePtr &output_node) {
  383. if (UpdateNetOutputDesc(output_node) != SUCCESS) {
  384. GELOGE(INTERNAL_ERROR, "Update net output desc failed.");
  385. return INTERNAL_ERROR;
  386. }
  387. if (UnLink(graph, output_node) != SUCCESS) {
  388. GELOGE(INTERNAL_ERROR, "UnLink connection between netoutput node and user set target node");
  389. return INTERNAL_ERROR;
  390. }
  391. if (AddCtrlEdgeForTargets(output_node) != SUCCESS) {
  392. GELOGE(INTERNAL_ERROR, "Add ctrl edge for targets failed.");
  393. return INTERNAL_ERROR;
  394. }
  395. return SUCCESS;
  396. }
  397. Status NetOutputPass::AddCtrlEdgesBetweenLeafAndNetOutput(const ge::ComputeGraphPtr &graph,
  398. const ge::NodePtr &net_out_node) {
  399. GE_CHECK_NOTNULL(net_out_node);
  400. if (!GetLocalOmgContext().user_out_nodes.empty() || is_user_define_ouput_nodes) {
  401. GELOGI("No need to add ctrl edge to netoutput because user out nodes have been set.");
  402. return SUCCESS;
  403. }
  404. bool graph_has_only_one_node_except_netoutput = (graph->GetDirectNodesSize() == kNodesCount);
  405. for (const auto &node : graph->GetDirectNode()) {
  406. if (node == nullptr || node->GetOpDesc() == nullptr || node->GetOpDesc()->GetType() == NETOUTPUT) {
  407. continue;
  408. }
  409. if ((node->GetInControlNodes().size() != 0 || node->GetInDataNodes().size() != 0 ||
  410. graph_has_only_one_node_except_netoutput) &&
  411. node->GetOutDataNodesSize() == 0 && node->GetOutControlNodes().size() == 0) {
  412. GE_CHK_STATUS_RET(GraphUtils::AddEdge(node->GetOutControlAnchor(), net_out_node->GetInControlAnchor()),
  413. "add edge failed");
  414. GELOGD("Add ctrl edge success. src name :%s, dst name :%s", node->GetName().c_str(),
  415. net_out_node->GetName().c_str());
  416. }
  417. }
  418. return SUCCESS;
  419. }
  420. Status NetOutputPass::CreateNetOutputNode(OpDescPtr &net_output_desc, const ge::ComputeGraphPtr &graph) {
  421. // Only flush subgraph name
  422. string node_name =
  423. (graph->GetParentGraph() != nullptr) ? (graph->GetName() + "_" + NODE_NAME_NET_OUTPUT) : NODE_NAME_NET_OUTPUT;
  424. net_output_desc = MakeShared<OpDesc>(node_name, NETOUTPUT);
  425. if (net_output_desc == nullptr) {
  426. GELOGE(MEMALLOC_FAILED, "Make shared net output op failed.");
  427. return MEMALLOC_FAILED;
  428. }
  429. (void)AttrUtils::SetListStr(net_output_desc, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES,
  430. std::move(std::vector<std::string>()));
  431. return SUCCESS;
  432. }
  433. Status NetOutputPass::Run(ge::ComputeGraphPtr graph) {
  434. if (graph == nullptr) {
  435. GELOGE(GE_GRAPH_PARAM_NULLPTR, "Compute graph is null.");
  436. return GE_GRAPH_PARAM_NULLPTR;
  437. }
  438. GELOGI("NetOutputPass Run.graph is [%s]", graph->GetName().c_str());
  439. NodePtr output_node = graph->FindFirstNodeMatchType(NETOUTPUT);
  440. // save user targets node
  441. SaveAndRemoveTargets(graph);
  442. // If graph already has a netoutput node, doesn't need to create it again.
  443. if (output_node != nullptr) {
  444. (void)AttrUtils::SetListStr(output_node->GetOpDesc(), ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES,
  445. std::move(std::vector<std::string>()));
  446. if (ProcessWithNetoutput(graph, output_node) != SUCCESS) {
  447. GELOGE(INTERNAL_ERROR, "Process with netoutput node failed.");
  448. return INTERNAL_ERROR;
  449. }
  450. } else {
  451. if (AddNetOutputNodeToGraph(graph, output_node) != SUCCESS) {
  452. GELOGE(INTERNAL_ERROR, "Set user define dtype and format for netoutput failed.");
  453. return INTERNAL_ERROR;
  454. }
  455. }
  456. // Add userdef attrs to netoutput node
  457. return SetUserDefDTypeAndFormatFromAtcParams(output_node);
  458. }
  459. Status NetOutputPass::AddNetOutputNodeToGraph(const ge::ComputeGraphPtr &graph, NodePtr &output_node) {
  460. OpDescPtr net_output_desc = nullptr;
  461. if (CreateNetOutputNode(net_output_desc, graph) != SUCCESS) {
  462. GELOGE(INTERNAL_ERROR, "Get net output nodes failed.");
  463. return INTERNAL_ERROR;
  464. }
  465. std::vector<RetvalInfo> output_nodes_info;
  466. if (GetOutputNode(graph, output_nodes_info) != SUCCESS) {
  467. GELOGE(INTERNAL_ERROR, "Get net output nodes failed.");
  468. return INTERNAL_ERROR;
  469. }
  470. GELOGI("[NETOUTPUT PASS] OutNodesInfo size:%zu, Targets Size:%zu, is_include_special_node_:%d",
  471. graph->GetGraphOutNodesInfo().size(), graph->GetGraphTargetNodesInfo().size(), is_include_special_node_);
  472. // If user does not set out nodes and targets and no retval node, also add netoutput node
  473. if ((graph->GetGraphOutNodesInfo().empty()) && (graph->GetGraphTargetNodesInfo().empty()) &&
  474. !is_include_special_node_) {
  475. GELOGI("[NETOUTPUT PASS] output_nodes and target_nodes and special nodes is empty!Add netoutput!");
  476. output_node = graph->AddNode(net_output_desc);
  477. GE_CHK_STATUS_RET(AddCtrlEdgesBetweenLeafAndNetOutput(graph, output_node),
  478. "add ctrl edge between leaf and netoutput failed");
  479. return SUCCESS;
  480. }
  481. GELOGI("[NETOUTPUT PASS] Output node size:%lu.", output_nodes_info.size());
  482. if (output_nodes_info.empty()) {
  483. // because retval node is contained by output_nodes_info, here means targets is non-empty
  484. output_node = graph->AddNode(net_output_desc);
  485. if (output_node == nullptr) {
  486. GELOGE(INTERNAL_ERROR, "Add output node failed.");
  487. return INTERNAL_ERROR;
  488. }
  489. GE_CHK_STATUS_RET(AddCtrlEdgeForTargets(output_node), "add ctrl edge for targets failed");
  490. // Add true stream, netoutput is 0
  491. GE_IF_BOOL_EXEC(!ge::AttrUtils::SetInt(output_node->GetOpDesc(), ATTR_NAME_TRUE_BRANCH_STREAM, 0),
  492. GELOGE(INTERNAL_ERROR, "set ATTR_NAME_TRUE_BRANCH_STREAM failed");
  493. return INTERNAL_ERROR);
  494. return SUCCESS;
  495. }
  496. AddInOutForNetOutputOp(graph, net_output_desc, output_nodes_info);
  497. output_node = graph->AddNode(net_output_desc);
  498. if (output_node == nullptr) {
  499. GELOGE(INTERNAL_ERROR, "Add output node failed.");
  500. return INTERNAL_ERROR;
  501. }
  502. if (AddEdgesForNetOutput(graph, output_node, output_nodes_info) != SUCCESS) {
  503. GELOGE(INTERNAL_ERROR, "Add edges for net output node failed.");
  504. return INTERNAL_ERROR;
  505. }
  506. if (AddCtrlEdgesBetweenLeafAndNetOutput(graph, output_node) != SUCCESS) {
  507. GELOGE(INTERNAL_ERROR, "Add control edges between leaf and netoutput failed.");
  508. return INTERNAL_ERROR;
  509. }
  510. GELOGI("Add NetOutput node success.");
  511. return SUCCESS;
  512. }
  513. void NetOutputPass::AddInOutForNetOutputOp(const ComputeGraphPtr &graph, OpDescPtr &net_output_desc,
  514. vector<RetvalInfo> &output_nodes_info) {
  515. std::vector<bool> is_input_const;
  516. for (auto iter = output_nodes_info.begin(); iter != output_nodes_info.end();) {
  517. NodePtr src_node = iter->output_node;
  518. if (src_node == nullptr) {
  519. continue;
  520. }
  521. int32_t src_index = iter->node_output_index;
  522. // if src_node is in targets_, no need to Add in and out for netoutput
  523. auto it = targets_.find(src_node);
  524. if (it != targets_.end()) {
  525. iter = output_nodes_info.erase(iter);
  526. GELOGD("node [%s] is in processed targets, do not add inout for netoutput!", src_node->GetName().c_str());
  527. continue;
  528. }
  529. /// Get the output attribute of src_node,
  530. /// and set to the input/output of net_out_node.
  531. if (src_node == nullptr || src_node->GetOpDesc() == nullptr || net_output_desc == nullptr) {
  532. GELOGE(INTERNAL_ERROR, "src node or net output desc is null.");
  533. return;
  534. }
  535. ge::GeTensorDesc out_desc = src_node->GetOpDesc()->GetOutputDesc(src_index);
  536. out_desc.SetFormat(FORMAT_ND);
  537. out_desc.SetOriginFormat(FORMAT_ND);
  538. GE_IF_BOOL_EXEC(net_output_desc->AddInputDesc(out_desc) != SUCCESS, GELOGW("add input desc failed"); return );
  539. is_input_const.push_back(PassUtils::IsConstant(src_node));
  540. ++iter;
  541. }
  542. net_output_desc->SetIsInputConst(is_input_const);
  543. }
  544. bool NeedUpdateOutputByOutputTypeParm(std::string &output_type, OpDescPtr &op_desc, uint32_t &src_index,
  545. ge::DataType &dt) {
  546. if (output_type_str_to_datatype.find(output_type) != output_type_str_to_datatype.end()) {
  547. dt = output_type_str_to_datatype[output_type];
  548. return true;
  549. }
  550. vector<string> output_dt_str;
  551. if (ge::AttrUtils::GetListStr(op_desc, "_user_defined_output_data_type", output_dt_str)) {
  552. for (const auto &dt_str : output_dt_str) {
  553. vector<string> dt_str_split = StringUtils::Split(dt_str, ':');
  554. if (dt_str_split.size() == kUserDefinedElementCount) {
  555. if (dt_str_split[0] == to_string(src_index)) {
  556. dt = TypeUtils::SerialStringToDataType(dt_str_split[1]);
  557. return true;
  558. }
  559. } else {
  560. GELOGW("The size of [%s] is not 2 after split.", dt_str.c_str());
  561. continue;
  562. }
  563. }
  564. }
  565. return false;
  566. }
  567. bool NeedUpdateOutputFp16Nc1hwc0(OpDescPtr &op_desc, uint32_t &src_index) {
  568. vector<string> output_dt_str;
  569. if (ge::AttrUtils::GetListStr(op_desc, "_user_defined_output_fp16_5hd", output_dt_str)) {
  570. for (const auto &dt_str : output_dt_str) {
  571. vector<string> dt_str_split = StringUtils::Split(dt_str, ':');
  572. if (dt_str_split.size() == kUserDefinedElementCount) {
  573. if (dt_str_split[0] == to_string(src_index)) {
  574. return true;
  575. }
  576. } else {
  577. GELOGW("The size of [%s] is not 2 after split.", dt_str.c_str());
  578. continue;
  579. }
  580. }
  581. }
  582. return false;
  583. }
  584. Status NetOutputPass::SetUserDefDTypeAndFormatFromAtcParams(const NodePtr &output_node) {
  585. if (output_node == nullptr) {
  586. GELOGI("[NETOUTPUT PASS] The graph no need netoutput node!");
  587. return SUCCESS;
  588. }
  589. auto output_type = GetLocalOmgContext().output_type;
  590. auto op_desc = output_node->GetOpDesc();
  591. GE_CHECK_NOTNULL(op_desc);
  592. std::vector<std::string> userdef_dtypes;
  593. std::vector<std::string> userdef_formats;
  594. ge::DataType output_data_type = ge::DT_FLOAT;
  595. for (const auto &in_anchor : output_node->GetAllInDataAnchors()) {
  596. auto index = static_cast<uint32_t>(in_anchor->GetIdx());
  597. auto peer_out = in_anchor->GetPeerOutAnchor();
  598. if (peer_out == nullptr) {
  599. // If user set target, peer_out anchor will be unlinked.
  600. continue;
  601. }
  602. auto src_index = static_cast<uint32_t>(peer_out->GetIdx());
  603. auto src_node = peer_out->GetOwnerNode();
  604. GE_CHECK_NOTNULL(src_node);
  605. OpDescPtr src_op_desc = src_node->GetOpDesc();
  606. GE_CHECK_NOTNULL(src_op_desc);
  607. // Update datatype
  608. if (NeedUpdateOutputByOutputTypeParm(output_type, src_op_desc, src_index, output_data_type)) {
  609. GELOGD("Add user-define datatype:%s to netoutput node.",
  610. TypeUtils::DataTypeToSerialString(output_data_type).c_str());
  611. userdef_dtypes.push_back(
  612. std::to_string(index).append(":").append(TypeUtils::DataTypeToSerialString(output_data_type)));
  613. continue;
  614. }
  615. // Output_node is not set,check if is_output_adjust_hw_layout is set
  616. bool set_fp16_nc1hwc0 = NeedUpdateOutputFp16Nc1hwc0(src_op_desc, src_index);
  617. if (set_fp16_nc1hwc0) {
  618. // Set DT_FLOAT16 & FORMAT_NC1HWC0
  619. userdef_dtypes.push_back(std::to_string(index).append(":").append(TypeUtils::DataTypeToSerialString(DT_FLOAT16)));
  620. userdef_formats.push_back(
  621. std::to_string(index).append(":").append(TypeUtils::FormatToSerialString(FORMAT_NC1HWC0)));
  622. }
  623. }
  624. if (!userdef_dtypes.empty() && !ge::AttrUtils::SetListStr(op_desc, ATTR_ATC_USER_DEFINE_DATATYPE, userdef_dtypes)) {
  625. GELOGE(INTERNAL_ERROR, "Set user_define_dtype attr list for netoutput failed.");
  626. return INTERNAL_ERROR;
  627. }
  628. if (!userdef_formats.empty() && !ge::AttrUtils::SetListStr(op_desc, ATTR_ATC_USER_DEFINE_FORMAT, userdef_formats)) {
  629. GELOGE(INTERNAL_ERROR, "Set user_define_format attr list for netoutput failed.");
  630. return INTERNAL_ERROR;
  631. }
  632. return SUCCESS;
  633. }
  634. } // namespace ge

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