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.

ascend_control_parser.cc 26 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /**
  2. * Copyright 2019 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 <utility>
  17. #include <memory>
  18. #include "session/ascend_control_parser.h"
  19. #include "session/anf_runtime_algorithm.h"
  20. #include "utils/union_find_set.h"
  21. static constexpr size_t kCNodePrim = 0;
  22. static constexpr size_t kCNodeCallArg = 1;
  23. static constexpr size_t kCNodeSwitchCond = 1;
  24. static constexpr size_t kCNodeSwitchTrue = 2;
  25. static constexpr size_t kCNodeSwitchFalse = 3;
  26. static constexpr size_t kCNodeSwitchLength = 4;
  27. static constexpr size_t kCNodePartialLength = 2;
  28. static constexpr size_t kCNodePartialFunc = 1;
  29. static constexpr size_t kCNodeSwitchLayerBranch = 2;
  30. static constexpr size_t kCNodeSwitchLayerLength = 3;
  31. namespace mindspore {
  32. namespace session {
  33. static void InitUnionFindSet(NotNull<KernelGraphPtr> kg, const NotNull<UnionFindSet<AnfNodePtr> *> union_find_set,
  34. const NotNull<std::set<KernelGraphPtr> *> memo) {
  35. if (memo->find(kg.get()) != memo->end()) {
  36. return;
  37. }
  38. memo->insert(kg.get());
  39. const std::vector<std::pair<AnfNodePtr, std::vector<AnfNodePtr>>> &real_inputs = kg->real_inputs();
  40. for (auto &iter : real_inputs) {
  41. auto &para = iter.first;
  42. MS_EXCEPTION_IF_NULL(para);
  43. if (para->isa<Parameter>()) {
  44. union_find_set->Add(para);
  45. }
  46. for (auto &arg : iter.second) {
  47. MS_EXCEPTION_IF_NULL(arg);
  48. if (!arg->isa<Parameter>()) {
  49. continue;
  50. }
  51. union_find_set->Add(arg);
  52. }
  53. }
  54. for (auto &child : kg->child_graph_order()) {
  55. InitUnionFindSet(NOT_NULL(child), union_find_set, memo);
  56. }
  57. }
  58. static void UnionParentParameter(NotNull<KernelGraphPtr> kg, const NotNull<UnionFindSet<AnfNodePtr> *> union_find_set,
  59. const NotNull<std::set<KernelGraphPtr> *> memo) {
  60. if (memo->find(kg.get()) != memo->end()) {
  61. return;
  62. }
  63. memo->insert(kg.get());
  64. const std::vector<std::pair<AnfNodePtr, std::vector<AnfNodePtr>>> &real_inputs = kg->real_inputs();
  65. for (auto &iter : real_inputs) {
  66. auto &para = iter.first;
  67. for (auto &arg : iter.second) {
  68. MS_EXCEPTION_IF_NULL(arg);
  69. if (!arg->isa<Parameter>()) {
  70. continue;
  71. }
  72. union_find_set->Union(arg, para);
  73. }
  74. }
  75. for (auto &child : kg->child_graph_order()) {
  76. UnionParentParameter(NOT_NULL(child), union_find_set, memo);
  77. }
  78. }
  79. static UnionFindSet<AnfNodePtr> MakeUnionFindSet(NotNull<KernelGraphPtr> root_kg) {
  80. UnionFindSet<AnfNodePtr> result;
  81. std::set<KernelGraphPtr> memo;
  82. InitUnionFindSet(root_kg, NOT_NULL(&result), NOT_NULL(&memo));
  83. memo.clear();
  84. UnionParentParameter(root_kg, NOT_NULL(&result), NOT_NULL(&memo));
  85. return result;
  86. }
  87. static void RecursiveReplaceNode(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> main_parameter,
  88. const std::set<AnfNodePtr> &parameter_reuse_set,
  89. const NotNull<std::set<KernelGraphPtr> *> memo) {
  90. if (parameter_reuse_set.empty()) {
  91. MS_LOG(EXCEPTION) << "parameter_reuse_set is empty.";
  92. }
  93. if (memo->find(kg.get()) != memo->end()) {
  94. return;
  95. }
  96. memo->insert(kg.get());
  97. for (auto &para : parameter_reuse_set) {
  98. if (para == main_parameter.get()) {
  99. continue;
  100. }
  101. MS_EXCEPTION_IF_NULL(para);
  102. MS_LOG(INFO) << "Replace " << para->DebugString() << " of graph " << AnfAlgo::GetGraphId(para.get()) << " to "
  103. << main_parameter->DebugString() << " of graph " << AnfAlgo::GetGraphId(main_parameter.get().get());
  104. kg->ReplaceNode(NOT_NULL(para), main_parameter);
  105. }
  106. for (auto &child : kg->child_graph_order()) {
  107. RecursiveReplaceNode(NOT_NULL(child), main_parameter, parameter_reuse_set, memo);
  108. }
  109. }
  110. static void ReuseParameter(NotNull<KernelGraphPtr> root_kg, NotNull<UnionFindSet<AnfNodePtr> *> parameter_set) {
  111. auto parameter_reuse_sets = parameter_set->GetSets();
  112. for (auto &[key, parameter_reuse_set] : parameter_reuse_sets) {
  113. if (parameter_reuse_set.size() <= 1) {
  114. continue;
  115. }
  116. AnfNodePtr main_parameter = key;
  117. std::set<AnfNodePtr> root_inputs_set;
  118. const auto &root_inputs_vector = root_kg->inputs();
  119. root_inputs_set.insert(root_inputs_vector.begin(), root_inputs_vector.end());
  120. for (auto &node : parameter_reuse_set) {
  121. if (root_inputs_set.find(node) != root_inputs_set.end()) {
  122. main_parameter = node;
  123. break;
  124. }
  125. }
  126. std::set<KernelGraphPtr> memo;
  127. RecursiveReplaceNode(root_kg, NOT_NULL(main_parameter), parameter_reuse_set, NOT_NULL(&memo));
  128. }
  129. }
  130. CNodePtr GetNextRealKernel(const std::vector<CNodePtr> &list, size_t start) {
  131. for (size_t i = start; i < list.size() - 1; ++i) {
  132. if (!IsPrimitiveCNode(list[i], prim::kPrimPartial) && AnfAlgo::IsRealKernel(list[i])) {
  133. return list[i];
  134. }
  135. }
  136. return nullptr;
  137. }
  138. void AscendControlParser::LinkGraph(NotNull<KernelGraphPtr> kg) {
  139. std::set<KernelGraphPtr> memo;
  140. (void)ProcessKernelGraph(kg, nullptr, nullptr, NOT_NULL(&memo));
  141. std::map<uint32_t, KernelGraphPtr> graph_id_map;
  142. for (auto &g : memo) {
  143. if (graph_id_map.find(g->graph_id()) != graph_id_map.end()) {
  144. MS_LOG(EXCEPTION) << "Two graph has same graph id " << g->graph_id()
  145. << ", graph: " << graph_id_map[g->graph_id()]->ToString() << " " << g->ToString();
  146. }
  147. graph_id_map[g->graph_id()] = g;
  148. }
  149. // Make UnionFindSet
  150. UnionFindSet<AnfNodePtr> parameter_set = MakeUnionFindSet(kg);
  151. // Reuse Parameter
  152. ReuseParameter(kg, NOT_NULL(&parameter_set));
  153. // Insert Assign
  154. ChildGraphDataAssign(graph_id_map);
  155. }
  156. void AscendControlParser::ExecutorValidate(NotNull<KernelGraphPtr> root_graph) {
  157. std::set<KernelGraphPtr> memo;
  158. (void)RecurseGraph(root_graph, NOT_NULL(&memo));
  159. }
  160. void AscendControlParser::ChildGraphDataAssign(const std::map<uint32_t, KernelGraphPtr> &graph_id_map) {
  161. for (auto &iter : graph_id_map) {
  162. auto &kg = iter.second;
  163. MS_EXCEPTION_IF_NULL(kg);
  164. std::set<std::pair<AnfNodePtr, AnfNodePtr>> memo;
  165. const std::vector<std::pair<AnfNodePtr, std::vector<AnfNodePtr>>> &real_inputs = kg->real_inputs();
  166. for (auto &it : real_inputs) {
  167. auto &parameter = it.first;
  168. auto &args = it.second;
  169. for (auto &arg : args) {
  170. MS_EXCEPTION_IF_NULL(arg);
  171. if (memo.find({parameter, arg}) != memo.end()) {
  172. continue;
  173. } else {
  174. memo.emplace(parameter, arg);
  175. }
  176. if (arg->isa<Parameter>()) {
  177. MS_EXCEPTION_IF_NULL(parameter);
  178. MS_LOG(DEBUG) << "Parameter should be reused, no need insert assign, parameter: " << parameter->DebugString()
  179. << ", arg:" << arg->DebugString();
  180. continue;
  181. }
  182. auto target_graph_iter = graph_id_map.find(AnfAlgo::GetGraphId(arg.get()));
  183. if (target_graph_iter == graph_id_map.end()) {
  184. MS_LOG(EXCEPTION) << "Graph id " << AnfAlgo::GetGraphId(arg.get()) << " not found.";
  185. }
  186. InsertMultipleAssignToGraph(NOT_NULL(target_graph_iter->second), NOT_NULL(arg), NOT_NULL(parameter));
  187. }
  188. }
  189. }
  190. }
  191. NotNull<CNodePtr> AscendControlParser::ProcessKernelGraph(NotNull<KernelGraphPtr> kg, const CNodePtr &last_node,
  192. const CNodePtr &last_label,
  193. const NotNull<std::set<KernelGraphPtr> *> memo) {
  194. MS_LOG(INFO) << "Start process KernelGraph " << kg->ToString();
  195. // 1. recursive condition
  196. if (memo->find(kg) != memo->end()) {
  197. MS_LOG(INFO) << "KernelGraph has beed processed: " << kg->ToString();
  198. return NOT_NULL(kg->get_start_label());
  199. }
  200. memo->insert(kg.get());
  201. // 2. args replace placeholder
  202. LinkParentGraph(kg, last_node, last_label);
  203. // 3. topological sort
  204. kg->SetExecOrderByDefault();
  205. const std::vector<CNodePtr> &nodes = kg->execution_order();
  206. // 4. insert first_label
  207. CNodePtr start_label;
  208. if (last_node != nullptr && last_label != nullptr) {
  209. start_label = kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSetOpName))});
  210. MS_LOG(INFO) << "Insert start label " << start_label->DebugString() << " to " << kg->ToString();
  211. kg->set_start_label(start_label);
  212. } else {
  213. // no goto node will jump to start label of root graph, so return a fake label
  214. start_label = std::make_shared<CNode>(std::vector<AnfNodePtr>(), FuncGraphPtr(nullptr));
  215. }
  216. // 5. traverse
  217. for (size_t i = 0; i < nodes.size(); ++i) {
  218. auto &cnode = nodes[i];
  219. if (cnode->size() < kCNodePrim + 1) {
  220. MS_LOG(EXCEPTION) << "Inputs of apply node is empty";
  221. }
  222. AnfNodePtr fn = cnode->input(kAnfPrimitiveIndex);
  223. if (!IsPrimitive(fn, prim::kPrimCall) || cnode->size() < kCNodeCallArg + 1) {
  224. MS_LOG(DEBUG) << "continue node " << cnode->DebugString();
  225. continue;
  226. }
  227. AnfNodePtr arg = cnode->input(kFirstDataInputIndex);
  228. if (IsValueNode<KernelGraph>(arg)) {
  229. RecurseCall(kg, NOT_NULL(cnode), GetNextRealKernel(nodes, i + 1), memo);
  230. } else if (!arg->isa<CNode>()) {
  231. MS_LOG(EXCEPTION) << "Unknown type call node " << cnode->DebugString();
  232. } else if (IsPrimitiveCNode(arg->cast<CNodePtr>(), prim::kPrimSwitch)) {
  233. auto arg_cnode = arg->cast<CNodePtr>();
  234. MS_EXCEPTION_IF_NULL(arg_cnode);
  235. cnode->set_inputs(arg_cnode->inputs());
  236. RecurseSwitch(kg, NOT_NULL(cnode), GetNextRealKernel(nodes, i + 1), memo);
  237. } else if (IsPrimitiveCNode(arg->cast<CNodePtr>(), prim::kPrimSwitchLayer)) {
  238. auto arg_cnode = arg->cast<CNodePtr>();
  239. MS_EXCEPTION_IF_NULL(arg_cnode);
  240. cnode->set_inputs(arg_cnode->inputs());
  241. RecurseSwitchLayer(kg, NOT_NULL(cnode), GetNextRealKernel(nodes, i + 1), memo);
  242. }
  243. }
  244. kg->SetExecOrderByDefault();
  245. MS_LOG(INFO) << "End KernelGraph process: " << kg->ToString();
  246. return NOT_NULL(start_label);
  247. }
  248. void AscendControlParser::InsertDependToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> attch_node) {
  249. auto return_node = kg->get_return();
  250. MS_EXCEPTION_IF_NULL(return_node);
  251. std::vector<AnfNodePtr> inputs = {NewValueNode(std::make_shared<Primitive>(prim::kPrimDepend->name())),
  252. return_node->input(kFirstDataInputIndex), attch_node.get()};
  253. auto depend_node = kg->NewCNode(inputs);
  254. return_node->set_input(1, depend_node);
  255. }
  256. void AscendControlParser::InsertControlDependToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> first_node,
  257. NotNull<AnfNodePtr> second_node) {
  258. MS_LOG(INFO) << "Insert control depend at the end of graph, the first node is " << first_node->DebugString()
  259. << ", the second node is " << second_node->DebugString();
  260. std::vector<AnfNodePtr> inputs = {NewValueNode(std::make_shared<Primitive>(prim::kPrimControlDepend->name())),
  261. first_node, second_node};
  262. auto control_depend = kg->NewCNode(inputs);
  263. InsertDependToGraph(kg, NOT_NULL(control_depend));
  264. }
  265. void AscendControlParser::LinkParentGraph(NotNull<KernelGraphPtr> kg, const CNodePtr &from_graph_call_node,
  266. const CNodePtr &last_label) {
  267. // if not entry graph, replace return with label_goto
  268. if (from_graph_call_node != nullptr && last_label != nullptr) {
  269. auto label_goto =
  270. kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelGotoOpName)), last_label});
  271. MS_LOG(INFO) << "Insert end goto " << label_goto->DebugString() << " to " << kg->ToString();
  272. kg->set_end_goto(label_goto);
  273. }
  274. }
  275. void AscendControlParser::RecurseCall(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node, const CNodePtr &next_node,
  276. const NotNull<std::set<KernelGraphPtr> *> memo) {
  277. MS_LOG(INFO) << "Process call func " << cur_node->DebugString();
  278. // 1 get kernel graph
  279. const std::vector<AnfNodePtr> &origin_inputs = cur_node->inputs();
  280. if (kCNodeCallArg >= origin_inputs.size()) {
  281. MS_LOG(EXCEPTION) << "Index out of range,size:" << origin_inputs.size();
  282. }
  283. std::vector<AnfNodePtr> new_inputs = {std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelGotoOpName))};
  284. if (!IsValueNode<KernelGraph>(origin_inputs[kCNodeCallArg])) {
  285. MS_LOG(WARNING) << "Node " << cur_node->DebugString(10) << " index " << kCNodeCallArg << " is not a ValueNode";
  286. return;
  287. }
  288. // 2 return label
  289. auto back_label = kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSetOpName))});
  290. MS_LOG(INFO) << "Insert back label " << back_label->DebugString() << " to " << kg->ToString() << " call node "
  291. << cur_node->DebugString();
  292. // 3 add depend relationship
  293. InsertControlDependToGraph(kg, cur_node, NOT_NULL(back_label));
  294. if (next_node != nullptr && next_node != kg->get_return()) {
  295. InsertControlDependToGraph(kg, NOT_NULL(back_label), NOT_NULL(next_node));
  296. }
  297. auto call_kg = GetValueNode<KernelGraphPtr>(origin_inputs[kCNodeCallArg]);
  298. // 4 modify call op to goto op
  299. cur_node->set_input(kCNodePrim, new_inputs[kCNodePrim]);
  300. // 5 recurse sub graph
  301. CNodePtr sub_label = ProcessKernelGraph(NOT_NULL(call_kg), cur_node, back_label, memo);
  302. new_inputs.push_back(sub_label);
  303. new_inputs.insert(new_inputs.end(), origin_inputs.begin(), origin_inputs.end());
  304. cur_node->set_inputs(new_inputs);
  305. cur_node->set_abstract(nullptr);
  306. MS_LOG(INFO) << "Succeed processing call func " << cur_node->DebugString();
  307. }
  308. void AscendControlParser::RecurseSwitch(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node,
  309. const CNodePtr &next_node, const NotNull<std::set<KernelGraphPtr> *> memo) {
  310. MS_LOG(INFO) << "Process switch node " << cur_node->DebugString();
  311. if (cur_node->size() < kCNodeSwitchLength) {
  312. MS_LOG(EXCEPTION) << "Inputs of apply node must more than " << kCNodeSwitchLength;
  313. }
  314. // 1 return label
  315. auto back_label = kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSetOpName))});
  316. MS_LOG(INFO) << "Insert back label " << back_label->DebugString() << " to " << kg->ToString() << " switch node "
  317. << cur_node->DebugString();
  318. // 2 add depend relationship
  319. InsertControlDependToGraph(kg, cur_node, NOT_NULL(back_label));
  320. if (next_node != nullptr && next_node != kg->get_return()) {
  321. InsertControlDependToGraph(kg, NOT_NULL(back_label), NOT_NULL(next_node));
  322. }
  323. // 3 recurse sub graph
  324. const std::vector<AnfNodePtr> &origin_switch_inputs = cur_node->inputs();
  325. std::vector<AnfNodePtr> new_switch_inputs = {
  326. std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSwitchOpName)),
  327. origin_switch_inputs[kCNodeSwitchCond]};
  328. for (size_t i = kCNodeSwitchCond + 1; i < kCNodeSwitchLength; ++i) {
  329. // 3.1 branch kernel graph and args
  330. KernelGraphPtr branch_fg;
  331. std::tie(std::ignore, branch_fg) = ParsePartial(NOT_NULL(origin_switch_inputs[i]));
  332. // 3.2 recurse sub graph
  333. CNodePtr branch_label = ProcessKernelGraph(NOT_NULL(branch_fg), cur_node, back_label, memo);
  334. new_switch_inputs.push_back(branch_label);
  335. }
  336. std::swap(new_switch_inputs[kCNodeSwitchTrue], new_switch_inputs[kCNodeSwitchFalse]);
  337. new_switch_inputs.insert(new_switch_inputs.end(), origin_switch_inputs.begin(), origin_switch_inputs.end());
  338. cur_node->set_inputs(new_switch_inputs);
  339. cur_node->set_abstract(nullptr);
  340. MS_LOG(INFO) << "Succeed processing switch func " << cur_node->DebugString();
  341. }
  342. void AscendControlParser::RecurseSwitchLayer(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node,
  343. const CNodePtr &next_node,
  344. const NotNull<std::set<KernelGraphPtr> *> memo) {
  345. MS_LOG(INFO) << "Process switch node " << cur_node->DebugString();
  346. if (cur_node->size() < kCNodeSwitchLayerLength) {
  347. MS_LOG(EXCEPTION) << "Inputs of apply node must more than " << kCNodeSwitchLayerLength;
  348. }
  349. auto branch_tuple = cur_node->input(kCNodeSwitchLayerBranch);
  350. MS_EXCEPTION_IF_NULL(branch_tuple);
  351. if (!branch_tuple->isa<CNode>()) {
  352. MS_LOG(EXCEPTION) << branch_tuple->DebugString() << " is not a CNode";
  353. }
  354. const std::vector<AnfNodePtr> &branch_partial = utils::cast<CNodePtr>(branch_tuple)->inputs();
  355. // 1 return label
  356. auto back_label = kg->NewCNode({std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSetOpName))});
  357. // 2 add depend relationship
  358. InsertControlDependToGraph(kg, cur_node, NOT_NULL(back_label));
  359. if (next_node != nullptr && next_node != kg->get_return()) {
  360. InsertControlDependToGraph(kg, NOT_NULL(back_label), NOT_NULL(next_node));
  361. }
  362. // 3 recurse sub graph
  363. const std::vector<AnfNodePtr> &origin_switch_inputs = cur_node->inputs();
  364. if (kCNodeSwitchCond >= origin_switch_inputs.size()) {
  365. MS_LOG(EXCEPTION) << "Index out of range:" << origin_switch_inputs.size() << ".";
  366. }
  367. std::vector<AnfNodePtr> new_switch_inputs = {
  368. std::make_shared<ValueNode>(std::make_shared<Primitive>(kLabelSwitchOpName)),
  369. origin_switch_inputs[kCNodeSwitchCond]};
  370. for (size_t i = 0; i < branch_partial.size(); ++i) {
  371. // 3.1 branch kernel graph and args
  372. KernelGraphPtr branch_fg;
  373. std::tie(std::ignore, branch_fg) = ParsePartial(NOT_NULL(origin_switch_inputs[i]));
  374. // 3.2 recurse sub graph
  375. CNodePtr branch_label = ProcessKernelGraph(NOT_NULL(branch_fg), cur_node, back_label, memo);
  376. new_switch_inputs.push_back(branch_label);
  377. }
  378. new_switch_inputs.insert(new_switch_inputs.end(), branch_partial.begin(), branch_partial.end());
  379. cur_node->set_inputs(new_switch_inputs);
  380. cur_node->set_abstract(nullptr);
  381. MS_LOG(INFO) << "Succeed processing switch layer " << cur_node->DebugString();
  382. }
  383. std::tuple<CNodePtr, KernelGraphPtr> AscendControlParser::ParsePartial(NotNull<AnfNodePtr> node) {
  384. if (!node.get()->isa<CNode>()) {
  385. MS_LOG(EXCEPTION) << "Switch branches must be partial, node: " << node->DebugString();
  386. }
  387. // 2.1 branch kernel graph and args
  388. auto partial_cnode = utils::cast<CNodePtr>(node.get());
  389. MS_EXCEPTION_IF_NULL(partial_cnode);
  390. if (partial_cnode->size() < kCNodePartialLength) {
  391. MS_LOG(EXCEPTION) << "Inputs of partial node must more than " << kCNodePartialLength;
  392. }
  393. const auto &partial_inputs = partial_cnode->inputs();
  394. if (kCNodePartialFunc >= partial_inputs.size()) {
  395. MS_LOG(EXCEPTION) << "Index out of range:" << partial_inputs.size() << ".";
  396. }
  397. auto branch_kg = GetValueNode<KernelGraphPtr>(partial_inputs[kCNodePartialFunc]);
  398. return {partial_cnode, branch_kg};
  399. }
  400. void AscendControlParser::InsertMultipleAssignToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> from,
  401. NotNull<AnfNodePtr> to) {
  402. std::vector<AnfNodePtr> from_outputs = AnfAlgo::GetAllOutput(from, {prim::kPrimTupleGetItem});
  403. std::vector<AnfNodePtr> to_outputs = AnfAlgo::GetAllOutput(to, {prim::kPrimTupleGetItem});
  404. MS_LOG(INFO) << "Insert multi-assign from [" << from->DebugString() << "] to [" << to->DebugString() << "]";
  405. if (from_outputs.size() != to_outputs.size()) {
  406. MS_LOG(EXCEPTION) << "From outputs size[" << from_outputs.size() << "] is not equal to to outputs size["
  407. << to_outputs.size() << "]";
  408. }
  409. for (size_t i = 0; i < from_outputs.size(); i++) {
  410. InsertAssignToGraph(kg, NOT_NULL(from_outputs[i]), NOT_NULL(to_outputs[i]));
  411. }
  412. }
  413. void AscendControlParser::InsertAssignToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> from,
  414. NotNull<AnfNodePtr> to) {
  415. if (AnfAlgo::OutputAddrExist(from, 0) && AnfAlgo::OutputAddrExist(to, 0) &&
  416. AnfAlgo::GetOutputAddr(from, 0) == AnfAlgo::GetOutputAddr(to, 0)) {
  417. return;
  418. }
  419. if (from.get() == to.get()) {
  420. return;
  421. }
  422. MS_LOG(INFO) << "Insert assign to graph " << kg->ToString() << " from " << from->DebugString() << " to "
  423. << to->DebugString();
  424. // config inputs of assign node
  425. std::vector<AnfNodePtr> inputs = {NewValueNode(std::make_shared<Primitive>(prim::kPrimAssign->name())), to, from};
  426. // generate a new cnode
  427. auto assign_node = kg->NewCNode(inputs);
  428. MS_EXCEPTION_IF_NULL(assign_node);
  429. assign_node->set_abstract(to->abstract());
  430. // append the assign at the end of from graph
  431. InsertDependToGraph(kg, NOT_NULL(assign_node));
  432. }
  433. std::vector<CNodePtr> AscendControlParser::RecurseGraph(NotNull<KernelGraphPtr> graph,
  434. const NotNull<std::set<KernelGraphPtr> *> memo) {
  435. MS_LOG(INFO) << "Graph:" << graph->graph_id() << " start";
  436. if (memo->find(graph) != memo->end()) {
  437. return {};
  438. }
  439. memo->insert(graph.get());
  440. graph->SetExecOrderByDefault();
  441. std::vector<CNodePtr> cnodes = graph->execution_order();
  442. auto end_label_goto = graph->get_end_goto();
  443. if (cnodes.rbegin() != cnodes.rend() && *cnodes.rbegin() == end_label_goto) {
  444. cnodes.pop_back();
  445. }
  446. AnfAlgo::ReorderExecList(NOT_NULL(&cnodes));
  447. if (end_label_goto != nullptr) {
  448. cnodes.push_back(end_label_goto);
  449. }
  450. std::vector<CNodePtr> execution_order;
  451. uint32_t child_order_index = 0;
  452. for (auto &node : cnodes) {
  453. execution_order.push_back(node);
  454. if (node == graph->get_end_goto()) {
  455. continue;
  456. }
  457. if (AnfAlgo::CheckPrimitiveType(node, prim::kPrimLabelSwitch)) {
  458. std::vector<uint32_t> label_switch_list = AnfAlgo::GetNodeAttr<std::vector<uint32_t>>(node, kAttrLabelSwitchList);
  459. for (auto iter = label_switch_list.rbegin(); iter != label_switch_list.rend(); ++iter) {
  460. if (!CheckLabelIndex(child_order_index, *iter, node, graph)) {
  461. MS_LOG(EXCEPTION) << "Check label index fail";
  462. }
  463. if (child_order_index >= graph->child_graph_order().size()) {
  464. MS_LOG(EXCEPTION) << "Index out of range:" << graph->child_graph_order().size();
  465. }
  466. auto child_graph = graph->child_graph_order()[child_order_index++];
  467. auto child_execution_order = RecurseGraph(NOT_NULL(child_graph), memo);
  468. execution_order.insert(execution_order.end(), child_execution_order.begin(), child_execution_order.end());
  469. }
  470. } else if (AnfAlgo::CheckPrimitiveType(node, prim::kPrimLabelGoto)) {
  471. uint32_t label_index = AnfAlgo::GetNodeAttr<uint32_t>(node, kAttrLabelIndex);
  472. if (!CheckLabelIndex(child_order_index, label_index, node, graph)) {
  473. MS_LOG(EXCEPTION) << "Check label index fail";
  474. }
  475. auto child_graph = graph->child_graph_order()[child_order_index++];
  476. auto child_execution_order = RecurseGraph(NOT_NULL(child_graph), memo);
  477. execution_order.insert(execution_order.end(), child_execution_order.begin(), child_execution_order.end());
  478. }
  479. }
  480. graph->set_execution_order(execution_order);
  481. graph->PrintGraphExecuteOrder();
  482. return execution_order;
  483. }
  484. bool AscendControlParser::CheckLabelIndex(uint32_t order_index, uint32_t label_index, const CNodePtr &cur_label,
  485. NotNull<KernelGraphPtr> graph) {
  486. const std::vector<std::shared_ptr<KernelGraph>> &child_graph_order = graph->child_graph_order();
  487. // check index and child order size
  488. if (child_graph_order.size() <= IntToSize(order_index)) {
  489. MS_LOG(EXCEPTION) << "Child graph order is wrong, graph " << graph->ToString() << " child graph size "
  490. << child_graph_order.size() << " goto index " << order_index;
  491. }
  492. auto child_graph = child_graph_order[order_index];
  493. MS_EXCEPTION_IF_NULL(child_graph);
  494. // get start_label_set_index of child graph
  495. auto start_label_set = child_graph->get_start_label();
  496. uint32_t start_label_set_index = AnfAlgo::GetNodeAttr<uint32_t>(start_label_set, kAttrLabelIndex);
  497. if (label_index != start_label_set_index) {
  498. MS_LOG(WARNING) << cur_label->DebugString() << " index " << label_index << " but " << start_label_set->DebugString()
  499. << " index " << start_label_set_index << " current child graph order : " << order_index;
  500. return false;
  501. } else {
  502. return true;
  503. }
  504. }
  505. void AscendControlParser::UpdateChildGraphOrder(NotNull<KernelGraphPtr> kg) {
  506. MS_LOG(INFO) << "Graph id:" << kg->graph_id();
  507. kg->SetExecOrderByDefault();
  508. auto call_nodes = kg->FindNodeByPrimitive(std::make_shared<Primitive>(prim::kPrimCall->name()));
  509. std::vector<KernelGraphPtr> child_graph_order;
  510. for (auto &call_node : call_nodes) {
  511. MS_EXCEPTION_IF_NULL(call_node);
  512. auto call_child_graphs = AnfAlgo::GetCallNodeKernelGraph(call_node->cast<CNodePtr>());
  513. for (const auto &child_graph : call_child_graphs) {
  514. MS_EXCEPTION_IF_NULL(child_graph);
  515. if (child_graph != kg->parent_graph()) {
  516. child_graph->set_parent_graph(kg.get());
  517. }
  518. child_graph_order.push_back(child_graph);
  519. }
  520. }
  521. for (size_t i = 0; i < child_graph_order.size(); i++) {
  522. MS_LOG(INFO) << "child graph[" << i << "][id:" << child_graph_order[i]->graph_id() << "]";
  523. }
  524. kg->set_child_graph_order(child_graph_order);
  525. }
  526. } // namespace session
  527. } // namespace mindspore