Skip to content

Commit 9861a92

Browse files
committed
change the return type of NewTempScope to unique ptr test=develop
1 parent fb6cc3a commit 9861a92

6 files changed

Lines changed: 17 additions & 21 deletions

File tree

paddle/fluid/framework/scope.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ Scope& Scope::NewScope() const {
5959
return *child;
6060
}
6161

62-
Scope* Scope::NewTmpScope() const { return new Scope(this); }
62+
std::unique_ptr<Scope> Scope::NewTmpScope() const {
63+
return std::unique_ptr<Scope>(new Scope(this));
64+
}
6365

6466
Variable* Scope::Var(const std::string& name) {
6567
SCOPE_VARS_WRITER_LOCK

paddle/fluid/framework/scope.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ class Scope {
5454

5555
/// Create a sub-scope for current scope but do not record it in the kids to
5656
/// avoid performance problems.
57-
/// Note!!! You should delete the result pointer yourself to avoid memory
58-
/// leak!
59-
Scope* NewTmpScope() const;
57+
std::unique_ptr<Scope> NewTmpScope() const;
6058

6159
/// Create a variable with given name if it doesn't exist.
6260
/// Caller doesn't own the returned Variable.

paddle/fluid/operators/distributed/parameter_prefetch.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void prefetch(const std::string& id_name, const std::string& out_name,
160160
const std::vector<int64_t>& height_sections,
161161
const framework::ExecutionContext& context,
162162
const framework::Scope& scope) {
163-
framework::Scope* local_scope = scope.NewTmpScope();
163+
std::unique_ptr<framework::Scope> local_scope = scope.NewTmpScope();
164164

165165
platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
166166
auto& cpu_ctx = *pool.Get(platform::CPUPlace());
@@ -206,7 +206,7 @@ void prefetch(const std::string& id_name, const std::string& out_name,
206206

207207
auto splited_ids = SplitIds(ids_vector, height_sections);
208208
SplitIdsIntoMultipleVarsBySection(in_var_names, height_sections, splited_ids,
209-
local_scope);
209+
local_scope.get());
210210

211211
// create output var in local scope
212212
for (auto& name : out_var_names) {
@@ -215,12 +215,12 @@ void prefetch(const std::string& id_name, const std::string& out_name,
215215

216216
std::vector<distributed::VarHandlePtr> rets;
217217
for (size_t i = 0; i < in_var_names.size(); i++) {
218-
if (NeedSend(*local_scope, in_var_names[i])) {
218+
if (NeedSend(*local_scope.get(), in_var_names[i])) {
219219
VLOG(3) << "sending " << in_var_names[i] << " to " << epmap[i]
220220
<< " to get " << out_var_names[i] << " back";
221221
rets.push_back(rpc_client->AsyncPrefetchVar(
222-
epmap[i], cpu_ctx, *local_scope, in_var_names[i], out_var_names[i],
223-
table_names[i]));
222+
epmap[i], cpu_ctx, *local_scope.get(), in_var_names[i],
223+
out_var_names[i], table_names[i]));
224224
} else {
225225
VLOG(3) << "don't send no-initialied variable: " << out_var_names[i];
226226
}
@@ -232,8 +232,7 @@ void prefetch(const std::string& id_name, const std::string& out_name,
232232

233233
MergeMultipleVarsIntoOneBySection(id_name, ids_vector, out_name,
234234
out_var_names, height_sections, splited_ids,
235-
context, local_scope, &actual_ctx);
236-
delete local_scope;
235+
context, local_scope.get(), &actual_ctx);
237236
}
238237

239238
}; // namespace distributed

paddle/fluid/operators/distributed/parameter_recv.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ template <typename T>
4242
void ParameterRecv<T>::operator()(const RpcContext &rpc_ctx,
4343
const framework::Scope &scope) {
4444
VLOG(3) << "ParameterRecv in";
45-
framework::Scope *local_scope = scope.NewTmpScope();
45+
std::unique_ptr<framework::Scope> local_scope = scope.NewTmpScope();
4646

4747
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
4848
auto &cpu_ctx = *pool.Get(platform::CPUPlace());
@@ -64,7 +64,7 @@ void ParameterRecv<T>::operator()(const RpcContext &rpc_ctx,
6464
recved_tensors.push_back(t);
6565
VLOG(3) << "recv " << recv_var_name << " from " << rpc_ctx.epmap[i];
6666
rets.push_back(rpc_client->AsyncGetVar(rpc_ctx.epmap[i], cpu_ctx,
67-
*local_scope, recv_var_name,
67+
*local_scope.get(), recv_var_name,
6868
recv_var_name));
6969
}
7070
for (size_t i = 0; i < rets.size(); i++) {
@@ -93,7 +93,6 @@ void ParameterRecv<T>::operator()(const RpcContext &rpc_ctx,
9393
PADDLE_ENFORCE_EQ(recv_numel, recv_tensor->numel());
9494
}
9595

96-
delete local_scope;
9796
VLOG(3) << "ParameterRecv out";
9897
}
9998

paddle/fluid/operators/distributed/parameter_send.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ using DDim = framework::DDim;
4040
template <typename T>
4141
void ParameterSend<T>::operator()(const RpcContext &rpc_ctx,
4242
const framework::Scope &scope, bool sync) {
43-
framework::Scope *local_scope = scope.NewTmpScope();
43+
std::unique_ptr<framework::Scope> local_scope = scope.NewTmpScope();
4444

4545
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
4646
auto &cpu_ctx = *pool.Get(platform::CPUPlace());
@@ -150,10 +150,10 @@ void ParameterSend<T>::operator()(const RpcContext &rpc_ctx,
150150
for (size_t i = 0; i < rpc_ctx.splited_var_names.size(); i++) {
151151
auto &send_var_name = rpc_ctx.splited_var_names[i];
152152
auto &endpoint = rpc_ctx.epmap[i];
153-
if (NeedSend(*local_scope, send_var_name)) {
153+
if (NeedSend(*local_scope.get(), send_var_name)) {
154154
VLOG(3) << "sending " << send_var_name << " to " << endpoint;
155-
rets.push_back(rpc_client->AsyncSendVar(endpoint, cpu_ctx, *local_scope,
156-
send_var_name));
155+
rets.push_back(rpc_client->AsyncSendVar(
156+
endpoint, cpu_ctx, *local_scope.get(), send_var_name));
157157
} else {
158158
VLOG(3) << "don't send non-initialized variable: "
159159
<< rpc_ctx.splited_var_names[i];
@@ -165,8 +165,6 @@ void ParameterSend<T>::operator()(const RpcContext &rpc_ctx,
165165
PADDLE_ENFORCE(handle->Wait(), "internal error in RPCClient");
166166
}
167167
}
168-
169-
delete local_scope;
170168
}
171169

172170
template struct ParameterSend<float>;

paddle/fluid/operators/distributed/variable_response.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class VariableResponse {
6060
bool create_scope = false)
6161
: scope_(scope), dev_ctx_(dev_ctx), create_scope_(create_scope) {
6262
if (create_scope) {
63-
local_scope_ = scope->NewTmpScope();
63+
local_scope_ = scope->NewTmpScope().release();
6464
}
6565
}
6666

0 commit comments

Comments
 (0)