From da130862bce6d2fb8f0c925f6ef5c47ff1c947b6 Mon Sep 17 00:00:00 2001 From: huzhiqiang <912790387@qq.com> Date: Mon, 27 Jul 2020 09:49:38 +0800 Subject: [PATCH] [Framework] Remove program_desc from Program and Update Clone method (#3976) --- lite/api/cxx_api.h | 81 +++++++++++++++++++++++++++++++++++---------- lite/core/program.h | 16 +++------ 2 files changed, 69 insertions(+), 28 deletions(-) diff --git a/lite/api/cxx_api.h b/lite/api/cxx_api.h index 1e523514c6..ceb823d581 100644 --- a/lite/api/cxx_api.h +++ b/lite/api/cxx_api.h @@ -49,18 +49,33 @@ class LITE_API Predictor { program_desc_ = std::make_shared(); } - // Create a predictor with the weight variable scope set. + /////////////////////////////////////////////////////////////////// + // Function: Predictor + // Usage: Constructor of Predictor. Create a predictor with the + // weight variable scope set given. + /////////////////////////////////////////////////////////////////// explicit Predictor(const std::shared_ptr& root_scope) : scope_(root_scope) {} + /////////////////////////////////////////////////////////////////// + // Function: Predictor + // Usage: Constructor of Predictor. This constructor function can + // only be called in Predictor->Clone. This Function will create + // a predictor from existed ProgramDesc, Scope and RuntimeProgram. + /////////////////////////////////////////////////////////////////// Predictor(const std::shared_ptr& program_desc, - const std::shared_ptr& root_scope, + const std::shared_ptr& root, const std::vector& valid_places, - const std::vector& vars_to_clone = {}) - : program_desc_(program_desc), scope_(root_scope) { - Program program(program_desc_, scope_, valid_places, vars_to_clone); - optimizer_ = Optimizer(std::move(program), valid_places); - exec_scope_ = optimizer_.exec_scope(); + const std::vector& var_names = {}) + : program_desc_(program_desc), scope_(root) { + // step1. Create a Program to construct the exec_scope and ops + Program program(program_desc_, scope_, valid_places, var_names); + exec_scope_ = program.exec_scope(); valid_places_ = valid_places; + + // step3. Create the RuntimeProgram. + program_.reset( + new RuntimeProgram(program_desc_, exec_scope_, kRootBlockIdx)); + program_generated_ = true; } // Build from a model, with places set for hardware config. @@ -83,26 +98,58 @@ class LITE_API Predictor { const std::vector& valid_places, const std::vector& passes = {}); - std::shared_ptr Clone() const { - return std::make_shared(program_desc_, scope_, valid_places_); + ////////////////////////////////////////////////////////// + // Function: Clone + // Usage: Create a Predictor from an existed one, + // the cloned predictor will share persistable variables + // in scope_ with the original predictor. + ////////////////////////////////////////////////////////// + std::shared_ptr Clone() { + // step 1. Generate runtime_program, update op_info and var_info in + // program_desc_ + if (!program_generated_) { + GenRuntimeProgram(); + } + program_->SaveToProgram(program_desc_); + // step 2. Create a predictor friom current program_desc_ and + // runtime_program. + auto predictor = + std::make_shared(program_desc_, scope_, valid_places_); + // step3. Return the result + return predictor; } - - std::shared_ptr Clone( - const std::vector& vars_to_clone) const { + ////////////////////////////////////////////////////////// + // Function: Clone(var_names) + // Usage: Create a Predictor from an existed one, + // the cloned predictor will share persistable variables + // but persistable variables of name var_names will not + // be shared. + ////////////////////////////////////////////////////////// + std::shared_ptr Clone(const std::vector& var_names) { CHECK(program_desc_) << "Both program and scope of current predicotr " "should be not be nullptr in Clone mode."; CHECK(scope_) << "Both program and scope of current predicotr should be " "not be nullptr in Clone mode."; + // step 1. Generate runtime_program, update op_info and var_info in + // program_desc_ + if (!program_generated_) { + GenRuntimeProgram(); + } + program_->SaveToProgram(program_desc_); + // step 2. Create a predictor friom current program_desc_ and + // runtime_program. auto predictor = std::make_shared( - program_desc_, scope_, valid_places_, vars_to_clone); - - for (auto var_name : vars_to_clone) { + program_desc_, scope_, valid_places_, var_names); + // step3. Copy some persistable variables into private scope. + for (auto var_name : var_names) { predictor->exec_scope_->LocalVar(var_name); - auto* tensor = predictor->scope_->Var(var_name)->GetMutable(); + auto* tensor = + predictor->scope_->Var(var_name)->GetMutable(); auto* sub_tensor = predictor->exec_scope_->Var(var_name)->GetMutable(); sub_tensor->CopyDataFrom(*tensor); } + // step4. Return the result return predictor; } @@ -161,7 +208,7 @@ class LITE_API Predictor { std::shared_ptr program_desc_; std::shared_ptr scope_; Scope* exec_scope_; - std::unique_ptr program_; + std::shared_ptr program_; bool program_generated_{false}; std::vector input_names_; std::vector output_names_; diff --git a/lite/core/program.h b/lite/core/program.h index 1f82b52d19..f0715b9760 100644 --- a/lite/core/program.h +++ b/lite/core/program.h @@ -47,21 +47,18 @@ struct Program { Program(const std::shared_ptr& program_desc, const std::shared_ptr& root_scope, const std::vector& valid_places, - const std::vector& vars_to_clone = {}) - : scope_(root_scope), - valid_places_(valid_places), - program_desc_(program_desc) { + const std::vector& var_names = {}) + : scope_(root_scope), valid_places_(valid_places) { CHECK(scope_) << "scope should be init first"; VLOG(4) << "prepare work"; - PrepareWorkspace(program_desc_, vars_to_clone); + PrepareWorkspace(program_desc, var_names); VLOG(4) << "build desc"; - Build(program_desc_); + Build(program_desc); VLOG(4) << "build desc finished"; } std::unique_ptr Clone() const { - return std::unique_ptr( - new Program(program_desc_, scope_, valid_places_)); + return std::unique_ptr(new Program(scope_)); } const std::list& weights() const { return weights_; } @@ -83,8 +80,6 @@ struct Program { Scope* exec_scope() { return exec_scope_; } Scope* scope() { return scope_.get(); } - cpp::ProgramDesc* program_desc() { return program_desc_.get(); } - const std::map& var_type_map() const { return var_type_map_; } @@ -106,7 +101,6 @@ struct Program { std::vector valid_places_; // Runtime scope. Scope* exec_scope_{}; - std::shared_ptr program_desc_; }; struct Instruction { -- GitLab