未验证 提交 93ce7f69 编写于 作者: L Leo Chen 提交者: GitHub

[cherry-pick] Async drop scope in executor (#29714) #30285

[cherry-pick] Async drop scope in executor (#29714)
上级 a2bbd06a
...@@ -478,21 +478,35 @@ void Executor::RunPartialPreparedContext(ExecutorPrepareContext* ctx, ...@@ -478,21 +478,35 @@ void Executor::RunPartialPreparedContext(ExecutorPrepareContext* ctx,
} }
} }
platform::DeviceContextPool::Instance().Get(place_)->Wait(); auto callback = [scope, local_scope, keep_kids]() {
if (local_scope != scope) {
VLOG(4) << "Delete scope: " << local_scope;
scope->DeleteScope(local_scope);
} else {
if (!keep_kids) {
VLOG(4) << "Drop kids: " << scope;
// By default, we should delete all kid scopes after run executor
// because
// some operators may create local scope when running, such as while_op.
// But when while_op also create a local executor to run it's sub block,
// the sub scopes it created should not be dropped immediately, because
// while_grad_op will use some variables created during while_op run, so
// we need to keep the kids and wait for the outer executor to drop
// them.
scope->DropKids();
}
VLOG(4) << "Keep kids: " << scope;
}
};
if (local_scope != scope) { if (gc) {
scope->DeleteScope(local_scope); VLOG(4) << "Async deleting scope";
gc->DirectClearCallback(callback);
} else { } else {
if (!keep_kids) { VLOG(4) << "Sync deleting scope";
// By default, we should delete all kid scopes after run executor because platform::DeviceContextPool::Instance().Get(place_)->Wait();
// some operators may create local scope when running, such as while_op. callback();
// But when while_op also create a local executor to run it's sub block,
// the sub scopes it created should not be dropped immediately, because
// while_grad_op will use some variables created during while_op run, so
// we need to keep the kids and wait for the outer executor to drop them.
scope->DropKids();
}
} }
} }
......
...@@ -48,6 +48,10 @@ class GarbageCollector { ...@@ -48,6 +48,10 @@ class GarbageCollector {
template <typename Container, typename Callback> template <typename Container, typename Callback>
void Add(Container &&objs, Callback &&callback); void Add(Container &&objs, Callback &&callback);
void DirectClearCallback(const std::function<void()> &callback) {
ClearCallback(callback);
}
protected: protected:
virtual void ClearCallback(const std::function<void()> &callback) = 0; virtual void ClearCallback(const std::function<void()> &callback) = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册