提交 76750eda 编写于 作者: J Jiaying Zhao 提交者: Yanzhan Yang

free memery when input changes smaller in lod mode (#1728)

上级 0457869a
......@@ -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<framework::LoDTensor>();
target_tensor->reset();
}
}
}
}
template <typename Device, typename T>
void Executor<Device, T>::InitNoPersistableMemory(const Tensor &input_tensor) {
for (const auto &block : program_desc_->Blocks()) {
......@@ -409,6 +423,7 @@ void Executor<Device, T>::SetInput(const Tensor &input,
target.Resize(input.dims());
target.ShareDataWith(input);
input_dim_cur_ = input.dims();
}
template <typename Device, typename T>
......@@ -458,6 +473,16 @@ PMStatus Executor<Device, T>::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<ProfInfo> profile(ops_of_block0_.size());
struct timespec ts;
......
......@@ -97,8 +97,9 @@ class Executor {
std::unordered_map<std::string, int> feed_indices_;
std::unordered_map<std::string, int> fetch_indices_;
// for super resoltion
// for lod_mode_
DDim input_dim_last_;
DDim input_dim_cur_;
#ifdef PADDLE_MOBILE_PROFILE
typedef typename DtypeTensorTrait<Device>::gtype ProfileTensorType;
......
......@@ -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_; }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册