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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 "device/kernel_adjust.h"
  17. #include <map>
  18. #include <algorithm>
  19. #include <string>
  20. #include <unordered_set>
  21. #include <unordered_map>
  22. #include <vector>
  23. #include "session/anf_runtime_algorithm.h"
  24. #include "utils/context/ms_context.h"
  25. #include "common/trans.h"
  26. #include "utils/config_manager.h"
  27. #include "common/utils.h"
  28. #include "kernel/kernel_build_info.h"
  29. #include "utils/utils.h"
  30. #include "device/ascend/profiling/profiling_manager.h"
  31. #include "device/ascend/kernel_select_ascend.h"
  32. #include "runtime/base.h"
  33. #include "device/ascend/ascend_stream_assign.h"
  34. namespace mindspore {
  35. namespace device {
  36. using device::ascend::ProfilingUtils;
  37. void KernelAdjust::ReorderGetNext(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  38. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  39. const std::vector<CNodePtr> &origin_cnode_list = kernel_graph_ptr->execution_order();
  40. std::vector<CNodePtr> getnext_list;
  41. std::vector<CNodePtr> other_list;
  42. for (const auto &cnode : origin_cnode_list) {
  43. if (AnfAlgo::GetCNodeName(cnode) == kGetNextOpName) {
  44. getnext_list.emplace_back(cnode);
  45. } else {
  46. other_list.emplace_back(cnode);
  47. }
  48. }
  49. std::vector<CNodePtr> new_order_list;
  50. new_order_list.insert(new_order_list.end(), getnext_list.begin(), getnext_list.end());
  51. new_order_list.insert(new_order_list.end(), other_list.begin(), other_list.end());
  52. kernel_graph_ptr->set_execution_order(new_order_list);
  53. }
  54. bool KernelAdjust::NeedInsertSwitch() {
  55. auto context_ptr = MsContext::GetInstance();
  56. MS_EXCEPTION_IF_NULL(context_ptr);
  57. return (context_ptr->enable_task_sink() && context_ptr->loop_sink_flag() &&
  58. ConfigManager::GetInstance().iter_num() > 1);
  59. }
  60. CNodePtr KernelAdjust::CreateSendApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr,
  61. uint32_t event_id) {
  62. MS_EXCEPTION_IF_NULL(graph_ptr);
  63. auto send_op = std::make_shared<Primitive>(kSendOpName);
  64. MS_EXCEPTION_IF_NULL(send_op);
  65. auto send_apply = std::make_shared<ValueNode>(send_op);
  66. MS_EXCEPTION_IF_NULL(send_apply);
  67. std::vector<AnfNodePtr> send_input_list = {send_apply};
  68. CNodePtr send_node_ptr = graph_ptr->NewCNode(send_input_list);
  69. MS_EXCEPTION_IF_NULL(send_node_ptr);
  70. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  71. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  72. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), send_node_ptr.get());
  73. AnfAlgo::SetNodeAttr(kAttrEventId, MakeValue(event_id), send_node_ptr);
  74. auto abstract_none = std::make_shared<abstract::AbstractNone>();
  75. MS_EXCEPTION_IF_NULL(abstract_none);
  76. send_node_ptr->set_abstract(abstract_none);
  77. return send_node_ptr;
  78. }
  79. CNodePtr KernelAdjust::CreateRecvApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr,
  80. uint32_t event_id) {
  81. MS_EXCEPTION_IF_NULL(graph_ptr);
  82. auto recv_op = std::make_shared<Primitive>(kRecvOpName);
  83. MS_EXCEPTION_IF_NULL(recv_op);
  84. auto recv_apply = std::make_shared<ValueNode>(recv_op);
  85. MS_EXCEPTION_IF_NULL(recv_apply);
  86. std::vector<AnfNodePtr> recv_input_list = {recv_apply};
  87. CNodePtr recv_node_ptr = graph_ptr->NewCNode(recv_input_list);
  88. MS_EXCEPTION_IF_NULL(recv_node_ptr);
  89. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  90. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  91. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), recv_node_ptr.get());
  92. AnfAlgo::SetNodeAttr(kAttrEventId, MakeValue(event_id), recv_node_ptr);
  93. auto abstract_none = std::make_shared<abstract::AbstractNone>();
  94. MS_EXCEPTION_IF_NULL(abstract_none);
  95. recv_node_ptr->set_abstract(abstract_none);
  96. return recv_node_ptr;
  97. }
  98. void KernelAdjust::InsertSwitchLoop(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  99. device::ascend::AscendResourceMng &resource_manager = device::ascend::AscendResourceMng::GetInstance();
  100. resource_manager.ResetResource();
  101. if (!NeedInsertSwitch()) {
  102. return;
  103. }
  104. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  105. bool eos_mode = ConfigManager::GetInstance().iter_num() == INT32_MAX;
  106. ReorderGetNext(kernel_graph_ptr);
  107. std::map<std::string, mindspore::ParameterPtr> switch_loop_input;
  108. CreateSwitchOpParameters(kernel_graph_ptr, &switch_loop_input);
  109. std::vector<AnfNodePtr> *mute_inputs = kernel_graph_ptr->MutableInputs();
  110. MS_EXCEPTION_IF_NULL(mute_inputs);
  111. mute_inputs->push_back(switch_loop_input[kLoopCountParamName]);
  112. mute_inputs->push_back(switch_loop_input[kIterLoopParamName]);
  113. mute_inputs->push_back(switch_loop_input[kZeroParamName]);
  114. mute_inputs->push_back(switch_loop_input[kOneParamName]);
  115. for (const auto &input : kernel_graph_ptr->inputs()) {
  116. MS_EXCEPTION_IF_NULL(input);
  117. if (input->isa<Parameter>()) {
  118. ParameterPtr param_ptr = input->cast<ParameterPtr>();
  119. if (param_ptr == nullptr) {
  120. MS_EXCEPTION(NotSupportError) << "Cast to parameter point failed !";
  121. }
  122. }
  123. }
  124. const std::vector<CNodePtr> &orders = kernel_graph_ptr->execution_order();
  125. if (orders.empty()) {
  126. MS_LOG(EXCEPTION) << "graph execution order is empty";
  127. }
  128. std::vector<CNodePtr> exec_order;
  129. std::vector<uint32_t> getnext_active_streams;
  130. std::vector<uint32_t> fpbp_active_streams;
  131. CNodePtr getnext_cnode;
  132. uint32_t eos_done_event_id = UINT32_MAX;
  133. // getnext loop process
  134. // getnext loop stream switch op
  135. CNodePtr getnext_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input);
  136. MS_EXCEPTION_IF_NULL(getnext_switch_app);
  137. uint32_t getnext_switch_stream_id = resource_manager.ApplyNewStream();
  138. AnfAlgo::SetStreamId(getnext_switch_stream_id, getnext_switch_app.get());
  139. exec_order.push_back(getnext_switch_app);
  140. // getnext op
  141. uint32_t getnext_stream_id = resource_manager.ApplyNewStream();
  142. size_t i = 0;
  143. for (; i < orders.size(); i++) {
  144. auto node = orders[i];
  145. exec_order.push_back(node);
  146. AnfAlgo::SetStreamId(getnext_stream_id, exec_order[exec_order.size() - 1].get());
  147. if (AnfAlgo::GetCNodeName(node) == kGetNextOpName) {
  148. getnext_cnode = node;
  149. break;
  150. }
  151. }
  152. // update getnext loop stream switch true_branch_stream attr
  153. AnfAlgo::SetNodeAttr(kAttrTrueBranchStream, MakeValue<uint32_t>(getnext_stream_id), getnext_switch_app);
  154. // getnext loop fpbp start send
  155. uint32_t fpbp_start_event_id = resource_manager.ApplyNewEvent();
  156. CNodePtr fpbp_start_send = CreateSendApplyKernel(kernel_graph_ptr, fpbp_start_event_id);
  157. AnfAlgo::SetStreamId(getnext_stream_id, fpbp_start_send.get());
  158. exec_order.push_back(fpbp_start_send);
  159. if (eos_mode) {
  160. // getnext loop eos start send
  161. uint32_t eos_start_event_id = resource_manager.ApplyNewEvent();
  162. CNodePtr eos_start_send = CreateSendApplyKernel(kernel_graph_ptr, eos_start_event_id);
  163. AnfAlgo::SetStreamId(getnext_stream_id, eos_start_send.get());
  164. exec_order.push_back(eos_start_send);
  165. // End Of Sequence loop process
  166. // eos loop stream switch
  167. CNodePtr eos_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input);
  168. MS_EXCEPTION_IF_NULL(eos_switch_app);
  169. uint32_t eos_switch_stream_id = resource_manager.ApplyNewStream();
  170. AnfAlgo::SetStreamId(eos_switch_stream_id, eos_switch_app.get());
  171. AnfAlgo::SetNodeAttr(kStreamNeedActivedFirst, MakeValue<bool>(true), eos_switch_app);
  172. exec_order.push_back(eos_switch_app);
  173. // eos loop eos start recv
  174. CNodePtr eos_start_recv = CreateRecvApplyKernel(kernel_graph_ptr, eos_start_event_id);
  175. uint32_t eos_stream_id = resource_manager.ApplyNewStream();
  176. AnfAlgo::SetStreamId(eos_stream_id, eos_start_recv.get());
  177. exec_order.push_back(eos_start_recv);
  178. // update eos loop stream switch true_branch_stream attr
  179. AnfAlgo::SetNodeAttr(kAttrTrueBranchStream, MakeValue<uint32_t>(eos_stream_id), eos_switch_app);
  180. // EndOfSequence op
  181. CNodePtr end_of_sequence_op = CreateEndOfSequenceOP(kernel_graph_ptr, getnext_cnode);
  182. MS_EXCEPTION_IF_NULL(end_of_sequence_op);
  183. AnfAlgo::SetStreamId(eos_stream_id, end_of_sequence_op.get());
  184. exec_order.push_back(end_of_sequence_op);
  185. // eos loop eos done send
  186. eos_done_event_id = resource_manager.ApplyNewEvent();
  187. CNodePtr eos_done_send = CreateSendApplyKernel(kernel_graph_ptr, eos_done_event_id);
  188. AnfAlgo::SetStreamId(eos_stream_id, eos_done_send.get());
  189. exec_order.push_back(eos_done_send);
  190. // eos loop stream active
  191. fpbp_active_streams.push_back(eos_switch_stream_id);
  192. }
  193. // fpbp loop process
  194. // fpbp loop stream switch
  195. CNodePtr fpbp_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input);
  196. MS_EXCEPTION_IF_NULL(fpbp_switch_app);
  197. uint32_t fpbp_switch_stream_id = resource_manager.ApplyNewStream();
  198. AnfAlgo::SetStreamId(fpbp_switch_stream_id, fpbp_switch_app.get());
  199. AnfAlgo::SetNodeAttr(kStreamNeedActivedFirst, MakeValue<bool>(true), fpbp_switch_app);
  200. exec_order.push_back(fpbp_switch_app);
  201. // fpbp loop fpbp start recv
  202. CNodePtr fpbp_start_recv = CreateRecvApplyKernel(kernel_graph_ptr, fpbp_start_event_id);
  203. uint32_t fpbp_stream_id = resource_manager.ApplyNewStream();
  204. AnfAlgo::SetStreamId(fpbp_stream_id, fpbp_start_recv.get());
  205. exec_order.push_back(fpbp_start_recv);
  206. // update fpbp loop stream switch true_branch_stream attr
  207. AnfAlgo::SetNodeAttr(kAttrTrueBranchStream, MakeValue<uint32_t>(fpbp_stream_id), fpbp_switch_app);
  208. // fpbp loop AssignAdd
  209. CNodePtr assign_add_one = CreateStreamAssignAddnOP(kernel_graph_ptr, switch_loop_input);
  210. MS_EXCEPTION_IF_NULL(assign_add_one);
  211. AnfAlgo::SetStreamId(fpbp_stream_id, assign_add_one.get());
  212. exec_order.push_back(assign_add_one);
  213. // fpbp memcpy
  214. std::vector<CNodePtr> memcpy_list;
  215. std::vector<CNodePtr> other_list;
  216. CNodePtr cur_cnode = nullptr;
  217. for (size_t idx = i + 1; idx < orders.size(); idx++) {
  218. cur_cnode = orders[idx];
  219. if (AnfAlgo::HasNodeAttr(kAttrLabelForInsertStreamActive, cur_cnode)) {
  220. memcpy_list.emplace_back(cur_cnode);
  221. } else {
  222. other_list.emplace_back(cur_cnode);
  223. }
  224. }
  225. (void)std::copy(memcpy_list.begin(), memcpy_list.end(), std::back_inserter(exec_order));
  226. // fpbp loop eos done recv
  227. if (eos_mode) {
  228. CNodePtr eos_done_recv = CreateRecvApplyKernel(kernel_graph_ptr, eos_done_event_id);
  229. AnfAlgo::SetStreamId(fpbp_stream_id, eos_done_recv.get());
  230. exec_order.push_back(eos_done_recv);
  231. }
  232. // stream active to activate getnext loop
  233. CNodePtr getnext_active_app = CreateStreamActiveOp(kernel_graph_ptr);
  234. MS_EXCEPTION_IF_NULL(getnext_active_app);
  235. getnext_active_streams.push_back(getnext_switch_stream_id);
  236. AnfAlgo::SetNodeAttr(kAttrActiveStreamList, MakeValue<std::vector<uint32_t>>(getnext_active_streams),
  237. getnext_active_app);
  238. exec_order.push_back(getnext_active_app);
  239. // fpbp loop other ops
  240. (void)std::copy(other_list.begin(), other_list.end(), std::back_inserter(exec_order));
  241. // stream active to activate fpbp loop and eos loop
  242. CNodePtr fpbp_active_app = CreateStreamActiveOp(kernel_graph_ptr);
  243. MS_EXCEPTION_IF_NULL(fpbp_active_app);
  244. fpbp_active_streams.push_back(fpbp_switch_stream_id);
  245. AnfAlgo::SetNodeAttr(kAttrActiveStreamList, MakeValue<std::vector<uint32_t>>(fpbp_active_streams), fpbp_active_app);
  246. exec_order.push_back(fpbp_active_app);
  247. kernel_graph_ptr->set_execution_order(exec_order);
  248. }
  249. void KernelAdjust::CreateSwitchOpParameters(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  250. std::map<std::string, mindspore::ParameterPtr> *switch_loop_input) {
  251. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  252. MS_EXCEPTION_IF_NULL(switch_loop_input);
  253. std::vector<int> shp = {1};
  254. tensor::TensorPtr tensor_ptr = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  255. MS_EXCEPTION_IF_NULL(tensor_ptr);
  256. mindspore::abstract::AbstractBasePtr paremeter_abstract_ptr = tensor_ptr->ToAbstract();
  257. if (paremeter_abstract_ptr == nullptr) {
  258. MS_LOG(EXCEPTION) << "create abstract before insert switch op failed!";
  259. }
  260. ParameterPtr loop_count = std::make_shared<Parameter>(kernel_graph_ptr);
  261. MS_EXCEPTION_IF_NULL(loop_count);
  262. loop_count->set_name(kLoopCountParamName);
  263. loop_count->set_abstract(paremeter_abstract_ptr);
  264. ParameterPtr loop_count_new = kernel_graph_ptr->NewParameter(loop_count);
  265. (*switch_loop_input)[kLoopCountParamName] = loop_count_new;
  266. ParameterPtr iter_loop = std::make_shared<Parameter>(kernel_graph_ptr);
  267. iter_loop->set_name(kIterLoopParamName);
  268. iter_loop->set_abstract(paremeter_abstract_ptr);
  269. ParameterPtr iter_loop_new = kernel_graph_ptr->NewParameter(iter_loop);
  270. (*switch_loop_input)[kIterLoopParamName] = iter_loop_new;
  271. ParameterPtr zero = std::make_shared<Parameter>(kernel_graph_ptr);
  272. zero->set_name(kZeroParamName);
  273. zero->set_abstract(paremeter_abstract_ptr);
  274. ParameterPtr zero_new = kernel_graph_ptr->NewParameter(zero);
  275. (*switch_loop_input)[kZeroParamName] = zero_new;
  276. ParameterPtr one = std::make_shared<Parameter>(kernel_graph_ptr);
  277. one->set_name(kOneParamName);
  278. one->set_abstract(paremeter_abstract_ptr);
  279. ParameterPtr one_new = kernel_graph_ptr->NewParameter(one);
  280. (*switch_loop_input)[kOneParamName] = one_new;
  281. }
  282. kernel::KernelBuildInfo::KernelBuildInfoBuilder KernelAdjust::CreateMngKernelBuilder(
  283. const std::vector<std::string> &formats, const std::vector<TypeId> &type_ids) {
  284. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  285. selected_kernel_builder.SetInputsFormat(formats);
  286. selected_kernel_builder.SetInputsDeviceType(type_ids);
  287. selected_kernel_builder.SetFusionType(kernel::FusionType::OPAQUE);
  288. selected_kernel_builder.SetProcessor(kernel::Processor::AICORE);
  289. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  290. return selected_kernel_builder;
  291. }
  292. CNodePtr KernelAdjust::CreateStreamSwitchOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  293. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input) {
  294. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  295. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  296. auto typeNone_abstract = std::make_shared<abstract::AbstractNone>();
  297. auto stream_switch = std::make_shared<Primitive>(kStreamSwitchOpName);
  298. std::vector<AnfNodePtr> inputs;
  299. inputs.push_back(NewValueNode(stream_switch));
  300. inputs.push_back(switch_loop_input.at(kLoopCountParamName));
  301. inputs.push_back(switch_loop_input.at(kIterLoopParamName));
  302. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  303. CNodePtr stream_switch_app = kernel_graph_ptr->NewCNode(inputs);
  304. MS_EXCEPTION_IF_NULL(stream_switch_app);
  305. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), stream_switch_app.get());
  306. stream_switch_app->set_abstract(typeNone_abstract);
  307. // set attr: cond_ RT_LESS
  308. int condition = static_cast<int>(RT_LESS);
  309. ValuePtr cond = MakeValue(condition);
  310. AnfAlgo::SetNodeAttr(kAttrSwitchCondition, cond, stream_switch_app);
  311. // set attr:data_type
  312. int data_type = static_cast<int>(RT_SWITCH_INT64);
  313. ValuePtr dt = MakeValue(data_type);
  314. AnfAlgo::SetNodeAttr(kAttrDataType, dt, stream_switch_app);
  315. // set distinction label and graph id
  316. return stream_switch_app;
  317. }
  318. CNodePtr KernelAdjust::CreateStreamActiveOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  319. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  320. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  321. abstract::AbstractBasePtr typeNone_abstract = std::make_shared<abstract::AbstractNone>();
  322. auto stream_active_others = std::make_shared<Primitive>(kStreamActiveOpName);
  323. std::vector<AnfNodePtr> inputs;
  324. inputs.push_back(NewValueNode(stream_active_others));
  325. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  326. CNodePtr stream_active_others_app = kernel_graph_ptr->NewCNode(inputs);
  327. MS_EXCEPTION_IF_NULL(stream_active_others_app);
  328. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), stream_active_others_app.get());
  329. stream_active_others_app->set_abstract(typeNone_abstract);
  330. return stream_active_others_app;
  331. }
  332. CNodePtr KernelAdjust::CreatTupleGetItemNode(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  333. const CNodePtr &node, size_t output_idx) {
  334. auto idx = NewValueNode(SizeToInt(output_idx));
  335. MS_EXCEPTION_IF_NULL(idx);
  336. auto imm = std::make_shared<Int32Imm>(SizeToInt(output_idx));
  337. auto abstract_scalar = std::make_shared<abstract::AbstractScalar>(imm);
  338. idx->set_abstract(abstract_scalar);
  339. CNodePtr tuple_getitem = kernel_graph_ptr->NewCNode({NewValueNode(prim::kPrimTupleGetItem), node, idx});
  340. MS_EXCEPTION_IF_NULL(tuple_getitem);
  341. tuple_getitem->set_scope(node->scope());
  342. std::vector<size_t> origin_shape = AnfAlgo::GetOutputInferShape(node, output_idx);
  343. TypeId origin_type = AnfAlgo::GetOutputInferDataType(node, output_idx);
  344. AnfAlgo::SetOutputInferTypeAndShape({origin_type}, {origin_shape}, tuple_getitem.get());
  345. return tuple_getitem;
  346. }
  347. CNodePtr KernelAdjust::CreateEndOfSequenceOP(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  348. const CNodePtr &getnext_cnode) {
  349. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  350. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  351. selected_kernel_builder.SetInputsFormat({kOpFormat_DEFAULT});
  352. selected_kernel_builder.SetInputsDeviceType({kNumberTypeUInt8});
  353. selected_kernel_builder.SetFusionType(kernel::FusionType::OPAQUE);
  354. selected_kernel_builder.SetProcessor(kernel::Processor::AICPU);
  355. selected_kernel_builder.SetKernelType(KernelType::AICPU_KERNEL);
  356. selected_kernel_builder.SetOutputsFormat({kOpFormat_DEFAULT});
  357. selected_kernel_builder.SetOutputsDeviceType({kNumberTypeUInt8});
  358. // EndOfSequence
  359. auto end_of_sequence = std::make_shared<Primitive>(kEndOfSequence);
  360. std::vector<AnfNodePtr> inputs;
  361. inputs.push_back(NewValueNode(end_of_sequence));
  362. // GetNext output 0 is EndOfSequence's input
  363. auto tuple_get_item = CreatTupleGetItemNode(kernel_graph_ptr, getnext_cnode, 0);
  364. inputs.push_back(tuple_get_item);
  365. CNodePtr end_of_sequence_node = kernel_graph_ptr->NewCNode(inputs);
  366. MS_EXCEPTION_IF_NULL(end_of_sequence_node);
  367. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), end_of_sequence_node.get());
  368. std::vector<std::string> input_names = {"x"};
  369. ValuePtr input_names_v = MakeValue(input_names);
  370. AnfAlgo::SetNodeAttr("input_names", input_names_v, end_of_sequence_node);
  371. std::vector<std::string> output_names = {"y"};
  372. ValuePtr output_names_v = MakeValue(output_names);
  373. AnfAlgo::SetNodeAttr("output_names", output_names_v, end_of_sequence_node);
  374. end_of_sequence_node->set_abstract(tuple_get_item->abstract());
  375. return end_of_sequence_node;
  376. }
  377. CNodePtr KernelAdjust::CreateStreamAssignAddnOP(
  378. const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  379. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input) {
  380. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  381. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  382. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  383. selected_kernel_builder.SetOutputsFormat({kOpFormat_DEFAULT});
  384. selected_kernel_builder.SetOutputsDeviceType({kNumberTypeInt32});
  385. // AssignAdd
  386. auto assign_add = std::make_shared<Primitive>(kAssignAddOpName);
  387. std::vector<AnfNodePtr> inputs;
  388. inputs.push_back(NewValueNode(assign_add));
  389. inputs.push_back(switch_loop_input.at(kLoopCountParamName));
  390. inputs.push_back(switch_loop_input.at(kOneParamName));
  391. CNodePtr assign_add_one = kernel_graph_ptr->NewCNode(inputs);
  392. MS_EXCEPTION_IF_NULL(assign_add_one);
  393. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), assign_add_one.get());
  394. std::vector<std::string> input_names = {"ref", "value"};
  395. std::vector<std::string> output_names = {"output"};
  396. ValuePtr input_names_v = MakeValue(input_names);
  397. ValuePtr output_names_v = MakeValue(output_names);
  398. AnfAlgo::SetNodeAttr("input_names", input_names_v, assign_add_one);
  399. AnfAlgo::SetNodeAttr("output_names", output_names_v, assign_add_one);
  400. selected_kernel_builder.SetKernelType(KernelType::TBE_KERNEL);
  401. MS_EXCEPTION_IF_NULL(switch_loop_input.at(kLoopCountParamName));
  402. assign_add_one->set_abstract(switch_loop_input.at(kLoopCountParamName)->abstract());
  403. return assign_add_one;
  404. }
  405. bool KernelAdjust::StepLoadCtrlInputs(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  406. if (!NeedInsertSwitch()) {
  407. return true;
  408. }
  409. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  410. auto input_nodes = kernel_graph_ptr->inputs();
  411. std::vector<tensor::TensorPtr> inputs;
  412. LoadSwitchInputs(&inputs);
  413. std::shared_ptr<std::vector<tensor::TensorPtr>> inputsPtr = std::make_shared<std::vector<tensor::TensorPtr>>(inputs);
  414. kernel_graph_ptr->set_input_ctrl_tensors(inputsPtr);
  415. size_t input_ctrl_size = inputs.size();
  416. // inputs_node:include four ctrl nodes in the back. such as:conv,loop_cnt, ites_loop, zero, one.
  417. // deal four ctrl nodes.
  418. for (size_t i = 0; i < inputs.size(); ++i) {
  419. auto tensor = inputs[i];
  420. size_t deal_index = input_nodes.size() - input_ctrl_size + i;
  421. if (deal_index >= input_nodes.size()) {
  422. MS_LOG(EXCEPTION) << "deal_index[" << deal_index << "] out of range";
  423. }
  424. auto input_node = input_nodes[deal_index];
  425. bool need_sync = false;
  426. MS_EXCEPTION_IF_NULL(input_node);
  427. if (input_node->isa<Parameter>()) {
  428. auto pk_node = input_node->cast<ParameterPtr>();
  429. MS_EXCEPTION_IF_NULL(tensor);
  430. MS_EXCEPTION_IF_NULL(pk_node);
  431. if (tensor->is_dirty() || !pk_node->has_default()) {
  432. need_sync = true;
  433. }
  434. }
  435. if (need_sync) {
  436. auto pk_node = input_node->cast<ParameterPtr>();
  437. MS_EXCEPTION_IF_NULL(pk_node);
  438. auto device_address = AnfAlgo::GetMutableOutputAddr(pk_node, 0);
  439. MS_EXCEPTION_IF_NULL(device_address);
  440. tensor->set_device_address(device_address);
  441. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  442. LongToSize(tensor->data().nbytes()), tensor->data_type(),
  443. tensor->data_c())) {
  444. MS_LOG(INFO) << "SyncHostToDevice failed.";
  445. return false;
  446. }
  447. }
  448. tensor->set_dirty(false);
  449. }
  450. return true;
  451. }
  452. void KernelAdjust::LoadSwitchInputs(std::vector<tensor::TensorPtr> *inputs) {
  453. MS_LOG(INFO) << "---------------- LoadSwitchInputs---";
  454. MS_EXCEPTION_IF_NULL(inputs);
  455. std::vector<int> shp = {1};
  456. tensor::TensorPtr loop_count_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  457. MS_EXCEPTION_IF_NULL(loop_count_tensor);
  458. int32_t *val = nullptr;
  459. val = static_cast<int32_t *>(loop_count_tensor->data_c());
  460. MS_EXCEPTION_IF_NULL(val);
  461. *val = 0;
  462. inputs->push_back(loop_count_tensor);
  463. tensor::TensorPtr iter_loop_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  464. MS_EXCEPTION_IF_NULL(iter_loop_tensor);
  465. val = static_cast<int32_t *>(iter_loop_tensor->data_c());
  466. MS_EXCEPTION_IF_NULL(val);
  467. *val = SizeToInt(LongToSize(ConfigManager::GetInstance().iter_num()));
  468. MS_LOG(INFO) << "iter_loop_tensor = " << *val;
  469. inputs->push_back(iter_loop_tensor);
  470. tensor::TensorPtr zero_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  471. MS_EXCEPTION_IF_NULL(zero_tensor);
  472. val = static_cast<int32_t *>(zero_tensor->data_c());
  473. MS_EXCEPTION_IF_NULL(val);
  474. *val = 0;
  475. inputs->push_back(zero_tensor);
  476. tensor::TensorPtr one_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  477. MS_EXCEPTION_IF_NULL(one_tensor);
  478. val = static_cast<int32_t *>(one_tensor->data_c());
  479. MS_EXCEPTION_IF_NULL(val);
  480. *val = 1;
  481. inputs->push_back(one_tensor);
  482. MS_LOG(INFO) << "---------------- LoadSwitchInputs End--";
  483. }
  484. void KernelAdjust::Profiling(NotNull<session::KernelGraph *> kernel_graph_ptr) {
  485. if (!ascend::ProfilingManager::GetInstance().IsProfiling()) {
  486. MS_LOG(INFO) << "No need to profiling";
  487. return;
  488. }
  489. ProfilingTraceInfo profiling_trace_info = ProfilingUtils::GetProfilingTraceFromEnv(kernel_graph_ptr);
  490. if (!profiling_trace_info.IsValid()) {
  491. MS_LOG(WARNING) << "[profiling] no profiling node found!";
  492. return;
  493. }
  494. InsertProfilingKernel(profiling_trace_info, kernel_graph_ptr);
  495. }
  496. void KernelAdjust::InsertProfilingKernel(const ProfilingTraceInfo &profiling_trace_info,
  497. NotNull<session::KernelGraph *> kernel_graph_ptr) {
  498. MS_LOG(INFO) << "[profiling] Insert profiling kernel start";
  499. if (!profiling_trace_info.IsValid()) {
  500. MS_LOG(WARNING) << "Profiling trace point not found";
  501. return;
  502. }
  503. std::vector<CNodePtr> new_cnode_list;
  504. std::vector<CNodePtr> cnode_ptr_list = kernel_graph_ptr->execution_order();
  505. if (cnode_ptr_list.empty()) {
  506. MS_LOG(ERROR) << "No CNode in graph";
  507. return;
  508. }
  509. for (const auto &cnode_ptr : cnode_ptr_list) {
  510. ProfilingUtils::ProfilingTraceFpStart(cnode_ptr, profiling_trace_info, kernel_graph_ptr, NOT_NULL(&new_cnode_list));
  511. new_cnode_list.emplace_back(cnode_ptr);
  512. ProfilingUtils::ProfilingCustomOp(cnode_ptr, profiling_trace_info, kernel_graph_ptr, NOT_NULL(&new_cnode_list));
  513. ProfilingUtils::ProfilingTraceBpEnd(cnode_ptr, profiling_trace_info, kernel_graph_ptr, NOT_NULL(&new_cnode_list));
  514. ProfilingUtils::ProfilingTraceEnd(cnode_ptr, profiling_trace_info, kernel_graph_ptr, NOT_NULL(&new_cnode_list));
  515. }
  516. kernel_graph_ptr->set_execution_order(new_cnode_list);
  517. }
  518. } // namespace device
  519. } // namespace mindspore