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

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