Skip to content

Commit 05a9277

Browse files
authored
Merge pull request #11524 from jacquesqiao/add-keep_kids-for-executor
add keep_kids flag for executor
2 parents 8ecf5dd + 2b1ecdf commit 05a9277

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
@@ -321,7 +321,8 @@ std::vector<std::shared_ptr<ExecutorPrepareContext>> Executor::Prepare(
321321
}
322322

323323
void Executor::RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope,
324-
bool create_local_scope, bool create_vars) {
324+
bool create_local_scope, bool create_vars,
325+
bool keep_kids) {
325326
Scope* local_scope = scope;
326327
if (create_vars) {
327328
if (create_local_scope) {
@@ -344,12 +345,20 @@ void Executor::RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope,
344345
}
345346
}
346347
platform::DeviceContextPool::Instance().Get(place_)->Wait();
347-
if (create_vars && create_local_scope) {
348+
if (local_scope != scope) {
348349
scope->DeleteScope(local_scope);
349350
} else {
350-
// Delete the local scopes created in operators.
351-
scope->DropKids();
351+
if (!keep_kids) {
352+
// By default, we should delete all kid scopes after run executor because
353+
// some operators may create local scope when running, such as while_op.
354+
// But when while_op also create a local executor to run it's sub block,
355+
// the sub scopes it created should not be dropped immediately, because
356+
// while_grad_op will use some variables created during while_op run, so
357+
// we need to keep the kids and wait for the outer executor to drop them.
358+
scope->DropKids();
359+
}
352360
}
361+
353362
if (FLAGS_benchmark) {
354363
VLOG(2) << "-------------------------------------------------------";
355364
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)