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 16 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. GELOGE(FAILED, "Replace ctrl edge fail, %s->%s, %s->%s.", in_ctrl_node->GetName().c_str(),
  69. node->GetName().c_str(), in_ctrl_node->GetName().c_str(), constant->GetName().c_str());
  70. return FAILED;
  71. }
  72. GELOGI("No need to insert new branch.");
  73. return SUCCESS;
  74. }
  75. }
  76. if (InsertOppositeBranch(graph, node, in_ctrl_node, switch_node, branch_flag) != SUCCESS) {
  77. GELOGE(FAILED, "InsertOppositeBranch fail.");
  78. return FAILED;
  79. }
  80. return SUCCESS;
  81. }
  82. ///
  83. /// @brief Find switch_node for ControlTrigger node
  84. /// @param [in] node
  85. /// @param [out] switch_node
  86. /// @param [out] branch_flag
  87. /// @return Status
  88. ///
  89. Status ControlTriggerPass::FindSwitchNode(const NodePtr &node, NodePtr &switch_node, bool &branch_flag) {
  90. std::set<std::pair<NodePtr, uint32_t>> handle_nodes;
  91. // {node, <idx, <cond_merge_num, loop_switchf_num>>}
  92. std::stack<std::pair<NodePtr, std::pair<uint32_t, std::pair<uint32_t, uint32_t>>>> nodes;
  93. nodes.push(std::make_pair(node, std::make_pair(UINT32_MAX, std::make_pair(0, 0))));
  94. std::set<std::pair<NodePtr, uint32_t>> in_nodes;
  95. while (!nodes.empty()) {
  96. auto iter = nodes.top();
  97. NodePtr tmp_node = iter.first;
  98. GE_CHECK_NOTNULL(tmp_node);
  99. nodes.pop();
  100. uint32_t index = iter.second.first;
  101. auto num_pair = iter.second.second;
  102. if (handle_nodes.count(std::make_pair(tmp_node, index)) > 0) {
  103. continue;
  104. }
  105. switch (TransferNodeType(tmp_node, index)) {
  106. case kCondSwitch:
  107. if (num_pair.first == 0) {
  108. switch_node = tmp_node;
  109. branch_flag = (index == SWITCH_TRUE_OUTPUT);
  110. GELOGI("FindSwitchNode succ, switch_node=%s, idx=%u", switch_node->GetName().c_str(), index);
  111. return SUCCESS;
  112. }
  113. num_pair.first--;
  114. break;
  115. case kCondMerge:
  116. num_pair.first++;
  117. break;
  118. case kLoopSwitchT:
  119. GELOGI("in while_body, no need handle");
  120. return SUCCESS;
  121. case kLoopSwitchF:
  122. num_pair.second++;
  123. break;
  124. case kEnter:
  125. if (num_pair.second > 0) {
  126. num_pair.second--;
  127. }
  128. break;
  129. case kNotControlOp:
  130. break;
  131. default:
  132. GELOGE(FAILED, "invalid type");
  133. return FAILED;
  134. }
  135. GetInNodes(tmp_node, in_nodes);
  136. for (auto &node_idx : in_nodes) {
  137. nodes.push(std::make_pair(node_idx.first, std::make_pair(node_idx.second, num_pair)));
  138. }
  139. (void)handle_nodes.insert(std::make_pair(tmp_node, index));
  140. }
  141. return SUCCESS;
  142. }
  143. ///
  144. /// @brief Check if need insert opposite branch
  145. /// @param [in] node
  146. /// @param [in] index
  147. /// @return ControlNodeType
  148. ///
  149. ControlNodeType ControlTriggerPass::TransferNodeType(const NodePtr &node, uint32_t index) {
  150. const std::string type = node->GetType();
  151. if ((type == SWITCH) || (type == REFSWITCH)) {
  152. if ((index != SWITCH_TRUE_OUTPUT) && (index != SWITCH_FALSE_OUTPUT)) {
  153. GELOGI("TransferNodeType: neither true nor false branch.");
  154. return kNotControlOp;
  155. }
  156. if (FindPredInput(node) != SUCCESS) {
  157. GELOGE(INTERNAL_ERROR, "FindPredInput fail, switch_node: %s.", node->GetName().c_str());
  158. return kInvalidType;
  159. }
  160. NodePtr pred_node = switch_cond_map_[node];
  161. bool branch_flag = (index == SWITCH_TRUE_OUTPUT);
  162. if (pred_node->GetType() != LOOPCOND) {
  163. GELOGI("TransferNodeType: kCondSwitch node=%s, idx=%u", node->GetName().c_str(), index);
  164. return kCondSwitch;
  165. } else {
  166. GELOGI("TransferNodeType: kLoopSwitch node=%s, idx=%u", node->GetName().c_str(), index);
  167. return branch_flag ? kLoopSwitchT : kLoopSwitchF;
  168. }
  169. } else if ((type == MERGE) || (type == REFMERGE)) {
  170. OpDescPtr merge_desc = node->GetOpDesc();
  171. if (merge_desc == nullptr) {
  172. GELOGE(INTERNAL_ERROR, "FindPredInput fail, merge_desc is null, merge_node: %s.", node->GetName().c_str());
  173. return kInvalidType;
  174. }
  175. if (!merge_desc->HasAttr(ATTR_NAME_NEXT_ITERATION)) {
  176. return kCondMerge;
  177. }
  178. } else if ((type == ENTER) || (type == REFENTER)) {
  179. return kEnter;
  180. }
  181. return kNotControlOp;
  182. }
  183. ///
  184. /// @brief Get in_node & idx pairs
  185. /// @param [in] node
  186. /// @param [out] in_nodes
  187. /// @return void
  188. ///
  189. void ControlTriggerPass::GetInNodes(const NodePtr &node, std::set<std::pair<NodePtr, uint32_t>> &in_nodes) {
  190. in_nodes.clear();
  191. for (auto &in_ctrl_node : node->GetInControlNodes()) {
  192. (void)in_nodes.insert(std::make_pair(in_ctrl_node, UINT32_MAX));
  193. }
  194. for (InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  195. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  196. if (peer_out_anchor == nullptr) {
  197. continue;
  198. }
  199. (void)in_nodes.insert(std::make_pair(peer_out_anchor->GetOwnerNode(), peer_out_anchor->GetIdx()));
  200. }
  201. return;
  202. }
  203. ///
  204. /// @brief Insert opposite branch for ControlTrigger
  205. /// @param [in] graph
  206. /// @param [in] ControlTrigger node
  207. /// @param [in] in_ctrl_node
  208. /// @param [in] switch_node
  209. /// @param [in] branch_flag
  210. /// @return Status
  211. ///
  212. Status ControlTriggerPass::InsertOppositeBranch(ComputeGraphPtr &graph, NodePtr &node, NodePtr &in_ctrl_node,
  213. NodePtr &switch_node, bool branch_flag) {
  214. GE_CHECK_NOTNULL(node);
  215. GE_CHECK_NOTNULL(in_ctrl_node);
  216. GE_CHECK_NOTNULL(switch_node);
  217. OpDescPtr switch_desc = switch_node->GetOpDesc();
  218. GE_CHECK_NOTNULL(switch_desc);
  219. GeTensorDesc data_desc(GeShape(), FORMAT_NCHW, DT_INT32);
  220. NodePtr merge_node = InsertMergeNode(graph, node, in_ctrl_node, data_desc);
  221. if (merge_node == nullptr) {
  222. GELOGE(FAILED, "InsertMergeNode fail.");
  223. return FAILED;
  224. }
  225. NodePtr const_f = InsertConstNode(graph, merge_node, data_desc, false);
  226. NodePtr const_t = InsertConstNode(graph, merge_node, data_desc, true);
  227. if ((const_f == nullptr) || (const_t == nullptr)) {
  228. GELOGE(FAILED, "InsertConstNode fail.");
  229. return FAILED;
  230. }
  231. NodePtr orig_const = branch_flag ? const_t : const_f;
  232. NodePtr new_const = !branch_flag ? const_t : const_f;
  233. uint32_t new_idx = branch_flag ? SWITCH_FALSE_OUTPUT : SWITCH_TRUE_OUTPUT;
  234. const std::string identity_name = switch_desc->GetName() + "_" + IDENTITY;
  235. NodePtr identity_node = InsertIdentityNode(graph, identity_name, switch_desc->GetOutputDesc(new_idx));
  236. if (identity_node == nullptr) {
  237. GELOGE(FAILED, "InsertIdentityNode fail.");
  238. return FAILED;
  239. }
  240. if (GraphUtils::AddEdge(in_ctrl_node->GetOutControlAnchor(), orig_const->GetInControlAnchor()) != GRAPH_SUCCESS) {
  241. GELOGE(FAILED, "Add in ctrl edge fail, %s->%s.", in_ctrl_node->GetName().c_str(), orig_const->GetName().c_str());
  242. return FAILED;
  243. }
  244. if (GraphUtils::AddEdge(switch_node->GetOutDataAnchor(new_idx), identity_node->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  245. GELOGE(FAILED, "Add in data edge fail, %s->%s.", switch_desc->GetName().c_str(), identity_node->GetName().c_str());
  246. return FAILED;
  247. }
  248. if (GraphUtils::AddEdge(identity_node->GetOutControlAnchor(), new_const->GetInControlAnchor()) != GRAPH_SUCCESS) {
  249. GELOGE(FAILED, "Add in ctrl edge fail, %s->%s.", identity_node->GetName().c_str(), new_const->GetName().c_str());
  250. return FAILED;
  251. }
  252. auto pred_const = std::make_pair(switch_cond_map_[switch_node], std::make_pair(const_f, const_t));
  253. auto iter = control_trigger_map_.find(node);
  254. if (iter == control_trigger_map_.end()) {
  255. control_trigger_map_[node] = {pred_const};
  256. } else {
  257. if (!iter->second.insert(pred_const).second) {
  258. GELOGE(FAILED, "control_trigger_map_ insert failed.");
  259. return FAILED;
  260. }
  261. }
  262. return SUCCESS;
  263. }
  264. ///
  265. /// @brief Insert Merge Node
  266. /// @param [in] graph
  267. /// @param [in] node
  268. /// @param [in] in_ctrl_node
  269. /// @param [in] data_desc
  270. /// @return NodePtr
  271. ///
  272. NodePtr ControlTriggerPass::InsertMergeNode(ComputeGraphPtr &graph, NodePtr &node, NodePtr &in_ctrl_node,
  273. const GeTensorDesc &data_desc) {
  274. const std::string name = node->GetName() + "_" + MERGE;
  275. OpDescPtr op_desc = MakeShared<OpDesc>(name, MERGE);
  276. if (op_desc == nullptr) {
  277. GELOGE(FAILED, "Create Merge op %s: create op_desc fail.", name.c_str());
  278. return nullptr;
  279. }
  280. if ((op_desc->AddInputDesc(data_desc) != GRAPH_SUCCESS) || (op_desc->AddInputDesc(data_desc) != GRAPH_SUCCESS) ||
  281. (op_desc->AddOutputDesc(data_desc) != GRAPH_SUCCESS) || (op_desc->AddOutputDesc(data_desc) != GRAPH_SUCCESS)) {
  282. GELOGE(INTERNAL_ERROR, "Create Merge op %s: add input/output desc fail.", name.c_str());
  283. return nullptr;
  284. }
  285. GELOGI("Create Merge op:%s.", name.c_str());
  286. NodePtr merge_node = graph->AddNode(op_desc);
  287. if (merge_node == nullptr) {
  288. GELOGE(INTERNAL_ERROR, "Create Merge op %s fail.", name.c_str());
  289. return nullptr;
  290. }
  291. if ((GraphUtils::RemoveEdge(in_ctrl_node->GetOutControlAnchor(), node->GetInControlAnchor()) != GRAPH_SUCCESS) ||
  292. (GraphUtils::AddEdge(merge_node->GetOutControlAnchor(), node->GetInControlAnchor()) != GRAPH_SUCCESS)) {
  293. GELOGE(FAILED, "Replace ctrl edge fail, %s->%s, %s->%s", in_ctrl_node->GetName().c_str(), node->GetName().c_str(),
  294. merge_node->GetName().c_str(), node->GetName().c_str());
  295. return nullptr;
  296. }
  297. return merge_node;
  298. }
  299. ///
  300. /// @brief Insert Const Node
  301. /// @param [in] graph
  302. /// @param [in] merge_node
  303. /// @param [in] data_desc
  304. /// @param [in] flag
  305. /// @return NodePtr
  306. ///
  307. NodePtr ControlTriggerPass::InsertConstNode(ComputeGraphPtr &graph, NodePtr &merge_node, const GeTensorDesc &data_desc,
  308. bool flag) {
  309. const std::string name = merge_node->GetName() + "_" + CONSTANT + (flag ? "_t" : "_f");
  310. OpDescPtr op_desc = MakeShared<OpDesc>(name, CONSTANT);
  311. if (op_desc == nullptr) {
  312. GELOGE(FAILED, "Create Const op %s: create op_desc fail.", name.c_str());
  313. return nullptr;
  314. }
  315. int32_t value = 0;
  316. GeTensorPtr const_value = MakeShared<GeTensor>(data_desc, reinterpret_cast<uint8_t *>(&value), sizeof(int32_t));
  317. if (const_value == nullptr) {
  318. GELOGE(FAILED, "Create tensor fail.");
  319. return nullptr;
  320. }
  321. if (!AttrUtils::SetTensor(op_desc, ATTR_NAME_WEIGHTS, const_value)) {
  322. GELOGE(INTERNAL_ERROR, "Create Const op %s: set attr ATTR_NAME_WEIGHTS fail.", name.c_str());
  323. return nullptr;
  324. }
  325. if (op_desc->AddOutputDesc(data_desc) != GRAPH_SUCCESS) {
  326. GELOGE(INTERNAL_ERROR, "Create Const op %s: add output desc fail.", name.c_str());
  327. return nullptr;
  328. }
  329. GELOGI("Create Const op: %s", name.c_str());
  330. NodePtr const_node = graph->AddNode(op_desc);
  331. if (const_node == nullptr) {
  332. GELOGE(INTERNAL_ERROR, "Create Const op %s fail.", name.c_str());
  333. return nullptr;
  334. }
  335. uint32_t out_idx = (flag ? SWITCH_TRUE_OUTPUT : SWITCH_FALSE_OUTPUT);
  336. if (GraphUtils::AddEdge(const_node->GetOutDataAnchor(0), merge_node->GetInDataAnchor(out_idx)) != GRAPH_SUCCESS) {
  337. GELOGE(FAILED, "Add in data edge fail, %s->%s", const_node->GetName().c_str(), merge_node->GetName().c_str());
  338. return nullptr;
  339. }
  340. return const_node;
  341. }
  342. ///
  343. /// @brief Insert Identity Node
  344. /// @param [in] graph
  345. /// @param [in] name
  346. /// @param [in] data_desc
  347. /// @return NodePtr
  348. ///
  349. NodePtr ControlTriggerPass::InsertIdentityNode(ComputeGraphPtr &graph, const std::string &name,
  350. const GeTensorDesc &data_desc) {
  351. OpDescPtr op_desc = MakeShared<OpDesc>(name, IDENTITY);
  352. if (op_desc == nullptr) {
  353. GELOGE(FAILED, "Create Identity op %s: create op_desc fail.", name.c_str());
  354. return nullptr;
  355. }
  356. if ((op_desc->AddInputDesc(data_desc) != GRAPH_SUCCESS) || (op_desc->AddOutputDesc(data_desc) != GRAPH_SUCCESS)) {
  357. GELOGE(INTERNAL_ERROR, "Create Identity op %s: add input/output desc fail.", name.c_str());
  358. return nullptr;
  359. }
  360. GELOGI("Create Identity op:%s.", name.c_str());
  361. NodePtr identity_node = graph->AddNode(op_desc);
  362. if (identity_node == nullptr) {
  363. GELOGE(INTERNAL_ERROR, "Create Identity op %s fail.", name.c_str());
  364. return nullptr;
  365. }
  366. return identity_node;
  367. }
  368. ///
  369. /// @brief Find pred_input of switch_node
  370. /// @param [in] switch_node
  371. /// @param [in] name
  372. /// @param [in] data_desc
  373. /// @return Status
  374. ///
  375. Status ControlTriggerPass::FindPredInput(const NodePtr &switch_node) {
  376. if (switch_node == nullptr) {
  377. GELOGE(INTERNAL_ERROR, "switch_node is null");
  378. return INTERNAL_ERROR;
  379. }
  380. InDataAnchorPtr in_cond_anchor = switch_node->GetInDataAnchor(SWITCH_PRED_INPUT);
  381. if (in_cond_anchor == nullptr) {
  382. GELOGE(INTERNAL_ERROR, "in_cond_anchor is nullptr, node: %s.", switch_node->GetName().c_str());
  383. return INTERNAL_ERROR;
  384. }
  385. OutDataAnchorPtr pred_cond_anchor = in_cond_anchor->GetPeerOutAnchor();
  386. if (pred_cond_anchor == nullptr) {
  387. GELOGE(INTERNAL_ERROR, "pred_cond_anchor is nullptr, node: %s.", switch_node->GetName().c_str());
  388. return INTERNAL_ERROR;
  389. }
  390. switch_cond_map_[switch_node] = pred_cond_anchor->GetOwnerNode();
  391. return SUCCESS;
  392. }
  393. ///
  394. /// @brief Clear Status, used for subgraph pass
  395. /// @return SUCCESS
  396. ///
  397. Status ControlTriggerPass::ClearStatus() {
  398. switch_cond_map_.clear();
  399. control_trigger_map_.clear();
  400. return SUCCESS;
  401. }
  402. } // namespace ge

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