Skip to content

Commit 60e0d1a

Browse files
authored
Merge pull request #10023 from reyoung/feature/DtorOfPE
Correctly implement destructor of ParallelExecutor
2 parents 28013ef + 5305c5f commit 60e0d1a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

paddle/fluid/framework/parallel_executor.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ParallelExecutorPrivate {
4444
#endif
4545

4646
std::vector<std::tuple<std::string, proto::VarType::Type, bool>> var_types_;
47+
bool own_local_scope;
4748
};
4849

4950
std::vector<Scope *> &ParallelExecutor::GetLocalScopes() {
@@ -63,11 +64,13 @@ ParallelExecutor::ParallelExecutor(
6364
// Step 1. Bcast the params to devs.
6465
// Create local scopes
6566
if (local_scopes.empty()) {
67+
member_->own_local_scope = true;
6668
member_->local_scopes_.emplace_back(member_->global_scope_);
6769
for (size_t i = 1; i < member_->places_.size(); ++i) {
6870
member_->local_scopes_.emplace_back(&scope->NewScope());
6971
}
7072
} else {
73+
member_->own_local_scope = false;
7174
PADDLE_ENFORCE_EQ(member_->places_.size(), local_scopes.size());
7275
for (size_t i = 0; i < member_->places_.size(); ++i) {
7376
member_->local_scopes_.emplace_back(local_scopes[i]);
@@ -231,5 +234,13 @@ void ParallelExecutor::FeedAndSplitTensorIntoLocalScopes(
231234
}
232235
}
233236

237+
ParallelExecutor::~ParallelExecutor() {
238+
if (member_->own_local_scope) {
239+
for (size_t i = 1; i < member_->local_scopes_.size(); ++i) {
240+
member_->global_scope_->DeleteScope(member_->local_scopes_[i]);
241+
}
242+
}
243+
}
244+
234245
} // namespace framework
235246
} // namespace paddle

paddle/fluid/framework/parallel_executor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class ParallelExecutor {
4242
const std::vector<Scope*>& local_scopes,
4343
bool allow_op_delay);
4444

45+
~ParallelExecutor();
46+
4547
std::vector<Scope*>& GetLocalScopes();
4648

4749
/**

0 commit comments

Comments
 (0)