From 7f87747cf1e45f37245e1c8caa345f221758bea9 Mon Sep 17 00:00:00 2001 From: huzhiqiang <912790387@qq.com> Date: Thu, 23 Apr 2020 16:45:05 +0800 Subject: [PATCH] [Python opt][Usability] modify functions of python opt (#3465) --- lite/api/opt.cc | 22 +++---- lite/api/opt_base.cc | 103 ++++++++++++++++++++++--------- lite/api/opt_base.h | 17 +++-- lite/api/python/pybind/pybind.cc | 4 +- 4 files changed, 98 insertions(+), 48 deletions(-) diff --git a/lite/api/opt.cc b/lite/api/opt.cc index a6ad7cff6f..a1b963ac4e 100644 --- a/lite/api/opt.cc +++ b/lite/api/opt.cc @@ -55,7 +55,7 @@ DEFINE_string(model_file, "", "model file path of the combined-param model"); DEFINE_string(param_file, "", "param file path of the combined-param model"); DEFINE_string( optimize_out_type, - "protobuf", + "naive_buffer", "store type of the output optimized model. protobuf/naive_buffer"); DEFINE_bool(display_kernels, false, "Display kernel information"); DEFINE_bool(record_tailoring_info, @@ -207,7 +207,7 @@ void PrintOpsInfo(std::set valid_ops = {}) { } std::cout << std::setiosflags(std::ios::internal); std::cout << std::setw(maximum_optype_length) << "OP_name"; - for (int i = 0; i < targets.size(); i++) { + for (size_t i = 0; i < targets.size(); i++) { std::cout << std::setw(10) << targets[i].substr(1); } std::cout << std::endl; @@ -215,7 +215,7 @@ void PrintOpsInfo(std::set valid_ops = {}) { for (auto it = supported_ops.begin(); it != supported_ops.end(); it++) { std::cout << std::setw(maximum_optype_length) << it->first; auto ops_valid_places = it->second; - for (int i = 0; i < targets.size(); i++) { + for (size_t i = 0; i < targets.size(); i++) { if (std::find(ops_valid_places.begin(), ops_valid_places.end(), targets[i]) != ops_valid_places.end()) { @@ -235,7 +235,7 @@ void PrintOpsInfo(std::set valid_ops = {}) { } // Print OP info. auto ops_valid_places = supported_ops.at(*op); - for (int i = 0; i < targets.size(); i++) { + for (size_t i = 0; i < targets.size(); i++) { if (std::find(ops_valid_places.begin(), ops_valid_places.end(), targets[i]) != ops_valid_places.end()) { @@ -288,11 +288,11 @@ void ParseInputCommand() { auto valid_places = paddle::lite_api::ParserValidPlaces(); // get valid_targets string std::vector target_types = {}; - for (int i = 0; i < valid_places.size(); i++) { + for (size_t i = 0; i < valid_places.size(); i++) { target_types.push_back(valid_places[i].target); } std::string targets_str = TargetToStr(target_types[0]); - for (int i = 1; i < target_types.size(); i++) { + for (size_t i = 1; i < target_types.size(); i++) { targets_str = targets_str + TargetToStr(target_types[i]); } @@ -301,7 +301,7 @@ void ParseInputCommand() { target_types.push_back(TARGET(kUnk)); std::set valid_ops; - for (int i = 0; i < target_types.size(); i++) { + for (size_t i = 0; i < target_types.size(); i++) { auto ops = supported_ops_target[static_cast(target_types[i])]; valid_ops.insert(ops.begin(), ops.end()); } @@ -318,7 +318,7 @@ void CheckIfModelSupported() { auto valid_unktype_ops = supported_ops_target[static_cast(TARGET(kUnk))]; valid_ops.insert( valid_ops.end(), valid_unktype_ops.begin(), valid_unktype_ops.end()); - for (int i = 0; i < valid_places.size(); i++) { + for (size_t i = 0; i < valid_places.size(); i++) { auto target = valid_places[i].target; auto ops = supported_ops_target[static_cast(target)]; valid_ops.insert(valid_ops.end(), ops.begin(), ops.end()); @@ -340,7 +340,7 @@ void CheckIfModelSupported() { std::set unsupported_ops; std::set input_model_ops; - for (int index = 0; index < cpp_prog.BlocksSize(); index++) { + for (size_t index = 0; index < cpp_prog.BlocksSize(); index++) { auto current_block = cpp_prog.GetBlock(index); for (size_t i = 0; i < current_block->OpsSize(); ++i) { auto& op_desc = *current_block->GetOp(i); @@ -364,13 +364,13 @@ void CheckIfModelSupported() { unsupported_ops_str = unsupported_ops_str + ", " + *op_str; } std::vector targets = {}; - for (int i = 0; i < valid_places.size(); i++) { + for (size_t i = 0; i < valid_places.size(); i++) { targets.push_back(valid_places[i].target); } std::sort(targets.begin(), targets.end()); targets.erase(unique(targets.begin(), targets.end()), targets.end()); std::string targets_str = TargetToStr(targets[0]); - for (int i = 1; i < targets.size(); i++) { + for (size_t i = 1; i < targets.size(); i++) { targets_str = targets_str + "," + TargetToStr(targets[i]); } diff --git a/lite/api/opt_base.cc b/lite/api/opt_base.cc index 14c1ca4a4e..5af001961a 100644 --- a/lite/api/opt_base.cc +++ b/lite/api/opt_base.cc @@ -82,27 +82,56 @@ void OptBase::SetValidPlaces(const std::string& valid_places) { "command argument 'valid_targets'"; } -void OptBase::SetOptimizeOut(const std::string& optimized_out_path) { - optimize_out_path_ = optimized_out_path; +void OptBase::SetLiteOut(const std::string& lite_out_name) { + lite_out_name_ = lite_out_name; } -void OptBase::RunOptimize(bool record_strip_info) { +void OptBase::RecordModelInfo(bool record_strip_info) { + record_strip_info_ = record_strip_info; +} + +void OptBase::Run() { CheckIfModelSupported(false); OpKernelInfoCollector::Global().SetKernel2path(kernel2path_map); opt_config_.set_valid_places(valid_places_); if (model_set_dir_ != "") { - RunOptimizeFromModelSet(record_strip_info); + RunOptimizeFromModelSet(record_strip_info_); } else { auto opt_predictor = lite_api::CreatePaddlePredictor(opt_config_); opt_predictor->SaveOptimizedModel( - optimize_out_path_, model_type_, record_strip_info); + lite_out_name_, model_type_, record_strip_info_); auto resulted_model_name = - record_strip_info ? "information of striped model" : "optimized model"; + record_strip_info_ ? "information of striped model" : "optimized model"; std::cout << "Save the " << resulted_model_name - << " into :" << optimize_out_path_ << "successfully"; + << " into :" << lite_out_name_ << "successfully"; } } +void OptBase::RunOptimize(const std::string& model_dir_path, + const std::string& model_path, + const std::string& param_path, + const std::string& valid_places, + const std::string& optimized_out_path) { + SetModelDir(model_dir_path); + SetModelFile(model_path); + SetParamFile(param_path); + SetValidPlaces(valid_places); + SetLiteOut(optimized_out_path); + CheckIfModelSupported(false); + OpKernelInfoCollector::Global().SetKernel2path(kernel2path_map); + opt_config_.set_valid_places(valid_places_); + if (model_set_dir_ != "") { + RunOptimizeFromModelSet(record_strip_info_); + } else { + auto opt_predictor = lite_api::CreatePaddlePredictor(opt_config_); + opt_predictor->SaveOptimizedModel( + lite_out_name_, model_type_, record_strip_info_); + auto resulted_model_name = + record_strip_info_ ? "information of striped model" : "optimized model"; + std::cout << "Save the " << resulted_model_name + << " into :" << lite_out_name_ << "successfully"; + } +} // collect ops info of modelset void CollectModelMetaInfo(const std::string& output_dir, const std::vector& models, @@ -125,7 +154,7 @@ void OptBase::SetModelSetDir(const std::string& model_set_path) { } void OptBase::RunOptimizeFromModelSet(bool record_strip_info) { // 1. mkdir of outputed optimized model set. - lite::MkDirRecur(optimize_out_path_); + lite::MkDirRecur(lite_out_name_); auto model_dirs = lite::ListDir(model_set_dir_, true); if (model_dirs.size() == 0) { LOG(FATAL) << "[" << model_set_dir_ << "] does not contain any model"; @@ -138,7 +167,7 @@ void OptBase::RunOptimizeFromModelSet(bool record_strip_info) { std::string input_model_dir = lite::Join({model_set_dir_, name}, "/"); std::string output_model_dir = - lite::Join({optimize_out_path_, name}, "/"); + lite::Join({lite_out_name_, name}, "/"); if (opt_config_.model_file() != "" && opt_config_.param_file() != "") { auto model_file_path = @@ -155,7 +184,7 @@ void OptBase::RunOptimizeFromModelSet(bool record_strip_info) { auto opt_predictor = lite_api::CreatePaddlePredictor(opt_config_); opt_predictor->SaveOptimizedModel( - optimize_out_path_, model_type_, record_strip_info); + lite_out_name_, model_type_, record_strip_info); std::cout << "Optimize done. "; } @@ -164,46 +193,60 @@ void OptBase::RunOptimizeFromModelSet(bool record_strip_info) { if (record_strip_info) { // Collect all models information CollectModelMetaInfo( - optimize_out_path_, model_dirs, lite::TAILORD_OPS_SOURCE_LIST_FILENAME); + lite_out_name_, model_dirs, lite::TAILORD_OPS_SOURCE_LIST_FILENAME); + CollectModelMetaInfo( + lite_out_name_, model_dirs, lite::TAILORD_OPS_LIST_NAME); CollectModelMetaInfo( - optimize_out_path_, model_dirs, lite::TAILORD_OPS_LIST_NAME); - CollectModelMetaInfo(optimize_out_path_, - model_dirs, - lite::TAILORD_KERNELS_SOURCE_LIST_FILENAME); + lite_out_name_, model_dirs, lite::TAILORD_KERNELS_SOURCE_LIST_FILENAME); CollectModelMetaInfo( - optimize_out_path_, model_dirs, lite::TAILORD_KERNELS_LIST_NAME); + lite_out_name_, model_dirs, lite::TAILORD_KERNELS_LIST_NAME); std::cout << "Record the information of stripped models into :" - << optimize_out_path_ << "successfully"; + << lite_out_name_ << "successfully"; } } void OptBase::PrintHelpInfo() { const std::string opt_version = lite::version(); const char help_info[] = - "At least one argument should be inputed. Valid arguments are listed " - "below:\n" + "------------------------------------------------------------------------" + "-----------------------------------------------------------\n" + " Valid arguments of Paddle-Lite opt are listed below:\n" + "------------------------------------------------------------------------" + "-----------------------------------------------------------\n" " Arguments of help information:\n" " `help()` Print help infomation\n" - " Arguments of model optimization:\n" + "\n" + " Arguments of model transformation:\n" " `set_model_dir(model_dir)`\n" " `set_model_file(model_file_path)`\n" " `set_param_file(param_file_path)`\n" - " `set_model_type(protobuf|naive_buffer)`\n" - " `set_optimize_out(output_optimize_model_dir)`\n" + " `set_model_type(protobuf|naive_buffer)`: naive_buffer by " + "default\n" + " `set_lite_out(output_optimize_model_dir)`\n" " `set_valid_places(arm|opencl|x86|npu|xpu|rknpu|apu)`\n" - " `run_optimize(false|true)`\n" - " ` ----fasle&true refer to whether to record ops info for " - "tailoring lib, false by default`\n" - " Arguments of model checking and ops information:\n" + " `record_model_info(false|true)`: refer to whether to record ops " + "info for striping lib, false by default`\n" + " `run() : start model transformation`\n" + " eg. `opt.set_model_dir(\"./mobilenetv1\"); " + "opt.set_lite_out(\"mobilenetv1_opt\"); opt.set_valid_places(\"arm\"); " + "opt.run();`\n" + "\n" + " You can also transform model through a single input argument:\n" + " `run_optimize(model_dir, model_file_path, param_file_path, " + "model_type, valid_places, lite_out_name) `\n" + " eg. `opt.run_optimize(\"./mobilenetv1\", \"\", \"\", " + "\"naive_buffer\", \"arm\", \"mobilenetv1_opt\");`" + "\n" + " Arguments of checking model and printing ops information:\n" " `print_all_ops()` Display all the valid operators of " "Paddle-Lite\n" " `print_supported_ops` Display supported operators of valid " "places\n" " `check_if_model_supported()` Check if the input model is " - "supported\n"; - - std::cout << "opt version:" << opt_version << std::endl - << help_info << std::endl; + "supported\n" + "------------------------------------------------------------------------" + "-----------------------------------------------------------\n"; + std::cout << "opt version:" << opt_version << std::endl << help_info; } // 2. Print supported info of inputed ops void OptBase::PrintOpsInfo(const std::set& valid_ops) { diff --git a/lite/api/opt_base.h b/lite/api/opt_base.h index a8d6d0390c..3c0051375d 100644 --- a/lite/api/opt_base.h +++ b/lite/api/opt_base.h @@ -44,16 +44,21 @@ class LITE_API OptBase { public: OptBase() = default; void SetModelSetDir(const std::string &model_set_path); - void SetModelDir(const std::string &model_path); + void SetModelDir(const std::string &model_dir_path); void SetModelFile(const std::string &model_path); void SetParamFile(const std::string ¶m_path); void SetValidPlaces(const std::string &valid_places); - void SetOptimizeOut(const std::string &optimized_out_path); + void SetLiteOut(const std::string &lite_out_name); + void RecordModelInfo(bool record_strip_info = true); // set optimized_model type void SetModelType(std::string model_type); // transform and save the optimized model - void RunOptimize(bool record_strip_info = false); - + void Run(); + void RunOptimize(const std::string &model_dir_path = "", + const std::string &model_path = "", + const std::string ¶m_path = "", + const std::string &valid_places = "", + const std::string &optimized_out_path = ""); // fuctions of printing info // 1. help info void PrintHelpInfo(); @@ -71,12 +76,12 @@ class LITE_API OptBase { // valid places for the optimized_model std::vector valid_places_; // filename of the optimized_model - std::string optimize_out_path_; + std::string lite_out_name_; // type of the optimized_model, kNaiveBuffer default. LiteModelType model_type_{LiteModelType::kNaiveBuffer}; // Dir path of a set of models, this should be combined with model std::string model_set_dir_; - + bool record_strip_info_{false}; void RunOptimizeFromModelSet(bool record_strip_info = false); }; diff --git a/lite/api/python/pybind/pybind.cc b/lite/api/python/pybind/pybind.cc index 06d1c607fd..104275e2e9 100644 --- a/lite/api/python/pybind/pybind.cc +++ b/lite/api/python/pybind/pybind.cc @@ -62,8 +62,10 @@ void BindLiteOpt(py::module *m) { .def("set_model_file", &OptBase::SetModelFile) .def("set_param_file", &OptBase::SetParamFile) .def("set_valid_places", &OptBase::SetValidPlaces) - .def("set_optimize_out", &OptBase::SetOptimizeOut) + .def("set_lite_out", &OptBase::SetLiteOut) .def("set_model_type", &OptBase::SetModelType) + .def("record_model_info", &OptBase::RecordModelInfo) + .def("run", &OptBase::Run) .def("run_optimize", &OptBase::RunOptimize) .def("help", &OptBase::PrintHelpInfo) .def("print_supported_ops", &OptBase::PrintSupportedOps) -- GitLab