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