From 63bd38bd74bc623eff75bd909d57981c538444c7 Mon Sep 17 00:00:00 2001 From: qiaolongfei Date: Thu, 26 Apr 2018 12:25:17 +0800 Subject: [PATCH] code optimize --- .../operators/detail/variable_response.h | 4 +++- paddle/fluid/operators/listen_and_serv_op.cc | 21 ++++++++++--------- paddle/fluid/operators/send_recv_op_test.cc | 2 +- python/paddle/fluid/distribute_transpiler.py | 5 ++++- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/paddle/fluid/operators/detail/variable_response.h b/paddle/fluid/operators/detail/variable_response.h index 8c05e60cecf..bf624da2a6c 100644 --- a/paddle/fluid/operators/detail/variable_response.h +++ b/paddle/fluid/operators/detail/variable_response.h @@ -63,7 +63,9 @@ class VariableResponse { // other: number of error field. int Parse(const ::grpc::ByteBuffer& byte_buffer); - framework::Scope& GetLocalScope() const { return *local_scope_; } + const framework::Scope& GetLocalScope() const { return *local_scope_; } + + framework::Scope* GetMutableLocalScope() const { return local_scope_; } inline std::string Varname() { return meta_.varname(); } inline std::string OutVarname() { return meta_.out_varname(); } diff --git a/paddle/fluid/operators/listen_and_serv_op.cc b/paddle/fluid/operators/listen_and_serv_op.cc index 3ee642dedb8..ba2ea0d13e5 100644 --- a/paddle/fluid/operators/listen_and_serv_op.cc +++ b/paddle/fluid/operators/listen_and_serv_op.cc @@ -207,18 +207,19 @@ void ListenAndServOp::RunAsyncLoop(framework::Executor *executor, framework::BlockDesc *prefetch_block) const { VLOG(3) << "RunAsyncLoop in"; // grad name to block id - std::unordered_map grad_to_id; + std::unordered_map grad_to_block_id; std::unordered_map id_to_grad; - auto grad_to_id_str = Attr>("grad_to_id"); - for (auto &grad_and_id : grad_to_id_str) { + auto grad_to_block_id_str = + Attr>("grad_to_block_id"); + for (auto &grad_and_id : grad_to_block_id_str) { std::vector pieces; split(grad_and_id, ':', &pieces); VLOG(3) << "after split, grad = " << pieces[0] << ", id=" << pieces[1]; PADDLE_ENFORCE_EQ(pieces.size(), 2); - PADDLE_ENFORCE_EQ(grad_to_id.count(pieces[0]), 0); + PADDLE_ENFORCE_EQ(grad_to_block_id.count(pieces[0]), 0); int block_id = std::stoi(pieces[1]); - grad_to_id[pieces[0]] = block_id; + grad_to_block_id[pieces[0]] = block_id; id_to_grad[block_id] = pieces[0]; } size_t num_blocks = program->Size(); @@ -232,9 +233,9 @@ void ListenAndServOp::RunAsyncLoop(framework::Executor *executor, auto optimize_prepared = executor->Prepare(*program, block_list); std::unordered_map> - grad_to_prepared; + grad_to_prepared_block; for (size_t i = 0; i < block_list.size(); ++i) { - grad_to_prepared[id_to_grad[block_list[i]]] = optimize_prepared[i]; + grad_to_prepared_block[id_to_grad[block_list[i]]] = optimize_prepared[i]; } VLOG(3) << "RunAsyncLoop into while"; @@ -253,8 +254,8 @@ void ListenAndServOp::RunAsyncLoop(framework::Executor *executor, LOG(ERROR) << "Can not find server side var: " << recv_var_name; PADDLE_THROW("Can not find server side var"); } - AsyncExecuteBlock(executor, grad_to_prepared[recv_var_name].get(), - &(v.second->GetLocalScope())); + AsyncExecuteBlock(executor, grad_to_prepared_block[recv_var_name].get(), + v.second->GetMutableLocalScope()); // TODO(qiao): explain why if (var->IsType()) { var->GetMutable()->mutable_rows()->clear(); @@ -328,7 +329,7 @@ from send_op and send back variables to recv_op. .SetDefault("127.0.0.1:6164") .AddCustomChecker([](const std::string &ip) { return !ip.empty(); }); AddAttr>( - "grad_to_id", + "grad_to_block_id", "['param1@GRAD.block0:1', 'param2@GRAD.blockn:2'] " "a map from grad name to it's optimize block id") .SetDefault({}); diff --git a/paddle/fluid/operators/send_recv_op_test.cc b/paddle/fluid/operators/send_recv_op_test.cc index 72dc1586c64..d2e1f3cb2ff 100644 --- a/paddle/fluid/operators/send_recv_op_test.cc +++ b/paddle/fluid/operators/send_recv_op_test.cc @@ -137,7 +137,7 @@ void StartServerNet(bool is_sparse) { attrs.insert({"GradList", std::vector({"x1"})}); attrs.insert({"OptimizeBlock", optimize_block}); attrs.insert({"PrefetchBlock", prefetch_block}); - attrs.insert({"grad_to_id", std::vector({""})}); + attrs.insert({"grad_to_block_id", std::vector({""})}); attrs.insert({"sync_mode", true}); listen_and_serv_op = f::OpRegistry::CreateOp("listen_and_serv", {{"X", {"x1"}}}, {}, attrs); diff --git a/python/paddle/fluid/distribute_transpiler.py b/python/paddle/fluid/distribute_transpiler.py index 3a3a94640a1..d17475cd28b 100644 --- a/python/paddle/fluid/distribute_transpiler.py +++ b/python/paddle/fluid/distribute_transpiler.py @@ -185,6 +185,9 @@ class DistributeTranspiler: :param split_method: A function to determin how to split variables to different servers equally. :type split_method: function + :type sync_mode: boolean default True + :param sync_mode: if sync_mode is set True, it means that dist transpiler + will transpile the program into sync_mode pserver and trainer program. """ assert (callable(split_method)) if program is None: @@ -479,7 +482,7 @@ class DistributeTranspiler: "Fanin": self.trainer_num, "PrefetchBlock": prefetch_block, "sync_mode": self.sync_mode, - "grad_to_id": grad_to_block_id + "grad_to_block_id": grad_to_block_id }) pserver_program.sync_with_cpp() -- GitLab