From 8ecad98578574175b029687d5b67a65b3dc5abde Mon Sep 17 00:00:00 2001 From: Liu Yiqun Date: Wed, 14 Mar 2018 02:57:29 +0000 Subject: [PATCH] Add the bool variable to decide whether to have a copy of the program in ExecutorPrepareContext. --- paddle/fluid/framework/executor.cc | 29 ++++++++++++++++++++++------- paddle/fluid/framework/executor.h | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/paddle/fluid/framework/executor.cc b/paddle/fluid/framework/executor.cc index 82f75ab74..d70a661cf 100644 --- a/paddle/fluid/framework/executor.cc +++ b/paddle/fluid/framework/executor.cc @@ -35,11 +35,26 @@ namespace paddle { namespace framework { struct ExecutorPrepareContext { - ExecutorPrepareContext(const framework::ProgramDesc& prog, size_t block_id) - : prog_(prog), block_id_(block_id) {} + ExecutorPrepareContext(const framework::ProgramDesc* prog, size_t block_id, + bool own_program = true) + : block_id_(block_id), own_program_(own_program) { + if (own_program_) { + prog_ = new ProgramDesc(*prog); + } else { + // If own_program_ is false, we can avoid a clone of the program. + prog_ = prog; + } + } + + ~ExecutorPrepareContext() { + if (own_program_) { + delete prog_; + } + } - framework::ProgramDesc prog_; + const framework::ProgramDesc* prog_; size_t block_id_; + bool own_program_; std::vector> ops_; }; @@ -94,7 +109,7 @@ static void CheckTensorNANOrInf(const std::string& name, void Executor::Run(const ProgramDesc& pdesc, Scope* scope, int block_id, bool create_local_scope, bool create_vars) { - auto* ctx = Prepare(pdesc, block_id); + auto* ctx = Prepare(pdesc, block_id, false); RunPreparedContext(ctx, scope, create_local_scope, create_vars); delete ctx; } @@ -267,8 +282,8 @@ void Executor::Run(const ProgramDesc& program, Scope* scope, } ExecutorPrepareContext* Executor::Prepare(const ProgramDesc& program, - int block_id) { - auto* ctx = new ExecutorPrepareContext(program, block_id); + int block_id, bool own_program) { + auto* ctx = new ExecutorPrepareContext(&program, block_id, own_program); PADDLE_ENFORCE_LT(static_cast(block_id), program.Size()); auto& block = program.Block(block_id); for (auto& op_desc : block.AllOps()) { @@ -279,7 +294,7 @@ ExecutorPrepareContext* Executor::Prepare(const ProgramDesc& program, void Executor::RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope, bool create_local_scope, bool create_vars) { - auto& block = ctx->prog_.Block(ctx->block_id_); + auto& block = ctx->prog_->Block(ctx->block_id_); Scope* local_scope = scope; if (create_vars) { diff --git a/paddle/fluid/framework/executor.h b/paddle/fluid/framework/executor.h index 28ce33151..2be48bd00 100644 --- a/paddle/fluid/framework/executor.h +++ b/paddle/fluid/framework/executor.h @@ -48,7 +48,7 @@ class Executor { const std::string& fetch_holder_name = "fetch"); static ExecutorPrepareContext* Prepare(const ProgramDesc& program, - int block_id); + int block_id, bool own_program = true); void RunPreparedContext(ExecutorPrepareContext* ctx, Scope* scope, bool create_local_scope = true, -- GitLab