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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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::Reorder(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> momentum_list;
  41. std::vector<CNodePtr> other_list;
  42. for (const auto &cnode : origin_cnode_list) {
  43. if (kOptOperatorSet.find(AnfAlgo::GetCNodeName(cnode)) != kOptOperatorSet.end()) {
  44. momentum_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(), other_list.begin(), other_list.end());
  51. new_order_list.insert(new_order_list.end(), momentum_list.begin(), momentum_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. uint32_t KernelAdjust::FindFirstStreamSwitchLabel(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  61. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  62. auto cnode_ptr_list = kernel_graph_ptr->execution_order();
  63. CNodePtr cur_cnode_ptr = nullptr;
  64. uint32_t label = kInvalidDistincLabel;
  65. for (uint32_t i = 0; i < cnode_ptr_list.size(); ++i) {
  66. cur_cnode_ptr = cnode_ptr_list[i];
  67. MS_EXCEPTION_IF_NULL(cur_cnode_ptr);
  68. if (AnfAlgo::GetCNodeName(cur_cnode_ptr) == kStreamSwitchOpName) {
  69. label = AnfAlgo::GetStreamDistinctionLabel(cur_cnode_ptr.get());
  70. break;
  71. }
  72. }
  73. return label;
  74. }
  75. CNodePtr KernelAdjust::CreateSendApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr,
  76. uint32_t event_id) {
  77. MS_EXCEPTION_IF_NULL(graph_ptr);
  78. auto send_op = std::make_shared<Primitive>(kSendOpName);
  79. MS_EXCEPTION_IF_NULL(send_op);
  80. auto send_apply = std::make_shared<ValueNode>(send_op);
  81. MS_EXCEPTION_IF_NULL(send_apply);
  82. std::vector<AnfNodePtr> send_input_list = {send_apply};
  83. CNodePtr send_node_ptr = graph_ptr->NewCNode(send_input_list);
  84. MS_EXCEPTION_IF_NULL(send_node_ptr);
  85. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  86. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  87. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), send_node_ptr.get());
  88. AnfAlgo::SetNodeAttr(kAttrEventId, MakeValue(event_id), send_node_ptr);
  89. auto abstract_none = std::make_shared<abstract::AbstractNone>();
  90. MS_EXCEPTION_IF_NULL(abstract_none);
  91. send_node_ptr->set_abstract(abstract_none);
  92. return send_node_ptr;
  93. }
  94. CNodePtr KernelAdjust::CreateRecvApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr,
  95. uint32_t event_id) {
  96. MS_EXCEPTION_IF_NULL(graph_ptr);
  97. auto recv_op = std::make_shared<Primitive>(kRecvOpName);
  98. MS_EXCEPTION_IF_NULL(recv_op);
  99. auto recv_apply = std::make_shared<ValueNode>(recv_op);
  100. MS_EXCEPTION_IF_NULL(recv_apply);
  101. std::vector<AnfNodePtr> recv_input_list = {recv_apply};
  102. CNodePtr recv_node_ptr = graph_ptr->NewCNode(recv_input_list);
  103. MS_EXCEPTION_IF_NULL(recv_node_ptr);
  104. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  105. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  106. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), recv_node_ptr.get());
  107. AnfAlgo::SetNodeAttr(kAttrEventId, MakeValue(event_id), recv_node_ptr);
  108. auto abstract_none = std::make_shared<abstract::AbstractNone>();
  109. MS_EXCEPTION_IF_NULL(abstract_none);
  110. recv_node_ptr->set_abstract(abstract_none);
  111. return recv_node_ptr;
  112. }
  113. void KernelAdjust::InsertSwitchLoop(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  114. if (!NeedInsertSwitch()) {
  115. return;
  116. }
  117. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  118. std::map<std::string, mindspore::ParameterPtr> switch_loop_input;
  119. CreateSwitchOpParameters(kernel_graph_ptr, &switch_loop_input);
  120. std::vector<AnfNodePtr> *mute_inputs = kernel_graph_ptr->MutableInputs();
  121. MS_EXCEPTION_IF_NULL(mute_inputs);
  122. mute_inputs->push_back(switch_loop_input[kLoopCountParamName]);
  123. mute_inputs->push_back(switch_loop_input[kIterLoopParamName]);
  124. mute_inputs->push_back(switch_loop_input[kZeroParamName]);
  125. mute_inputs->push_back(switch_loop_input[kOneParamName]);
  126. for (const auto &input : kernel_graph_ptr->inputs()) {
  127. MS_EXCEPTION_IF_NULL(input);
  128. if (input->isa<Parameter>()) {
  129. ParameterPtr param_ptr = input->cast<ParameterPtr>();
  130. if (param_ptr == nullptr) {
  131. MS_EXCEPTION(NotSupportError) << "Cast to parameter point failed !";
  132. }
  133. }
  134. }
  135. auto orders = kernel_graph_ptr->execution_order();
  136. if (orders.empty()) {
  137. MS_LOG(EXCEPTION) << "graph execution order is empty";
  138. }
  139. uint32_t first_cnode_stream_label = AnfAlgo::GetStreamDistinctionLabel(orders[0].get());
  140. std::vector<CNodePtr> exec_order;
  141. CNodePtr first_stream_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input);
  142. MS_EXCEPTION_IF_NULL(first_stream_switch_app);
  143. AnfAlgo::SetStreamDistinctionLabel(kFirstStreamSwitchLabel, first_stream_switch_app.get());
  144. AnfAlgo::SetNodeAttr(kAttrTrueBranchStream, MakeValue<uint32_t>(kGetNextLabel), first_stream_switch_app);
  145. CNodePtr second_stream_switch_app = CreateStreamSwitchOp(kernel_graph_ptr, switch_loop_input);
  146. MS_EXCEPTION_IF_NULL(second_stream_switch_app);
  147. AnfAlgo::SetStreamDistinctionLabel(kSecondStreamSwitchLabel, second_stream_switch_app.get());
  148. AnfAlgo::SetNodeAttr(kAttrTrueBranchStream, MakeValue<uint32_t>(first_cnode_stream_label), second_stream_switch_app);
  149. // add attr "stream_need_active"
  150. AnfAlgo::SetNodeAttr(kStreamNeedActivedFirst, MakeValue<bool>(true), second_stream_switch_app);
  151. CNodePtr first_stream_active_app = CreateStreamActiveOp(kernel_graph_ptr);
  152. MS_EXCEPTION_IF_NULL(first_stream_active_app);
  153. AnfAlgo::SetStreamDistinctionLabel(first_cnode_stream_label, first_stream_active_app.get());
  154. std::vector<uint32_t> first_active_streams = {kFirstStreamSwitchLabel};
  155. AnfAlgo::SetNodeAttr(kAttrActiveStreamList, MakeValue<std::vector<uint32_t>>(first_active_streams),
  156. first_stream_active_app);
  157. CNodePtr second_stream_active_app = CreateStreamActiveOp(kernel_graph_ptr);
  158. MS_EXCEPTION_IF_NULL(second_stream_active_app);
  159. // specific deal for common ctrl stream policy
  160. uint32_t first_common_stream_switch_label = FindFirstStreamSwitchLabel(kernel_graph_ptr);
  161. if (first_common_stream_switch_label == kInvalidDistincLabel) {
  162. AnfAlgo::SetStreamDistinctionLabel(first_cnode_stream_label, second_stream_active_app.get());
  163. } else {
  164. AnfAlgo::SetStreamDistinctionLabel(first_common_stream_switch_label, second_stream_active_app.get());
  165. }
  166. std::vector<uint32_t> second_active_streams = {kSecondStreamSwitchLabel};
  167. AnfAlgo::SetNodeAttr(kAttrActiveStreamList, MakeValue<std::vector<uint32_t>>(second_active_streams),
  168. second_stream_active_app);
  169. CNodePtr assign_add_one = CreateStreamAssignAddnOP(kernel_graph_ptr, switch_loop_input);
  170. MS_EXCEPTION_IF_NULL(assign_add_one);
  171. AnfAlgo::SetStreamDistinctionLabel(first_cnode_stream_label, assign_add_one.get());
  172. CNodePtr send = CreateSendApplyKernel(kernel_graph_ptr, kFirstEventId);
  173. AnfAlgo::SetStreamDistinctionLabel(kGetNextLabel, send.get());
  174. CNodePtr recv = CreateRecvApplyKernel(kernel_graph_ptr, kFirstEventId);
  175. AnfAlgo::SetStreamDistinctionLabel(first_cnode_stream_label, recv.get());
  176. // reorder graph orders
  177. exec_order.push_back(first_stream_switch_app);
  178. size_t i = 0;
  179. for (; i < orders.size(); i++) {
  180. auto node = orders[i];
  181. exec_order.push_back(node);
  182. AnfAlgo::SetStreamDistinctionLabel(kGetNextLabel, exec_order[exec_order.size() - 1].get());
  183. if (AnfAlgo::GetCNodeName(node) == kGetNextOpName) {
  184. break;
  185. }
  186. }
  187. exec_order.push_back(send);
  188. exec_order.push_back(second_stream_switch_app);
  189. exec_order.push_back(recv);
  190. exec_order.push_back(assign_add_one);
  191. std::vector<CNodePtr> memcpy_list;
  192. std::vector<CNodePtr> before_list;
  193. std::vector<CNodePtr> after_list;
  194. bool first_memcpy_found = false;
  195. CNodePtr cur_cnode = nullptr;
  196. for (size_t idx = i + 1; idx < orders.size(); idx++) {
  197. cur_cnode = orders[idx];
  198. if (AnfAlgo::HasNodeAttr(kAttrLabelForInsertStreamActive, cur_cnode)) {
  199. memcpy_list.emplace_back(cur_cnode);
  200. first_memcpy_found = true;
  201. } else if (first_memcpy_found) {
  202. after_list.emplace_back(cur_cnode);
  203. } else {
  204. before_list.emplace_back(cur_cnode);
  205. }
  206. }
  207. (void)std::copy(before_list.begin(), before_list.end(), std::back_inserter(exec_order));
  208. (void)std::copy(memcpy_list.begin(), memcpy_list.end(), std::back_inserter(exec_order));
  209. exec_order.push_back(first_stream_active_app);
  210. (void)std::copy(after_list.begin(), after_list.end(), std::back_inserter(exec_order));
  211. exec_order.push_back(second_stream_active_app);
  212. kernel_graph_ptr->set_execution_order(exec_order);
  213. }
  214. void KernelAdjust::CreateSwitchOpParameters(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  215. std::map<std::string, mindspore::ParameterPtr> *switch_loop_input) {
  216. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  217. MS_EXCEPTION_IF_NULL(switch_loop_input);
  218. std::vector<int> shp = {1};
  219. tensor::TensorPtr tensor_ptr = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  220. MS_EXCEPTION_IF_NULL(tensor_ptr);
  221. mindspore::abstract::AbstractBasePtr paremeter_abstract_ptr = tensor_ptr->ToAbstract();
  222. if (paremeter_abstract_ptr == nullptr) {
  223. MS_LOG(EXCEPTION) << "create abstract before insert switch op failed!";
  224. }
  225. ParameterPtr loop_count = std::make_shared<Parameter>(kernel_graph_ptr);
  226. MS_EXCEPTION_IF_NULL(loop_count);
  227. loop_count->set_name(kLoopCountParamName);
  228. loop_count->set_abstract(paremeter_abstract_ptr);
  229. ParameterPtr loop_count_new = kernel_graph_ptr->NewParameter(loop_count);
  230. (*switch_loop_input)[kLoopCountParamName] = loop_count_new;
  231. ParameterPtr iter_loop = std::make_shared<Parameter>(kernel_graph_ptr);
  232. iter_loop->set_name(kIterLoopParamName);
  233. iter_loop->set_abstract(paremeter_abstract_ptr);
  234. ParameterPtr iter_loop_new = kernel_graph_ptr->NewParameter(iter_loop);
  235. (*switch_loop_input)[kIterLoopParamName] = iter_loop_new;
  236. ParameterPtr zero = std::make_shared<Parameter>(kernel_graph_ptr);
  237. zero->set_name(kZeroParamName);
  238. zero->set_abstract(paremeter_abstract_ptr);
  239. ParameterPtr zero_new = kernel_graph_ptr->NewParameter(zero);
  240. (*switch_loop_input)[kZeroParamName] = zero_new;
  241. ParameterPtr one = std::make_shared<Parameter>(kernel_graph_ptr);
  242. one->set_name(kOneParamName);
  243. one->set_abstract(paremeter_abstract_ptr);
  244. ParameterPtr one_new = kernel_graph_ptr->NewParameter(one);
  245. (*switch_loop_input)[kOneParamName] = one_new;
  246. }
  247. kernel::KernelBuildInfo::KernelBuildInfoBuilder KernelAdjust::CreateMngKernelBuilder(
  248. const std::vector<std::string> &formats, const std::vector<TypeId> &type_ids) {
  249. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder;
  250. selected_kernel_builder.SetInputsFormat(formats);
  251. selected_kernel_builder.SetInputsDeviceType(type_ids);
  252. selected_kernel_builder.SetFusionType(kernel::FusionType::OPAQUE);
  253. selected_kernel_builder.SetProcessor(kernel::Processor::AICORE);
  254. selected_kernel_builder.SetKernelType(KernelType::RT_KERNEL);
  255. return selected_kernel_builder;
  256. }
  257. CNodePtr KernelAdjust::CreateStreamSwitchOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  258. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input) {
  259. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  260. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  261. auto typeNone_abstract = std::make_shared<abstract::AbstractNone>();
  262. auto stream_switch = std::make_shared<Primitive>(kStreamSwitchOpName);
  263. std::vector<AnfNodePtr> inputs;
  264. inputs.push_back(NewValueNode(stream_switch));
  265. inputs.push_back(switch_loop_input.at(kLoopCountParamName));
  266. inputs.push_back(switch_loop_input.at(kIterLoopParamName));
  267. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  268. CNodePtr stream_switch_app = kernel_graph_ptr->NewCNode(inputs);
  269. MS_EXCEPTION_IF_NULL(stream_switch_app);
  270. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), stream_switch_app.get());
  271. stream_switch_app->set_abstract(typeNone_abstract);
  272. // set attr: cond_ RT_LESS
  273. int condition = static_cast<int>(RT_LESS);
  274. ValuePtr cond = MakeValue(condition);
  275. AnfAlgo::SetNodeAttr(kAttrSwitchCondition, cond, stream_switch_app);
  276. // set attr:data_type
  277. int data_type = static_cast<int>(RT_SWITCH_INT64);
  278. ValuePtr dt = MakeValue(data_type);
  279. AnfAlgo::SetNodeAttr(kAttrDataType, dt, stream_switch_app);
  280. // set distinction label and graph id
  281. return stream_switch_app;
  282. }
  283. CNodePtr KernelAdjust::CreateStreamActiveOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  284. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  285. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  286. abstract::AbstractBasePtr typeNone_abstract = std::make_shared<abstract::AbstractNone>();
  287. auto stream_active_others = std::make_shared<Primitive>(kStreamActiveOpName);
  288. std::vector<AnfNodePtr> inputs;
  289. inputs.push_back(NewValueNode(stream_active_others));
  290. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  291. CNodePtr stream_active_others_app = kernel_graph_ptr->NewCNode(inputs);
  292. MS_EXCEPTION_IF_NULL(stream_active_others_app);
  293. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), stream_active_others_app.get());
  294. stream_active_others_app->set_abstract(typeNone_abstract);
  295. return stream_active_others_app;
  296. }
  297. CNodePtr KernelAdjust::CreateStreamAssignAddnOP(
  298. const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  299. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input) {
  300. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  301. kernel::KernelBuildInfo::KernelBuildInfoBuilder selected_kernel_builder = CreateMngKernelBuilder(
  302. {kOpFormat_DEFAULT, kOpFormat_DEFAULT}, {TypeId::kNumberTypeInt32, TypeId::kNumberTypeInt32});
  303. selected_kernel_builder.SetOutputsFormat({kOpFormat_DEFAULT});
  304. selected_kernel_builder.SetOutputsDeviceType({kNumberTypeInt32});
  305. // AssignAdd
  306. auto assign_add = std::make_shared<Primitive>(kAssignAddOpName);
  307. std::vector<AnfNodePtr> inputs;
  308. inputs.push_back(NewValueNode(assign_add));
  309. inputs.push_back(switch_loop_input.at(kLoopCountParamName));
  310. inputs.push_back(switch_loop_input.at(kOneParamName));
  311. CNodePtr assign_add_one = kernel_graph_ptr->NewCNode(inputs);
  312. MS_EXCEPTION_IF_NULL(assign_add_one);
  313. AnfAlgo::SetSelectKernelBuildInfo(selected_kernel_builder.Build(), assign_add_one.get());
  314. std::vector<std::string> input_names = {"ref", "value"};
  315. std::vector<std::string> output_names = {"output"};
  316. ValuePtr input_names_v = MakeValue(input_names);
  317. ValuePtr output_names_v = MakeValue(output_names);
  318. AnfAlgo::SetNodeAttr("input_names", input_names_v, assign_add_one);
  319. AnfAlgo::SetNodeAttr("output_names", output_names_v, assign_add_one);
  320. selected_kernel_builder.SetKernelType(KernelType::TBE_KERNEL);
  321. MS_EXCEPTION_IF_NULL(switch_loop_input.at(kLoopCountParamName));
  322. assign_add_one->set_abstract(switch_loop_input.at(kLoopCountParamName)->abstract());
  323. return assign_add_one;
  324. }
  325. bool KernelAdjust::StepLoadCtrlInputs(const std::shared_ptr<session::Context> &context,
  326. const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr) {
  327. if (!NeedInsertSwitch()) {
  328. return true;
  329. }
  330. MS_EXCEPTION_IF_NULL(context);
  331. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  332. auto input_nodes = kernel_graph_ptr->inputs();
  333. std::vector<tensor::TensorPtr> inputs;
  334. LoadSwitchInputs(&inputs);
  335. std::shared_ptr<std::vector<tensor::TensorPtr>> inputsPtr = std::make_shared<std::vector<tensor::TensorPtr>>(inputs);
  336. context->SetResult(session::kInputCtrlTensors, inputsPtr);
  337. size_t input_ctrl_size = inputs.size();
  338. // inputs_node:include four ctrl nodes in the back. such as:conv,loop_cnt, ites_loop, zero, one.
  339. // deal four ctrl nodes.
  340. for (size_t i = 0; i < inputs.size(); ++i) {
  341. auto tensor = inputs[i];
  342. size_t deal_index = input_nodes.size() - input_ctrl_size + i;
  343. if (deal_index >= input_nodes.size()) {
  344. MS_LOG(EXCEPTION) << "deal_index[" << deal_index << "] out of range";
  345. }
  346. auto input_node = input_nodes[deal_index];
  347. bool need_sync = false;
  348. MS_EXCEPTION_IF_NULL(input_node);
  349. if (input_node->isa<Parameter>()) {
  350. auto pk_node = input_node->cast<ParameterPtr>();
  351. MS_EXCEPTION_IF_NULL(tensor);
  352. MS_EXCEPTION_IF_NULL(pk_node);
  353. if (tensor->is_dirty() || !pk_node->has_default()) {
  354. need_sync = true;
  355. }
  356. }
  357. if (need_sync) {
  358. auto pk_node = input_node->cast<ParameterPtr>();
  359. MS_EXCEPTION_IF_NULL(pk_node);
  360. auto device_address = AnfAlgo::GetMutableOutputAddr(pk_node, 0);
  361. MS_EXCEPTION_IF_NULL(device_address);
  362. tensor->set_device_address(device_address);
  363. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  364. LongToSize(tensor->data().nbytes()), tensor->data_type(),
  365. tensor->data_c(false))) {
  366. MS_LOG(INFO) << "SyncHostToDevice failed.";
  367. return false;
  368. }
  369. }
  370. tensor->set_dirty(false);
  371. }
  372. return true;
  373. }
  374. void KernelAdjust::LoadSwitchInputs(std::vector<tensor::TensorPtr> *inputs) {
  375. MS_LOG(INFO) << "---------------- LoadSwitchInputs---";
  376. MS_EXCEPTION_IF_NULL(inputs);
  377. std::vector<int> shp = {1};
  378. tensor::TensorPtr loop_count_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  379. MS_EXCEPTION_IF_NULL(loop_count_tensor);
  380. int32_t *val = nullptr;
  381. val = static_cast<int32_t *>(loop_count_tensor->data_c(true));
  382. MS_EXCEPTION_IF_NULL(val);
  383. *val = 0;
  384. inputs->push_back(loop_count_tensor);
  385. tensor::TensorPtr iter_loop_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  386. MS_EXCEPTION_IF_NULL(iter_loop_tensor);
  387. val = static_cast<int32_t *>(iter_loop_tensor->data_c(true));
  388. MS_EXCEPTION_IF_NULL(val);
  389. *val = SizeToInt(LongToSize(ConfigManager::GetInstance().iter_num()));
  390. MS_LOG(INFO) << "iter_loop_tensor = " << *val;
  391. inputs->push_back(iter_loop_tensor);
  392. tensor::TensorPtr zero_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  393. MS_EXCEPTION_IF_NULL(zero_tensor);
  394. val = static_cast<int32_t *>(zero_tensor->data_c(true));
  395. MS_EXCEPTION_IF_NULL(val);
  396. *val = 0;
  397. inputs->push_back(zero_tensor);
  398. tensor::TensorPtr one_tensor = std::make_shared<tensor::Tensor>(kInt32->type_id(), shp);
  399. MS_EXCEPTION_IF_NULL(one_tensor);
  400. val = static_cast<int32_t *>(one_tensor->data_c(true));
  401. MS_EXCEPTION_IF_NULL(val);
  402. *val = 1;
  403. inputs->push_back(one_tensor);
  404. MS_LOG(INFO) << "---------------- LoadSwitchInputs End--";
  405. }
  406. void KernelAdjust::Profiling(NotNull<session::KernelGraph *> kernel_graph_ptr) {
  407. if (!ascend::ProfilingManager::GetInstance().IsProfiling()) {
  408. MS_LOG(INFO) << "No need to profiling";
  409. return;
  410. }
  411. ProfilingTraceInfo profiling_trace_info = ProfilingUtils::GetProfilingTraceFromEnv(kernel_graph_ptr);
  412. if (!profiling_trace_info.IsValid()) {
  413. MS_LOG(WARNING) << "[profiling] no profiling node found!";
  414. return;
  415. }
  416. InsertProfilingKernel(profiling_trace_info, kernel_graph_ptr);
  417. }
  418. void KernelAdjust::InsertProfilingKernel(const ProfilingTraceInfo &profiling_trace_info,
  419. NotNull<session::KernelGraph *> kernel_graph_ptr) {
  420. MS_LOG(INFO) << "[profiling] Insert profiling kernel start";
  421. if (!profiling_trace_info.IsValid()) {
  422. MS_LOG(WARNING) << "Profiling trace point not found";
  423. return;
  424. }
  425. std::vector<CNodePtr> new_cnode_list;
  426. std::vector<CNodePtr> cnode_ptr_list = kernel_graph_ptr->execution_order();
  427. if (cnode_ptr_list.empty()) {
  428. MS_LOG(ERROR) << "No CNode in graph";
  429. return;
  430. }
  431. for (const auto &cnode_ptr : cnode_ptr_list) {
  432. ProfilingUtils::ProfilingTraceFpStart(cnode_ptr, profiling_trace_info, kernel_graph_ptr, NOT_NULL(&new_cnode_list));
  433. new_cnode_list.emplace_back(cnode_ptr);
  434. ProfilingUtils::ProfilingCustomOp(cnode_ptr, profiling_trace_info, kernel_graph_ptr, NOT_NULL(&new_cnode_list));
  435. ProfilingUtils::ProfilingTraceBpEnd(cnode_ptr, profiling_trace_info, kernel_graph_ptr, NOT_NULL(&new_cnode_list));
  436. ProfilingUtils::ProfilingTraceEnd(cnode_ptr, profiling_trace_info, kernel_graph_ptr, NOT_NULL(&new_cnode_list));
  437. }
  438. kernel_graph_ptr->set_execution_order(new_cnode_list);
  439. }
  440. } // namespace device
  441. } // namespace mindspore