From 743cb840f1235911d163eb4927161cbfc0e803b5 Mon Sep 17 00:00:00 2001 From: Tao Luo Date: Thu, 6 Dec 2018 21:43:05 +0800 Subject: [PATCH] update with comments test=develop --- .../fluid/framework/executor_thread_worker.cc | 2 +- paddle/fluid/inference/analysis/argument.h | 2 +- .../analysis/passes/ir_graph_build_pass.cc | 10 +++-- .../analysis/passes/ir_graph_build_pass.h | 2 +- paddle/fluid/inference/api/analysis_config.cc | 13 ++++--- .../fluid/inference/api/analysis_predictor.cc | 7 ++-- .../inference/api/paddle_analysis_config.h | 10 ++--- paddle/fluid/inference/io.cc | 39 ++++++++++--------- paddle/fluid/inference/io.h | 11 +++--- .../tests/api/analyzer_ner_tester.cc | 4 +- .../inference/tests/api/config_printer.h | 2 +- paddle/fluid/operators/load_combine_op.cc | 6 +-- 12 files changed, 56 insertions(+), 52 deletions(-) diff --git a/paddle/fluid/framework/executor_thread_worker.cc b/paddle/fluid/framework/executor_thread_worker.cc index e3849931d4..3d53511615 100644 --- a/paddle/fluid/framework/executor_thread_worker.cc +++ b/paddle/fluid/framework/executor_thread_worker.cc @@ -97,7 +97,7 @@ void ExecutorThreadWorker::SetDevice() { static unsigned concurrency_cap = std::thread::hardware_concurrency(); int thread_id = this->thread_id_; - if ((unsigned)thread_id < concurrency_cap) { + if (static_cast(thread_id) < concurrency_cap) { unsigned proc = thread_id; cpu_set_t mask; diff --git a/paddle/fluid/inference/analysis/argument.h b/paddle/fluid/inference/analysis/argument.h index d205465abf..53cc7039f2 100644 --- a/paddle/fluid/inference/analysis/argument.h +++ b/paddle/fluid/inference/analysis/argument.h @@ -103,7 +103,7 @@ struct Argument { // Model specified with program and parameters files. DECL_ARGUMENT_FIELD(model_program_path, ModelProgramPath, std::string); DECL_ARGUMENT_FIELD(model_params_path, ModelParamsPath, std::string); - DECL_ARGUMENT_FIELD(is_memory_load, IsMemoryLoad, bool); + DECL_ARGUMENT_FIELD(model_from_memory, ModelFromMemory, bool); // The overall graph to work on. DECL_ARGUMENT_UNIQUE_FIELD(main_graph, MainGraph, framework::ir::Graph); diff --git a/paddle/fluid/inference/analysis/passes/ir_graph_build_pass.cc b/paddle/fluid/inference/analysis/passes/ir_graph_build_pass.cc index 31138d7342..b8a045c18f 100644 --- a/paddle/fluid/inference/analysis/passes/ir_graph_build_pass.cc +++ b/paddle/fluid/inference/analysis/passes/ir_graph_build_pass.cc @@ -46,7 +46,7 @@ void IrGraphBuildPass::RunImpl(Argument *argument) { argument->model_params_path_valid()) { auto program = LoadModel(argument->model_program_path(), argument->model_params_path(), - argument->scope_ptr(), place, argument->is_memory_load()); + argument->scope_ptr(), place, argument->model_from_memory()); argument->SetMainProgram(program.release()); } else { PADDLE_THROW( @@ -69,9 +69,13 @@ std::unique_ptr IrGraphBuildPass::LoadModel( std::unique_ptr IrGraphBuildPass::LoadModel( const std::string &program_path, const std::string ¶ms_path, framework::Scope *scope, const platform::Place &place, - bool is_memory_load) { + bool model_from_memory) { framework::Executor exe(place); - return Load(&exe, scope, program_path, params_path, is_memory_load); + if (!model_from_memory) { + return Load(&exe, scope, program_path, params_path); + } else { + return LoadFromMemory(&exe, scope, program_path, params_path); + } } std::string IrGraphBuildPass::repr() const { return "ir-graph-build-pass"; } diff --git a/paddle/fluid/inference/analysis/passes/ir_graph_build_pass.h b/paddle/fluid/inference/analysis/passes/ir_graph_build_pass.h index ae7de676d6..adbde0433f 100644 --- a/paddle/fluid/inference/analysis/passes/ir_graph_build_pass.h +++ b/paddle/fluid/inference/analysis/passes/ir_graph_build_pass.h @@ -39,7 +39,7 @@ class IrGraphBuildPass : public AnalysisPass { std::unique_ptr LoadModel( const std::string &program_path, const std::string ¶ms_path, framework::Scope *scope, const platform::Place &place, - bool is_memory_load); + bool model_from_memory); std::string model_binary_str_; }; diff --git a/paddle/fluid/inference/api/analysis_config.cc b/paddle/fluid/inference/api/analysis_config.cc index bfa1f1908c..384d1dc27d 100644 --- a/paddle/fluid/inference/api/analysis_config.cc +++ b/paddle/fluid/inference/api/analysis_config.cc @@ -53,7 +53,7 @@ contrib::AnalysisConfig::AnalysisConfig(const contrib::AnalysisConfig &other) { use_tensorrt_ = other.use_tensorrt_; tensorrt_max_batchsize_ = other.tensorrt_max_batchsize_; tensorrt_workspace_size_ = other.tensorrt_workspace_size_; - is_memory_load_ = other.is_memory_load_; + model_from_memory_ = other.model_from_memory_; if (use_gpu) { pass_builder_.reset(new GpuPassStrategy( @@ -81,7 +81,7 @@ contrib::AnalysisConfig::AnalysisConfig(contrib::AnalysisConfig &&other) { use_tensorrt_ = other.use_tensorrt_; tensorrt_max_batchsize_ = other.tensorrt_max_batchsize_; tensorrt_workspace_size_ = other.tensorrt_workspace_size_; - is_memory_load_ = other.is_memory_load_; + model_from_memory_ = other.model_from_memory_; pass_builder_ = std::move(other.pass_builder_); } @@ -105,12 +105,13 @@ void contrib::AnalysisConfig::EnableTensorRtEngine(int workspace_size, pass_builder()->InsertPass(1, "tensorrt_subgraph_pass"); } -void contrib::AnalysisConfig::SetProgBufferAndParamBuffer( - const char *prog_buffer, size_t prog_buffer_size, const char *param_buffer, - size_t param_buffer_size) { +void contrib::AnalysisConfig::SetModelBuffer(const char *prog_buffer, + size_t prog_buffer_size, + const char *param_buffer, + size_t param_buffer_size) { prog_file = std::string(prog_buffer, prog_buffer + prog_buffer_size); param_file = std::string(param_buffer, param_buffer + param_buffer_size); - is_memory_load_ = true; + model_from_memory_ = true; } } // namespace paddle diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index f70c12dc87..84f7eca057 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -308,7 +308,7 @@ void AnalysisPredictor::OptimizeInferenceProgram() { argument_.SetUseGPU(config_.use_gpu); argument_.SetGPUDeviceId(config_.device); - argument_.SetIsMemoryLoad(config_.is_memory_load_); + argument_.SetModelFromMemory(config_.model_from_memory_); // Analyze inference_program if (!config_.model_dir.empty()) { argument_.SetModelDir(config_.model_dir); @@ -451,11 +451,12 @@ bool AnalysisPredictor::LoadProgramDesc() { // Create ProgramDesc framework::proto::ProgramDesc proto; - if (!config_.is_memory_load()) { + if (!config_.model_from_memory()) { std::string pb_content; // Read binary std::ifstream fin(filename, std::ios::in | std::ios::binary); - PADDLE_ENFORCE(static_cast(fin), "Cannot open file %s", filename); + PADDLE_ENFORCE(static_cast(fin.is_open()), "Cannot open file %s", + filename); fin.seekg(0, std::ios::end); pb_content.resize(fin.tellg()); fin.seekg(0, std::ios::beg); diff --git a/paddle/fluid/inference/api/paddle_analysis_config.h b/paddle/fluid/inference/api/paddle_analysis_config.h index 4e9d5bc329..a08e3d027e 100644 --- a/paddle/fluid/inference/api/paddle_analysis_config.h +++ b/paddle/fluid/inference/api/paddle_analysis_config.h @@ -55,11 +55,9 @@ struct AnalysisConfig : public NativeConfig { bool use_mkldnn() const { return use_mkldnn_; } // Specify the memory buffer of program and parameter - void SetProgBufferAndParamBuffer(const char* prog_buffer, - size_t prog_buffer_size, - const char* program_buffer, - size_t program_buffer_size); - bool is_memory_load() const { return is_memory_load_; } + void SetModelBuffer(const char* prog_buffer, size_t prog_buffer_size, + const char* program_buffer, size_t program_buffer_size); + bool model_from_memory() const { return model_from_memory_; } friend class ::paddle::AnalysisPredictor; @@ -69,7 +67,7 @@ struct AnalysisConfig : public NativeConfig { int tensorrt_workspace_size_; int tensorrt_max_batchsize_; std::unique_ptr pass_builder_; - bool is_memory_load_{false}; + bool model_from_memory_{false}; }; // Configurations for Anakin engine. diff --git a/paddle/fluid/inference/io.cc b/paddle/fluid/inference/io.cc index 060d6a89d4..24d15f12f9 100644 --- a/paddle/fluid/inference/io.cc +++ b/paddle/fluid/inference/io.cc @@ -70,7 +70,7 @@ void LoadPersistables(framework::Executor* executor, framework::Scope* scope, const framework::ProgramDesc& main_program, const std::string& dirname, const std::string& param_filename, - bool is_memory_load = false) { + bool model_from_memory = false) { const framework::BlockDesc& global_block = main_program.Block(0); framework::ProgramDesc* load_program = new framework::ProgramDesc(); @@ -109,7 +109,7 @@ void LoadPersistables(framework::Executor* executor, framework::Scope* scope, op->SetType("load_combine"); op->SetOutput("Out", paramlist); op->SetAttr("file_path", {param_filename}); - op->SetAttr("is_memory_load", {is_memory_load}); + op->SetAttr("model_from_memory", {model_from_memory}); op->CheckAttrs(); } @@ -132,23 +132,17 @@ std::unique_ptr Load(framework::Executor* executor, "model version %ld is not supported.", main_program->Version()); - // is_memory_load is false in seperate parameters. + // model_from_memory is false in seperate parameters. LoadPersistables(executor, scope, *main_program, dirname, "", - false /* is_memory_load */); + false /* model_from_memory */); return main_program; } -std::unique_ptr Load(framework::Executor* executor, - framework::Scope* scope, - const std::string& prog_filename, - const std::string& param_filename, - bool is_memory_load = false) { +std::unique_ptr Load( + framework::Executor* executor, framework::Scope* scope, + const std::string& prog_filename, const std::string& param_filename) { std::string program_desc_str; - if (!is_memory_load) { - ReadBinaryFile(prog_filename, &program_desc_str); - } else { - program_desc_str = prog_filename; - } + ReadBinaryFile(prog_filename, &program_desc_str); std::unique_ptr main_program( new framework::ProgramDesc(program_desc_str)); @@ -157,15 +151,22 @@ std::unique_ptr Load(framework::Executor* executor, main_program->Version()); LoadPersistables(executor, scope, *main_program, "", param_filename, - is_memory_load); + false /* model_from_memory */); return main_program; } -std::unique_ptr Load( +std::unique_ptr LoadFromMemory( framework::Executor* executor, framework::Scope* scope, - const std::string& prog_filename, const std::string& param_filename) { - return Load(executor, scope, prog_filename, param_filename, - false /* is_memory_load */); + const std::string& prog_buffer, const std::string& param_buffer) { + std::unique_ptr main_program( + new framework::ProgramDesc(prog_buffer)); + PADDLE_ENFORCE(framework::IsProgramVersionSupported(main_program->Version()), + "model version %ld is not supported.", + main_program->Version()); + + LoadPersistables(executor, scope, *main_program, "", param_buffer, + true /* model_filename */); + return main_program; } void SaveVars(const framework::Scope& scope, diff --git a/paddle/fluid/inference/io.h b/paddle/fluid/inference/io.h index 20d635575d..317ef9d93a 100644 --- a/paddle/fluid/inference/io.h +++ b/paddle/fluid/inference/io.h @@ -30,7 +30,8 @@ void Init(const std::vector argv); void LoadPersistables(framework::Executor* executor, framework::Scope* scope, const framework::ProgramDesc& main_program, const std::string& dirname, - const std::string& param_filename, bool is_memory_load); + const std::string& param_filename, + bool model_from_memory); std::unique_ptr Load(framework::Executor* executor, framework::Scope* scope, @@ -41,11 +42,9 @@ std::unique_ptr Load(framework::Executor* executor, const std::string& prog_filename, const std::string& param_filename); -std::unique_ptr Load(framework::Executor* executor, - framework::Scope* scope, - const std::string& prog_filename, - const std::string& param_filename, - bool is_memory_load); +std::unique_ptr LoadFromMemory( + framework::Executor* executor, framework::Scope* scope, + const std::string& prog_buffer, const std::string& param_buffer); // Save the variables from a scope to disk. void SaveVars(const framework::Scope& scope, diff --git a/paddle/fluid/inference/tests/api/analyzer_ner_tester.cc b/paddle/fluid/inference/tests/api/analyzer_ner_tester.cc index aaa94c8937..66d85420c5 100644 --- a/paddle/fluid/inference/tests/api/analyzer_ner_tester.cc +++ b/paddle/fluid/inference/tests/api/analyzer_ner_tester.cc @@ -98,8 +98,8 @@ void SetConfig(contrib::AnalysisConfig *cfg, bool memory_load = false) { std::string buffer_prog, buffer_param; ReadBinaryFile(FLAGS_infer_model + "/__model__", &buffer_prog); ReadBinaryFile(FLAGS_infer_model + "/param", &buffer_param); - cfg->SetProgBufferAndParamBuffer(&buffer_prog[0], buffer_prog.size(), - &buffer_param[0], buffer_param.size()); + cfg->SetModelBuffer(&buffer_prog[0], buffer_prog.size(), &buffer_param[0], + buffer_param.size()); } else { cfg->prog_file = FLAGS_infer_model + "/__model__"; cfg->param_file = FLAGS_infer_model + "/param"; diff --git a/paddle/fluid/inference/tests/api/config_printer.h b/paddle/fluid/inference/tests/api/config_printer.h index c07a27674e..7046bce303 100644 --- a/paddle/fluid/inference/tests/api/config_printer.h +++ b/paddle/fluid/inference/tests/api/config_printer.h @@ -63,7 +63,7 @@ std::ostream &operator<<(std::ostream &os, os << GenSpaces(num_spaces) << "contrib::AnalysisConfig {\n"; num_spaces++; os << *reinterpret_cast(&config); - if (!config.is_memory_load()) { + if (!config.model_from_memory()) { os << GenSpaces(num_spaces) << "prog_file: " << config.prog_file << "\n"; os << GenSpaces(num_spaces) << "param_file: " << config.param_file << "\n"; } else { diff --git a/paddle/fluid/operators/load_combine_op.cc b/paddle/fluid/operators/load_combine_op.cc index 8cb7c6c9c6..9d1423915a 100644 --- a/paddle/fluid/operators/load_combine_op.cc +++ b/paddle/fluid/operators/load_combine_op.cc @@ -32,12 +32,12 @@ class LoadCombineOp : public framework::OperatorBase { const platform::Place &place) const override { auto filename = Attr("file_path"); auto load_as_fp16 = Attr("load_as_fp16"); - auto is_memory_load = Attr("is_memory_load"); + auto model_from_memory = Attr("model_from_memory"); auto out_var_names = Outputs("Out"); PADDLE_ENFORCE_GT( static_cast(out_var_names.size()), 0, "The number of output variables should be greater than 0."); - if (!is_memory_load) { + if (!model_from_memory) { std::ifstream fin(filename); PADDLE_ENFORCE(static_cast(fin), "Cannot open file %s for load_combine op", filename); @@ -112,7 +112,7 @@ class LoadCombineOpProtoMaker : public framework::OpProtoAndCheckerMaker { "LoDTensors will be loaded from \"file_path\".") .AddCustomChecker( [](const std::string &path) { return !path.empty(); }); - AddAttr("is_memory_load", + AddAttr("model_from_memory", "(boolean, default false)" "If true, file_path is in memory, and LoDTensors will be " "loaded directly from memory") -- GitLab