Skip to content

Commit ebc7303

Browse files
authored
listen_and_serv use local scope (#10663)
* listen_and_serv use localscope * fix ut
1 parent 868bdc9 commit ebc7303

File tree

7 files changed

+30
-19
lines changed

7 files changed

+30
-19
lines changed

paddle/fluid/framework/executor.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ static bool has_fetch_operators(
228228
void Executor::Run(const ProgramDesc& program, Scope* scope,
229229
std::map<std::string, const LoDTensor*>* feed_targets,
230230
std::map<std::string, LoDTensor*>* fetch_targets,
231-
bool create_vars, const std::string& feed_holder_name,
231+
bool create_local_scope, bool create_vars,
232+
const std::string& feed_holder_name,
232233
const std::string& fetch_holder_name) {
233234
platform::RecordBlock b(kProgramId);
234235
bool has_feed_ops =
@@ -290,8 +291,9 @@ void Executor::Run(const ProgramDesc& program, Scope* scope,
290291
}
291292

292293
auto ctx = Prepare(*copy_program, 0);
293-
RunPreparedContext(ctx.get(), scope, feed_targets, fetch_targets, create_vars,
294-
feed_holder_name, fetch_holder_name);
294+
RunPreparedContext(ctx.get(), scope, feed_targets, fetch_targets,
295+
create_local_scope, create_vars, feed_holder_name,
296+
fetch_holder_name);
295297
}
296298

297299
std::unique_ptr<ExecutorPrepareContext> Executor::Prepare(
@@ -366,8 +368,9 @@ void Executor::RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope,
366368
void Executor::RunPreparedContext(
367369
ExecutorPrepareContext* ctx, Scope* scope,
368370
std::map<std::string, const LoDTensor*>* feed_targets,
369-
std::map<std::string, LoDTensor*>* fetch_targets, bool create_vars,
370-
const std::string& feed_holder_name, const std::string& fetch_holder_name) {
371+
std::map<std::string, LoDTensor*>* fetch_targets, bool create_local_scope,
372+
bool create_vars, const std::string& feed_holder_name,
373+
const std::string& fetch_holder_name) {
371374
auto& global_block = ctx->prog_.Block(ctx->block_id_);
372375

373376
PADDLE_ENFORCE(
@@ -387,7 +390,7 @@ void Executor::RunPreparedContext(
387390
}
388391
}
389392

390-
RunPreparedContext(ctx, scope, create_vars, create_vars);
393+
RunPreparedContext(ctx, scope, create_local_scope, create_vars);
391394

392395
// obtain the data of fetch_targets from fetch_holder
393396
for (auto* op : global_block.AllOps()) {

paddle/fluid/framework/executor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Executor {
5757
void Run(const ProgramDesc& program, Scope* scope,
5858
std::map<std::string, const LoDTensor*>* feed_targets,
5959
std::map<std::string, LoDTensor*>* fetch_targets,
60-
bool create_vars = true,
60+
bool create_local_scope = true, bool create_vars = true,
6161
const std::string& feed_holder_name = "feed",
6262
const std::string& fetch_holder_name = "fetch");
6363

@@ -76,6 +76,7 @@ class Executor {
7676
void RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope,
7777
std::map<std::string, const LoDTensor*>* feed_targets,
7878
std::map<std::string, LoDTensor*>* fetch_targets,
79+
bool create_local_scope = true,
7980
bool create_vars = true,
8081
const std::string& feed_holder_name = "feed",
8182
const std::string& fetch_holder_name = "fetch");

paddle/fluid/inference/tests/test_helper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ void TestInference(const std::string& dirname,
208208
if (PrepareContext) {
209209
ctx = executor.Prepare(*inference_program, 0);
210210
executor.RunPreparedContext(ctx.get(), scope, &feed_targets,
211-
&fetch_targets, CreateVars);
211+
&fetch_targets, true, CreateVars);
212212
} else {
213213
executor.Run(*inference_program, scope, &feed_targets, &fetch_targets,
214-
CreateVars);
214+
true, CreateVars);
215215
}
216216

217217
// Enable the profiler

paddle/fluid/operators/detail/grpc_server.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class RequestPrefetch final : public RequestBase {
184184
framework::Scope* local_scope = &scope_->NewScope();
185185
auto* var = local_scope->FindVar(var_name);
186186
InitializeVariable(var, var_desc->GetType());
187-
executor_->RunPreparedContext(prefetch_ctx_, scope_, false, false);
187+
executor_->RunPreparedContext(prefetch_ctx_, scope_);
188188

189189
SerializeToByteBuffer(var_name, var, *dev_ctx_, &reply);
190190

paddle/fluid/operators/listen_and_serv_op.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ static void ParallelExecuteBlocks(
5757
framework::Async([&executor, &prepared, &program, &scope, idx]() {
5858
int run_block = idx; // thread local
5959
try {
60-
executor->RunPreparedContext(prepared[run_block].get(), scope,
61-
false, false);
60+
executor->RunPreparedContext(prepared[run_block].get(), scope);
6261
} catch (std::exception &e) {
6362
LOG(ERROR) << "run sub program error " << e.what();
6463
}
@@ -211,8 +210,8 @@ static void AsyncUpdateThread(
211210
}
212211
auto fs = framework::Async([var_name, &executor, &v, prepared] {
213212
try {
214-
executor->RunPreparedContext(prepared, v.second->GetMutableLocalScope(),
215-
false, false);
213+
executor->RunPreparedContext(prepared,
214+
v.second->GetMutableLocalScope());
216215
} catch (std::exception &e) {
217216
LOG(ERROR) << "run sub program error " << e.what();
218217
}

paddle/fluid/operators/send_recv_op_test.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,16 @@ void InitSelectedRowsInScope(const p::CPUPlace &place, f::Scope *scope) {
9292

9393
void AddOp(const std::string &type, const f::VariableNameMap &inputs,
9494
const f::VariableNameMap &outputs, f::AttributeMap attrs,
95-
f::BlockDesc *block) {
95+
f::BlockDesc *block, bool is_sparse) {
9696
// insert output
9797
for (auto kv : outputs) {
9898
for (auto v : kv.second) {
9999
auto var = block->Var(v);
100100
var->SetDataType(f::proto::VarType::FP32);
101+
var->SetPersistable(true);
102+
if (is_sparse) {
103+
var->SetType(f::proto::VarType::SELECTED_ROWS);
104+
}
101105
}
102106
}
103107

@@ -128,7 +132,8 @@ void StartServerNet(bool is_sparse, std::atomic<bool> *initialized) {
128132
auto *optimize_block = program.AppendBlock(root_block);
129133
auto *prefetch_block = program.AppendBlock(root_block);
130134
// X for server side tensors, RX for received tensors, must be of same shape.
131-
AddOp("sum", {{"X", {"x0", "x1"}}}, {{"Out", {"Out"}}}, {}, optimize_block);
135+
AddOp("sum", {{"X", {"x0", "x1"}}}, {{"Out", {"Out"}}}, {}, optimize_block,
136+
is_sparse);
132137
f::AttributeMap attrs;
133138
attrs.insert({"endpoint", std::string("127.0.0.1:0")});
134139
attrs.insert({"Fanin", 1});

python/paddle/fluid/tests/unittests/test_dist_train.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@ def init_serv(self, place):
5252
serv = layers.ListenAndServ(
5353
"127.0.0.1:0", ["X"], optimizer_mode=False)
5454
with serv.do():
55+
out_var = main.global_block().create_var(
56+
name="scale_0.tmp_0",
57+
psersistable=True,
58+
dtype="float32",
59+
shape=[32, 32])
5560
x = layers.data(
5661
shape=[32, 32],
5762
dtype='float32',
5863
name="X",
5964
append_batch_size=False)
6065
fluid.initializer.Constant(value=1.0)(x, main.global_block())
61-
o = layers.scale(x=x, scale=10.0)
62-
main.global_block().create_var(
63-
name=o.name, psersistable=False, dtype=o.dtype, shape=o.shape)
66+
layers.scale(x=x, scale=10.0, out=out_var)
6467

6568
self.server_exe = fluid.Executor(place)
6669
self.server_exe.run(main)

0 commit comments

Comments
 (0)