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_runtime.cc 30 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "device/kernel_runtime.h"
  17. #include <vector>
  18. #include <utility>
  19. #include <numeric>
  20. #include <functional>
  21. #include "common/utils.h"
  22. #include "common/trans.h"
  23. #include "utils/utils.h"
  24. #include "utils/context/ms_context.h"
  25. #include "operator/ops.h"
  26. #include "pipeline/parse/python_adapter.h"
  27. #include "session/kernel_graph.h"
  28. #include "session/anf_runtime_algorithm.h"
  29. #include "kernel/common_utils.h"
  30. #include "kernel/oplib/oplib.h"
  31. #include "ir/value.h"
  32. #include "pre_activate/common/helper.h"
  33. using mindspore::kernel::Address;
  34. using mindspore::kernel::AddressPtr;
  35. namespace mindspore {
  36. namespace device {
  37. KernelRuntime::~KernelRuntime() {
  38. #ifdef ENABLE_DUMP_E2E
  39. dump_conf_ptr_ = nullptr;
  40. #endif
  41. }
  42. bool KernelRuntime::Run(session::KernelGraph *graph) {
  43. bool ret = false;
  44. auto context_ptr = MsContext::GetInstance();
  45. MS_EXCEPTION_IF_NULL(context_ptr);
  46. #if defined(_WIN32) || defined(_WIN64)
  47. auto start_time = std::chrono::steady_clock::now();
  48. #else
  49. struct timeval start_time, end_time;
  50. (void)gettimeofday(&start_time, nullptr);
  51. #endif
  52. bool is_task_sink = context_ptr->enable_task_sink();
  53. if (is_task_sink) {
  54. ret = RunTask(graph);
  55. } else {
  56. ret = LaunchKernel(graph);
  57. }
  58. #if defined(_WIN32) || defined(_WIN64)
  59. auto end_time = std::chrono::steady_clock::now();
  60. std::chrono::duration<double, std::ratio<1, 1000000>> cost = end_time - start_time;
  61. MS_LOG(INFO) << "Call MS Run Success in " << cost.count() << " us";
  62. #else
  63. (void)gettimeofday(&end_time, nullptr);
  64. const uint64_t kUSecondInSecond = 1000000;
  65. uint64_t cost = kUSecondInSecond * static_cast<uint64_t>(end_time.tv_sec - start_time.tv_sec);
  66. cost += static_cast<uint64_t>(end_time.tv_usec - start_time.tv_usec);
  67. MS_LOG(INFO) << "Call MS Run Success in " << cost << " us";
  68. #endif
  69. return ret;
  70. }
  71. // for D to impl
  72. bool KernelRuntime::DumpData(mindspore::session::KernelGraph *graph) {
  73. if (graph != nullptr) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. // for D to impl
  79. bool KernelRuntime::LoadData(mindspore::session::KernelGraph *graph, Debugger *debugger) {
  80. if (graph != nullptr) {
  81. return true;
  82. }
  83. return false;
  84. }
  85. // for D to impl
  86. bool KernelRuntime::GenTask(const session::KernelGraph *graph) {
  87. if (graph != nullptr) {
  88. return true;
  89. }
  90. return false;
  91. }
  92. bool KernelRuntime::LoadTask(const session::KernelGraph *graph) {
  93. if (graph != nullptr) {
  94. return true;
  95. }
  96. return false;
  97. }
  98. // for D to impl
  99. bool KernelRuntime::RunTask(const session::KernelGraph *graph) {
  100. if (graph != nullptr) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. bool KernelRuntime::NodeOutputDeviceAddressExist(const AnfNodePtr &kernel, size_t index) {
  106. MS_EXCEPTION_IF_NULL(kernel);
  107. if (AnfAlgo::OutputAddrExist(kernel, index)) {
  108. return true;
  109. }
  110. return false;
  111. }
  112. size_t KernelRuntime::CountNodeDeviceMemorySize(const mindspore::AnfNodePtr &node, size_t output_index) {
  113. MS_EXCEPTION_IF_NULL(node);
  114. if (output_index >= AnfAlgo::GetOutputTensorNum(node)) {
  115. MS_EXCEPTION(ArgumentError) << "output index [" << output_index << "] large than the output size ["
  116. << AnfAlgo::GetOutputTensorNum(node) << "] of node!";
  117. }
  118. TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(node, output_index);
  119. if (output_type_id == kTypeUnknown) {
  120. output_type_id = AnfAlgo::GetOutputInferDataType(node, output_index);
  121. }
  122. size_t type_size = GetTypeByte(TypeIdToType(output_type_id));
  123. std::vector<size_t> shape = AnfAlgo::GetOutputDeviceShape(node, output_index);
  124. auto format = AnfAlgo::GetOutputFormat(node, output_index);
  125. if (shape.empty() && format != kOpFormat_DEFAULT) {
  126. shape = trans::PaddingShapeTo4d(shape, AnfAlgo::GetOutputReshapeType(node, output_index));
  127. shape = trans::TransShapeToDevice(shape, format);
  128. }
  129. // scalar's output shape is a empty vector
  130. size_t tensor_size = std::accumulate(shape.begin(), shape.end(), type_size, std::multiplies<size_t>());
  131. return tensor_size;
  132. }
  133. void KernelRuntime::AssignMemory(session::KernelGraph *graph) {
  134. auto context_ptr = MsContext::GetInstance();
  135. MS_EXCEPTION_IF_NULL(context_ptr);
  136. MS_EXCEPTION_IF_NULL(mem_manager_);
  137. mem_manager_->ResetDynamicMemory();
  138. AssignStaticMemory(graph);
  139. AssignDynamicMemory(graph);
  140. UpdateRefNodeOutputMem(graph);
  141. }
  142. void KernelRuntime::RunOpAssignMemory(const std::vector<tensor::TensorPtr> &input_tensors,
  143. session::KernelGraph *graph) {
  144. MS_EXCEPTION_IF_NULL(graph);
  145. RunOpAssignInputMemory(input_tensors, graph);
  146. AssignStaticMemoryValueNode(graph);
  147. for (const auto &cnode : graph->execution_order()) {
  148. RunOpAssignOutputMemory(cnode);
  149. RunOpAssignWorkSpaceMemory(cnode);
  150. }
  151. UpdateRefNodeOutputMem(graph);
  152. }
  153. void KernelRuntime::RunOpClearMemory(const session::KernelGraph *graph) {
  154. MS_EXCEPTION_IF_NULL(graph);
  155. // clear input parameter memory resource
  156. for (const auto &input_node : graph->inputs()) {
  157. MS_EXCEPTION_IF_NULL(input_node);
  158. AnfAlgo::SetOutputAddr(nullptr, 0, input_node.get());
  159. }
  160. // clear input value node memory resource
  161. for (const auto &value_node : graph->graph_value_nodes()) {
  162. MS_EXCEPTION_IF_NULL(value_node);
  163. AnfAlgo::SetOutputAddr(nullptr, 0, value_node.get());
  164. }
  165. for (const auto &cnode : graph->execution_order()) {
  166. MS_EXCEPTION_IF_NULL(cnode);
  167. // clear output memory resource
  168. for (size_t index = 0; index < AnfAlgo::GetOutputTensorNum(cnode); ++index) {
  169. AnfAlgo::SetOutputAddr(nullptr, index, cnode.get());
  170. }
  171. // clear workspace memory resource
  172. auto kernel_mod = AnfAlgo::GetKernelMod(cnode);
  173. MS_EXCEPTION_IF_NULL(kernel_mod);
  174. auto workspace_lists = kernel_mod->GetWorkspaceSizeList();
  175. for (size_t index = 0; index < workspace_lists.size(); ++index) {
  176. AnfAlgo::SetWorkspaceAddr(nullptr, index, cnode.get());
  177. }
  178. }
  179. }
  180. void KernelRuntime::AssignStaticMemory(session::KernelGraph *graph) {
  181. AssignStaticMemoryInput(graph);
  182. AssignStaticMemoryValueNode(graph);
  183. AssignStaticMemoryOutput(graph);
  184. }
  185. void KernelRuntime::RunOpAssignInputMemory(const std::vector<tensor::TensorPtr> &input_tensors,
  186. const session::KernelGraph *graph) {
  187. MS_EXCEPTION_IF_NULL(graph);
  188. MS_EXCEPTION_IF_NULL(mem_manager_);
  189. if (input_tensors.size() != graph->inputs().size()) {
  190. MS_LOG(EXCEPTION) << "Input tensors size " << input_tensors.size()
  191. << " should be equal to graph input parameter size " << graph->inputs().size();
  192. }
  193. for (size_t input_index = 0; input_index < graph->inputs().size(); ++input_index) {
  194. auto item = graph->inputs()[input_index];
  195. MS_EXCEPTION_IF_NULL(item);
  196. if (!item->isa<Parameter>()) {
  197. continue;
  198. }
  199. auto output_size = AnfAlgo::GetOutputTensorNum(item);
  200. for (size_t index = 0; index < output_size; index++) {
  201. MS_EXCEPTION_IF_NULL(input_tensors[input_index]);
  202. if (input_tensors[input_index]->device_address().get() != nullptr) {
  203. AnfAlgo::SetOutputAddr(input_tensors[input_index]->device_address(), index, item.get());
  204. continue;
  205. }
  206. TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(item, index);
  207. if (output_type_id == kTypeUnknown) {
  208. output_type_id = AnfAlgo::GetOutputInferDataType(item, index);
  209. }
  210. auto tensor_size = CountNodeDeviceMemorySize(item, index);
  211. auto device_address =
  212. CreateDeviceAddress(nullptr, tensor_size, AnfAlgo::GetOutputFormat(item, index), output_type_id);
  213. MS_EXCEPTION_IF_NULL(device_address);
  214. MS_EXCEPTION_IF_NULL(mem_manager_);
  215. auto ret = mem_manager_->MallocMemFromMemPool(device_address, tensor_size);
  216. if (!ret) {
  217. MS_LOG(EXCEPTION) << "Malloc device memory failed.";
  218. }
  219. AnfAlgo::SetOutputAddr(device_address, index, item.get());
  220. }
  221. }
  222. }
  223. void KernelRuntime::RunOpAssignOutputMemory(const AnfNodePtr &kernel) {
  224. MS_EXCEPTION_IF_NULL(kernel);
  225. MS_EXCEPTION_IF_NULL(mem_manager_);
  226. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  227. MS_EXCEPTION_IF_NULL(kernel_mod);
  228. auto output_sizes = kernel_mod->GetOutputSizeList();
  229. if (output_sizes.empty()) {
  230. return;
  231. }
  232. for (size_t i = 0; i < output_sizes.size(); ++i) {
  233. if (AnfAlgo::OutputAddrExist(kernel, i)) {
  234. continue;
  235. }
  236. if (AnfAlgo::GetCNodeName(kernel) == kApplyMomentumOpName) {
  237. auto device_address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, i);
  238. AnfAlgo::SetOutputAddr(device_address, i, kernel.get());
  239. continue;
  240. }
  241. std::string output_format = AnfAlgo::GetOutputFormat(kernel, i);
  242. auto output_type = AnfAlgo::GetOutputDeviceDataType(kernel, i);
  243. auto device_address = CreateDeviceAddress(nullptr, output_sizes[i], output_format, output_type);
  244. MS_EXCEPTION_IF_NULL(device_address);
  245. auto ret = mem_manager_->MallocMemFromMemPool(device_address, output_sizes[i]);
  246. if (!ret) {
  247. MS_LOG(EXCEPTION) << "Malloc device memory failed.";
  248. }
  249. AnfAlgo::SetOutputAddr(device_address, i, kernel.get());
  250. }
  251. }
  252. void KernelRuntime::RunOpAssignWorkSpaceMemory(const AnfNodePtr &kernel) {
  253. MS_EXCEPTION_IF_NULL(kernel);
  254. MS_EXCEPTION_IF_NULL(mem_manager_);
  255. if (kernel->isa<CNode>()) {
  256. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  257. MS_EXCEPTION_IF_NULL(kernel_mod);
  258. auto workspace_lists = kernel_mod->GetWorkspaceSizeList();
  259. for (size_t i = 0; i < workspace_lists.size(); ++i) {
  260. auto device_address = CreateDeviceAddress(nullptr, workspace_lists[i], "", kTypeUnknown);
  261. MS_EXCEPTION_IF_NULL(device_address);
  262. auto ret = mem_manager_->MallocMemFromMemPool(device_address, workspace_lists[i]);
  263. if (!ret) {
  264. MS_LOG(EXCEPTION) << "Malloc device memory failed.";
  265. }
  266. AnfAlgo::SetWorkspaceAddr(device_address, i, kernel.get());
  267. }
  268. }
  269. }
  270. void KernelRuntime::AssignStaticMemoryInput(const session::KernelGraph *graph) {
  271. MS_EXCEPTION_IF_NULL(graph);
  272. MS_EXCEPTION_IF_NULL(mem_manager_);
  273. auto graph_inputs = graph->inputs();
  274. auto graph_valid_input = graph->valid_inputs();
  275. std::vector<AnfNodePtr> need_alloc_nodes;
  276. for (size_t i = 0; i < graph_inputs.size(); ++i) {
  277. auto item = graph_inputs[i];
  278. MS_EXCEPTION_IF_NULL(item);
  279. if (i < graph_valid_input.size() && !graph_valid_input[i]) {
  280. continue;
  281. }
  282. if (AnfAlgo::CheckPrimitiveType(item, prim::kPrimMakeTuple)) {
  283. auto outs = AnfAlgo::GetAllOutput(item);
  284. for (auto &out : outs) {
  285. MS_EXCEPTION_IF_NULL(out);
  286. if (!out->isa<Parameter>()) {
  287. continue;
  288. }
  289. if (NodeOutputDeviceAddressExist(out, 0)) {
  290. continue;
  291. }
  292. need_alloc_nodes.push_back(out);
  293. }
  294. }
  295. if (!item->isa<Parameter>()) {
  296. continue;
  297. }
  298. if (NodeOutputDeviceAddressExist(item, 0)) {
  299. continue;
  300. }
  301. need_alloc_nodes.push_back(item);
  302. }
  303. for (auto &item : need_alloc_nodes) {
  304. auto output_size = AnfAlgo::GetOutputTensorNum(item);
  305. for (size_t index = 0; index < output_size; index++) {
  306. TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(item, index);
  307. // if graph output is a weight and doesn't link to any cnode, it's data type will be unknown
  308. if (output_type_id == kTypeUnknown) {
  309. MS_LOG(WARNING) << "It is not suggested to use a lonely weight parameter as the output of graph";
  310. output_type_id = AnfAlgo::GetOutputInferDataType(item, index);
  311. }
  312. auto tensor_size = CountNodeDeviceMemorySize(item, index);
  313. auto ptr = mem_manager_->MallocMem(kStaticMem, tensor_size);
  314. auto address = CreateDeviceAddress(ptr, tensor_size, AnfAlgo::GetOutputFormat(item, index), output_type_id);
  315. AnfAlgo::SetOutputAddr(address, index, item.get());
  316. }
  317. }
  318. }
  319. void KernelRuntime::AssignStaticMemoryOutput(const session::KernelGraph *graph) {
  320. MS_EXCEPTION_IF_NULL(graph);
  321. auto nodes = AnfAlgo::GetAllOutput(graph->output(), {prim::kPrimTupleGetItem});
  322. std::vector<session::KernelWithIndex> non_communication_op;
  323. // Assign Communicate Op Memory firstly.
  324. for (const auto &node : nodes) {
  325. auto item_with_index = AnfAlgo::VisitKernelWithReturnType(node, 0, true);
  326. MS_EXCEPTION_IF_NULL(item_with_index.first);
  327. if (!item_with_index.first->isa<CNode>() || !AnfAlgo::IsRealKernel(item_with_index.first)) {
  328. continue;
  329. }
  330. if (AnfAlgo::IsCommunicationOp(item_with_index.first)) {
  331. AssignCommunicationNodeMem(kStaticMem, item_with_index.first);
  332. } else {
  333. non_communication_op.emplace_back(item_with_index);
  334. }
  335. }
  336. for (const auto &item_with_index : non_communication_op) {
  337. AssignNodeOutputMem(kStaticMem, item_with_index.first, SizeToInt(item_with_index.second));
  338. }
  339. }
  340. void KernelRuntime::UpdateRefNodeOutputMem(const session::KernelGraph *graph) {
  341. MS_EXCEPTION_IF_NULL(graph);
  342. auto &kernels = graph->execution_order();
  343. for (auto &kernel : kernels) {
  344. MS_EXCEPTION_IF_NULL(kernel);
  345. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  346. MS_EXCEPTION_IF_NULL(kernel_mod);
  347. auto output_sizes = kernel_mod->GetOutputSizeList();
  348. if (output_sizes.empty()) {
  349. MS_LOG(INFO) << "This kernel has no output size.";
  350. continue;
  351. }
  352. for (size_t i = 0; i < output_sizes.size(); ++i) {
  353. session::AnfWithOutIndex out_pair(kernel, i);
  354. if (graph->IsInRefOutputMap(out_pair)) {
  355. auto origin_pair = graph->GetRefCorrespondOutput(out_pair);
  356. MS_EXCEPTION_IF_NULL(origin_pair.first);
  357. auto origin_node_output_addr = AnfAlgo::GetMutableOutputAddr(origin_pair.first, origin_pair.second);
  358. MS_EXCEPTION_IF_NULL(origin_node_output_addr);
  359. auto cur_node_output_addr = AnfAlgo::GetMutableOutputAddr(kernel, i);
  360. if (origin_node_output_addr.get() != cur_node_output_addr.get()) {
  361. MS_LOG(INFO) << "REF address is not same, ref node output need address update";
  362. MS_LOG(INFO) << "REF origin op is " << origin_pair.first->DebugString() << ", output index is "
  363. << origin_pair.second << ", cur op is " << kernel->DebugString() << ", out index is " << i;
  364. AnfAlgo::SetOutputAddr(origin_node_output_addr, i, kernel.get());
  365. }
  366. }
  367. }
  368. }
  369. }
  370. void KernelRuntime::AssignCommunicationNodeMem(int flag, const AnfNodePtr &node) {
  371. AssignCommunicationNodeInputMem(node);
  372. AssignCommunicationNodeOutputMem(flag, node);
  373. }
  374. void KernelRuntime::AssignCommunicationNodeOutputMem(int flag, const AnfNodePtr &node) {
  375. MS_EXCEPTION_IF_NULL(node);
  376. MS_EXCEPTION_IF_NULL(mem_manager_);
  377. auto kernel_mod = AnfAlgo::GetKernelMod(node);
  378. MS_EXCEPTION_IF_NULL(kernel_mod);
  379. auto output_sizes = kernel_mod->GetOutputSizeList();
  380. if (output_sizes.empty()) {
  381. MS_LOG(INFO) << "This kernel[" << node->DebugString() << "] has no output size.";
  382. return;
  383. }
  384. auto context_ptr = MsContext::GetInstance();
  385. MS_EXCEPTION_IF_NULL(context_ptr);
  386. size_t total_size = 0;
  387. size_t output_index = 0;
  388. std::vector<size_t> align_size_list;
  389. for (uint64_t mem_size : output_sizes) {
  390. if (AnfAlgo::OutputAddrExist(node, output_index++)) {
  391. MS_LOG(INFO) << "communication op addr exist";
  392. continue;
  393. }
  394. if (context_ptr->enable_hccl()) {
  395. mem_size = mem_manager_->GetCommonAlignSize(mem_size);
  396. }
  397. total_size += mem_size;
  398. align_size_list.emplace_back(mem_size);
  399. }
  400. uint8_t *output_ptr = mem_manager_->MallocOutputMem(node, 0, flag, total_size);
  401. for (size_t j = 0; j < align_size_list.size(); ++j) {
  402. std::string output_format = AnfAlgo::GetOutputFormat(node, j);
  403. auto output_type = AnfAlgo::GetOutputDeviceDataType(node, j);
  404. auto address = CreateDeviceAddress(output_ptr, output_sizes[j], output_format, output_type);
  405. AnfAlgo::SetOutputAddr(address, j, node.get());
  406. output_ptr += align_size_list[j];
  407. }
  408. }
  409. DeviceAddressPtr KernelRuntime::PreAssignCNodeMemory(const AnfNodePtr &anf_node, size_t index) {
  410. MS_EXCEPTION_IF_NULL(anf_node);
  411. auto kernel_mod = AnfAlgo::GetKernelMod(anf_node);
  412. auto output_sizes = kernel_mod->GetOutputSizeList();
  413. if (output_sizes.size() <= index) {
  414. MS_LOG(EXCEPTION) << "Previous node output size < node index";
  415. }
  416. std::string output_format = AnfAlgo::GetOutputFormat(anf_node, index);
  417. auto output_type = AnfAlgo::GetOutputDeviceDataType(anf_node, index);
  418. auto address = CreateDeviceAddress(nullptr, output_sizes[index], output_format, output_type);
  419. AnfAlgo::SetOutputAddr(address, index, anf_node.get());
  420. return address;
  421. }
  422. void KernelRuntime::AssignCommunicationNodeInputMem(const AnfNodePtr &node) {
  423. auto context_ptr = MsContext::GetInstance();
  424. MS_EXCEPTION_IF_NULL(context_ptr);
  425. MS_EXCEPTION_IF_NULL(node);
  426. MS_EXCEPTION_IF_NULL(mem_manager_);
  427. size_t total_size = 0;
  428. std::vector<std::pair<mindspore::device::DeviceAddress *, size_t>> addr_size;
  429. for (size_t i = 0; i < AnfAlgo::GetInputTensorNum(node); ++i) {
  430. auto input_node_with_index = AnfAlgo::GetPrevNodeOutput(node, i);
  431. auto input_node = input_node_with_index.first;
  432. DeviceAddressPtr address = nullptr;
  433. if (input_node->isa<CNode>()) {
  434. address = PreAssignCNodeMemory(input_node, input_node_with_index.second);
  435. } else {
  436. MS_LOG(EXCEPTION) << "Communication node inputs only support CNode";
  437. }
  438. MS_EXCEPTION_IF_NULL(address);
  439. auto mem_size = mem_manager_->GetCommonAlignSize(address->size());
  440. total_size += mem_size;
  441. addr_size.emplace_back(address.get(), mem_size);
  442. }
  443. uint8_t *input_ptr = mem_manager_->MallocOutputMem(node, 0, kDynamicMem, total_size);
  444. for (const auto &iter : addr_size) {
  445. MS_EXCEPTION_IF_NULL(iter.first);
  446. iter.first->set_ptr(input_ptr);
  447. input_ptr += iter.second;
  448. }
  449. }
  450. void KernelRuntime::AssignNodeOutputMem(int flag, const AnfNodePtr &node, int index) {
  451. MS_EXCEPTION_IF_NULL(node);
  452. MS_EXCEPTION_IF_NULL(mem_manager_);
  453. if (AnfAlgo::IsGetNext(NOT_NULL(node)) && flag == kReuseDynamicMem) {
  454. MS_LOG(INFO) << "GetNext disable mem_reuse";
  455. flag = kDynamicMem;
  456. }
  457. auto kernel_mod = AnfAlgo::GetKernelMod(node);
  458. MS_EXCEPTION_IF_NULL(kernel_mod);
  459. auto output_sizes = kernel_mod->GetOutputSizeList();
  460. if (output_sizes.empty()) {
  461. MS_LOG(INFO) << "This kernel[" << node->DebugString() << "] has no output size.";
  462. return;
  463. }
  464. for (size_t i = 0; i < output_sizes.size(); ++i) {
  465. if ((kGetAllOuts != index) && (SizeToInt(i) != index)) {
  466. continue;
  467. }
  468. if (NodeOutputDeviceAddressExist(node, i)) {
  469. MS_LOG(INFO) << "Already malloc index:" << i;
  470. continue;
  471. }
  472. auto ptr = mem_manager_->MallocOutputMem(node, i, flag, output_sizes[i]);
  473. if (ptr == nullptr) {
  474. // reused ptr, no need alloc, continue;
  475. continue;
  476. }
  477. std::string output_format = AnfAlgo::GetOutputFormat(node, i);
  478. auto output_type = AnfAlgo::GetOutputDeviceDataType(node, i);
  479. AnfAlgo::SetOutputAddr(CreateDeviceAddress(ptr, output_sizes[i], output_format, output_type), i, node.get());
  480. }
  481. }
  482. void KernelRuntime::AssignValueNodeTensor(const ValueNodePtr &value_node, const ValuePtr &node_value,
  483. size_t output_idx) {
  484. MS_EXCEPTION_IF_NULL(value_node);
  485. MS_EXCEPTION_IF_NULL(node_value);
  486. MS_EXCEPTION_IF_NULL(mem_manager_);
  487. auto ms_context = MsContext::GetInstance();
  488. MS_EXCEPTION_IF_NULL(ms_context);
  489. auto tensor = node_value->cast<TensorPtr>();
  490. if (tensor == nullptr) {
  491. MS_LOG(WARNING) << "Tensor is null";
  492. return;
  493. }
  494. size_t tensor_size = tensor->data().nbytes();
  495. auto node_size = CountNodeDeviceMemorySize(value_node, output_idx);
  496. TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(value_node, output_idx);
  497. if (output_type_id == kTypeUnknown) {
  498. output_type_id = AnfAlgo::GetOutputInferDataType(value_node, output_idx);
  499. }
  500. auto output_format = AnfAlgo::GetOutputFormat(value_node, output_idx);
  501. DeviceAddressPtr address = nullptr;
  502. if (ms_context->enable_pynative_infer()) {
  503. address = CreateDeviceAddress(nullptr, node_size, output_format, output_type_id);
  504. MS_EXCEPTION_IF_NULL(address);
  505. if (!mem_manager_->MallocMemFromMemPool(address, node_size)) {
  506. MS_LOG(EXCEPTION) << "Malloc value node device memory failed !";
  507. }
  508. } else {
  509. auto ptr = mem_manager_->MallocMem(kStaticMem, node_size);
  510. address = CreateDeviceAddress(ptr, node_size, output_format, output_type_id);
  511. MS_EXCEPTION_IF_NULL(address);
  512. }
  513. AnfAlgo::SetOutputAddr(address, output_idx, value_node.get());
  514. if (!address->SyncHostToDevice(trans::GetRuntimePaddingShape(value_node, 0), tensor_size, tensor->data_type(),
  515. tensor->data_c())) {
  516. MS_EXCEPTION(NotExistsError) << "ValueNode SyncHostToDevice fail!" << value_node->DebugString() << "node format is"
  517. << AnfAlgo::GetOutputFormat(value_node, output_idx) << "node dtype is "
  518. << AnfAlgo::GetOutputInferDataType(value_node, output_idx);
  519. }
  520. }
  521. void KernelRuntime::AssignStaticMemoryValueNode(session::KernelGraph *graph) {
  522. MS_EXCEPTION_IF_NULL(graph);
  523. MS_EXCEPTION_IF_NULL(mem_manager_);
  524. auto ms_context = MsContext::GetInstance();
  525. MS_EXCEPTION_IF_NULL(ms_context);
  526. for (auto &value_node : graph->graph_value_nodes()) {
  527. MS_EXCEPTION_IF_NULL(value_node);
  528. if (NodeOutputDeviceAddressExist(value_node, 0)) {
  529. MS_LOG(INFO) << "value_node[" << value_node->DebugString() << "] address already exist";
  530. continue;
  531. }
  532. auto &node_value = value_node->value();
  533. MS_EXCEPTION_IF_NULL(node_value);
  534. if (node_value->isa<Tensor>()) {
  535. AssignValueNodeTensor(value_node, node_value, 0);
  536. } else if (node_value->isa<StringImm>()) {
  537. auto value = GetValue<std::string>(node_value);
  538. size_t tensor_size = value.size();
  539. DeviceAddressPtr address = nullptr;
  540. if (ms_context->enable_pynative_infer()) {
  541. address = CreateDeviceAddress(nullptr, tensor_size, kOpFormat_DEFAULT, kNumberTypeUInt8);
  542. MS_EXCEPTION_IF_NULL(address);
  543. if (!mem_manager_->MallocMemFromMemPool(address, tensor_size)) {
  544. MS_LOG(EXCEPTION) << "Malloc value node device memory failed !";
  545. }
  546. } else {
  547. auto ptr = mem_manager_->MallocMem(kStaticMem, tensor_size);
  548. address = CreateDeviceAddress(ptr, tensor_size, kOpFormat_DEFAULT, kNumberTypeUInt8);
  549. MS_EXCEPTION_IF_NULL(address);
  550. }
  551. AnfAlgo::SetOutputAddr(address, 0, value_node.get());
  552. std::vector<int> shape = {1, SizeToInt(tensor_size)};
  553. if (!address->SyncHostToDevice(shape, tensor_size, kNumberTypeUInt8, value.data())) {
  554. MS_LOG(EXCEPTION) << "kValueNode SyncHostToDevice fail!";
  555. }
  556. }
  557. }
  558. }
  559. void KernelRuntime::AssignDynamicMemory(session::KernelGraph *graph) {
  560. MS_EXCEPTION_IF_NULL(graph);
  561. MS_EXCEPTION_IF_NULL(mem_manager_);
  562. auto context_ptr = MsContext::GetInstance();
  563. MS_EXCEPTION_IF_NULL(context_ptr);
  564. bool is_enable_mem_reuse = context_ptr->enable_mem_reuse();
  565. auto mem_flag = kDynamicMem;
  566. if (is_enable_mem_reuse) {
  567. mem_manager_->MallocReusedDynamicMem(graph);
  568. mem_flag = kReuseDynamicMem;
  569. }
  570. auto &execution_nodes = graph->execution_order();
  571. std::vector<CNodePtr> compute_nodes;
  572. // communication nodes first
  573. for (auto &node : execution_nodes) {
  574. if (AnfAlgo::IsCommunicationOp(node)) {
  575. // skip if the memory is already alocated
  576. AssignCommunicationNodeMem(mem_flag, node);
  577. } else {
  578. compute_nodes.emplace_back(node);
  579. }
  580. }
  581. // then compute nodes
  582. for (auto &node : compute_nodes) {
  583. AssignNodeOutputMem(mem_flag, node, kGetAllOuts);
  584. AssignWorkSpaceMem(mem_flag, node);
  585. }
  586. }
  587. void KernelRuntime::AssignWorkSpaceMem(int flag, const AnfNodePtr &node) {
  588. MS_EXCEPTION_IF_NULL(node);
  589. MS_EXCEPTION_IF_NULL(mem_manager_);
  590. auto kernel_mod = AnfAlgo::GetKernelMod(node);
  591. MS_EXCEPTION_IF_NULL(kernel_mod);
  592. size_t index = 0;
  593. for (auto &size : kernel_mod->GetWorkspaceSizeList()) {
  594. auto ptr = mem_manager_->MallocWorkSpaceMem(node, index, flag, size);
  595. AnfAlgo::SetWorkspaceAddr(CreateDeviceAddress(ptr, size, "", kTypeUnknown), index, node.get());
  596. index++;
  597. }
  598. }
  599. void KernelRuntime::GenLaunchArgs(const session::KernelGraph &graph, const mindspore::AnfNodePtr &kernel,
  600. AddressPtrList *kernel_inputs, AddressPtrList *const kernel_workspaces,
  601. AddressPtrList *kernel_outputs) {
  602. MS_EXCEPTION_IF_NULL(kernel);
  603. MS_EXCEPTION_IF_NULL(kernel_inputs);
  604. MS_EXCEPTION_IF_NULL(kernel_workspaces);
  605. MS_EXCEPTION_IF_NULL(kernel_outputs);
  606. auto cnode = kernel->cast<CNodePtr>();
  607. MS_EXCEPTION_IF_NULL(cnode);
  608. if (AnfAlgo::GetCNodeName(cnode) == kAtomicAddrCleanOpName) {
  609. return GenAddrCleanLaunchArgs(cnode, kernel_inputs);
  610. }
  611. auto is_all_nop_node = opt::IsAllNopNode(&graph);
  612. for (size_t i = 0; i < AnfAlgo::GetInputTensorNum(kernel); ++i) {
  613. auto real_input = AnfAlgo::GetRealInputIndex(kernel, i);
  614. DeviceAddressPtr device_address;
  615. if (is_all_nop_node) {
  616. device_address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, real_input, false);
  617. } else {
  618. device_address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, real_input, true);
  619. }
  620. MS_EXCEPTION_IF_NULL(device_address);
  621. kernel::AddressPtr input = std::make_shared<kernel::Address>();
  622. MS_EXCEPTION_IF_NULL(input);
  623. input->addr = device_address->ptr_;
  624. MS_EXCEPTION_IF_NULL(input->addr);
  625. input->size = device_address->size_;
  626. kernel_inputs->emplace_back(input);
  627. }
  628. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  629. MS_EXCEPTION_IF_NULL(kernel_mod);
  630. for (size_t i = 0; i < kernel_mod->GetOutputSizeList().size(); ++i) {
  631. DeviceAddressPtr device_address;
  632. if (is_all_nop_node) {
  633. device_address = AnfAlgo::GetMutableOutputAddr(kernel, i, false);
  634. } else {
  635. device_address = AnfAlgo::GetMutableOutputAddr(kernel, i, true);
  636. }
  637. MS_EXCEPTION_IF_NULL(device_address);
  638. kernel::AddressPtr output = std::make_shared<kernel::Address>();
  639. MS_EXCEPTION_IF_NULL(output);
  640. output->addr = device_address->ptr_;
  641. MS_EXCEPTION_IF_NULL(output->addr);
  642. output->size = device_address->size_;
  643. kernel_outputs->emplace_back(output);
  644. }
  645. for (size_t i = 0; i < kernel_mod->GetWorkspaceSizeList().size(); ++i) {
  646. auto device_address = AnfAlgo::GetWorkspaceAddr(kernel, i);
  647. kernel::AddressPtr workspace = std::make_shared<kernel::Address>();
  648. MS_EXCEPTION_IF_NULL(workspace);
  649. workspace->addr = device_address->ptr_;
  650. MS_EXCEPTION_IF_NULL(workspace->addr);
  651. workspace->size = device_address->size_;
  652. kernel_workspaces->emplace_back(workspace);
  653. }
  654. }
  655. void KernelRuntime::GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs) {
  656. if (cnode->inputs().size() != 2) {
  657. MS_LOG(EXCEPTION) << "Atomic Addr clean Node Input nodes not equal 2.";
  658. }
  659. MS_EXCEPTION_IF_NULL(cnode->inputs()[1]);
  660. auto pre_node = (cnode->inputs()[1])->cast<CNodePtr>();
  661. // set clean output address
  662. if (AnfAlgo::HasNodeAttr(kAttrAtomicOutputIndexs, pre_node)) {
  663. auto clean_output_indexs = AnfAlgo::GetNodeAttr<std::vector<size_t>>(pre_node, kAttrAtomicOutputIndexs);
  664. for (auto index : clean_output_indexs) {
  665. auto device_address = AnfAlgo::GetOutputAddr(pre_node, index);
  666. kernel::AddressPtr input = std::make_shared<kernel::Address>();
  667. MS_EXCEPTION_IF_NULL(input);
  668. input->addr = device_address->ptr_;
  669. MS_EXCEPTION_IF_NULL(input->addr);
  670. input->size = device_address->size_;
  671. kernel_inputs->emplace_back(input);
  672. }
  673. MS_LOG(INFO) << "AtomicAddClean clean output size:" << clean_output_indexs.size();
  674. }
  675. // set clean workspace address
  676. if (AnfAlgo::HasNodeAttr(kAttrAtomicWorkspaceIndexs, pre_node)) {
  677. auto clean_workspaces_indexs = AnfAlgo::GetNodeAttr<std::vector<size_t>>(pre_node, kAttrAtomicWorkspaceIndexs);
  678. for (const auto &index : clean_workspaces_indexs) {
  679. auto device_address = AnfAlgo::GetWorkspaceAddr(pre_node, index);
  680. kernel::AddressPtr workspace = std::make_shared<kernel::Address>();
  681. MS_EXCEPTION_IF_NULL(workspace);
  682. workspace->addr = device_address->ptr_;
  683. MS_EXCEPTION_IF_NULL(workspace->addr);
  684. workspace->size = device_address->size_;
  685. kernel_inputs->emplace_back(workspace);
  686. }
  687. }
  688. }
  689. bool KernelRuntime::LaunchKernelMod(const session::KernelGraph &graph) {
  690. auto &kernels = graph.execution_order();
  691. for (const auto &kernel : kernels) {
  692. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  693. MS_EXCEPTION_IF_NULL(kernel_mod);
  694. AddressPtrList kernel_inputs;
  695. AddressPtrList kernel_workspaces;
  696. AddressPtrList kernel_outputs;
  697. GenLaunchArgs(graph, kernel, &kernel_inputs, &kernel_workspaces, &kernel_outputs);
  698. auto ret = kernel_mod->Launch(kernel_inputs, kernel_workspaces, kernel_outputs, stream_);
  699. if (!ret) {
  700. MS_LOG(ERROR) << "Launch kernel failed.";
  701. return false;
  702. }
  703. }
  704. return true;
  705. }
  706. bool KernelRuntime::LaunchKernel(const session::KernelGraph *graph) {
  707. MS_EXCEPTION_IF_NULL(graph);
  708. if (!LaunchKernelMod(*graph)) {
  709. MS_LOG(ERROR) << "LaunchKernelMod failed!";
  710. return false;
  711. }
  712. return true;
  713. }
  714. void KernelRuntime::ClearGraphRuntimeResource(uint32_t graph_id) {
  715. MS_LOG(INFO) << "Clear graph:" << graph_id << " runtime resource";
  716. }
  717. #ifdef ENABLE_DUMP_E2E
  718. bool KernelRuntime::SetDumpConf() {
  719. dump_conf_ptr_ = std::make_shared<Dump>();
  720. MS_EXCEPTION_IF_NULL(dump_conf_ptr_);
  721. bool ret = dump_conf_ptr_->SetDumpConfFromJsonFile();
  722. return ret;
  723. }
  724. DumpConfPtr KernelRuntime::GetDumpConf() { return dump_conf_ptr_; }
  725. #endif
  726. } // namespace device
  727. } // namespace mindspore