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.

control_trigger_pass.cc 20 kB

5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/control_trigger_pass.h"
  17. #include <stack>
  18. #include "common/ge/ge_util.h"
  19. #include "graph/common/omg_util.h"
  20. #include "graph/utils/type_utils.h"
  21. namespace ge {
  22. Status ControlTriggerPass::Run(ComputeGraphPtr graph) {
  23. GELOGD("ControlTriggerPass Enter");
  24. for (NodePtr &node : graph->GetDirectNode()) {
  25. if (node->GetType() != CONTROLTRIGGER) {
  26. continue;
  27. }
  28. auto in_ctrl_nodes = node->GetInControlNodes();
  29. for (NodePtr &in_ctrl_node : in_ctrl_nodes) {
  30. if (HandleDynamicCtrlEdges(graph, node, in_ctrl_node) != SUCCESS) {
  31. GELOGE(FAILED, "HandleDynamicCtrlEdges for %s->%s fail.", in_ctrl_node->GetName().c_str(),
  32. node->GetName().c_str());
  33. return FAILED;
  34. }
  35. }
  36. }
  37. GELOGD("ControlTriggerPass Leave");
  38. return SUCCESS;
  39. }
  40. ///
  41. /// @brief Handle input ctrl edges for ControlTrigger node
  42. /// @param [in] graph
  43. /// @param [in] node
  44. /// @param [in] in_ctrl_node
  45. /// @return Status
  46. ///
  47. Status ControlTriggerPass::HandleDynamicCtrlEdges(ComputeGraphPtr &graph, NodePtr &node, NodePtr &in_ctrl_node) {
  48. GE_CHECK_NOTNULL(node);
  49. GE_CHECK_NOTNULL(in_ctrl_node);
  50. GELOGI("HandleDynamicCtrlEdges: node=%s, in_ctrl_node=%s", node->GetName().c_str(), in_ctrl_node->GetName().c_str());
  51. NodePtr switch_node = nullptr;
  52. bool branch_flag = false;
  53. if (FindSwitchNode(in_ctrl_node, switch_node, branch_flag) != SUCCESS) {
  54. GELOGE(FAILED, "FindSwitchNode fail.");
  55. return FAILED;
  56. }
  57. if (switch_node == nullptr) {
  58. GELOGI("Not find valid switch node.");
  59. return SUCCESS;
  60. }
  61. auto iter1 = control_trigger_map_.find(node);
  62. if (iter1 != control_trigger_map_.end()) {
  63. auto iter2 = iter1->second.find(switch_cond_map_[switch_node]);
  64. if (iter2 != iter1->second.end()) {
  65. NodePtr constant = (branch_flag ? iter2->second.second : iter2->second.first);
  66. if ((GraphUtils::RemoveEdge(in_ctrl_node->GetOutControlAnchor(), node->GetInControlAnchor()) != GRAPH_SUCCESS) ||
  67. (GraphUtils::AddEdge(in_ctrl_node->GetOutControlAnchor(), constant->GetInControlAnchor()) != GRAPH_SUCCESS)) {
  68. REPORT_CALL_ERROR("E19999", "Remove control edge between op:%s(%s) and op:%s(%s), then "
  69. "add control edge between op:%s(%s) and op:%s(%s) failed",
  70. in_ctrl_node->GetName().c_str(), in_ctrl_node->GetType().c_str(),
  71. node->GetName().c_str(), node->GetType().c_str(),
  72. in_ctrl_node->GetName().c_str(), in_ctrl_node->GetType().c_str(),
  73. constant->GetName().c_str(), constant->GetType().c_str());
  74. GELOGE(FAILED, "Replace ctrl edge fail, %s->%s, %s->%s.", in_ctrl_node->GetName().c_str(),
  75. node->GetName().c_str(), in_ctrl_node->GetName().c_str(), constant->GetName().c_str());
  76. return FAILED;
  77. }
  78. GELOGI("No need to insert new branch.");
  79. return SUCCESS;
  80. }
  81. }
  82. if (InsertOppositeBranch(graph, node, in_ctrl_node, switch_node, branch_flag) != SUCCESS) {
  83. GELOGE(FAILED, "InsertOppositeBranch fail.");
  84. return FAILED;
  85. }
  86. return SUCCESS;
  87. }
  88. ///
  89. /// @brief Find switch_node for ControlTrigger node
  90. /// @param [in] node
  91. /// @param [out] switch_node
  92. /// @param [out] branch_flag
  93. /// @return Status
  94. ///
  95. Status ControlTriggerPass::FindSwitchNode(const NodePtr &node, NodePtr &switch_node, bool &branch_flag) {
  96. std::set<std::pair<NodePtr, uint32_t>> handle_nodes;
  97. // {node, <idx, <cond_merge_num, loop_switchf_num>>}
  98. std::stack<std::pair<NodePtr, std::pair<uint32_t, std::pair<uint32_t, uint32_t>>>> nodes;
  99. nodes.push(std::make_pair(node, std::make_pair(UINT32_MAX, std::make_pair(0, 0))));
  100. std::set<std::pair<NodePtr, uint32_t>> in_nodes;
  101. while (!nodes.empty()) {
  102. auto iter = nodes.top();
  103. NodePtr tmp_node = iter.first;
  104. GE_CHECK_NOTNULL(tmp_node);
  105. nodes.pop();
  106. uint32_t index = iter.second.first;
  107. auto num_pair = iter.second.second;
  108. if (handle_nodes.count(std::make_pair(tmp_node, index)) > 0) {
  109. continue;
  110. }
  111. switch (TransferNodeType(tmp_node, index)) {
  112. case kCondSwitch:
  113. if (num_pair.first == 0) {
  114. switch_node = tmp_node;
  115. branch_flag = (index == SWITCH_TRUE_OUTPUT);
  116. GELOGI("FindSwitchNode succ, switch_node=%s, idx=%u", switch_node->GetName().c_str(), index);
  117. return SUCCESS;
  118. }
  119. num_pair.first--;
  120. break;
  121. case kCondMerge:
  122. num_pair.first++;
  123. break;
  124. case kLoopSwitchT:
  125. GELOGI("in while_body, no need handle");
  126. return SUCCESS;
  127. case kLoopSwitchF:
  128. num_pair.second++;
  129. break;
  130. case kEnter:
  131. if (num_pair.second > 0) {
  132. num_pair.second--;
  133. }
  134. break;
  135. case kNotControlOp:
  136. break;
  137. default:
  138. GELOGE(FAILED, "invalid type");
  139. return FAILED;
  140. }
  141. GetInNodes(tmp_node, in_nodes);
  142. for (auto &node_idx : in_nodes) {
  143. nodes.push(std::make_pair(node_idx.first, std::make_pair(node_idx.second, num_pair)));
  144. }
  145. (void)handle_nodes.insert(std::make_pair(tmp_node, index));
  146. }
  147. return SUCCESS;
  148. }
  149. ///
  150. /// @brief Check if need insert opposite branch
  151. /// @param [in] node
  152. /// @param [in] index
  153. /// @return ControlNodeType
  154. ///
  155. ControlNodeType ControlTriggerPass::TransferNodeType(const NodePtr &node, uint32_t index) {
  156. const std::string type = node->GetType();
  157. if ((type == SWITCH) || (type == REFSWITCH)) {
  158. if ((index != SWITCH_TRUE_OUTPUT) && (index != SWITCH_FALSE_OUTPUT)) {
  159. GELOGI("TransferNodeType: neither true nor false branch.");
  160. return kNotControlOp;
  161. }
  162. if (FindPredInput(node) != SUCCESS) {
  163. GELOGE(INTERNAL_ERROR, "FindPredInput fail, switch_node: %s.", node->GetName().c_str());
  164. return kInvalidType;
  165. }
  166. NodePtr pred_node = switch_cond_map_[node];
  167. bool branch_flag = (index == SWITCH_TRUE_OUTPUT);
  168. if (pred_node->GetType() != LOOPCOND) {
  169. GELOGI("TransferNodeType: kCondSwitch node=%s, idx=%u", node->GetName().c_str(), index);
  170. return kCondSwitch;
  171. } else {
  172. GELOGI("TransferNodeType: kLoopSwitch node=%s, idx=%u", node->GetName().c_str(), index);
  173. return branch_flag ? kLoopSwitchT : kLoopSwitchF;
  174. }
  175. } else if ((type == MERGE) || (type == REFMERGE)) {
  176. OpDescPtr merge_desc = node->GetOpDesc();
  177. if (merge_desc == nullptr) {
  178. REPORT_INNER_ERROR("E19999", "op_desc in merge node is nullptr, check invalid");
  179. GELOGE(INTERNAL_ERROR, "FindPredInput fail, merge_desc is null, merge_node: %s.", node->GetName().c_str());
  180. return kInvalidType;
  181. }
  182. if (!merge_desc->HasAttr(ATTR_NAME_NEXT_ITERATION)) {
  183. return kCondMerge;
  184. }
  185. } else if ((type == ENTER) || (type == REFENTER)) {
  186. return kEnter;
  187. }
  188. return kNotControlOp;
  189. }
  190. ///
  191. /// @brief Get in_node & idx pairs
  192. /// @param [in] node
  193. /// @param [out] in_nodes
  194. /// @return void
  195. ///
  196. void ControlTriggerPass::GetInNodes(const NodePtr &node, std::set<std::pair<NodePtr, uint32_t>> &in_nodes) {
  197. in_nodes.clear();
  198. for (auto &in_ctrl_node : node->GetInControlNodes()) {
  199. (void)in_nodes.insert(std::make_pair(in_ctrl_node, UINT32_MAX));
  200. }
  201. for (InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  202. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  203. if (peer_out_anchor == nullptr) {
  204. continue;
  205. }
  206. (void)in_nodes.insert(std::make_pair(peer_out_anchor->GetOwnerNode(), peer_out_anchor->GetIdx()));
  207. }
  208. return;
  209. }
  210. ///
  211. /// @brief Insert opposite branch for ControlTrigger
  212. /// @param [in] graph
  213. /// @param [in] ControlTrigger node
  214. /// @param [in] in_ctrl_node
  215. /// @param [in] switch_node
  216. /// @param [in] branch_flag
  217. /// @return Status
  218. ///
  219. Status ControlTriggerPass::InsertOppositeBranch(ComputeGraphPtr &graph, NodePtr &node, NodePtr &in_ctrl_node,
  220. NodePtr &switch_node, bool branch_flag) {
  221. GE_CHECK_NOTNULL(node);
  222. GE_CHECK_NOTNULL(in_ctrl_node);
  223. GE_CHECK_NOTNULL(switch_node);
  224. OpDescPtr switch_desc = switch_node->GetOpDesc();
  225. GE_CHECK_NOTNULL(switch_desc);
  226. GeTensorDesc data_desc(GeShape(), FORMAT_NCHW, DT_INT32);
  227. NodePtr merge_node = InsertMergeNode(graph, node, in_ctrl_node, data_desc);
  228. if (merge_node == nullptr) {
  229. GELOGE(FAILED, "InsertMergeNode fail.");
  230. return FAILED;
  231. }
  232. NodePtr const_f = InsertConstNode(graph, merge_node, data_desc, false);
  233. NodePtr const_t = InsertConstNode(graph, merge_node, data_desc, true);
  234. if ((const_f == nullptr) || (const_t == nullptr)) {
  235. GELOGE(FAILED, "InsertConstNode fail.");
  236. return FAILED;
  237. }
  238. NodePtr orig_const = branch_flag ? const_t : const_f;
  239. NodePtr new_const = !branch_flag ? const_t : const_f;
  240. uint32_t new_idx = branch_flag ? SWITCH_FALSE_OUTPUT : SWITCH_TRUE_OUTPUT;
  241. const std::string identity_name = switch_desc->GetName() + "_" + IDENTITY;
  242. NodePtr identity_node = InsertIdentityNode(graph, identity_name, switch_desc->GetOutputDesc(new_idx));
  243. if (identity_node == nullptr) {
  244. GELOGE(FAILED, "InsertIdentityNode fail.");
  245. return FAILED;
  246. }
  247. if (GraphUtils::AddEdge(in_ctrl_node->GetOutControlAnchor(), orig_const->GetInControlAnchor()) != GRAPH_SUCCESS) {
  248. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  249. in_ctrl_node->GetName().c_str(), in_ctrl_node->GetType().c_str(),
  250. orig_const->GetName().c_str(), orig_const->GetType().c_str());
  251. GELOGE(FAILED, "Add in ctrl edge fail, %s->%s.", in_ctrl_node->GetName().c_str(), orig_const->GetName().c_str());
  252. return FAILED;
  253. }
  254. if (GraphUtils::AddEdge(switch_node->GetOutDataAnchor(new_idx), identity_node->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  255. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%u) and op:%s(%s)(index:0) failed",
  256. switch_node->GetName().c_str(), switch_node->GetType().c_str(), new_idx,
  257. identity_node->GetName().c_str(), identity_node->GetType().c_str());
  258. GELOGE(FAILED, "Add in data edge fail, %s->%s.", switch_desc->GetName().c_str(), identity_node->GetName().c_str());
  259. return FAILED;
  260. }
  261. if (GraphUtils::AddEdge(identity_node->GetOutControlAnchor(), new_const->GetInControlAnchor()) != GRAPH_SUCCESS) {
  262. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  263. identity_node->GetName().c_str(), identity_node->GetType().c_str(),
  264. new_const->GetName().c_str(), new_const->GetType().c_str());
  265. GELOGE(FAILED, "Add in ctrl edge fail, %s->%s.", identity_node->GetName().c_str(), new_const->GetName().c_str());
  266. return FAILED;
  267. }
  268. auto pred_const = std::make_pair(switch_cond_map_[switch_node], std::make_pair(const_f, const_t));
  269. auto iter = control_trigger_map_.find(node);
  270. if (iter == control_trigger_map_.end()) {
  271. control_trigger_map_[node] = {pred_const};
  272. } else {
  273. if (!iter->second.insert(pred_const).second) {
  274. REPORT_INNER_ERROR("E19999", "Insert to control_trigger_map_ failed");
  275. GELOGE(FAILED, "control_trigger_map_ insert failed.");
  276. return FAILED;
  277. }
  278. }
  279. return SUCCESS;
  280. }
  281. ///
  282. /// @brief Insert Merge Node
  283. /// @param [in] graph
  284. /// @param [in] node
  285. /// @param [in] in_ctrl_node
  286. /// @param [in] data_desc
  287. /// @return NodePtr
  288. ///
  289. NodePtr ControlTriggerPass::InsertMergeNode(ComputeGraphPtr &graph, NodePtr &node, NodePtr &in_ctrl_node,
  290. const GeTensorDesc &data_desc) {
  291. const std::string name = node->GetName() + "_" + MERGE;
  292. OpDescPtr op_desc = MakeShared<OpDesc>(name, MERGE);
  293. if (op_desc == nullptr) {
  294. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  295. GELOGE(FAILED, "Create Merge op %s: create op_desc fail.", name.c_str());
  296. return nullptr;
  297. }
  298. if ((op_desc->AddInputDesc(data_desc) != GRAPH_SUCCESS) || (op_desc->AddInputDesc(data_desc) != GRAPH_SUCCESS) ||
  299. (op_desc->AddOutputDesc(data_desc) != GRAPH_SUCCESS) || (op_desc->AddOutputDesc(data_desc) != GRAPH_SUCCESS)) {
  300. REPORT_CALL_ERROR("E19999", "Add input or ouput desc to op:%s(%s) failed",
  301. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  302. GELOGE(INTERNAL_ERROR, "Create Merge op %s: add input/output desc fail.", name.c_str());
  303. return nullptr;
  304. }
  305. GELOGI("Create Merge op:%s.", name.c_str());
  306. NodePtr merge_node = graph->AddNode(op_desc);
  307. if (merge_node == nullptr) {
  308. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  309. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph->GetName().c_str());
  310. GELOGE(INTERNAL_ERROR, "Create Merge op %s fail.", name.c_str());
  311. return nullptr;
  312. }
  313. if ((GraphUtils::RemoveEdge(in_ctrl_node->GetOutControlAnchor(), node->GetInControlAnchor()) != GRAPH_SUCCESS) ||
  314. (GraphUtils::AddEdge(merge_node->GetOutControlAnchor(), node->GetInControlAnchor()) != GRAPH_SUCCESS)) {
  315. REPORT_CALL_ERROR("E19999", "Remove control edge between op:%s(%s) and op:%s(%s), then "
  316. "add control edge between op:%s(%s) and op:%s(%s) failed",
  317. in_ctrl_node->GetName().c_str(), in_ctrl_node->GetType().c_str(),
  318. node->GetName().c_str(), node->GetType().c_str(),
  319. merge_node->GetName().c_str(), merge_node->GetType().c_str(),
  320. node->GetName().c_str(), node->GetType().c_str());
  321. GELOGE(FAILED, "Replace ctrl edge fail, %s->%s, %s->%s", in_ctrl_node->GetName().c_str(), node->GetName().c_str(),
  322. merge_node->GetName().c_str(), node->GetName().c_str());
  323. return nullptr;
  324. }
  325. return merge_node;
  326. }
  327. ///
  328. /// @brief Insert Const Node
  329. /// @param [in] graph
  330. /// @param [in] merge_node
  331. /// @param [in] data_desc
  332. /// @param [in] flag
  333. /// @return NodePtr
  334. ///
  335. NodePtr ControlTriggerPass::InsertConstNode(ComputeGraphPtr &graph, NodePtr &merge_node, const GeTensorDesc &data_desc,
  336. bool flag) {
  337. const std::string name = merge_node->GetName() + "_" + CONSTANT + (flag ? "_t" : "_f");
  338. OpDescPtr op_desc = MakeShared<OpDesc>(name, CONSTANT);
  339. if (op_desc == nullptr) {
  340. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  341. GELOGE(FAILED, "Create Const op %s: create op_desc fail.", name.c_str());
  342. return nullptr;
  343. }
  344. int32_t value = 0;
  345. GeTensorPtr const_value = MakeShared<GeTensor>(data_desc, reinterpret_cast<uint8_t *>(&value), sizeof(int32_t));
  346. if (const_value == nullptr) {
  347. REPORT_CALL_ERROR("E19999", "New GeTensor failed");
  348. GELOGE(FAILED, "Create tensor fail.");
  349. return nullptr;
  350. }
  351. if (!AttrUtils::SetTensor(op_desc, ATTR_NAME_WEIGHTS, const_value)) {
  352. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_WEIGHTS.c_str(),
  353. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  354. GELOGE(INTERNAL_ERROR, "Create Const op %s: set attr ATTR_NAME_WEIGHTS fail.", name.c_str());
  355. return nullptr;
  356. }
  357. if (op_desc->AddOutputDesc(data_desc) != GRAPH_SUCCESS) {
  358. REPORT_CALL_ERROR("E19999", "Add ouput desc to op:%s(%s) failed",
  359. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  360. GELOGE(INTERNAL_ERROR, "Create Const op %s: add output desc fail.", name.c_str());
  361. return nullptr;
  362. }
  363. GELOGI("Create Const op: %s", name.c_str());
  364. NodePtr const_node = graph->AddNode(op_desc);
  365. if (const_node == nullptr) {
  366. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  367. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph->GetName().c_str());
  368. GELOGE(INTERNAL_ERROR, "Create Const op %s fail.", name.c_str());
  369. return nullptr;
  370. }
  371. uint32_t out_idx = (flag ? SWITCH_TRUE_OUTPUT : SWITCH_FALSE_OUTPUT);
  372. if (GraphUtils::AddEdge(const_node->GetOutDataAnchor(0), merge_node->GetInDataAnchor(out_idx)) != GRAPH_SUCCESS) {
  373. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:0) and op:%s(%s)(index:%u) failed",
  374. const_node->GetName().c_str(), const_node->GetType().c_str(),
  375. merge_node->GetName().c_str(), merge_node->GetType().c_str(), out_idx);
  376. GELOGE(FAILED, "Add in data edge fail, %s->%s", const_node->GetName().c_str(), merge_node->GetName().c_str());
  377. return nullptr;
  378. }
  379. return const_node;
  380. }
  381. ///
  382. /// @brief Insert Identity Node
  383. /// @param [in] graph
  384. /// @param [in] name
  385. /// @param [in] data_desc
  386. /// @return NodePtr
  387. ///
  388. NodePtr ControlTriggerPass::InsertIdentityNode(ComputeGraphPtr &graph, const std::string &name,
  389. const GeTensorDesc &data_desc) {
  390. OpDescPtr op_desc = MakeShared<OpDesc>(name, IDENTITY);
  391. if (op_desc == nullptr) {
  392. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  393. GELOGE(FAILED, "Create Identity op %s: create op_desc fail.", name.c_str());
  394. return nullptr;
  395. }
  396. if ((op_desc->AddInputDesc(data_desc) != GRAPH_SUCCESS) || (op_desc->AddOutputDesc(data_desc) != GRAPH_SUCCESS)) {
  397. REPORT_CALL_ERROR("E19999", "Add input or output desc to op:%s(%s) failed",
  398. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  399. GELOGE(INTERNAL_ERROR, "Create Identity op %s: add input/output desc fail.", name.c_str());
  400. return nullptr;
  401. }
  402. GELOGI("Create Identity op:%s.", name.c_str());
  403. NodePtr identity_node = graph->AddNode(op_desc);
  404. if (identity_node == nullptr) {
  405. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  406. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph->GetName().c_str());
  407. GELOGE(INTERNAL_ERROR, "Create Identity op %s fail.", name.c_str());
  408. return nullptr;
  409. }
  410. return identity_node;
  411. }
  412. ///
  413. /// @brief Find pred_input of switch_node
  414. /// @param [in] switch_node
  415. /// @param [in] name
  416. /// @param [in] data_desc
  417. /// @return Status
  418. ///
  419. Status ControlTriggerPass::FindPredInput(const NodePtr &switch_node) {
  420. if (switch_node == nullptr) {
  421. REPORT_INNER_ERROR("E19999", "Param switch_node is nullptr, check invalid");
  422. GELOGE(INTERNAL_ERROR, "switch_node is null");
  423. return INTERNAL_ERROR;
  424. }
  425. InDataAnchorPtr in_cond_anchor = switch_node->GetInDataAnchor(SWITCH_PRED_INPUT);
  426. if (in_cond_anchor == nullptr) {
  427. REPORT_INNER_ERROR("E19999", "Index:%d in anchor of switch_node:%s(%s) is nullptr, check invalid",
  428. SWITCH_PRED_INPUT,
  429. switch_node->GetName().c_str(), switch_node->GetType().c_str());
  430. GELOGE(INTERNAL_ERROR, "in_cond_anchor is nullptr, node: %s.", switch_node->GetName().c_str());
  431. return INTERNAL_ERROR;
  432. }
  433. OutDataAnchorPtr pred_cond_anchor = in_cond_anchor->GetPeerOutAnchor();
  434. if (pred_cond_anchor == nullptr) {
  435. REPORT_INNER_ERROR("E19999", "Index:%d in anchor of switch_node:%s(%s), it's peer anchor is nullptr, "
  436. "check invalid", SWITCH_PRED_INPUT,
  437. switch_node->GetName().c_str(), switch_node->GetType().c_str());
  438. GELOGE(INTERNAL_ERROR, "pred_cond_anchor is nullptr, node: %s.", switch_node->GetName().c_str());
  439. return INTERNAL_ERROR;
  440. }
  441. switch_cond_map_[switch_node] = pred_cond_anchor->GetOwnerNode();
  442. return SUCCESS;
  443. }
  444. ///
  445. /// @brief Clear Status, used for subgraph pass
  446. /// @return SUCCESS
  447. ///
  448. Status ControlTriggerPass::ClearStatus() {
  449. switch_cond_map_.clear();
  450. control_trigger_map_.clear();
  451. return SUCCESS;
  452. }
  453. } // namespace ge

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