diff --git a/src/framework/executor.cpp b/src/framework/executor.cpp index c06abc841631a59711e7c8cf524626be2ed2be78..aedc9307b07d80e1565b1ac31b8271424afe2c13 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 c2d096182d1a94317c4909a7a468f04148b79695..0fb2acc67f70952d69baa4b63f16a38c76d37a74 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 027f1165a08509431fd1281f7b05174a7c64b7cc..937cedb0cbb2c7d1e005d57357df1fd098a2ff71 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_; }