From e4f1b1c5e1fcde4683e2e79076df277412edf224 Mon Sep 17 00:00:00 2001 From: Tao Luo Date: Fri, 10 Apr 2020 10:59:36 +0800 Subject: [PATCH] solve mklml memory leak (#23557) --- .../fluid/inference/api/analysis_predictor.cc | 19 +++++++++++++++++++ paddle/fluid/platform/dynload/mklml.h | 1 + 2 files changed, 20 insertions(+) diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index f5cf7e52040..94b5400a470 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -37,6 +37,9 @@ #include "paddle/fluid/inference/utils/singleton.h" #include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/platform/cpu_helper.h" +#ifdef PADDLE_WITH_MKLML +#include "paddle/fluid/platform/dynload/mklml.h" +#endif #include "paddle/fluid/platform/gpu_info.h" #include "paddle/fluid/platform/place.h" #include "paddle/fluid/platform/profiler.h" @@ -308,6 +311,14 @@ bool AnalysisPredictor::Run(const std::vector &inputs, paddle::platform::SetNumThreads(1); #ifdef PADDLE_WITH_MKLDNN if (config_.use_mkldnn_) MkldnnPostReset(); +#endif +#if defined(PADDLE_WITH_MKLML) && defined(_LINUX) + // Frees unused memory allocated by the IntelĀ® MKL Memory Allocator to + // avoid memory leak. See: + // https://software.intel.com/en-us/mkl-developer-reference-c-mkl-free-buffers + platform::dynload::MKL_Free_Buffers(); +// We don't support windows since MKL_Free_Buffers is not in +// mklml_win_2019.0.1.20181227.zip. We will upgrade mklml_win version later. #endif return true; } @@ -648,6 +659,14 @@ bool AnalysisPredictor::ZeroCopyRun() { // recover the cpu_math_library_num_threads to 1, in order to avoid thread // conflict when integrating it into deployment service. paddle::platform::SetNumThreads(1); +#if defined(PADDLE_WITH_MKLML) && defined(_LINUX) + // Frees unused memory allocated by the IntelĀ® MKL Memory Allocator to + // avoid memory leak. See: + // https://software.intel.com/en-us/mkl-developer-reference-c-mkl-free-buffers + platform::dynload::MKL_Free_Buffers(); +// We don't support windows since MKL_Free_Buffers is not in +// mklml_win_2019.0.1.20181227.zip. We will upgrade mklml_win version later. +#endif return true; } diff --git a/paddle/fluid/platform/dynload/mklml.h b/paddle/fluid/platform/dynload/mklml.h index 839dcd87f57..e5e818f5fba 100644 --- a/paddle/fluid/platform/dynload/mklml.h +++ b/paddle/fluid/platform/dynload/mklml.h @@ -89,6 +89,7 @@ extern void* mklml_dso_handle; __macro(vdInv); \ __macro(vmsErf); \ __macro(vmdErf); \ + __macro(MKL_Free_Buffers); \ __macro(MKL_Set_Num_Threads) MKLML_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_MKLML_WRAP); -- GitLab