From 4c7291078cb3af01a42ff183e17ac85dd92bf511 Mon Sep 17 00:00:00 2001 From: liangzelang Date: Fri, 4 Sep 2020 15:30:46 +0800 Subject: [PATCH] clean idle mem in the beginning of ascend session --- mindspore/ccsrc/backend/session/ascend_session.cc | 1 + mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc | 2 ++ mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.h | 1 + mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.cc | 2 ++ mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.h | 1 + mindspore/ccsrc/runtime/device/kernel_runtime.h | 1 + mindspore/ccsrc/runtime/device/memory_manager.h | 1 + 7 files changed, 9 insertions(+) diff --git a/mindspore/ccsrc/backend/session/ascend_session.cc b/mindspore/ccsrc/backend/session/ascend_session.cc index 4d4ad4ad064..9a50907f306 100644 --- a/mindspore/ccsrc/backend/session/ascend_session.cc +++ b/mindspore/ccsrc/backend/session/ascend_session.cc @@ -1032,6 +1032,7 @@ void AscendSession::AssignStaticMemory(NotNull graph, // assign static memory for parameters auto runtime_instance = device::KernelRuntimeManager::Instance().GetKernelRuntime(kAscendDevice, device_id_); MS_EXCEPTION_IF_NULL(runtime_instance); + runtime_instance->ClearGlobalIdleMem(); runtime_instance->AssignStaticMemoryInput(graph.get().get()); runtime_instance->AssignStaticMemoryValueNode(graph.get().get()); for (auto &child_graph : graph->child_graph_order()) { diff --git a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc index a58a1c08628..dd43b7154e7 100644 --- a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc +++ b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc @@ -155,6 +155,8 @@ void AscendKernelRuntime::ClearGraphRuntimeResource(uint32_t graph_id, const std } } +void AscendKernelRuntime::ClearGlobalIdleMem() { mem_manager_->ClearGlobalIdleMem(); } + bool AscendKernelRuntime::NeedDestroyHccl() { auto context_ptr = MsContext::GetInstance(); MS_EXCEPTION_IF_NULL(context_ptr); diff --git a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.h b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.h index 6b7ccc085cd..01001b8ce76 100644 --- a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.h +++ b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.h @@ -47,6 +47,7 @@ class AscendKernelRuntime : public KernelRuntime { void ClearGraphRuntimeResource(uint32_t graph_id, const std::vector &inputs, const std::unordered_set &value_nodes, const std::vector &execution_order) override; + void ClearGlobalIdleMem() override; bool SyncStream() override; protected: diff --git a/mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.cc b/mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.cc index 93e38278f14..bf2f0316f86 100644 --- a/mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.cc +++ b/mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.cc @@ -77,6 +77,8 @@ void AscendMemoryManager::ResetDynamicMemory() { AscendMemoryPool::GetInstance().set_graph_dynamic_mem_offset(dynamic_mem_offset_); } +void AscendMemoryManager::ClearGlobalIdleMem() { AscendMemoryPool::GetInstance().ResetIdleMemBuf(); } + void *AscendMemoryManager::MallocMemFromMemPool(size_t size) { auto align_size = GetCommonAlignSize(size); return AscendMemoryPool::GetInstance().AllocTensorMem(align_size); diff --git a/mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.h b/mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.h index fc684f3fd85..77812b489cb 100644 --- a/mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.h +++ b/mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.h @@ -28,6 +28,7 @@ class AscendMemoryManager : public MemoryManager { void MallocDeviceMemory() override; void FreeDeviceMemory() override; void ResetDynamicMemory() override; + void ClearGlobalIdleMem() override; void *MallocMemFromMemPool(size_t size) override; protected: diff --git a/mindspore/ccsrc/runtime/device/kernel_runtime.h b/mindspore/ccsrc/runtime/device/kernel_runtime.h index b81954a557e..a03ccaa16c3 100644 --- a/mindspore/ccsrc/runtime/device/kernel_runtime.h +++ b/mindspore/ccsrc/runtime/device/kernel_runtime.h @@ -76,6 +76,7 @@ class KernelRuntime { const std::unordered_set &value_nodes, const std::vector &execution_order); virtual bool SyncStream() = 0; + virtual void ClearGlobalIdleMem() {} #ifdef ENABLE_DUMP_E2E DumpConfPtr GetDumpConf(); diff --git a/mindspore/ccsrc/runtime/device/memory_manager.h b/mindspore/ccsrc/runtime/device/memory_manager.h index cb045f8d274..ad2142d1a22 100644 --- a/mindspore/ccsrc/runtime/device/memory_manager.h +++ b/mindspore/ccsrc/runtime/device/memory_manager.h @@ -39,6 +39,7 @@ class MemoryManager { total_dynamic_size_ = 0; dynamic_mem_offset_ = 0; } + virtual void ClearGlobalIdleMem() {} void MallocReusedDynamicMem(const session::KernelGraph *graph); uint8_t *MallocOutputMem(const AnfNodePtr &node, size_t index, MemType type, size_t size,