From 76750eda77589663539a3ebb16baa4ec1480ddee Mon Sep 17 00:00:00 2001 From: Jiaying Zhao Date: Fri, 5 Jul 2019 17:50:14 +0800 Subject: [PATCH] free memery when input changes smaller in lod mode (#1728) --- src/framework/executor.cpp | 25 +++++++++++++++++++++++++ src/framework/executor.h | 3 ++- src/framework/tensor_base.h | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/framework/executor.cpp b/src/framework/executor.cpp index c06abc8416..aedc9307b0 100644 --- a/src/framework/executor.cpp +++ b/src/framework/executor.cpp @@ -294,6 +294,20 @@ static void ClearNoPersistableTensorArray(const framework::ProgramDesc *program, } } +static void ClearNoPersistableTensor(const framework::ProgramDesc *program, + framework::Scope *scope) { + for (const auto &block : program->Blocks()) { + for (const auto &var_desc : block->Vars()) { + if (!var_desc->Persistable() && + var_desc->Type() == VARTYPE_TYPE_LOD_TENSOR) { + auto var = scope->Var(var_desc->Name()); + auto target_tensor = var->template GetMutable(); + target_tensor->reset(); + } + } + } +} + template void Executor::InitNoPersistableMemory(const Tensor &input_tensor) { for (const auto &block : program_desc_->Blocks()) { @@ -409,6 +423,7 @@ void Executor::SetInput(const Tensor &input, target.Resize(input.dims()); target.ShareDataWith(input); + input_dim_cur_ = input.dims(); } template @@ -458,6 +473,16 @@ PMStatus Executor::Predict() { // is always push back a new tensor in the array ClearNoPersistableTensorArray(program_desc_.get(), program_.scope.get()); + // in lod_mode_, free no persistable memery when input changes smaller. + if (lod_mode_) { + if (product(input_dim_cur_) <= 0.7 * product(input_dim_last_)) { + ClearNoPersistableTensor(program_desc_.get(), program_.scope.get()); + input_dim_last_ = input_dim_cur_; + } else if (product(input_dim_cur_) > product(input_dim_last_)) { + input_dim_last_ = input_dim_cur_; + } + } + #ifdef PADDLE_MOBILE_PROFILE std::vector profile(ops_of_block0_.size()); struct timespec ts; diff --git a/src/framework/executor.h b/src/framework/executor.h index c2d096182d..0fb2acc67f 100644 --- a/src/framework/executor.h +++ b/src/framework/executor.h @@ -97,8 +97,9 @@ class Executor { std::unordered_map feed_indices_; std::unordered_map fetch_indices_; - // for super resoltion + // for lod_mode_ DDim input_dim_last_; + DDim input_dim_cur_; #ifdef PADDLE_MOBILE_PROFILE typedef typename DtypeTensorTrait::gtype ProfileTensorType; diff --git a/src/framework/tensor_base.h b/src/framework/tensor_base.h index 027f1165a0..937cedb0cb 100644 --- a/src/framework/tensor_base.h +++ b/src/framework/tensor_base.h @@ -70,6 +70,8 @@ class TensorBase { inline bool IsInitialized() const { return holder_ != nullptr; } + inline bool reset() const { holder_ == nullptr; } + /*! Return the dimensions of the memory block. */ inline const DDim &dims() const { return dims_; } -- GitLab