Skip to content

Commit f4a49cb

Browse files
committed
Merge remote-tracking branch 'origin/develop' into doc/api1
2 parents 4970414 + 0ddc5d8 commit f4a49cb

File tree

3 files changed

+62
-37
lines changed

3 files changed

+62
-37
lines changed

paddle/fluid/framework/scope.cc

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,55 +43,37 @@ Scope& Scope::NewScope() const {
4343
}
4444

4545
Variable* Scope::Var(const std::string& name) {
46-
// acquire the lock when new var under this scope
4746
std::unique_lock<std::mutex> lock(mutex_);
48-
auto* v = FindVarLocally(name);
49-
if (v != nullptr) return v;
50-
51-
v = new Variable();
52-
vars_[name].reset(v);
53-
VLOG(3) << "Create variable " << name;
54-
v->name_ = &(vars_.find(name)->first);
55-
return v;
47+
return VarInternal(name);
5648
}
5749

5850
Variable* Scope::Var(std::string* name) {
59-
auto var_name = string::Sprintf("%p.%d", this, vars_.size());
51+
std::unique_lock<std::mutex> lock(mutex_);
52+
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
6053
if (name != nullptr) {
61-
*name = var_name;
54+
*name = new_name;
6255
}
63-
return Var(var_name);
56+
return VarInternal(new_name);
6457
}
6558

6659
Variable* Scope::FindVar(const std::string& name) const {
67-
// acquire the lock when find var
6860
std::unique_lock<std::mutex> lock(mutex_);
6961
return FindVarInternal(name);
7062
}
7163

72-
Variable* Scope::FindVarInternal(const std::string& name) const {
73-
auto var = FindVarLocally(name);
74-
if (var != nullptr) {
75-
return var;
76-
}
77-
return (parent_ == nullptr) ? nullptr : parent_->FindVarInternal(name);
78-
}
79-
8064
const Scope* Scope::FindScope(const Variable* var) const {
81-
for (auto& kv : vars_) {
82-
if (kv.second.get() == var) {
83-
return this;
84-
}
85-
}
86-
return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
65+
std::unique_lock<std::mutex> lock(mutex_);
66+
return FindScopeInternal(var);
8767
}
68+
8869
void Scope::DropKids() {
8970
std::unique_lock<std::mutex> lock(mutex_);
9071
for (Scope* s : kids_) delete s;
9172
kids_.clear();
9273
}
9374

9475
std::vector<std::string> Scope::LocalVarNames() const {
76+
std::unique_lock<std::mutex> lock(mutex_);
9577
std::vector<std::string> known_vars;
9678
known_vars.reserve(this->vars_.size());
9779
for (auto& p : vars_) {
@@ -127,6 +109,39 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
127109

128110
void Scope::Rename(const std::string& origin_name,
129111
const std::string& new_name) const {
112+
std::unique_lock<std::mutex> lock(mutex_);
113+
RenameInternal(origin_name, new_name);
114+
}
115+
116+
std::string Scope::Rename(const std::string& origin_name) const {
117+
std::unique_lock<std::mutex> lock(mutex_);
118+
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
119+
RenameInternal(origin_name, new_name);
120+
return new_name;
121+
}
122+
123+
Variable* Scope::VarInternal(const std::string& name) {
124+
auto* v = FindVarLocally(name);
125+
if (v != nullptr) return v;
126+
127+
v = new Variable();
128+
vars_[name].reset(v);
129+
VLOG(3) << "Create variable " << name;
130+
v->name_ = &(vars_.find(name)->first);
131+
return v;
132+
}
133+
134+
const Scope* Scope::FindScopeInternal(const Variable* var) const {
135+
for (auto& kv : vars_) {
136+
if (kv.second.get() == var) {
137+
return this;
138+
}
139+
}
140+
return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
141+
}
142+
143+
void Scope::RenameInternal(const std::string& origin_name,
144+
const std::string& new_name) const {
130145
auto origin_it = vars_.find(origin_name);
131146
PADDLE_ENFORCE(origin_it != vars_.end(),
132147
"Cannot find original variable with name %s", origin_name);
@@ -137,10 +152,12 @@ void Scope::Rename(const std::string& origin_name,
137152
vars_.erase(origin_it);
138153
}
139154

140-
std::string Scope::Rename(const std::string& origin_name) const {
141-
auto var_name = string::Sprintf("%p.%d", this, vars_.size());
142-
Rename(origin_name, var_name);
143-
return var_name;
155+
Variable* Scope::FindVarInternal(const std::string& name) const {
156+
auto var = FindVarLocally(name);
157+
if (var != nullptr) {
158+
return var;
159+
}
160+
return (parent_ == nullptr) ? nullptr : parent_->FindVar(name);
144161
}
145162

146163
Variable* Scope::FindVarLocally(const std::string& name) const {

paddle/fluid/framework/scope.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,20 @@ class Scope {
8888
// Call Scope::NewScope for a sub-scope.
8989
explicit Scope(Scope const* parent) : parent_(parent) {}
9090

91+
// Called by Var.
92+
Variable* VarInternal(const std::string& name);
93+
94+
// Called by FindScope.
95+
const Scope* FindScopeInternal(const Variable* var) const;
96+
97+
// Called by Rename.
98+
void RenameInternal(const std::string& origin_name,
99+
const std::string& new_name) const;
100+
91101
// Called by FindVar recursively.
92-
// Caller doesn't own the returned Variable.
93102
Variable* FindVarInternal(const std::string& name) const;
94103

95104
// Called by FindVarInternal and Var.
96-
// Caller doesn't own the returned Variable.
97105
Variable* FindVarLocally(const std::string& name) const;
98106

99107
// Scope in `kids_` are owned by this class.

paddle/fluid/inference/tests/book/test_inference_nlp.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ void ThreadRunInfer(
103103
const int tid, paddle::framework::Scope* scope,
104104
const std::vector<std::vector<const paddle::framework::LoDTensor*>>& jobs) {
105105
// maybe framework:ProgramDesc is not thread-safe
106+
paddle::platform::CPUPlace place;
107+
paddle::framework::Executor executor(place);
106108
auto& sub_scope = scope->NewScope();
107-
auto place = paddle::platform::CPUPlace();
108-
auto executor = paddle::framework::Executor(place);
109109
auto inference_program =
110110
paddle::inference::Load(&executor, scope, FLAGS_model_path);
111111

@@ -182,8 +182,8 @@ TEST(inference, nlp) {
182182
stop_ms = GetCurrentMs();
183183
} else {
184184
// 1. Define place, executor, scope
185-
auto place = paddle::platform::CPUPlace();
186-
auto executor = paddle::framework::Executor(place);
185+
paddle::platform::CPUPlace place;
186+
paddle::framework::Executor executor(place);
187187

188188
// 2. Initialize the inference_program and load parameters
189189
std::unique_ptr<paddle::framework::ProgramDesc> inference_program;

0 commit comments

Comments
 (0)