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.

kernel_adjust.cc 43 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 "runtime/device/kernel_adjust.h"
  17. #include <map>
  18. #include <algorithm>
  19. #include <string>
  20. #include <vector>
  21. #include <utility>
  22. #include "backend/session/anf_runtime_algorithm.h"
  23. #include "utils/ms_context.h"
  24. #include "common/trans.h"
  25. #include "utils/config_manager.h"
  26. #include "utils/ms_utils.h"
  27. #include "backend/kernel_compiler/kernel_build_info.h"
  28. #include "utils/utils.h"
  29. #include "runtime/device/ascend/profiling/profiling_manager.h"
  30. #include "runtime/base.h"
  31. #include "runtime/device/ascend/ascend_stream_assign.h"
  32. #include "utils/shape_utils.h"
  33. namespace {
  34. constexpr auto kProfilingGraphId = "PROFILING_GRAPH_ID";
  35. } // namespace
  36. namespace mindspore {
  37. namespace device {
  38. using device::ascend::ProfilingUtils;
  39. void KernelAdjust::ReorderGetNext(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  40. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  41. const std::vector<CNodePtr> &origin_cnode_list = kernel_graph_ptr->execution_order();
  42. std::vector<CNodePtr> getnext_list;
  43. std::vector<CNodePtr> other_list;
  44. for (const auto &cnode : origin_cnode_list) {
  45. if (AnfAlgo::GetCNodeName(cnode) == kGetNextOpName) {
  46. getnext_list.emplace_back(cnode);
  47. } else {
  48. other_list.emplace_back(cnode);
  49. }
  50. }
  51. std::vector<CNodePtr> new_order_list;
  52. new_order_list.insert(new_order_list.end(), getnext_list.begin(), getnext_list.end());
  53. new_order_list.insert(new_order_list.end(), other_list.begin(), other_list.end());
  54. kernel_graph_ptr->set_execution_order(new_order_list);
  55. }
  56. bool KernelAdjust::NeedInsertSwitch() {
  57. auto context_ptr = MsContext::GetInstance();
  58. MS_EXCEPTION_IF_NULL(context_ptr);
  59. return (context_ptr->get_param<bool>(MS_CTX_ENABLE_TASK_SINK) &&
  60. context_ptr->get_param<bool>(MS_CTX_ENABLE_LOOP_SINK) && ConfigManager::GetInstance().iter_num() > 1);
  61. }
  62. CNodePtr KernelAdjust::CreateSendApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr,
  63. uint32_t event_id) {
  64. MS_EXCEPTION_IF_NULL(graph_ptr);
  65. auto send_op = std::make_shared<Primitive>(kSendOpName);
  66. MS_EXCEPTION_IF_NULL(send_op);
  67. auto send_apply = std::make_shared<ValueNode>(send_op);
  68. MS_EXCEPTION_IF_NULL(send_apply);
  69. std::vector<AnfNodePtr> send_input_list = {send_apply};
  70. CNodePtr send_node_ptr = graph_ptr->NewCNode(send_input_list);
  71. MS_EXCEPTION_IF_NULL(send_node_ptr);
  72. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  73. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  74. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), send_node_ptr.get());
  75. AnfAlgo::SetNodeAttr(kAttrEventId, MakeValue(event_id), send_node_ptr);
  76. auto abstract_none = std::make_shared<abstract::AbstractNone>();
  77. MS_EXCEPTION_IF_NULL(abstract_none);
  78. send_node_ptr->set_abstract(abstract_none);
  79. return send_node_ptr;
  80. }
  81. CNodePtr KernelAdjust::CreateRecvApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr,
  82. uint32_t event_id) {
  83. MS_EXCEPTION_IF_NULL(graph_ptr);
  84. auto recv_op = std::make_shared<Primitive>(kRecvOpName);
  85. MS_EXCEPTION_IF_NULL(recv_op);
  86. auto recv_apply = std::make_shared<ValueNode>(recv_op);
  87. MS_EXCEPTION_IF_NULL(recv_apply);
  88. std::vector<AnfNodePtr> recv_input_list = {recv_apply};
  89. CNodePtr recv_node_ptr = graph_ptr->NewCNode(recv_input_list);
  90. MS_EXCEPTION_IF_NULL(recv_node_ptr);
  91. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  92. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  93. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), recv_node_ptr.get());
  94. AnfAlgo::SetNodeAttr(kAttrEventId, MakeValue(event_id), recv_node_ptr);
  95. auto abstract_none = std::make_shared<abstract::AbstractNone>();
  96. MS_EXCEPTION_IF_NULL(abstract_none);
  97. recv_node_ptr->set_abstract(abstract_none);
  98. return recv_node_ptr;
  99. }
  100. bool KernelAdjust::ExistGetNext(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  101. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  102. const std::vector<CNodePtr> &cnode_list = kernel_graph_ptr->execution_order();
  103. for (const auto &cnode : cnode_list) {
  104. if (AnfAlgo::GetCNodeName(cnode) == kGetNextOpName) {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. bool KernelAdjust::ExistIndependent(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  111. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  112. const auto &exe_orders = kernel_graph_ptr->execution_order();
  113. for (const auto &node : exe_orders) {
  114. if (AnfAlgo::IsIndependentNode(node)) {
  115. MS_LOG(INFO) << "graph exit independent node";
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. void KernelAdjust::InsertIndepentParallel(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  122. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  123. std::vector<CNodePtr> *exec_order) {
  124. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  125. MS_EXCEPTION_IF_NULL(exec_order);
  126. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  127. CNodePtr independent_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input, kIndependentStreamSwitch);
  128. MS_EXCEPTION_IF_NULL(independent_switch_app);
  129. uint32_t independent_switch_stream_id = resource_manager.ApplyNewStream();
  130. AnfAlgo::SetStreamId(independent_switch_stream_id, independent_switch_app.get());
  131. AnfAlgo::SetNodeAttr(kStreamNeedActivedFirst, MakeValue<bool>(true), independent_switch_app);
  132. AnfAlgo::SetNodeAttr(kAttrStreamSwitchKind, MakeValue<uint32_t>(kIndependentStreamSwitch), independent_switch_app);
  133. (*exec_order).push_back(independent_switch_app);
  134. MS_LOG(INFO) << "Independent op loop insert Stream Switch " << independent_switch_app->fullname_with_scope();
  135. }
  136. void KernelAdjust::InsertFpBpLoopStreamSwitch(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  137. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  138. std::vector<CNodePtr> *exec_order, uint32_t *fpbp_stream_id,
  139. uint32_t *fpbp_switch_stream_id) {
  140. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  141. MS_EXCEPTION_IF_NULL(exec_order);
  142. MS_EXCEPTION_IF_NULL(fpbp_stream_id);
  143. MS_EXCEPTION_IF_NULL(fpbp_switch_stream_id);
  144. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  145. *fpbp_switch_stream_id = resource_manager.ApplyNewStream();
  146. *fpbp_stream_id = resource_manager.ApplyNewStream();
  147. CNodePtr fpbp_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input, kFpBpStreamSwitch);
  148. MS_EXCEPTION_IF_NULL(fpbp_switch_app);
  149. AnfAlgo::SetStreamId(*fpbp_switch_stream_id, fpbp_switch_app.get());
  150. AnfAlgo::SetNodeAttr(kStreamNeedActivedFirst, MakeValue<bool>(true), fpbp_switch_app);
  151. // update fpbp loop stream switch true_branch_stream attr
  152. AnfAlgo::SetNodeAttr(kAttrTrueBranchStream, MakeValue<uint32_t>(*fpbp_stream_id), fpbp_switch_app);
  153. AnfAlgo::SetNodeAttr(kAttrStreamSwitchKind, MakeValue<uint32_t>(kFpBpStreamSwitch), fpbp_switch_app);
  154. (*exec_order).push_back(fpbp_switch_app);
  155. MS_LOG(INFO) << "FpBp loop insert Stream Switch " << fpbp_switch_app->fullname_with_scope();
  156. }
  157. void KernelAdjust::CopyMemcpyList(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  158. const std::vector<CNodePtr> &orders, size_t order_index,
  159. std::vector<CNodePtr> *memcpy_list, std::vector<CNodePtr> *other_list) {
  160. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  161. MS_EXCEPTION_IF_NULL(memcpy_list);
  162. MS_EXCEPTION_IF_NULL(other_list);
  163. CNodePtr cur_cnode = nullptr;
  164. for (size_t idx = order_index + 1; idx < orders.size(); idx++) {
  165. cur_cnode = orders[idx];
  166. if (AnfAlgo::HasNodeAttr(kAttrLabelForInsertStreamActive, cur_cnode)) {
  167. auto pre_node = orders[idx - 1];
  168. auto pre_kernel_name = AnfAlgo::GetCNodeName(pre_node);
  169. if (pre_kernel_name == kAtomicAddrCleanOpName) {
  170. (*other_list).pop_back();
  171. (*memcpy_list).push_back(pre_node);
  172. }
  173. (*memcpy_list).emplace_back(cur_cnode);
  174. } else {
  175. (*other_list).emplace_back(cur_cnode);
  176. }
  177. }
  178. }
  179. void KernelAdjust::InsertEosDoneRecv(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  180. std::vector<CNodePtr> *exec_order, uint32_t eos_done_event_id,
  181. uint32_t fpbp_stream_id) {
  182. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  183. MS_EXCEPTION_IF_NULL(exec_order);
  184. CNodePtr eos_done_recv = CreateRecvApplyKernel(kernel_graph_ptr, eos_done_event_id);
  185. AnfAlgo::SetStreamId(fpbp_stream_id, eos_done_recv.get());
  186. (*exec_order).push_back(eos_done_recv);
  187. MS_LOG(INFO) << "FpBp loop insert EoS done Recv " << eos_done_recv->fullname_with_scope();
  188. }
  189. void KernelAdjust::InsertGetNextLoopStreamActive(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  190. std::vector<CNodePtr> *exec_order,
  191. const std::vector<uint32_t> &getnext_active_streams,
  192. uint32_t getnext_switch_stream_id) {
  193. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  194. MS_EXCEPTION_IF_NULL(exec_order);
  195. CNodePtr getnext_active_app = CreateStreamActiveOp(kernel_graph_ptr);
  196. MS_EXCEPTION_IF_NULL(getnext_active_app);
  197. AnfAlgo::SetNodeAttr(kAttrActiveStreamList, MakeValue<std::vector<uint32_t>>(getnext_active_streams),
  198. getnext_active_app);
  199. (*exec_order).push_back(getnext_active_app);
  200. MS_LOG(INFO) << "FpBp loop insert GetNext loop Stream Active " << getnext_active_app->fullname_with_scope();
  201. }
  202. void KernelAdjust::InsertFpBpStartRecv(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  203. std::vector<CNodePtr> *exec_order, uint32_t fpbp_start_event_id,
  204. uint32_t fpbp_stream_id) {
  205. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  206. MS_EXCEPTION_IF_NULL(exec_order);
  207. CNodePtr fpbp_start_recv = CreateRecvApplyKernel(kernel_graph_ptr, fpbp_start_event_id);
  208. AnfAlgo::SetStreamId(fpbp_stream_id, fpbp_start_recv.get());
  209. (*exec_order).push_back(fpbp_start_recv);
  210. MS_LOG(INFO) << "FpBp loop insert FpBp start Recv " << fpbp_start_recv->fullname_with_scope();
  211. }
  212. void KernelAdjust::InsertNextLoopAssignAdd(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  213. std::vector<CNodePtr> *exec_order,
  214. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  215. uint32_t fpbp_stream_id) {
  216. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  217. MS_EXCEPTION_IF_NULL(exec_order);
  218. CNodePtr assign_add_one = CreateStreamAssignAddnOP(kernel_graph_ptr, switch_loop_input, false);
  219. MS_EXCEPTION_IF_NULL(assign_add_one);
  220. AnfAlgo::SetStreamId(fpbp_stream_id, assign_add_one.get());
  221. (*exec_order).push_back(assign_add_one);
  222. MS_LOG(INFO) << "FpBp loop insert next loop AssignAdd " << assign_add_one->fullname_with_scope();
  223. }
  224. void KernelAdjust::InsertCurrentLoopAssignAdd(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  225. std::vector<CNodePtr> *exec_order,
  226. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input) {
  227. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  228. MS_EXCEPTION_IF_NULL(exec_order);
  229. CNodePtr cur_assign_add = CreateStreamAssignAddnOP(kernel_graph_ptr, switch_loop_input, true);
  230. MS_EXCEPTION_IF_NULL(cur_assign_add);
  231. AnfAlgo::SetNodeAttr(kAttrFpBpEnd, MakeValue<bool>(true), cur_assign_add);
  232. (*exec_order).push_back(cur_assign_add);
  233. MS_LOG(INFO) << "FpBp loop insert current loop AssignAdd " << cur_assign_add->fullname_with_scope();
  234. }
  235. void KernelAdjust::InsertFpBpAndEosLoopStreamActive(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  236. std::vector<CNodePtr> *exec_order,
  237. const std::vector<uint32_t> &fpbp_active_streams) {
  238. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  239. MS_EXCEPTION_IF_NULL(exec_order);
  240. CNodePtr fpbp_active_app = CreateStreamActiveOp(kernel_graph_ptr);
  241. MS_EXCEPTION_IF_NULL(fpbp_active_app);
  242. AnfAlgo::SetNodeAttr(kAttrActiveStreamList, MakeValue<std::vector<uint32_t>>(fpbp_active_streams), fpbp_active_app);
  243. (*exec_order).push_back(fpbp_active_app);
  244. MS_LOG(INFO) << "FpBp loop insert FpBp loop and Eos loop Stream Active " << fpbp_active_app->fullname_with_scope();
  245. }
  246. void KernelAdjust::InsertSwitchLoopInput(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  247. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input) {
  248. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  249. std::vector<AnfNodePtr> *mute_inputs = kernel_graph_ptr->MutableInputs();
  250. MS_EXCEPTION_IF_NULL(mute_inputs);
  251. mute_inputs->push_back(switch_loop_input.at(kCurLoopCountParamName));
  252. mute_inputs->push_back(switch_loop_input.at(kNextLoopCountParamName));
  253. mute_inputs->push_back(switch_loop_input.at(kEpochParamName));
  254. mute_inputs->push_back(switch_loop_input.at(kIterLoopParamName));
  255. mute_inputs->push_back(switch_loop_input.at(kOneParamName));
  256. for (const auto &input : kernel_graph_ptr->inputs()) {
  257. MS_EXCEPTION_IF_NULL(input);
  258. if (input->isa<Parameter>()) {
  259. ParameterPtr param_ptr = input->cast<ParameterPtr>();
  260. if (param_ptr == nullptr) {
  261. MS_EXCEPTION(NotSupportError) << "Cast to parameter point failed !";
  262. }
  263. }
  264. }
  265. }
  266. void KernelAdjust::InsertGetNextLoopStreamSwitch(
  267. const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr, std::vector<CNodePtr> *exec_order,
  268. uint32_t *getnext_switch_stream_id, uint32_t *getnext_stream_id,
  269. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input) {
  270. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  271. MS_EXCEPTION_IF_NULL(exec_order);
  272. MS_EXCEPTION_IF_NULL(getnext_switch_stream_id);
  273. MS_EXCEPTION_IF_NULL(getnext_stream_id);
  274. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  275. *getnext_switch_stream_id = resource_manager.ApplyNewStream();
  276. *getnext_stream_id = resource_manager.ApplyNewStream();
  277. CNodePtr getnext_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input, kGetNextStreamSwitch);
  278. MS_EXCEPTION_IF_NULL(getnext_switch_app);
  279. AnfAlgo::SetStreamId(*getnext_switch_stream_id, getnext_switch_app.get());
  280. // update getnext loop stream switch true_branch_stream attr
  281. AnfAlgo::SetNodeAttr(kStreamNeedActivedFirst, MakeValue<bool>(true), getnext_switch_app);
  282. AnfAlgo::SetNodeAttr(kAttrTrueBranchStream, MakeValue<uint32_t>(*getnext_stream_id), getnext_switch_app);
  283. AnfAlgo::SetNodeAttr(kAttrStreamSwitchKind, MakeValue<uint32_t>(kGetNextStreamSwitch), getnext_switch_app);
  284. (*exec_order).push_back(getnext_switch_app);
  285. MS_LOG(INFO) << "GetNext loop insert Stream Switch " << getnext_switch_app->fullname_with_scope();
  286. }
  287. void KernelAdjust::SetBeforeGetNextStreamID(std::vector<CNodePtr> *exec_order, const std::vector<CNodePtr> &orders,
  288. size_t *order_index, CNodePtr getnext_cnode, uint32_t getnext_stream_id) {
  289. MS_EXCEPTION_IF_NULL(exec_order);
  290. MS_EXCEPTION_IF_NULL(order_index);
  291. for (; *order_index < orders.size(); (*order_index)++) {
  292. auto node = orders[*order_index];
  293. (*exec_order).push_back(node);
  294. AnfAlgo::SetStreamId(getnext_stream_id, (*exec_order)[(*exec_order).size() - 1].get());
  295. if (AnfAlgo::GetCNodeName(node) == kGetNextOpName) {
  296. getnext_cnode = node;
  297. break;
  298. }
  299. }
  300. }
  301. void KernelAdjust::InsertGetNextLoopFpBpStartSend(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  302. std::vector<CNodePtr> *exec_order, uint32_t *fpbp_start_event_id,
  303. uint32_t getnext_stream_id) {
  304. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  305. MS_EXCEPTION_IF_NULL(exec_order);
  306. MS_EXCEPTION_IF_NULL(fpbp_start_event_id);
  307. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  308. *fpbp_start_event_id = resource_manager.ApplyNewEvent();
  309. CNodePtr fpbp_start_send = CreateSendApplyKernel(kernel_graph_ptr, *fpbp_start_event_id);
  310. AnfAlgo::SetStreamId(getnext_stream_id, fpbp_start_send.get());
  311. (*exec_order).push_back(fpbp_start_send);
  312. MS_LOG(INFO) << "GetNext loop insert FpBp start Send " << fpbp_start_send->fullname_with_scope();
  313. }
  314. void KernelAdjust::InsertGetNextLoopEosStartSend(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  315. std::vector<CNodePtr> *exec_order, uint32_t *eos_start_event_id,
  316. uint32_t getnext_stream_id) {
  317. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  318. MS_EXCEPTION_IF_NULL(exec_order);
  319. MS_EXCEPTION_IF_NULL(eos_start_event_id);
  320. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  321. *eos_start_event_id = resource_manager.ApplyNewEvent();
  322. CNodePtr eos_start_send = CreateSendApplyKernel(kernel_graph_ptr, *eos_start_event_id);
  323. AnfAlgo::SetStreamId(getnext_stream_id, eos_start_send.get());
  324. (*exec_order).push_back(eos_start_send);
  325. MS_LOG(INFO) << "GetNext loop insert EoS start Send " << eos_start_send->fullname_with_scope();
  326. }
  327. void KernelAdjust::InsertEosStreamSwitch(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  328. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  329. std::vector<CNodePtr> *exec_order, uint32_t *eos_switch_stream_id,
  330. uint32_t *eos_stream_id) {
  331. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  332. MS_EXCEPTION_IF_NULL(exec_order);
  333. MS_EXCEPTION_IF_NULL(eos_switch_stream_id);
  334. MS_EXCEPTION_IF_NULL(eos_stream_id);
  335. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  336. *eos_switch_stream_id = resource_manager.ApplyNewStream();
  337. *eos_stream_id = resource_manager.ApplyNewStream();
  338. CNodePtr eos_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input, kEosStreamSwitch);
  339. MS_EXCEPTION_IF_NULL(eos_switch_app);
  340. AnfAlgo::SetStreamId(*eos_switch_stream_id, eos_switch_app.get());
  341. AnfAlgo::SetNodeAttr(kStreamNeedActivedFirst, MakeValue<bool>(true), eos_switch_app);
  342. // update eos loop stream switch true_branch_stream attr
  343. AnfAlgo::SetNodeAttr(kAttrTrueBranchStream, MakeValue<uint32_t>(*eos_stream_id), eos_switch_app);
  344. AnfAlgo::SetNodeAttr(kAttrStreamSwitchKind, MakeValue<uint32_t>(kEosStreamSwitch), eos_switch_app);
  345. (*exec_order).push_back(eos_switch_app);
  346. MS_LOG(INFO) << "EoS loop insert Stream Switch " << eos_switch_app->fullname_with_scope();
  347. }
  348. void KernelAdjust::InsertGetNextLoopEosStartRecv(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  349. std::vector<CNodePtr> *exec_order, uint32_t eos_start_event_id,
  350. uint32_t eos_stream_id) {
  351. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  352. MS_EXCEPTION_IF_NULL(exec_order);
  353. CNodePtr eos_start_recv = CreateRecvApplyKernel(kernel_graph_ptr, eos_start_event_id);
  354. AnfAlgo::SetStreamId(eos_stream_id, eos_start_recv.get());
  355. (*exec_order).push_back(eos_start_recv);
  356. MS_LOG(INFO) << "EoS loop insert EoS Recv " << eos_start_recv->fullname_with_scope();
  357. }
  358. void KernelAdjust::InsertEosOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  359. std::vector<CNodePtr> *exec_order, const CNodePtr &getnext_cnode,
  360. uint32_t eos_stream_id) {
  361. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  362. MS_EXCEPTION_IF_NULL(exec_order);
  363. MS_EXCEPTION_IF_NULL(getnext_cnode);
  364. CNodePtr end_of_sequence_op = CreateEndOfSequenceOP(kernel_graph_ptr, getnext_cnode);
  365. MS_EXCEPTION_IF_NULL(end_of_sequence_op);
  366. AnfAlgo::SetStreamId(eos_stream_id, end_of_sequence_op.get());
  367. (*exec_order).push_back(end_of_sequence_op);
  368. MS_LOG(INFO) << "EoS loop insert Eos Op " << end_of_sequence_op->fullname_with_scope();
  369. }
  370. void KernelAdjust::InsertEosDoneSend(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  371. std::vector<CNodePtr> *exec_order, uint32_t *eos_done_event_id,
  372. uint32_t eos_stream_id) {
  373. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  374. MS_EXCEPTION_IF_NULL(exec_order);
  375. MS_EXCEPTION_IF_NULL(eos_done_event_id);
  376. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  377. *eos_done_event_id = resource_manager.ApplyNewEvent();
  378. CNodePtr eos_done_send = CreateSendApplyKernel(kernel_graph_ptr, *eos_done_event_id);
  379. AnfAlgo::SetStreamId(eos_stream_id, eos_done_send.get());
  380. (*exec_order).push_back(eos_done_send);
  381. MS_LOG(INFO) << "EoS loop insert EoS done Send " << eos_done_send->fullname_with_scope();
  382. }
  383. void KernelAdjust::InsertSwitchLoop(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  384. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  385. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  386. resource_manager.ResetResource();
  387. if (!NeedInsertSwitch()) {
  388. return;
  389. }
  390. if (kernel_graph_ptr->is_dynamic_shape()) {
  391. MS_LOG(INFO) << "KernelGraph:" << kernel_graph_ptr->graph_id() << " is dynamic shape, skip InsertSwitchLoop";
  392. return;
  393. }
  394. bool exist_getnext = ExistGetNext(kernel_graph_ptr);
  395. bool eos_mode = ConfigManager::GetInstance().iter_num() == INT32_MAX && exist_getnext;
  396. MS_LOG(INFO) << "GetNext exist:" << exist_getnext << " End of Sequence mode:" << eos_mode
  397. << " iter num:" << ConfigManager::GetInstance().iter_num();
  398. if (exist_getnext) {
  399. ReorderGetNext(kernel_graph_ptr);
  400. }
  401. std::map<std::string, mindspore::ParameterPtr> switch_loop_input;
  402. CreateSwitchOpParameters(kernel_graph_ptr, &switch_loop_input);
  403. InsertSwitchLoopInput(kernel_graph_ptr, switch_loop_input);
  404. const std::vector<CNodePtr> &orders = kernel_graph_ptr->execution_order();
  405. if (orders.empty()) {
  406. MS_LOG(EXCEPTION) << "graph execution order is empty";
  407. }
  408. std::vector<CNodePtr> exec_order;
  409. CNodePtr getnext_cnode;
  410. uint32_t getnext_switch_stream_id = UINT32_MAX;
  411. uint32_t fpbp_start_event_id = UINT32_MAX;
  412. uint32_t eos_start_event_id = UINT32_MAX;
  413. uint32_t getnext_stream_id = UINT32_MAX;
  414. size_t order_index = 0;
  415. if (exist_getnext) {
  416. InsertGetNextLoopStreamSwitch(kernel_graph_ptr, &exec_order, &getnext_switch_stream_id, &getnext_stream_id,
  417. switch_loop_input);
  418. SetBeforeGetNextStreamID(&exec_order, orders, &order_index, getnext_cnode, getnext_stream_id);
  419. InsertGetNextLoopFpBpStartSend(kernel_graph_ptr, &exec_order, &fpbp_start_event_id, getnext_stream_id);
  420. if (eos_mode) {
  421. InsertGetNextLoopEosStartSend(kernel_graph_ptr, &exec_order, &eos_start_event_id, getnext_stream_id);
  422. }
  423. }
  424. uint32_t eos_switch_stream_id = UINT32_MAX;
  425. uint32_t eos_stream_id = UINT32_MAX;
  426. uint32_t eos_done_event_id = UINT32_MAX;
  427. std::vector<uint32_t> fpbp_active_streams;
  428. if (eos_mode) {
  429. InsertEosStreamSwitch(kernel_graph_ptr, switch_loop_input, &exec_order, &eos_switch_stream_id, &eos_stream_id);
  430. InsertGetNextLoopEosStartRecv(kernel_graph_ptr, &exec_order, eos_start_event_id, eos_stream_id);
  431. InsertEosOp(kernel_graph_ptr, &exec_order, getnext_cnode, eos_stream_id);
  432. InsertEosDoneSend(kernel_graph_ptr, &exec_order, &eos_done_event_id, eos_stream_id);
  433. fpbp_active_streams.push_back(eos_switch_stream_id);
  434. }
  435. bool exist_independent = ExistIndependent(kernel_graph_ptr);
  436. if (exist_independent) {
  437. InsertIndepentParallel(kernel_graph_ptr, switch_loop_input, &exec_order);
  438. }
  439. uint32_t fpbp_stream_id = UINT32_MAX;
  440. uint32_t fpbp_switch_stream_id = UINT32_MAX;
  441. InsertFpBpLoopStreamSwitch(kernel_graph_ptr, switch_loop_input, &exec_order, &fpbp_stream_id, &fpbp_switch_stream_id);
  442. if (exist_getnext) {
  443. InsertFpBpStartRecv(kernel_graph_ptr, &exec_order, fpbp_start_event_id, fpbp_stream_id);
  444. }
  445. InsertNextLoopAssignAdd(kernel_graph_ptr, &exec_order, switch_loop_input, fpbp_stream_id);
  446. std::vector<CNodePtr> memcpy_list;
  447. std::vector<CNodePtr> other_list;
  448. if (exist_getnext) {
  449. CopyMemcpyList(kernel_graph_ptr, orders, order_index, &memcpy_list, &other_list);
  450. (void)std::copy(memcpy_list.begin(), memcpy_list.end(), std::back_inserter(exec_order));
  451. } else {
  452. other_list = orders;
  453. }
  454. if (eos_mode) {
  455. InsertEosDoneRecv(kernel_graph_ptr, &exec_order, eos_done_event_id, fpbp_stream_id);
  456. }
  457. std::vector<uint32_t> getnext_active_streams;
  458. if (exist_getnext) {
  459. // small loop active
  460. getnext_active_streams.push_back(getnext_switch_stream_id);
  461. InsertGetNextLoopStreamActive(kernel_graph_ptr, &exec_order, getnext_active_streams, getnext_switch_stream_id);
  462. }
  463. (void)std::copy(other_list.begin(), other_list.end(), std::back_inserter(exec_order));
  464. InsertCurrentLoopAssignAdd(kernel_graph_ptr, &exec_order, switch_loop_input);
  465. // big loop active
  466. fpbp_active_streams.push_back(fpbp_switch_stream_id);
  467. InsertFpBpAndEosLoopStreamActive(kernel_graph_ptr, &exec_order, fpbp_active_streams);
  468. kernel_graph_ptr->set_execution_order(exec_order);
  469. }
  470. void KernelAdjust::CreateSwitchOpParameters(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  471. std::map<std::string, mindspore::ParameterPtr> *switch_loop_input) {
  472. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  473. MS_EXCEPTION_IF_NULL(switch_loop_input);
  474. ShapeVector shp = {1};
  475. tensor::TensorPtr tensor_ptr = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  476. MS_EXCEPTION_IF_NULL(tensor_ptr);
  477. mindspore::abstract::AbstractBasePtr paremeter_abstract_ptr = tensor_ptr->ToAbstract();
  478. if (paremeter_abstract_ptr == nullptr) {
  479. MS_LOG(EXCEPTION) << "create abstract before insert switch op failed!";
  480. }
  481. ParameterPtr cur_loop_count = std::make_shared<Parameter>(kernel_graph_ptr);
  482. MS_EXCEPTION_IF_NULL(cur_loop_count);
  483. cur_loop_count->set_name(kCurLoopCountParamName);
  484. cur_loop_count->set_abstract(paremeter_abstract_ptr);
  485. ParameterPtr loop_count_cur = kernel_graph_ptr->NewParameter(cur_loop_count);
  486. (*switch_loop_input)[kCurLoopCountParamName] = loop_count_cur;
  487. ParameterPtr next_loop_count = std::make_shared<Parameter>(kernel_graph_ptr);
  488. MS_EXCEPTION_IF_NULL(next_loop_count);
  489. next_loop_count->set_name(kNextLoopCountParamName);
  490. next_loop_count->set_abstract(paremeter_abstract_ptr);
  491. ParameterPtr loop_count_next = kernel_graph_ptr->NewParameter(next_loop_count);
  492. (*switch_loop_input)[kNextLoopCountParamName] = loop_count_next;
  493. ParameterPtr iter_loop = std::make_shared<Parameter>(kernel_graph_ptr);
  494. iter_loop->set_name(kIterLoopParamName);
  495. iter_loop->set_abstract(paremeter_abstract_ptr);
  496. ParameterPtr iter_loop_new = kernel_graph_ptr->NewParameter(iter_loop);
  497. (*switch_loop_input)[kIterLoopParamName] = iter_loop_new;
  498. ParameterPtr one = std::make_shared<Parameter>(kernel_graph_ptr);
  499. one->set_name(kOneParamName);
  500. one->set_abstract(paremeter_abstract_ptr);
  501. ParameterPtr one_new = kernel_graph_ptr->NewParameter(one);
  502. (*switch_loop_input)[kOneParamName] = one_new;
  503. ParameterPtr epoch = std::make_shared<Parameter>(kernel_graph_ptr);
  504. MS_EXCEPTION_IF_NULL(epoch);
  505. epoch->set_name(kEpochParamName);
  506. epoch->set_abstract(paremeter_abstract_ptr);
  507. ParameterPtr epoch_new = kernel_graph_ptr->NewParameter(epoch);
  508. (*switch_loop_input)[kEpochParamName] = epoch_new;
  509. }
  510. kernel::KernelBuildInfo::KernelBuildInfoBuilder KernelAdjust::CreateMngKernelBuilder(
  511. const std::vector<std::string> &formats, const std::vector<TypeId> &type_ids) {
  512. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  513. selected_kernel_builder.SetInputsFormat(formats);
  514. selected_kernel_builder.SetInputsDeviceType(type_ids);
  515. selected_kernel_builder.SetFusionType(kernel::FusionType::OPAQUE);
  516. selected_kernel_builder.SetProcessor(kernel::Processor::AICORE);
  517. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  518. return selected_kernel_builder;
  519. }
  520. CNodePtr KernelAdjust::CreateStreamSwitchOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  521. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  522. StreamSwitchKind kind) {
  523. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  524. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  525. auto typeNone_abstract = std::make_shared<abstract::AbstractNone>();
  526. auto stream_switch = std::make_shared<Primitive>(kStreamSwitchOpName);
  527. std::vector<AnfNodePtr> inputs;
  528. inputs.push_back(NewValueNode(stream_switch));
  529. if (kind == kFpBpStreamSwitch || kind == kEosStreamSwitch) {
  530. inputs.push_back(switch_loop_input.at(kNextLoopCountParamName));
  531. } else if (kind == kGetNextStreamSwitch || kind == kIndependentStreamSwitch) {
  532. inputs.push_back(switch_loop_input.at(kNextLoopCountParamName));
  533. } else {
  534. MS_LOG(ERROR) << "unknown stream switch kind";
  535. }
  536. inputs.push_back(switch_loop_input.at(kIterLoopParamName));
  537. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  538. CNodePtr stream_switch_app = kernel_graph_ptr->NewCNode(inputs);
  539. MS_EXCEPTION_IF_NULL(stream_switch_app);
  540. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), stream_switch_app.get());
  541. stream_switch_app->set_abstract(typeNone_abstract);
  542. // set attr: cond_ RT_LESS
  543. int condition = static_cast<int>(RT_LESS);
  544. ValuePtr cond = MakeValue(condition);
  545. AnfAlgo::SetNodeAttr(kAttrSwitchCondition, cond, stream_switch_app);
  546. // set attr:data_type
  547. int data_type = static_cast<int>(RT_SWITCH_INT64);
  548. ValuePtr dt = MakeValue(data_type);
  549. AnfAlgo::SetNodeAttr(kAttrDataType, dt, stream_switch_app);
  550. // set distinction label and graph id
  551. return stream_switch_app;
  552. }
  553. CNodePtr KernelAdjust::CreateStreamActiveOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  554. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  555. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  556. abstract::AbstractBasePtr typeNone_abstract = std::make_shared<abstract::AbstractNone>();
  557. auto stream_active_others = std::make_shared<Primitive>(kStreamActiveOpName);
  558. std::vector<AnfNodePtr> inputs;
  559. inputs.push_back(NewValueNode(stream_active_others));
  560. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  561. CNodePtr stream_active_others_app = kernel_graph_ptr->NewCNode(inputs);
  562. MS_EXCEPTION_IF_NULL(stream_active_others_app);
  563. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), stream_active_others_app.get());
  564. stream_active_others_app->set_abstract(typeNone_abstract);
  565. return stream_active_others_app;
  566. }
  567. CNodePtr KernelAdjust::CreatTupleGetItemNode(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  568. const CNodePtr &node, size_t output_idx) {
  569. auto idx = NewValueNode(SizeToLong(output_idx));
  570. MS_EXCEPTION_IF_NULL(idx);
  571. auto imm = std::make_shared<Int64Imm>(SizeToInt(output_idx));
  572. auto abstract_scalar = std::make_shared<abstract::AbstractScalar>(imm);
  573. idx->set_abstract(abstract_scalar);
  574. CNodePtr tuple_getitem = kernel_graph_ptr->NewCNode({NewValueNode(prim::kPrimTupleGetItem), node, idx});
  575. MS_EXCEPTION_IF_NULL(tuple_getitem);
  576. tuple_getitem->set_scope(node->scope());
  577. std::vector<size_t> origin_shape = AnfAlgo::GetOutputInferShape(node, output_idx);
  578. TypeId origin_type = AnfAlgo::GetOutputInferDataType(node, output_idx);
  579. AnfAlgo::SetOutputInferTypeAndShape({origin_type}, {origin_shape}, tuple_getitem.get());
  580. return tuple_getitem;
  581. }
  582. CNodePtr KernelAdjust::CreateEndOfSequenceOP(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  583. const CNodePtr &getnext_cnode) {
  584. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  585. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  586. selected_kernel_builder.SetInputsFormat({kOpFormat_DEFAULT});
  587. selected_kernel_builder.SetInputsDeviceType({kNumberTypeUInt8});
  588. selected_kernel_builder.SetFusionType(kernel::FusionType::OPAQUE);
  589. selected_kernel_builder.SetProcessor(kernel::Processor::AICPU);
  590. selected_kernel_builder.SetKernelType(KernelType::AICPU_KERNEL);
  591. selected_kernel_builder.SetOutputsFormat({kOpFormat_DEFAULT});
  592. selected_kernel_builder.SetOutputsDeviceType({kNumberTypeUInt8});
  593. // EndOfSequence
  594. auto end_of_sequence = std::make_shared<Primitive>(kEndOfSequence);
  595. std::vector<AnfNodePtr> inputs;
  596. inputs.push_back(NewValueNode(end_of_sequence));
  597. // GetNext output 0 is EndOfSequence's input
  598. auto tuple_get_item = CreatTupleGetItemNode(kernel_graph_ptr, getnext_cnode, 0);
  599. inputs.push_back(tuple_get_item);
  600. CNodePtr end_of_sequence_node = kernel_graph_ptr->NewCNode(inputs);
  601. MS_EXCEPTION_IF_NULL(end_of_sequence_node);
  602. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), end_of_sequence_node.get());
  603. std::vector<std::string> input_names = {"x"};
  604. ValuePtr input_names_v = MakeValue(input_names);
  605. AnfAlgo::SetNodeAttr("input_names", input_names_v, end_of_sequence_node);
  606. std::vector<std::string> output_names = {"y"};
  607. ValuePtr output_names_v = MakeValue(output_names);
  608. AnfAlgo::SetNodeAttr("output_names", output_names_v, end_of_sequence_node);
  609. end_of_sequence_node->set_abstract(tuple_get_item->abstract());
  610. return end_of_sequence_node;
  611. }
  612. CNodePtr KernelAdjust::CreateStreamAssignAddnOP(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  613. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  614. bool cur_loop) {
  615. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  616. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  617. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  618. selected_kernel_builder.SetOutputsFormat({kOpFormat_DEFAULT});
  619. selected_kernel_builder.SetOutputsDeviceType({kNumberTypeInt32});
  620. // AssignAdd
  621. auto assign_add = std::make_shared<Primitive>(kAssignAddOpName);
  622. std::vector<AnfNodePtr> inputs;
  623. inputs.push_back(NewValueNode(assign_add));
  624. if (cur_loop) {
  625. inputs.push_back(switch_loop_input.at(kCurLoopCountParamName));
  626. } else {
  627. inputs.push_back(switch_loop_input.at(kNextLoopCountParamName));
  628. }
  629. inputs.push_back(switch_loop_input.at(kOneParamName));
  630. CNodePtr assign_add_one = kernel_graph_ptr->NewCNode(inputs);
  631. MS_EXCEPTION_IF_NULL(assign_add_one);
  632. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), assign_add_one.get());
  633. std::vector<std::string> input_names = {"ref", "value"};
  634. std::vector<std::string> output_names = {"output"};
  635. ValuePtr input_names_v = MakeValue(input_names);
  636. ValuePtr output_names_v = MakeValue(output_names);
  637. AnfAlgo::SetNodeAttr("input_names", input_names_v, assign_add_one);
  638. AnfAlgo::SetNodeAttr("output_names", output_names_v, assign_add_one);
  639. selected_kernel_builder.SetKernelType(KernelType::TBE_KERNEL);
  640. MS_EXCEPTION_IF_NULL(switch_loop_input.at(kCurLoopCountParamName));
  641. assign_add_one->set_abstract(switch_loop_input.at(kCurLoopCountParamName)->abstract());
  642. // add AssignAdd op to kernel ref node map
  643. session::AnfWithOutIndex final_pair = std::make_pair(assign_add_one, 0);
  644. session::KernelWithIndex kernel_with_index = AnfAlgo::VisitKernel(AnfAlgo::GetInputNode(assign_add_one, 0), 0);
  645. kernel_graph_ptr->AddRefCorrespondPairs(final_pair, kernel_with_index);
  646. return assign_add_one;
  647. }
  648. bool KernelAdjust::StepLoadCtrlInputs(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  649. if (!NeedInsertSwitch()) {
  650. return true;
  651. }
  652. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  653. if (kernel_graph_ptr->is_dynamic_shape()) {
  654. MS_LOG(INFO) << "Skip StepLoadCtrlInputs";
  655. return true;
  656. }
  657. auto input_nodes = kernel_graph_ptr->inputs();
  658. std::vector<tensor::TensorPtr> inputs;
  659. LoadSwitchInputs(&inputs);
  660. std::shared_ptr<std::vector<tensor::TensorPtr>> inputsPtr = std::make_shared<std::vector<tensor::TensorPtr>>(inputs);
  661. kernel_graph_ptr->set_input_ctrl_tensors(inputsPtr);
  662. size_t input_ctrl_size = inputs.size();
  663. // inputs_node:include four ctrl nodes in the back. such as:conv,loop_cnt, ites_loop, zero, one.
  664. // deal four ctrl nodes.
  665. for (size_t i = 0; i < inputs.size(); ++i) {
  666. auto tensor = inputs[i];
  667. size_t deal_index = input_nodes.size() - input_ctrl_size + i;
  668. if (deal_index >= input_nodes.size()) {
  669. MS_LOG(EXCEPTION) << "deal_index[" << deal_index << "] out of range";
  670. }
  671. auto input_node = input_nodes[deal_index];
  672. bool need_sync = false;
  673. MS_EXCEPTION_IF_NULL(input_node);
  674. if (input_node->isa<Parameter>()) {
  675. auto pk_node = input_node->cast<ParameterPtr>();
  676. MS_EXCEPTION_IF_NULL(tensor);
  677. MS_EXCEPTION_IF_NULL(pk_node);
  678. if (tensor->NeedSyncHostToDevice() || !pk_node->has_default()) {
  679. need_sync = true;
  680. }
  681. }
  682. if (need_sync) {
  683. auto pk_node = input_node->cast<ParameterPtr>();
  684. MS_EXCEPTION_IF_NULL(pk_node);
  685. auto device_address = AnfAlgo::GetMutableOutputAddr(pk_node, 0);
  686. MS_EXCEPTION_IF_NULL(device_address);
  687. tensor->set_device_address(device_address);
  688. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  689. LongToSize(tensor->data().nbytes()), tensor->data_type(), tensor->data_c(),
  690. tensor->device_info().host_format_)) {
  691. MS_LOG(INFO) << "SyncHostToDevice failed.";
  692. return false;
  693. }
  694. }
  695. tensor->set_sync_status(kNoNeedSync);
  696. }
  697. return true;
  698. }
  699. void KernelAdjust::LoadSwitchInputs(std::vector<tensor::TensorPtr> *inputs) {
  700. MS_LOG(INFO) << "---------------- LoadSwitchInputs---";
  701. MS_EXCEPTION_IF_NULL(inputs);
  702. // current loop count
  703. ShapeVector shp = {1};
  704. tensor::TensorPtr cur_loop_count = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  705. MS_EXCEPTION_IF_NULL(cur_loop_count);
  706. int32_t *val = nullptr;
  707. val = static_cast<int32_t *>(cur_loop_count->data_c());
  708. MS_EXCEPTION_IF_NULL(val);
  709. *val = 0;
  710. inputs->push_back(cur_loop_count);
  711. // next loop count
  712. tensor::TensorPtr next_loop_count = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  713. MS_EXCEPTION_IF_NULL(next_loop_count);
  714. val = static_cast<int32_t *>(next_loop_count->data_c());
  715. MS_EXCEPTION_IF_NULL(val);
  716. *val = 0;
  717. inputs->push_back(next_loop_count);
  718. // Epoch in device
  719. tensor::TensorPtr epoch_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  720. MS_EXCEPTION_IF_NULL(epoch_tensor);
  721. val = static_cast<int32_t *>(epoch_tensor->data_c());
  722. MS_EXCEPTION_IF_NULL(val);
  723. *val = 0;
  724. inputs->push_back(epoch_tensor);
  725. // total loop count per iter
  726. tensor::TensorPtr iter_loop_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  727. MS_EXCEPTION_IF_NULL(iter_loop_tensor);
  728. val = static_cast<int32_t *>(iter_loop_tensor->data_c());
  729. MS_EXCEPTION_IF_NULL(val);
  730. *val = SizeToInt(LongToSize(ConfigManager::GetInstance().iter_num()));
  731. MS_LOG(INFO) << "iter_loop_tensor = " << *val;
  732. inputs->push_back(iter_loop_tensor);
  733. tensor::TensorPtr one_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  734. MS_EXCEPTION_IF_NULL(one_tensor);
  735. val = static_cast<int32_t *>(one_tensor->data_c());
  736. MS_EXCEPTION_IF_NULL(val);
  737. *val = 1;
  738. inputs->push_back(one_tensor);
  739. MS_LOG(INFO) << "---------------- LoadSwitchInputs End--";
  740. }
  741. void KernelAdjust::Profiling(NotNull<session::KernelGraph *> kernel_graph_ptr) {
  742. if (!ascend::ProfilingManager::GetInstance().IsProfiling()) {
  743. MS_LOG(INFO) << "No need to profiling";
  744. return;
  745. }
  746. auto graph_id_env = std::getenv(kProfilingGraphId);
  747. if (graph_id_env != nullptr) {
  748. auto graph_id = std::stoul(graph_id_env);
  749. if (graph_id != kernel_graph_ptr->graph_id()) {
  750. MS_LOG(WARNING) << "Get PROFILING_GRAPH_ID " << graph_id
  751. << " Not Match Current Graph Id:" << kernel_graph_ptr->graph_id();
  752. return;
  753. }
  754. }
  755. ProfilingTraceInfo profiling_trace_info = ProfilingUtils::GenerateProfilingTrace(*kernel_graph_ptr);
  756. if (!profiling_trace_info.IsValid()) {
  757. MS_LOG(INFO) << "[profiling] no profiling node found!";
  758. return;
  759. }
  760. InsertProfilingKernel(profiling_trace_info, kernel_graph_ptr);
  761. }
  762. void KernelAdjust::InsertProfilingKernel(const ProfilingTraceInfo &profiling_trace_info,
  763. NotNull<session::KernelGraph *> kernel_graph_ptr) {
  764. MS_LOG(INFO) << "[profiling] Insert profiling kernel start";
  765. if (!profiling_trace_info.IsValid()) {
  766. MS_LOG(WARNING) << "Profiling trace point not found";
  767. return;
  768. }
  769. std::vector<CNodePtr> new_cnode_list;
  770. std::vector<CNodePtr> cnode_ptr_list = kernel_graph_ptr->execution_order();
  771. if (cnode_ptr_list.empty()) {
  772. MS_LOG(ERROR) << "No CNode in graph";
  773. return;
  774. }
  775. for (const auto &cnode_ptr : cnode_ptr_list) {
  776. ProfilingUtils::InsertProfilingTraceFp(cnode_ptr, profiling_trace_info, kernel_graph_ptr,
  777. NOT_NULL(&new_cnode_list));
  778. new_cnode_list.emplace_back(cnode_ptr);
  779. ProfilingUtils::InsertProfilingCustomOp(cnode_ptr, profiling_trace_info, kernel_graph_ptr,
  780. NOT_NULL(&new_cnode_list));
  781. ProfilingUtils::InsertProfilingTraceBpEnd(cnode_ptr, profiling_trace_info, kernel_graph_ptr,
  782. NOT_NULL(&new_cnode_list));
  783. ProfilingUtils::InsertProfilingTraceIterEnd(cnode_ptr, profiling_trace_info, kernel_graph_ptr,
  784. NOT_NULL(&new_cnode_list));
  785. }
  786. kernel_graph_ptr->set_execution_order(new_cnode_list);
  787. }
  788. } // namespace device
  789. } // namespace mindspore