diff --git a/paddle/fluid/framework/executor.cc b/paddle/fluid/framework/executor.cc index 82f75ab741a4689bbbdc7f5d8c33f27d1da26181..d70a661cf365f0e5bd4a5178ccf7e032563a4a5e 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 28ce3315154cea45412984df4daf7385ce2cf572..2be48bd0061ce2d6860bef3eae86ec3ae7a51fcd 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,