From 91f5d2b9cb23cbb6048180ed791e53659532cf04 Mon Sep 17 00:00:00 2001 From: qijun Date: Fri, 6 Oct 2017 16:09:19 -0700 Subject: [PATCH] follow comments and create local_scope inside executor run method --- paddle/framework/executor.cc | 6 ++---- paddle/framework/executor_test.cc | 12 ++++++------ paddle/framework/scope.cc | 2 +- paddle/framework/scope.h | 2 +- paddle/operators/feed_op.cc | 2 +- paddle/operators/feed_op.h | 6 ++---- paddle/operators/fetch_op.cc | 2 +- paddle/operators/fetch_op.h | 6 ++---- 8 files changed, 16 insertions(+), 22 deletions(-) diff --git a/paddle/framework/executor.cc b/paddle/framework/executor.cc index c18ba049c8f..7fc407ebc94 100644 --- a/paddle/framework/executor.cc +++ b/paddle/framework/executor.cc @@ -56,9 +56,7 @@ void Executor::Run(const ProgramDesc& pdesc, Scope* scope) { auto& block = pdesc.blocks(0); auto& device = device_contexts_[0]; - // TODO(tonyyang-svail): - // - runs on a new local scope - // Scope& local_scope = scope->NewScope(); + Scope& local_scope = scope->NewScope(); for (auto& var : block.vars()) { scope->NewVar(var.name()); @@ -67,7 +65,7 @@ void Executor::Run(const ProgramDesc& pdesc, Scope* scope) { for (auto& op_desc : block.ops()) { auto op = paddle::framework::OpRegistry::CreateOp(op_desc); std::cout << op->DebugString() << std::endl; - op->Run(*scope, *device); + op->Run(local_scope, *device); } // TODO(tonyyang-svail): need to test gpu device diff --git a/paddle/framework/executor_test.cc b/paddle/framework/executor_test.cc index 82f9bd6f2dd..bf6c1dffc13 100644 --- a/paddle/framework/executor_test.cc +++ b/paddle/framework/executor_test.cc @@ -131,7 +131,7 @@ template void set_feed_variable(const std::vector>& inputs) { typedef std::vector FeedInputs; // Tensors in feed value variable will only be in CPUPlace - Variable* g_feed_value = GetScope()->FindVar("feed_value"); + Variable* g_feed_value = GetGlobalScope()->FindVar("feed_value"); FeedInputs& feed_inputs = *(g_feed_value->GetMutable()); auto size = inputs.size(); feed_inputs.resize(size); @@ -146,7 +146,7 @@ template std::vector> get_fetch_variable() { typedef std::vector FetchOutputs; // Tensors in fetch value variable will only be in CPUPlace - Variable* g_fetch_value = GetScope()->FindVar("fetch_value"); + Variable* g_fetch_value = GetGlobalScope()->FindVar("fetch_value"); FetchOutputs& fetch_outputs = *(g_fetch_value->GetMutable()); auto size = fetch_outputs.size(); @@ -252,7 +252,7 @@ TEST_F(ExecutorTesterRandom, CPU) { paddle::memory::Used(cpu_place); Executor* executor = new Executor(places); - executor->Run(pdesc_, GetScope()); + executor->Run(pdesc_, GetGlobalScope()); std::vector> result = get_fetch_variable(); for (auto& vec : result) { for (auto& num : vec) { @@ -281,7 +281,7 @@ TEST_F(ExecutorTesterFeed, CPU) { // need to set feed variable before Executor::Run std::cout << "start mini-batch " << i << std::endl; set_feed_variable(inputs_); - executor->Run(pdesc_, GetScope()); + executor->Run(pdesc_, GetGlobalScope()); std::vector> result = get_fetch_variable(); for (auto& vec : result) { for (auto& num : vec) { @@ -309,7 +309,7 @@ TEST_F(ExecutorTesterRandom, GPU) { paddle::memory::Used(gpu_place); Executor* executor = new Executor(places); - executor->Run(pdesc_, GetScope()); + executor->Run(pdesc_, GetGlobalScope()); delete executor; } @@ -333,7 +333,7 @@ TEST_F(ExecutorTesterFeed, GPU) { // need to set feed variable before Executor::Run std::cout << "start mini-batch " << i << std::endl; set_feed_variable(inputs_); - executor->Run(pdesc_, GetScope()); + executor->Run(pdesc_, GetGlobalScope()); std::vector> result = get_fetch_variable(); for (auto& vec : result) { for (auto& num : vec) { diff --git a/paddle/framework/scope.cc b/paddle/framework/scope.cc index b6a9d7fbc29..2a0d9bbf334 100644 --- a/paddle/framework/scope.cc +++ b/paddle/framework/scope.cc @@ -66,7 +66,7 @@ void Scope::DropKids() { std::once_flag feed_variable_flag; -framework::Scope* GetScope() { +framework::Scope* GetGlobalScope() { static std::unique_ptr g_scope{nullptr}; std::call_once(feed_variable_flag, [&]() { g_scope.reset(new framework::Scope()); diff --git a/paddle/framework/scope.h b/paddle/framework/scope.h index 96f3ae875b2..319d291efec 100644 --- a/paddle/framework/scope.h +++ b/paddle/framework/scope.h @@ -73,7 +73,7 @@ class Scope { DISABLE_COPY_AND_ASSIGN(Scope); }; -framework::Scope* GetScope(); +framework::Scope* GetGlobalScope(); } // namespace framework } // namespace paddle diff --git a/paddle/operators/feed_op.cc b/paddle/operators/feed_op.cc index f2c498e2e2c..b9e43be9666 100644 --- a/paddle/operators/feed_op.cc +++ b/paddle/operators/feed_op.cc @@ -27,7 +27,7 @@ class FeedOp : public framework::OperatorWithKernel { PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output should be not null."); int col = ctx->Attrs().Get("col"); framework::Variable* g_feed_variable = - framework::GetScope()->FindVar("feed_value"); + framework::GetGlobalScope()->FindVar("feed_value"); const FeedInputs& tensors = g_feed_variable->Get(); diff --git a/paddle/operators/feed_op.h b/paddle/operators/feed_op.h index cf93b6f4345..de8ec6ff61f 100644 --- a/paddle/operators/feed_op.h +++ b/paddle/operators/feed_op.h @@ -19,17 +19,15 @@ limitations under the License. */ namespace paddle { namespace operators { -using Tensor = framework::Tensor; - template class FeedKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { typedef std::vector FeedInputs; - Tensor* out = ctx.Output("Out"); + framework::Tensor* out = ctx.Output("Out"); out->mutable_data(ctx.GetPlace()); framework::Variable* g_feed_variable = - framework::GetScope()->FindVar("feed_value"); + framework::GetGlobalScope()->FindVar("feed_value"); int col = ctx.template Attr("col"); const FeedInputs& tensors = g_feed_variable->Get(); out->CopyFrom(tensors[col], ctx.GetPlace()); diff --git a/paddle/operators/fetch_op.cc b/paddle/operators/fetch_op.cc index 4b6b3ca85ac..7bde4953cde 100644 --- a/paddle/operators/fetch_op.cc +++ b/paddle/operators/fetch_op.cc @@ -27,7 +27,7 @@ class FetchOp : public framework::OperatorWithKernel { PADDLE_ENFORCE(ctx->HasInput("Input"), "Input should be not null."); int col = ctx->Attrs().Get("col"); framework::Variable* g_fetch_variable = - framework::GetScope()->FindVar("fetch_value"); + framework::GetGlobalScope()->FindVar("fetch_value"); FetchOutputs* tensors = g_fetch_variable->GetMutable(); if (tensors->size() < static_cast(col + 1)) { diff --git a/paddle/operators/fetch_op.h b/paddle/operators/fetch_op.h index e8d5e3a9c00..3bec9c9974a 100644 --- a/paddle/operators/fetch_op.h +++ b/paddle/operators/fetch_op.h @@ -19,17 +19,15 @@ limitations under the License. */ namespace paddle { namespace operators { -using Tensor = framework::Tensor; - template class FetchKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { typedef std::vector FetchOutputs; - const Tensor* input = ctx.Input("Input"); + const framework::Tensor* input = ctx.Input("Input"); int col = ctx.template Attr("col"); framework::Variable* g_fetch_variable = - framework::GetScope()->FindVar("fetch_value"); + framework::GetGlobalScope()->FindVar("fetch_value"); FetchOutputs* tensors = g_fetch_variable->GetMutable(); (*tensors)[col].mutable_data(platform::CPUPlace()); (*tensors)[col].CopyFrom(*input, platform::CPUPlace()); -- GitLab