Skip to content

Commit daa0fbd

Browse files
committed
add keep_kids flag for executor
1 parent dd55cc1 commit daa0fbd

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

paddle/fluid/framework/executor.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ std::vector<std::shared_ptr<ExecutorPrepareContext>> Executor::Prepare(
320320
}
321321

322322
void Executor::RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope,
323-
bool create_local_scope, bool create_vars) {
323+
bool create_local_scope, bool create_vars,
324+
bool keep_kids) {
324325
Scope* local_scope = scope;
325326
if (create_vars) {
326327
if (create_local_scope) {
@@ -343,12 +344,20 @@ void Executor::RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope,
343344
}
344345
}
345346
platform::DeviceContextPool::Instance().Get(place_)->Wait();
346-
if (create_vars && create_local_scope) {
347+
if (local_scope != scope) {
347348
scope->DeleteScope(local_scope);
348349
} else {
349-
// Delete the local scopes created in operators.
350-
scope->DropKids();
350+
if (!keep_kids) {
351+
// By default, we should delete all kid scopes after run executor because
352+
// some operators may create local scope when running, such as while_op.
353+
// But when while_op also create a local executor to run it's sub block,
354+
// the sub scopes it created should not be dropped immediately, because
355+
// while_grad_op will use some variables during while_op run, so we need
356+
// to keep the kids and wait for the outer executor to drop them.
357+
scope->DropKids();
358+
}
351359
}
360+
352361
if (FLAGS_benchmark) {
353362
VLOG(2) << "-------------------------------------------------------";
354363
VLOG(2) << "Memory used after deleting local scope: "

paddle/fluid/framework/executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Executor {
7878

7979
void RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope,
8080
bool create_local_scope = true,
81-
bool create_vars = true);
81+
bool create_vars = true, bool keep_kids = false);
8282

8383
void RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope,
8484
std::map<std::string, const LoDTensor*>* feed_targets,

0 commit comments

Comments
 (0)