From 660d2d1e97e94eea140da722e6433417adf0ab7a Mon Sep 17 00:00:00 2001 From: chenzupeng Date: Fri, 25 Dec 2020 16:24:39 +0800 Subject: [PATCH] fix memory leak bug --- build.sh | 2 +- mindspore/lite/src/lite_session.cc | 2 +- mindspore/lite/src/lite_session.h | 2 +- mindspore/lite/src/runtime/opencl/opencl_runtime.cc | 12 ++++++++---- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/build.sh b/build.sh index d49f760f9e9..93646221249 100755 --- a/build.sh +++ b/build.sh @@ -503,7 +503,7 @@ build_lite() LITE_ENABLE_GPU=${ENABLE_GPU} LITE_ENABLE_NPU=${ENABLE_NPU} - if [ "${DEVICE}" == "" ] && [ "${LITE_PLATFORM}" == "arm64" ]; then + if [[ "${DEVICE}" == "" && "${LITE_PLATFORM}" == "arm64" ]]; then LITE_ENABLE_GPU="on" LITE_ENABLE_NPU="on" fi diff --git a/mindspore/lite/src/lite_session.cc b/mindspore/lite/src/lite_session.cc index 4c2386cded8..7ddc0296e13 100644 --- a/mindspore/lite/src/lite_session.cc +++ b/mindspore/lite/src/lite_session.cc @@ -670,7 +670,7 @@ int LiteSession::Resize(const std::vector &inputs } int LiteSession::InitGPURuntime() { -#if SUPPORT_GPU +#if SUPPORT_GPU && !SUPPORT_TRAIN if (this->context_->IsGpuEnabled()) { auto gpu_device_info = this->context_->GetGpuInfo(); auto opencl_runtime = ocl_runtime_wrap_.GetInstance(); diff --git a/mindspore/lite/src/lite_session.h b/mindspore/lite/src/lite_session.h index 6819abf175e..91e47bf5eb8 100644 --- a/mindspore/lite/src/lite_session.h +++ b/mindspore/lite/src/lite_session.h @@ -124,7 +124,7 @@ class LiteSession : public session::LiteSession { std::unordered_map output_tensor_map_; Executor *executor_ = nullptr; std::atomic is_running_ = false; -#if SUPPORT_GPU +#if SUPPORT_GPU && !SUPPORT_TRAIN opencl::OpenCLRuntimeWrapper ocl_runtime_wrap_; #endif }; diff --git a/mindspore/lite/src/runtime/opencl/opencl_runtime.cc b/mindspore/lite/src/runtime/opencl/opencl_runtime.cc index 3e324180c6c..1e44fc0cc0e 100644 --- a/mindspore/lite/src/runtime/opencl/opencl_runtime.cc +++ b/mindspore/lite/src/runtime/opencl/opencl_runtime.cc @@ -234,8 +234,7 @@ int OpenCLRuntime::Init() { MS_LOG(INFO) << "Compute Unit: " << compute_units_; MS_LOG(INFO) << "Clock Frequency: " << max_freq_ << " MHz"; - const cl_command_queue_properties properties = 0; - default_command_queue_ = new (std::nothrow) cl::CommandQueue(*context_, *device_, properties, &ret); + default_command_queue_ = new (std::nothrow) cl::CommandQueue(*context_, *device_, 0, &ret); if (ret != CL_SUCCESS) { delete device_; delete context_; @@ -243,8 +242,7 @@ int OpenCLRuntime::Init() { return RET_ERROR; } - const cl_command_queue_properties profiling_properties = CL_QUEUE_PROFILING_ENABLE; - profiling_command_queue_ = new (std::nothrow) cl::CommandQueue(*context_, *device_, profiling_properties, &ret); + profiling_command_queue_ = new (std::nothrow) cl::CommandQueue(*context_, *device_, CL_QUEUE_PROFILING_ENABLE, &ret); if (ret != CL_SUCCESS) { delete device_; delete context_; @@ -258,6 +256,7 @@ int OpenCLRuntime::Init() { delete device_; delete context_; delete default_command_queue_; + delete profiling_command_queue_; MS_LOG(ERROR) << "Command OpenCL allocator failed!"; return RET_ERROR; } @@ -275,6 +274,9 @@ int OpenCLRuntime::Init() { } int OpenCLRuntime::Uninit() { + if (!init_done_) { + return RET_OK; + } if (enable_cache_ && !binary_map_.empty()) { StoreCache(); } @@ -282,10 +284,12 @@ int OpenCLRuntime::Uninit() { program_map_.clear(); delete allocator_; delete default_command_queue_; + delete profiling_command_queue_; delete context_; delete device_; allocator_ = nullptr; default_command_queue_ = nullptr; + profiling_command_queue_ = nullptr; context_ = nullptr; device_ = nullptr; #ifdef USE_OPENCL_WRAPPER