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 37 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 "runtime/device/kernel_runtime.h"
  17. #include <vector>
  18. #include <utility>
  19. #include <numeric>
  20. #include <functional>
  21. #include "utils/ms_utils.h"
  22. #include "common/trans.h"
  23. #include "utils/utils.h"
  24. #include "utils/ms_context.h"
  25. #include "frontend/operator/ops.h"
  26. #include "backend/session/kernel_graph.h"
  27. #include "backend/session/anf_runtime_algorithm.h"
  28. #include "backend/optimizer/common/helper.h"
  29. #include "debug/data_dump/dump_json_parser.h"
  30. #include "ir/value.h"
  31. #include "utils/shape_utils.h"
  32. using mindspore::kernel::Address;
  33. using mindspore::kernel::AddressPtr;
  34. namespace mindspore {
  35. namespace device {
  36. KernelRuntime::~KernelRuntime() {}
  37. bool KernelRuntime::Load(session::KernelGraph *graph, bool is_task_sink) { return true; }
  38. bool KernelRuntime::LoadData(session::KernelGraph *graph, Debugger *debugger) { return false; }
  39. bool KernelRuntime::NodeOutputDeviceAddressExist(const AnfNodePtr &kernel, size_t index) {
  40. MS_EXCEPTION_IF_NULL(kernel);
  41. if (AnfAlgo::OutputAddrExist(kernel, index)) {
  42. return true;
  43. }
  44. return false;
  45. }
  46. size_t KernelRuntime::CountNodeDeviceMemorySize(const mindspore::AnfNodePtr &node, size_t output_index) {
  47. MS_EXCEPTION_IF_NULL(node);
  48. if (output_index >= AnfAlgo::GetOutputTensorNum(node)) {
  49. MS_EXCEPTION(ArgumentError) << "output index [" << output_index << "] large than the output size ["
  50. << AnfAlgo::GetOutputTensorNum(node) << "] of node!";
  51. }
  52. TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(node, output_index);
  53. if (output_type_id == kTypeUnknown) {
  54. output_type_id = AnfAlgo::GetOutputInferDataType(node, output_index);
  55. }
  56. size_t type_size = GetTypeByte(TypeIdToType(output_type_id));
  57. std::vector<size_t> shape = AnfAlgo::GetOutputDeviceShape(node, output_index);
  58. auto format = AnfAlgo::GetOutputFormat(node, output_index);
  59. if (shape.empty() && format != kOpFormat_DEFAULT) {
  60. shape = trans::PaddingShapeTo4d(shape, AnfAlgo::GetOutputReshapeType(node, output_index));
  61. shape = trans::TransShapeToDevice(shape, format);
  62. }
  63. // scalar's output shape is a empty vector
  64. size_t tensor_size = std::accumulate(shape.begin(), shape.end(), type_size, std::multiplies<size_t>());
  65. return tensor_size;
  66. }
  67. void KernelRuntime::AssignMemory(session::KernelGraph *graph) {
  68. auto context_ptr = MsContext::GetInstance();
  69. MS_EXCEPTION_IF_NULL(context_ptr);
  70. MS_EXCEPTION_IF_NULL(mem_manager_);
  71. mem_manager_->ResetDynamicMemory();
  72. AssignStaticMemory(graph);
  73. AssignDynamicMemory(graph);
  74. UpdateRefNodeOutputMem(graph);
  75. }
  76. void KernelRuntime::RunOpAssignMemory(const ValuePtr &pre_output_value,
  77. const std::vector<tensor::TensorPtr> &input_tensors,
  78. session::KernelGraph *graph) {
  79. MS_EXCEPTION_IF_NULL(graph);
  80. MS_EXCEPTION_IF_NULL(mem_manager_);
  81. mem_manager_->ResetDynamicMemory();
  82. RunOpAssignInputMemory(input_tensors, graph);
  83. AssignStaticMemoryValueNode(graph);
  84. RunOpAssignOutputNodeMemory(pre_output_value, graph);
  85. for (const auto &cnode : graph->execution_order()) {
  86. RunOpAssignOutputMemory(cnode);
  87. RunOpAssignWorkSpaceMemory(cnode);
  88. }
  89. UpdateRefNodeOutputMem(graph);
  90. }
  91. void KernelRuntime::RunOpClearMemory(const session::KernelGraph *graph) {
  92. MS_EXCEPTION_IF_NULL(graph);
  93. // clear input parameter memory resource
  94. for (const auto &input_node : graph->inputs()) {
  95. MS_EXCEPTION_IF_NULL(input_node);
  96. AnfAlgo::SetOutputAddr(nullptr, 0, input_node.get());
  97. }
  98. // clear input value node memory resource
  99. for (const auto &value_node : graph->graph_value_nodes()) {
  100. MS_EXCEPTION_IF_NULL(value_node);
  101. AnfAlgo::SetOutputAddr(nullptr, 0, value_node.get());
  102. }
  103. for (const auto &cnode : graph->execution_order()) {
  104. MS_EXCEPTION_IF_NULL(cnode);
  105. // clear output memory resource
  106. for (size_t index = 0; index < AnfAlgo::GetOutputTensorNum(cnode); ++index) {
  107. AnfAlgo::SetOutputAddr(nullptr, index, cnode.get());
  108. }
  109. // clear workspace memory resource
  110. auto kernel_mod = AnfAlgo::GetKernelMod(cnode);
  111. MS_EXCEPTION_IF_NULL(kernel_mod);
  112. auto workspace_lists = kernel_mod->GetWorkspaceSizeList();
  113. for (size_t index = 0; index < workspace_lists.size(); ++index) {
  114. AnfAlgo::SetWorkspaceAddr(nullptr, index, cnode.get());
  115. }
  116. }
  117. }
  118. bool KernelRuntime::DumpDataEnabled() {
  119. auto &dump_json_parser = DumpJsonParser::GetInstance();
  120. return dump_json_parser.e2e_dump_enabled();
  121. }
  122. bool KernelRuntime::DumpDataEnabledIteration() {
  123. auto &dump_json_parser = DumpJsonParser::GetInstance();
  124. if (!dump_json_parser.e2e_dump_enabled()) {
  125. return false;
  126. }
  127. auto cur_iter = dump_json_parser.cur_dump_iter() + 1;
  128. if (dump_json_parser.iteration() != 0) {
  129. return cur_iter == dump_json_parser.iteration();
  130. }
  131. return true;
  132. }
  133. void KernelRuntime::AssignStaticMemory(session::KernelGraph *graph) {
  134. AssignStaticMemoryInput(graph);
  135. AssignStaticMemoryValueNode(graph);
  136. AssignStaticMemoryOutput(graph);
  137. }
  138. void KernelRuntime::RunOpAssignInputMemory(const std::vector<tensor::TensorPtr> &input_tensors,
  139. const session::KernelGraph *graph) {
  140. MS_EXCEPTION_IF_NULL(graph);
  141. MS_EXCEPTION_IF_NULL(mem_manager_);
  142. if (input_tensors.size() != graph->inputs().size()) {
  143. MS_LOG(EXCEPTION) << "Input tensors size " << input_tensors.size()
  144. << " should be equal to graph input parameter size " << graph->inputs().size();
  145. }
  146. for (size_t input_index = 0; input_index < graph->inputs().size(); ++input_index) {
  147. auto item = graph->inputs()[input_index];
  148. MS_EXCEPTION_IF_NULL(item);
  149. if (!item->isa<Parameter>()) {
  150. continue;
  151. }
  152. auto output_size = AnfAlgo::GetOutputTensorNum(item);
  153. for (size_t index = 0; index < output_size; index++) {
  154. MS_EXCEPTION_IF_NULL(input_tensors[input_index]);
  155. auto output_address =
  156. std::dynamic_pointer_cast<device::DeviceAddress>(input_tensors[input_index]->device_address());
  157. if (output_address != nullptr) {
  158. AnfAlgo::SetOutputAddr(output_address, index, item.get());
  159. continue;
  160. }
  161. TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(item, index);
  162. if (output_type_id == kTypeUnknown) {
  163. output_type_id = AnfAlgo::GetOutputInferDataType(item, index);
  164. }
  165. auto tensor_size = CountNodeDeviceMemorySize(item, index);
  166. auto device_address =
  167. CreateDeviceAddress(nullptr, tensor_size, AnfAlgo::GetOutputFormat(item, index), output_type_id);
  168. MS_EXCEPTION_IF_NULL(device_address);
  169. MS_EXCEPTION_IF_NULL(mem_manager_);
  170. auto ret = mem_manager_->MallocMemFromMemPool(device_address, tensor_size);
  171. if (!ret) {
  172. MS_LOG(EXCEPTION) << "Malloc device memory failed.";
  173. }
  174. AnfAlgo::SetOutputAddr(device_address, index, item.get());
  175. }
  176. }
  177. }
  178. void KernelRuntime::RunOpAssignOutputMemory(const AnfNodePtr &kernel) {
  179. MS_EXCEPTION_IF_NULL(kernel);
  180. MS_EXCEPTION_IF_NULL(mem_manager_);
  181. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  182. MS_EXCEPTION_IF_NULL(kernel_mod);
  183. auto output_sizes = kernel_mod->GetOutputSizeList();
  184. if (output_sizes.empty()) {
  185. return;
  186. }
  187. for (size_t i = 0; i < output_sizes.size(); ++i) {
  188. if (AnfAlgo::OutputAddrExist(kernel, i)) {
  189. continue;
  190. }
  191. if (AnfAlgo::GetCNodeName(kernel) == kApplyMomentumOpName) {
  192. auto device_address = AnfAlgo::GetPrevNodeMutableOutputAddr(kernel, i);
  193. AnfAlgo::SetOutputAddr(device_address, i, kernel.get());
  194. continue;
  195. }
  196. std::string output_format = AnfAlgo::GetOutputFormat(kernel, i);
  197. auto output_type = AnfAlgo::GetOutputDeviceDataType(kernel, i);
  198. auto device_address = CreateDeviceAddress(nullptr, output_sizes[i], output_format, output_type);
  199. device_address->set_host_shape(trans::GetRuntimePaddingShape(kernel, i));
  200. MS_EXCEPTION_IF_NULL(device_address);
  201. auto ret = mem_manager_->MallocMemFromMemPool(device_address, output_sizes[i]);
  202. if (!ret) {
  203. MS_LOG(EXCEPTION) << "Malloc device memory failed.";
  204. }
  205. AnfAlgo::SetOutputAddr(device_address, i, kernel.get());
  206. }
  207. }
  208. void KernelRuntime::RunOpAssignWorkSpaceMemory(const AnfNodePtr &kernel) {
  209. MS_EXCEPTION_IF_NULL(kernel);
  210. MS_EXCEPTION_IF_NULL(mem_manager_);
  211. if (kernel->isa<CNode>()) {
  212. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  213. MS_EXCEPTION_IF_NULL(kernel_mod);
  214. auto workspace_lists = kernel_mod->GetWorkspaceSizeList();
  215. for (size_t i = 0; i < workspace_lists.size(); ++i) {
  216. auto device_address = CreateDeviceAddress(nullptr, workspace_lists[i], "", kTypeUnknown);
  217. MS_EXCEPTION_IF_NULL(device_address);
  218. auto ret = mem_manager_->MallocMemFromMemPool(device_address, workspace_lists[i]);
  219. if (!ret) {
  220. MS_LOG(EXCEPTION) << "Malloc device memory failed.";
  221. }
  222. AnfAlgo::SetWorkspaceAddr(device_address, i, kernel.get());
  223. }
  224. }
  225. }
  226. void KernelRuntime::RunOpAssignOutputNodeMemory(const ValuePtr &pre_output_value, session::KernelGraph *graph) {
  227. if (pre_output_value == nullptr) {
  228. return;
  229. }
  230. std::vector<tensor::TensorPtr> pre_output_tensors;
  231. TensorValueToTensor(pre_output_value, &pre_output_tensors);
  232. MS_EXCEPTION_IF_NULL(graph);
  233. auto output_nodes = graph->outputs();
  234. if (pre_output_tensors.size() != output_nodes.size()) {
  235. MS_LOG(EXCEPTION) << "The size of pre output tensors [" << pre_output_tensors.size()
  236. << "] is not equal to the size of output nodes of graph [" << output_nodes.size() << "]";
  237. }
  238. // share output address with pre output tensors
  239. for (size_t i = 0; i < output_nodes.size(); ++i) {
  240. auto output_node_with_index = AnfAlgo::VisitKernel(output_nodes[i], 0);
  241. if (!output_node_with_index.first->isa<CNode>()) {
  242. if (output_node_with_index.first->isa<Parameter>()) {
  243. auto param = output_node_with_index.first->cast<ParameterPtr>();
  244. if (!param->has_default()) {
  245. MS_LOG(EXCEPTION) << "The output parameter should be real parameter!";
  246. }
  247. }
  248. continue;
  249. }
  250. auto real_output_cnode = output_node_with_index.first->cast<CNodePtr>();
  251. MS_EXCEPTION_IF_NULL(real_output_cnode);
  252. MS_EXCEPTION_IF_NULL(pre_output_tensors[i]);
  253. if (pre_output_tensors[i]->device_address() == nullptr) {
  254. MS_LOG(INFO) << "The address of pre output tensor [" << i << "] is a nullptr!";
  255. continue;
  256. }
  257. if (opt::IsNopNode(real_output_cnode)) {
  258. if (real_output_cnode->inputs().size() < 2) {
  259. MS_LOG(EXCEPTION) << "The input size of output node: " << real_output_cnode->DebugString()
  260. << " should large than one!";
  261. }
  262. AnfAlgo::SetOutputAddr(std::dynamic_pointer_cast<device::DeviceAddress>(pre_output_tensors[i]->device_address()),
  263. output_node_with_index.second, real_output_cnode->input(1).get());
  264. } else {
  265. AnfAlgo::SetOutputAddr(std::dynamic_pointer_cast<device::DeviceAddress>(pre_output_tensors[i]->device_address()),
  266. output_node_with_index.second, output_node_with_index.first.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. MS_LOG(INFO) << "AssignStaticMemoryInput start";
  274. auto graph_inputs = graph->inputs();
  275. auto graph_valid_input = graph->valid_inputs();
  276. graph_inputs.insert(graph_inputs.end(), graph->child_graph_result().begin(), graph->child_graph_result().end());
  277. std::vector<AnfNodePtr> need_alloc_nodes;
  278. for (size_t i = 0; i < graph_inputs.size(); ++i) {
  279. auto item = graph_inputs[i];
  280. MS_EXCEPTION_IF_NULL(item);
  281. if (i < graph_valid_input.size() && !graph_valid_input[i]) {
  282. continue;
  283. }
  284. if (AnfAlgo::CheckPrimitiveType(item, prim::kPrimMakeTuple)) {
  285. auto outs = AnfAlgo::GetAllOutput(item);
  286. for (auto &out : outs) {
  287. MS_EXCEPTION_IF_NULL(out);
  288. if (!out->isa<Parameter>()) {
  289. continue;
  290. }
  291. if (NodeOutputDeviceAddressExist(out, 0)) {
  292. continue;
  293. }
  294. need_alloc_nodes.push_back(out);
  295. }
  296. }
  297. if (!item->isa<Parameter>()) {
  298. continue;
  299. }
  300. if (NodeOutputDeviceAddressExist(item, 0)) {
  301. continue;
  302. }
  303. need_alloc_nodes.push_back(item);
  304. }
  305. for (auto &item : need_alloc_nodes) {
  306. auto output_size = AnfAlgo::GetOutputTensorNum(item);
  307. for (size_t index = 0; index < output_size; index++) {
  308. TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(item, index);
  309. // if graph output is a weight and doesn't link to any cnode, it's data type will be unknown
  310. if (output_type_id == kTypeUnknown) {
  311. MS_LOG(WARNING) << "It is not suggested to use a lonely weight parameter as the output of graph";
  312. continue;
  313. }
  314. auto tensor_size = CountNodeDeviceMemorySize(item, index);
  315. auto address = CreateDeviceAddress(nullptr, tensor_size, AnfAlgo::GetOutputFormat(item, index), output_type_id);
  316. MS_LOG(DEBUG) << "Malloc static memory for " << item->fullname_with_scope();
  317. if (mem_manager_->MallocMem(kStaticMem, tensor_size, address) == nullptr) {
  318. MS_LOG(EXCEPTION) << "Cannot alloc address when flag is: " << kStaticMem << ", tensor size is: " << tensor_size;
  319. }
  320. MS_LOG(INFO) << "Malloc Input for graph " << graph->graph_id() << ", node: " << item->fullname_with_scope()
  321. << " index: " << index << " size: " << tensor_size;
  322. AnfAlgo::SetOutputAddr(address, index, item.get());
  323. }
  324. }
  325. MS_LOG(INFO) << "AssignStaticMemoryInput end";
  326. }
  327. void KernelRuntime::AssignStaticMemoryOutput(const session::KernelGraph *graph) {
  328. MS_EXCEPTION_IF_NULL(graph);
  329. MS_LOG(INFO) << "AssignStaticMemoryOutput start";
  330. auto nodes = AnfAlgo::GetAllOutput(graph->output(), {prim::kPrimTupleGetItem});
  331. std::vector<session::KernelWithIndex> non_communication_op;
  332. // Assign Communicate Op Memory firstly.
  333. for (const auto &node : nodes) {
  334. auto item_with_index = AnfAlgo::VisitKernelWithReturnType(node, 0, true);
  335. MS_EXCEPTION_IF_NULL(item_with_index.first);
  336. if (!item_with_index.first->isa<CNode>() || !AnfAlgo::IsRealKernel(item_with_index.first)) {
  337. continue;
  338. }
  339. if (AnfAlgo::IsCommunicationOp(item_with_index.first)) {
  340. AssignCommunicationNodeMem(kStaticMem, item_with_index.first);
  341. } else {
  342. non_communication_op.emplace_back(item_with_index);
  343. }
  344. }
  345. for (const auto &item_with_index : non_communication_op) {
  346. MS_LOG(DEBUG) << "AssignNodeOutputMem for " << item_with_index.first->fullname_with_scope();
  347. AssignNodeOutputMem(kStaticMem, item_with_index.first, SizeToInt(item_with_index.second));
  348. }
  349. MS_LOG(INFO) << "AssignStaticMemoryOutput end";
  350. }
  351. void KernelRuntime::UpdateRefNodeOutputMem(const session::KernelGraph *graph) {
  352. MS_EXCEPTION_IF_NULL(graph);
  353. auto &kernels = graph->execution_order();
  354. for (auto &kernel : kernels) {
  355. MS_EXCEPTION_IF_NULL(kernel);
  356. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  357. MS_EXCEPTION_IF_NULL(kernel_mod);
  358. auto output_sizes = kernel_mod->GetOutputSizeList();
  359. if (output_sizes.empty()) {
  360. MS_LOG(INFO) << "This kernel has no output size.";
  361. continue;
  362. }
  363. for (size_t i = 0; i < output_sizes.size(); ++i) {
  364. session::AnfWithOutIndex out_pair(kernel, i);
  365. if (graph->IsInRefOutputMap(out_pair)) {
  366. auto origin_pair = graph->GetRefCorrespondOutput(out_pair);
  367. MS_EXCEPTION_IF_NULL(origin_pair.first);
  368. auto origin_node_output_addr = AnfAlgo::GetMutableOutputAddr(origin_pair.first, origin_pair.second);
  369. MS_EXCEPTION_IF_NULL(origin_node_output_addr);
  370. auto cur_node_output_addr = AnfAlgo::GetMutableOutputAddr(kernel, i);
  371. if (origin_node_output_addr.get() != cur_node_output_addr.get()) {
  372. MS_LOG(INFO) << "REF address is not same, ref node output need address update";
  373. MS_LOG(INFO) << "REF origin op is " << origin_pair.first->DebugString() << ", output index is "
  374. << origin_pair.second << ", cur op is " << kernel->DebugString() << ", out index is " << i;
  375. AnfAlgo::SetOutputAddr(origin_node_output_addr, i, kernel.get());
  376. }
  377. }
  378. }
  379. }
  380. }
  381. void KernelRuntime::AssignCommunicationNodeMem(MemType type, const AnfNodePtr &node) {
  382. AssignCommunicationNodeInputMem(type, node);
  383. AssignCommunicationNodeOutputMem(type, node);
  384. }
  385. void KernelRuntime::AssignCommunicationNodeOutputMem(MemType type, const AnfNodePtr &node) {
  386. MS_EXCEPTION_IF_NULL(node);
  387. MS_EXCEPTION_IF_NULL(mem_manager_);
  388. auto kernel_mod = AnfAlgo::GetKernelMod(node);
  389. MS_EXCEPTION_IF_NULL(kernel_mod);
  390. auto output_sizes = kernel_mod->GetOutputSizeList();
  391. if (output_sizes.empty()) {
  392. MS_LOG(INFO) << "This kernel[" << node->DebugString() << "] has no output size.";
  393. return;
  394. }
  395. auto context_ptr = MsContext::GetInstance();
  396. MS_EXCEPTION_IF_NULL(context_ptr);
  397. size_t total_size = 0;
  398. size_t output_index = 0;
  399. std::vector<size_t> align_size_list;
  400. for (uint64_t mem_size : output_sizes) {
  401. if (AnfAlgo::OutputAddrExist(node, output_index++)) {
  402. MS_LOG(INFO) << "communication op addr exist";
  403. continue;
  404. }
  405. if (context_ptr->get_param<bool>(MS_CTX_ENABLE_HCCL)) {
  406. mem_size = mem_manager_->GetCommonAlignSize(mem_size);
  407. }
  408. total_size += mem_size;
  409. align_size_list.emplace_back(mem_size);
  410. }
  411. if (type == kReuseDynamicMem) {
  412. // reuse communication op's all outputs' memory
  413. type = kReuseDynamicCommMem;
  414. bool not_reuse = KernelMemNotReuse(node);
  415. if (not_reuse) {
  416. type = kDynamicMem;
  417. MS_LOG(INFO) << "Disable Memory Reuse for " << node->fullname_with_scope() << "'s output.";
  418. }
  419. }
  420. uint8_t *output_ptr = nullptr;
  421. for (size_t j = 0; j < align_size_list.size(); ++j) {
  422. std::string output_format = AnfAlgo::GetOutputFormat(node, j);
  423. auto output_type = AnfAlgo::GetOutputDeviceDataType(node, j);
  424. auto address = CreateDeviceAddress(nullptr, output_sizes[j], output_format, output_type);
  425. MS_EXCEPTION_IF_NULL(address);
  426. if (output_ptr == nullptr) {
  427. output_ptr = mem_manager_->MallocOutputMem(node, 0, type, total_size, address);
  428. MS_EXCEPTION_IF_NULL(output_ptr);
  429. } else {
  430. address->set_ptr(output_ptr);
  431. }
  432. AnfAlgo::SetOutputAddr(address, j, node.get());
  433. output_ptr += align_size_list[j];
  434. }
  435. }
  436. bool KernelRuntime::KernelMemNotReuse(const AnfNodePtr &node) { return false; }
  437. DeviceAddressPtr KernelRuntime::PreAssignCNodeMemory(const AnfNodePtr &anf_node, size_t index) {
  438. MS_EXCEPTION_IF_NULL(anf_node);
  439. auto kernel_mod = AnfAlgo::GetKernelMod(anf_node);
  440. auto output_sizes = kernel_mod->GetOutputSizeList();
  441. if (output_sizes.size() <= index) {
  442. MS_LOG(EXCEPTION) << "Previous node output size < node index";
  443. }
  444. std::string output_format = AnfAlgo::GetOutputFormat(anf_node, index);
  445. auto output_type = AnfAlgo::GetOutputDeviceDataType(anf_node, index);
  446. auto address = CreateDeviceAddress(nullptr, output_sizes[index], output_format, output_type);
  447. AnfAlgo::SetOutputAddr(address, index, anf_node.get());
  448. return address;
  449. }
  450. void KernelRuntime::AssignCommunicationNodeInputMem(MemType type, const AnfNodePtr &node) {
  451. auto context_ptr = MsContext::GetInstance();
  452. MS_EXCEPTION_IF_NULL(context_ptr);
  453. MS_EXCEPTION_IF_NULL(node);
  454. MS_EXCEPTION_IF_NULL(mem_manager_);
  455. size_t total_size = 0;
  456. std::vector<std::pair<DeviceAddressPtr, size_t>> addr_size;
  457. for (size_t i = 0; i < AnfAlgo::GetInputTensorNum(node); ++i) {
  458. auto input_node_with_index = AnfAlgo::GetPrevNodeOutput(node, i);
  459. auto input_node = input_node_with_index.first;
  460. DeviceAddressPtr address = nullptr;
  461. if (input_node->isa<CNode>()) {
  462. address = PreAssignCNodeMemory(input_node, input_node_with_index.second);
  463. } else {
  464. MS_LOG(EXCEPTION) << "Communication node inputs only support CNode";
  465. }
  466. MS_EXCEPTION_IF_NULL(address);
  467. auto mem_size = mem_manager_->GetCommonAlignSize(address->size());
  468. total_size += mem_size;
  469. addr_size.emplace_back(address, mem_size);
  470. }
  471. if (addr_size.empty()) {
  472. return;
  473. }
  474. if (type == kReuseDynamicMem) {
  475. bool not_reuse = KernelMemNotReuse(node);
  476. if (not_reuse) {
  477. type = kDynamicMem;
  478. MS_LOG(INFO) << "Disable Memory Reuse for " << node->fullname_with_scope() << "'s input.";
  479. }
  480. }
  481. uint8_t *input_ptr = mem_manager_->MallocOutputMem(node, 0, type, total_size, addr_size[0].first);
  482. for (const auto &iter : addr_size) {
  483. MS_EXCEPTION_IF_NULL(iter.first);
  484. iter.first->set_ptr(input_ptr);
  485. input_ptr += iter.second;
  486. }
  487. }
  488. void KernelRuntime::AssignNodeOutputMem(MemType type, const AnfNodePtr &node, int index) {
  489. MS_EXCEPTION_IF_NULL(node);
  490. MS_EXCEPTION_IF_NULL(mem_manager_);
  491. if (AnfAlgo::IsGetNext(NOT_NULL(node)) && type == kReuseDynamicMem) {
  492. MS_LOG(INFO) << "GetNext disable mem_reuse";
  493. type = kDynamicMem;
  494. }
  495. if (node->isa<CNode>()) {
  496. bool independent = AnfAlgo::IsIndependentNode(node->cast<CNodePtr>());
  497. if (independent && type == kReuseDynamicMem) {
  498. MS_LOG(INFO) << "Independent disable mem_reuse";
  499. type = kDynamicMem;
  500. }
  501. }
  502. if (type == kReuseDynamicMem) {
  503. bool not_reuse = KernelMemNotReuse(node);
  504. if (not_reuse) {
  505. type = kDynamicMem;
  506. MS_LOG(INFO) << "Disable Memory Reuse for " << node->fullname_with_scope() << "'s output.";
  507. }
  508. }
  509. auto kernel_mod = AnfAlgo::GetKernelMod(node);
  510. MS_EXCEPTION_IF_NULL(kernel_mod);
  511. auto output_sizes = kernel_mod->GetOutputSizeList();
  512. if (output_sizes.empty()) {
  513. MS_LOG(INFO) << "This kernel[" << node->DebugString() << "] has no output size.";
  514. return;
  515. }
  516. for (size_t i = 0; i < output_sizes.size(); ++i) {
  517. if ((kGetAllOuts != index) && (SizeToInt(i) != index)) {
  518. continue;
  519. }
  520. if (NodeOutputDeviceAddressExist(node, i)) {
  521. MS_LOG(INFO) << "Already malloc index:" << i;
  522. continue;
  523. }
  524. MS_LOG(DEBUG) << "Assign Node:" << node->fullname_with_scope() << " output memeory size:" << output_sizes[i];
  525. std::string output_format = AnfAlgo::GetOutputFormat(node, i);
  526. auto output_type = AnfAlgo::GetOutputDeviceDataType(node, i);
  527. auto device_address = CreateDeviceAddress(nullptr, output_sizes[i], output_format, output_type);
  528. MS_EXCEPTION_IF_NULL(device_address);
  529. uint8_t *ptr = mem_manager_->MallocOutputMem(node, i, type, output_sizes[i], device_address);
  530. MS_EXCEPTION_IF_NULL(ptr);
  531. device_address->set_host_shape(trans::GetRuntimePaddingShape(node, i));
  532. AnfAlgo::SetOutputAddr(device_address, i, node.get());
  533. }
  534. }
  535. void KernelRuntime::AssignValueNodeTensor(const ValueNodePtr &value_node, const ValuePtr &node_value,
  536. size_t output_idx) {
  537. MS_EXCEPTION_IF_NULL(value_node);
  538. MS_EXCEPTION_IF_NULL(node_value);
  539. MS_EXCEPTION_IF_NULL(mem_manager_);
  540. auto ms_context = MsContext::GetInstance();
  541. MS_EXCEPTION_IF_NULL(ms_context);
  542. std::vector<tensor::TensorPtr> tensors;
  543. TensorValueToTensor(node_value, &tensors);
  544. for (const auto &tensor : tensors) {
  545. if (tensor == nullptr) {
  546. MS_LOG(WARNING) << "Tensor is null";
  547. return;
  548. }
  549. if (tensor->device_address() != nullptr) {
  550. AnfAlgo::SetOutputAddr(std::dynamic_pointer_cast<device::DeviceAddress>(tensor->device_address()), output_idx++,
  551. value_node.get());
  552. continue;
  553. }
  554. size_t tensor_size = tensor->data().nbytes();
  555. auto node_size = CountNodeDeviceMemorySize(value_node, output_idx);
  556. TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(value_node, output_idx);
  557. if (output_type_id == kTypeUnknown) {
  558. output_type_id = AnfAlgo::GetOutputInferDataType(value_node, output_idx);
  559. }
  560. auto output_format = AnfAlgo::GetOutputFormat(value_node, output_idx);
  561. DeviceAddressPtr address = nullptr;
  562. address = CreateDeviceAddress(nullptr, node_size, output_format, output_type_id);
  563. MS_EXCEPTION_IF_NULL(address);
  564. if (ms_context->get_param<bool>(MS_CTX_ENABLE_PYNATIVE_INFER) &&
  565. !mem_manager_->MallocMemFromMemPool(address, node_size)) {
  566. MS_LOG(EXCEPTION) << "Cannot alloc address from memory pool when tensor size is: " << node_size;
  567. } else if (mem_manager_->MallocMem(kStaticMem, node_size, address) == nullptr) {
  568. MS_LOG(EXCEPTION) << "Cannot alloc address when flag is: " << kStaticMem << ", tensor size is: " << node_size;
  569. }
  570. AnfAlgo::SetOutputAddr(address, output_idx, value_node.get());
  571. if (!address->SyncHostToDevice(trans::GetRuntimePaddingShape(value_node, 0), tensor_size, tensor->data_type(),
  572. tensor->data_c())) {
  573. MS_EXCEPTION(NotExistsError) << "ValueNode SyncHostToDevice fail!" << value_node->DebugString()
  574. << "node format is" << AnfAlgo::GetOutputFormat(value_node, output_idx)
  575. << "node dtype is " << AnfAlgo::GetOutputInferDataType(value_node, output_idx);
  576. }
  577. }
  578. }
  579. void KernelRuntime::AssignStaticMemoryValueNode(session::KernelGraph *graph) {
  580. MS_EXCEPTION_IF_NULL(graph);
  581. MS_EXCEPTION_IF_NULL(mem_manager_);
  582. MS_LOG(INFO) << "AssignStaticMemoryValueNode start";
  583. auto ms_context = MsContext::GetInstance();
  584. MS_EXCEPTION_IF_NULL(ms_context);
  585. for (auto &value_node : graph->graph_value_nodes()) {
  586. MS_EXCEPTION_IF_NULL(value_node);
  587. if (NodeOutputDeviceAddressExist(value_node, 0)) {
  588. MS_LOG(INFO) << "value_node[" << value_node->DebugString() << "] address already exist";
  589. continue;
  590. }
  591. auto &node_value = value_node->value();
  592. MS_EXCEPTION_IF_NULL(node_value);
  593. MS_LOG(DEBUG) << "Malloc memeory for " << value_node->fullname_with_scope();
  594. if (node_value->isa<Tensor>() || node_value->isa<ValueTuple>()) {
  595. AssignValueNodeTensor(value_node, node_value, 0);
  596. } else if (node_value->isa<StringImm>()) {
  597. auto value = GetValue<std::string>(node_value);
  598. size_t tensor_size = value.size();
  599. DeviceAddressPtr address = nullptr;
  600. address = CreateDeviceAddress(nullptr, tensor_size, kOpFormat_DEFAULT, kNumberTypeUInt8);
  601. MS_EXCEPTION_IF_NULL(address);
  602. if (ms_context->get_param<bool>(MS_CTX_ENABLE_PYNATIVE_INFER) &&
  603. !mem_manager_->MallocMemFromMemPool(address, tensor_size)) {
  604. MS_LOG(EXCEPTION) << "Cannot alloc address from memory pool when tensor size is: " << tensor_size;
  605. } else if (mem_manager_->MallocMem(kStaticMem, tensor_size, address) == nullptr) {
  606. MS_LOG(EXCEPTION) << "Cannot alloc address when flag is: " << kStaticMem << ", tensor size is: " << tensor_size;
  607. }
  608. AnfAlgo::SetOutputAddr(address, 0, value_node.get());
  609. ShapeVector shape = {1, SizeToInt(tensor_size)};
  610. if (!address->SyncHostToDevice(shape, tensor_size, kNumberTypeUInt8, value.data())) {
  611. MS_LOG(EXCEPTION) << "kValueNode SyncHostToDevice fail!";
  612. }
  613. }
  614. }
  615. MS_LOG(INFO) << "AssignStaticMemoryValueNode end";
  616. }
  617. void KernelRuntime::AssignDynamicMemory(session::KernelGraph *graph) {
  618. MS_EXCEPTION_IF_NULL(graph);
  619. MS_EXCEPTION_IF_NULL(mem_manager_);
  620. auto context_ptr = MsContext::GetInstance();
  621. MS_EXCEPTION_IF_NULL(context_ptr);
  622. bool is_enable_mem_reuse = context_ptr->get_param<bool>(MS_CTX_ENABLE_MEM_REUSE);
  623. auto mem_type = kDynamicMem;
  624. auto &dump_json_parser = DumpJsonParser::GetInstance();
  625. if (dump_json_parser.e2e_dump_enabled() && dump_json_parser.dump_mode() == 0) {
  626. context_ptr->set_param<bool>(MS_CTX_ENABLE_MEM_REUSE, false);
  627. is_enable_mem_reuse = false;
  628. MS_LOG(INFO) << "Disable Memory Reuse when e2e dump is enable and dump mode is set to dump all kernels";
  629. }
  630. if (is_enable_mem_reuse) {
  631. MS_LOG(INFO) << "Memory Reuse is enable...";
  632. mem_manager_->MallocReusedDynamicMem(graph);
  633. mem_type = kReuseDynamicMem;
  634. } else {
  635. MS_LOG(INFO) << "Memory Reuse is disable...";
  636. }
  637. auto &execution_nodes = graph->execution_order();
  638. std::vector<CNodePtr> compute_nodes;
  639. // communication nodes first
  640. for (auto &node : execution_nodes) {
  641. if (AnfAlgo::IsCommunicationOp(node)) {
  642. // skip if the memory is already alocated
  643. AssignCommunicationNodeMem(mem_type, node);
  644. } else {
  645. compute_nodes.emplace_back(node);
  646. }
  647. }
  648. // then compute nodes
  649. for (auto &node : compute_nodes) {
  650. AssignNodeOutputMem(mem_type, node, kGetAllOuts);
  651. AssignWorkSpaceMem(mem_type, node);
  652. }
  653. }
  654. void KernelRuntime::AssignWorkSpaceMem(MemType type, const AnfNodePtr &node) {
  655. MS_EXCEPTION_IF_NULL(node);
  656. MS_EXCEPTION_IF_NULL(mem_manager_);
  657. auto kernel_mod = AnfAlgo::GetKernelMod(node);
  658. MS_EXCEPTION_IF_NULL(kernel_mod);
  659. size_t index = 0;
  660. for (auto &size : kernel_mod->GetWorkspaceSizeList()) {
  661. auto ptr = mem_manager_->MallocWorkSpaceMem(node, index, type, size);
  662. AnfAlgo::SetWorkspaceAddr(CreateDeviceAddress(ptr, size, "", kTypeUnknown), index, node.get());
  663. index++;
  664. }
  665. }
  666. void KernelRuntime::GenLaunchArgs(const mindspore::kernel::KernelMod &kernel_mod, const mindspore::AnfNodePtr &kernel,
  667. AddressPtrList *kernel_inputs, AddressPtrList *const kernel_workspaces,
  668. AddressPtrList *kernel_outputs) {
  669. MS_EXCEPTION_IF_NULL(kernel);
  670. MS_EXCEPTION_IF_NULL(kernel_inputs);
  671. MS_EXCEPTION_IF_NULL(kernel_workspaces);
  672. MS_EXCEPTION_IF_NULL(kernel_outputs);
  673. auto cnode = kernel->cast<CNodePtr>();
  674. MS_EXCEPTION_IF_NULL(cnode);
  675. if (AnfAlgo::GetCNodeName(cnode) == kAtomicAddrCleanOpName) {
  676. return GenAddrCleanLaunchArgs(cnode, kernel_inputs);
  677. }
  678. for (size_t i = 0; i < AnfAlgo::GetInputTensorNum(kernel); ++i) {
  679. auto real_input = AnfAlgo::GetRealInputIndex(kernel, i);
  680. auto device_address = AnfAlgo::GetPrevNodeOutputAddr(kernel, real_input);
  681. MS_EXCEPTION_IF_NULL(device_address);
  682. kernel::AddressPtr input = std::make_shared<kernel::Address>();
  683. MS_EXCEPTION_IF_NULL(input);
  684. input->addr = device_address->ptr_;
  685. MS_EXCEPTION_IF_NULL(input->addr);
  686. input->size = device_address->size_;
  687. kernel_inputs->emplace_back(input);
  688. }
  689. for (size_t i = 0; i < kernel_mod.GetOutputSizeList().size(); ++i) {
  690. auto device_address = AnfAlgo::GetOutputAddr(kernel, i);
  691. kernel::AddressPtr output = std::make_shared<kernel::Address>();
  692. MS_EXCEPTION_IF_NULL(output);
  693. output->addr = device_address->ptr_;
  694. MS_EXCEPTION_IF_NULL(output->addr);
  695. output->size = device_address->size_;
  696. kernel_outputs->emplace_back(output);
  697. }
  698. for (size_t i = 0; i < kernel_mod.GetWorkspaceSizeList().size(); ++i) {
  699. auto device_address = AnfAlgo::GetWorkspaceAddr(kernel, i);
  700. kernel::AddressPtr workspace = std::make_shared<kernel::Address>();
  701. MS_EXCEPTION_IF_NULL(workspace);
  702. workspace->addr = device_address->ptr_;
  703. MS_EXCEPTION_IF_NULL(workspace->addr);
  704. workspace->size = device_address->size_;
  705. kernel_workspaces->emplace_back(workspace);
  706. }
  707. }
  708. void KernelRuntime::GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs) {
  709. if (cnode->inputs().size() != 2) {
  710. MS_LOG(EXCEPTION) << "Atomic Addr clean Node Input nodes not equal 2.";
  711. }
  712. MS_EXCEPTION_IF_NULL(cnode->inputs()[1]);
  713. auto pre_node = (cnode->inputs()[1])->cast<CNodePtr>();
  714. // set clean output address
  715. if (AnfAlgo::HasNodeAttr(kAttrAtomicOutputIndexs, pre_node)) {
  716. auto clean_output_indexs = AnfAlgo::GetNodeAttr<std::vector<size_t>>(pre_node, kAttrAtomicOutputIndexs);
  717. for (auto index : clean_output_indexs) {
  718. auto device_address = AnfAlgo::GetOutputAddr(pre_node, index);
  719. kernel::AddressPtr input = std::make_shared<kernel::Address>();
  720. MS_EXCEPTION_IF_NULL(input);
  721. input->addr = device_address->ptr_;
  722. MS_EXCEPTION_IF_NULL(input->addr);
  723. input->size = device_address->size_;
  724. kernel_inputs->emplace_back(input);
  725. }
  726. MS_LOG(INFO) << "AtomicAddClean clean output size:" << clean_output_indexs.size();
  727. }
  728. // set clean workspace address
  729. if (AnfAlgo::HasNodeAttr(kAttrAtomicWorkspaceIndexs, pre_node)) {
  730. auto clean_workspaces_indexs = AnfAlgo::GetNodeAttr<std::vector<size_t>>(pre_node, kAttrAtomicWorkspaceIndexs);
  731. for (const auto &index : clean_workspaces_indexs) {
  732. auto device_address = AnfAlgo::GetWorkspaceAddr(pre_node, index);
  733. kernel::AddressPtr workspace = std::make_shared<kernel::Address>();
  734. MS_EXCEPTION_IF_NULL(workspace);
  735. workspace->addr = device_address->ptr_;
  736. MS_EXCEPTION_IF_NULL(workspace->addr);
  737. workspace->size = device_address->size_;
  738. kernel_inputs->emplace_back(workspace);
  739. }
  740. }
  741. }
  742. bool KernelRuntime::LaunchKernelMod(const session::KernelGraph &graph) {
  743. auto &kernels = graph.execution_order();
  744. for (const auto &kernel : kernels) {
  745. auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
  746. MS_EXCEPTION_IF_NULL(kernel_mod);
  747. AddressPtrList kernel_inputs;
  748. AddressPtrList kernel_workspaces;
  749. AddressPtrList kernel_outputs;
  750. GenLaunchArgs(*kernel_mod, kernel, &kernel_inputs, &kernel_workspaces, &kernel_outputs);
  751. auto ret = kernel_mod->Launch(kernel_inputs, kernel_workspaces, kernel_outputs, stream_);
  752. if (!ret) {
  753. MS_LOG(ERROR) << "Launch kernel failed.";
  754. return false;
  755. }
  756. }
  757. return true;
  758. }
  759. bool KernelRuntime::LaunchKernel(const session::KernelGraph *graph) {
  760. MS_EXCEPTION_IF_NULL(graph);
  761. if (!LaunchKernelMod(*graph)) {
  762. MS_LOG(ERROR) << "LaunchKernelMod failed!";
  763. return false;
  764. }
  765. return true;
  766. }
  767. void KernelRuntime::ClearGraphRuntimeResource(uint32_t graph_id, const std::vector<AnfNodePtr> &,
  768. const std::unordered_set<ValueNodePtr> &, const std::vector<CNodePtr> &) {
  769. MS_LOG(INFO) << "Clear graph:" << graph_id << " runtime resource";
  770. }
  771. void KernelRuntime::ClearOutputAddress(const std::vector<AnfNodePtr> &inputs,
  772. const std::unordered_set<ValueNodePtr> &value_nodes,
  773. const std::vector<CNodePtr> &execution_order) {
  774. // clear input parameter output address.
  775. for (const auto &input_node : inputs) {
  776. MS_EXCEPTION_IF_NULL(input_node);
  777. if (!input_node->isa<Parameter>()) {
  778. continue;
  779. }
  780. auto parameter = input_node->cast<ParameterPtr>();
  781. MS_EXCEPTION_IF_NULL(parameter);
  782. parameter->DecreaseUsedGraphCount();
  783. // Only the parameter has no graph used, then clear the output address.
  784. if (parameter->used_graph_count() != 0) {
  785. continue;
  786. }
  787. for (size_t index = 0; index < AnfAlgo::GetOutputTensorNum(input_node); ++index) {
  788. if (!AnfAlgo::OutputAddrExist(input_node, index)) {
  789. continue;
  790. }
  791. AnfAlgo::SetOutputAddr(nullptr, index, input_node.get());
  792. }
  793. }
  794. // clear input value node output address.
  795. for (const auto &value_node : value_nodes) {
  796. if (!AnfAlgo::OutputAddrExist(value_node, 0)) {
  797. continue;
  798. }
  799. AnfAlgo::SetOutputAddr(nullptr, 0, value_node.get());
  800. }
  801. // clear cnode output address.
  802. for (const auto &cnode : execution_order) {
  803. for (size_t index = 0; index < AnfAlgo::GetOutputTensorNum(cnode); ++index) {
  804. if (!AnfAlgo::OutputAddrExist(cnode, index)) {
  805. continue;
  806. }
  807. AnfAlgo::SetOutputAddr(nullptr, index, cnode.get());
  808. }
  809. }
  810. }
  811. bool KernelRuntime::LaunchTaskBasedOnSingleKernel(kernel::KernelModPtr kernel_mod_ptr,
  812. const AddressPtrList &kernel_inputs,
  813. const AddressPtrList &kernel_outputs,
  814. const AddressPtrList &kernel_workspaces) const {
  815. MS_EXCEPTION_IF_NULL(kernel_mod_ptr);
  816. auto ret = kernel_mod_ptr->Launch(kernel_inputs, kernel_workspaces, kernel_outputs, stream_);
  817. if (!ret) {
  818. MS_LOG(ERROR) << "Launch kernel failed.";
  819. return false;
  820. }
  821. return true;
  822. }
  823. DeviceAddressPtr KernelRuntime::AssignSingleOpLaunchMemory(size_t size, const std::string &format, TypeId type) {
  824. auto device_address = CreateDeviceAddress(nullptr, size, format, type);
  825. MS_EXCEPTION_IF_NULL(device_address);
  826. MS_EXCEPTION_IF_NULL(mem_manager_);
  827. auto base_ptr = mem_manager_->MallocMem(kStaticMem, size, device_address);
  828. MS_EXCEPTION_IF_NULL(base_ptr);
  829. return device_address;
  830. }
  831. } // namespace device
  832. } // namespace mindspore