| 3068 | } |
| 3069 | |
| 3070 | bool AnalysisPredictor::LoadParameters() { |
| 3071 | PADDLE_ENFORCE_NOT_NULL(inference_program_.get(), |
| 3072 | common::errors::PreconditionNotMet( |
| 3073 | "The inference program should be loaded first.")); |
| 3074 | |
| 3075 | const auto &global_block = inference_program_->MutableBlock(0); |
| 3076 | |
| 3077 | // create a temporary program to load parameters. |
| 3078 | |
| 3079 | std::unique_ptr<framework::ProgramDesc> load_program( |
| 3080 | new framework::ProgramDesc()); |
| 3081 | framework::BlockDesc *load_block = load_program->MutableBlock(0); |
| 3082 | std::vector<std::string> params; |
| 3083 | |
| 3084 | for (auto *var : global_block->AllVars()) { |
| 3085 | if (IsPersistable(var)) { |
| 3086 | VLOG(3) << "persistable variable's name: " << var->Name(); |
| 3087 | |
| 3088 | framework::VarDesc *new_var = load_block->Var(var->Name()); |
| 3089 | new_var->SetShape(var->GetShape()); |
| 3090 | new_var->SetDataType(var->GetDataType()); |
| 3091 | new_var->SetType(var->GetType()); |
| 3092 | new_var->SetLoDLevel(var->GetLoDLevel()); |
| 3093 | new_var->SetPersistable(true); |
| 3094 | |
| 3095 | if (!config_.params_file().empty()) { |
| 3096 | params.push_back(new_var->Name()); |
| 3097 | } else { |
| 3098 | // append_op |
| 3099 | framework::OpDesc *op = load_block->AppendOp(); |
| 3100 | op->SetType("load"); |
| 3101 | op->SetOutput("Out", {new_var->Name()}); |
| 3102 | op->SetAttr("file_path", {config_.model_dir() + "/" + new_var->Name()}); |
| 3103 | op->CheckAttrs(); |
| 3104 | } |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | if (!config_.params_file().empty()) { |
| 3109 | // sort paramlist to have consistent ordering |
| 3110 | std::sort(params.begin(), params.end()); |
| 3111 | // append just the load_combine op |
| 3112 | framework::OpDesc *op = load_block->AppendOp(); |
| 3113 | op->SetType("load_combine"); |
| 3114 | op->SetOutput("Out", params); |
| 3115 | op->SetAttr("file_path", {config_.params_file()}); |
| 3116 | op->CheckAttrs(); |
| 3117 | } |
| 3118 | |
| 3119 | // Use NaiveExecutor to Load parameters. |
| 3120 | framework::NaiveExecutor e(place_); |
| 3121 | e.Prepare(scope_.get(), *load_program, 0); |
| 3122 | e.Run(); |
| 3123 | VLOG(3) << "get " << scope_->LocalVarNames().size() << " vars after load"; |
| 3124 | |
| 3125 | return true; |
| 3126 | } |
| 3127 |
nothing calls this directly
no test coverage detected