From af4dac4ac30cbf84bebadf09c823f0432300fa4d Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Thu, 19 Oct 2017 18:57:03 -0700 Subject: [PATCH] Feature/free kid scope (#4951) * Delete kid * Delete local scope --- paddle/framework/executor.cc | 3 +-- paddle/framework/scope.cc | 7 +++++++ paddle/framework/scope.h | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/paddle/framework/executor.cc b/paddle/framework/executor.cc index d50f0da0324..1f1e4edda82 100644 --- a/paddle/framework/executor.cc +++ b/paddle/framework/executor.cc @@ -84,8 +84,7 @@ void Executor::Run(const ProgramDesc& pdesc, Scope* scope, int block_id) { op->Run(local_scope, *device); } - // TODO(tonyyang-svail): - // - Destroy local_scope + scope->DeleteScope(&local_scope); } } // namespace framework diff --git a/paddle/framework/scope.cc b/paddle/framework/scope.cc index 5bf5e91f25a..b8e116c430d 100644 --- a/paddle/framework/scope.cc +++ b/paddle/framework/scope.cc @@ -65,6 +65,13 @@ void Scope::DropKids() { kids_.clear(); } +void Scope::DeleteScope(Scope* scope) { + auto it = std::find(this->kids_.begin(), this->kids_.end(), scope); + PADDLE_ENFORCE(it != this->kids_.end(), "Cannot find %p as kid scope", scope); + this->kids_.erase(it); + delete scope; +} + framework::Scope& GetGlobalScope() { static framework::Scope* g_scope = nullptr; if (g_scope == nullptr) { diff --git a/paddle/framework/scope.h b/paddle/framework/scope.h index a7fce3514b1..78ff136ee17 100644 --- a/paddle/framework/scope.h +++ b/paddle/framework/scope.h @@ -59,6 +59,8 @@ class Scope { /// Find the scope or an ancestor scope that contains the given variable. const Scope* FindScope(const Variable* var) const; + void DeleteScope(Scope* scope); + /// Drop all kids scopes belonged to this scope. void DropKids(); -- GitLab