Skip to content

Commit 00ca945

Browse files
authored
Merge pull request #13657 from panyx0718/fix
Current scope needs to be thread-safe for training
2 parents 1cbaf71 + d24f1f0 commit 00ca945

File tree

1 file changed

+0
-31
lines changed

1 file changed

+0
-31
lines changed

paddle/fluid/framework/scope.cc

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ limitations under the License. */
2020
#include "paddle/fluid/framework/threadpool.h"
2121
#include "paddle/fluid/string/printf.h"
2222

23-
// The mutex is not needed by training and inference, only for distribution.
24-
#if PADDLE_WITH_DISTRIBUTE
25-
#define WITH_LOCK 1
26-
#else
27-
#define WITH_LOCK 0
28-
#endif
29-
3023
DEFINE_bool(benchmark, false,
3124
"Doing memory benchmark. It will make deleting scope synchronized, "
3225
"and add some memory usage logs."
@@ -56,24 +49,18 @@ int64_t GetEagerDeletionThreshold() {
5649
Scope::~Scope() { DropKids(); }
5750

5851
Scope& Scope::NewScope() const {
59-
#if WITH_LOCK
6052
std::unique_lock<std::mutex> lock(mutex_);
61-
#endif
6253
kids_.push_back(new Scope(this));
6354
return *kids_.back();
6455
}
6556

6657
Variable* Scope::Var(const std::string& name) {
67-
#if WITH_LOCK
6858
std::unique_lock<std::mutex> lock(mutex_);
69-
#endif
7059
return VarInternal(name);
7160
}
7261

7362
Variable* Scope::Var(std::string* name) {
74-
#if WITH_LOCK
7563
std::unique_lock<std::mutex> lock(mutex_);
76-
#endif
7764
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
7865
if (name != nullptr) {
7966
*name = new_name;
@@ -82,39 +69,29 @@ Variable* Scope::Var(std::string* name) {
8269
}
8370

8471
Variable* Scope::FindVar(const std::string& name) const {
85-
#if WITH_LOCK
8672
std::unique_lock<std::mutex> lock(mutex_);
87-
#endif
8873
return FindVarInternal(name);
8974
}
9075

9176
const Scope* Scope::FindScope(const Variable* var) const {
92-
#if WITH_LOCK
9377
std::unique_lock<std::mutex> lock(mutex_);
94-
#endif
9578
return FindScopeInternal(var);
9679
}
9780

9881
void Scope::DropKids() {
99-
#if WITH_LOCK
10082
std::unique_lock<std::mutex> lock(mutex_);
101-
#endif
10283
for (Scope* s : kids_) delete s;
10384
kids_.clear();
10485
}
10586

10687
bool Scope::HasKid(const Scope* scope) const {
107-
#if WITH_LOCK
10888
std::unique_lock<std::mutex> lock(mutex_);
109-
#endif
11089
auto it = std::find(this->kids_.begin(), this->kids_.end(), scope);
11190
return it != this->kids_.end();
11291
}
11392

11493
std::vector<std::string> Scope::LocalVarNames() const {
115-
#if WITH_LOCK
11694
std::unique_lock<std::mutex> lock(mutex_);
117-
#endif
11895
std::vector<std::string> known_vars;
11996
known_vars.reserve(this->vars_.size());
12097
for (auto& p : vars_) {
@@ -124,9 +101,7 @@ std::vector<std::string> Scope::LocalVarNames() const {
124101
}
125102

126103
void Scope::DeleteScope(Scope* scope) const {
127-
#if WITH_LOCK
128104
std::unique_lock<std::mutex> lock(mutex_);
129-
#endif
130105
auto it = std::find(this->kids_.begin(), this->kids_.end(), scope);
131106
PADDLE_ENFORCE(it != this->kids_.end(), "Cannot find %p as kid scope", scope);
132107
this->kids_.erase(it);
@@ -139,9 +114,7 @@ void Scope::DeleteScope(Scope* scope) const {
139114
}
140115

141116
void Scope::EraseVars(const std::vector<std::string>& var_names) {
142-
#if WITH_LOCK
143117
std::unique_lock<std::mutex> lock(mutex_);
144-
#endif
145118
std::set<std::string> var_set(var_names.begin(), var_names.end());
146119
for (auto it = vars_.begin(); it != vars_.end();) {
147120
if (var_set.find(it->first) != var_set.end()) {
@@ -154,16 +127,12 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
154127

155128
void Scope::Rename(const std::string& origin_name,
156129
const std::string& new_name) const {
157-
#if WITH_LOCK
158130
std::unique_lock<std::mutex> lock(mutex_);
159-
#endif
160131
RenameInternal(origin_name, new_name);
161132
}
162133

163134
std::string Scope::Rename(const std::string& origin_name) const {
164-
#if WITH_LOCK
165135
std::unique_lock<std::mutex> lock(mutex_);
166-
#endif
167136
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
168137
RenameInternal(origin_name, new_name);
169138
return new_name;

0 commit comments

Comments
 (0)