From 6aba3b8b0f8cfd30bbcb3fa62808ceb87f544d6e Mon Sep 17 00:00:00 2001 From: huzhiqiang <912790387@qq.com> Date: Fri, 11 Oct 2019 10:49:53 +0800 Subject: [PATCH] move the method of SetThread and SetPowerMode from MobileConfig into ConfigBase (#2147) * move the method of SetThread and SetPowerMode from MobileConfig into ConfigBase * cxxPredictor will support SetThread and SetPowerMode method --- lite/api/CMakeLists.txt | 2 +- lite/api/light_api_impl.cc | 24 ------------------------ lite/api/paddle_api.cc | 26 ++++++++++++++++++++++++++ lite/api/paddle_api.h | 18 ++++++++++-------- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/lite/api/CMakeLists.txt b/lite/api/CMakeLists.txt index 173de922f9..5d9787f4cb 100644 --- a/lite/api/CMakeLists.txt +++ b/lite/api/CMakeLists.txt @@ -176,7 +176,7 @@ if(LITE_WITH_LIGHT_WEIGHT_FRAMEWORK AND WITH_TESTING) # FPGA_DEPS ${fpga_kernels}) endif() -lite_cc_library(paddle_api SRCS paddle_api.cc DEPS op_params tensor) +lite_cc_library(paddle_api SRCS paddle_api.cc DEPS op_params tensor device_info) #----------------------------------------------------------------------------------------------------- # The final inference library for both CxxConfig and MobileConfig. diff --git a/lite/api/light_api_impl.cc b/lite/api/light_api_impl.cc index 5aa9bfb085..4dedc5332d 100644 --- a/lite/api/light_api_impl.cc +++ b/lite/api/light_api_impl.cc @@ -77,29 +77,5 @@ std::shared_ptr CreatePaddlePredictor( return x; } -MobileConfig::MobileConfig(PowerMode mode, int threads) { -#ifdef LITE_WITH_ARM - lite::DeviceInfo::Global().SetRunMode(mode, threads); - mode_ = lite::DeviceInfo::Global().mode(); - threads_ = lite::DeviceInfo::Global().threads(); -#endif -} - -void MobileConfig::set_power_mode(paddle::lite_api::PowerMode mode) { -#ifdef LITE_WITH_ARM - lite::DeviceInfo::Global().SetRunMode(mode, threads_); - mode_ = lite::DeviceInfo::Global().mode(); - threads_ = lite::DeviceInfo::Global().threads(); -#endif -} - -void MobileConfig::set_threads(int threads) { -#ifdef LITE_WITH_ARM - lite::DeviceInfo::Global().SetRunMode(mode_, threads); - mode_ = lite::DeviceInfo::Global().mode(); - threads_ = lite::DeviceInfo::Global().threads(); -#endif -} - } // namespace lite_api } // namespace paddle diff --git a/lite/api/paddle_api.cc b/lite/api/paddle_api.cc index f2841cee0f..954d687a48 100644 --- a/lite/api/paddle_api.cc +++ b/lite/api/paddle_api.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "lite/api/paddle_api.h" +#include "lite/core/device_info.h" #include "lite/core/tensor.h" namespace paddle { @@ -73,5 +74,30 @@ std::shared_ptr CreatePaddlePredictor(const ConfigT &) { return std::shared_ptr(); } +ConfigBase::ConfigBase(PowerMode mode, int threads) { +#ifdef LITE_WITH_ARM + lite::DeviceInfo::Init(); + lite::DeviceInfo::Global().SetRunMode(mode, threads); + mode_ = lite::DeviceInfo::Global().mode(); + threads_ = lite::DeviceInfo::Global().threads(); +#endif +} + +void ConfigBase::set_power_mode(paddle::lite_api::PowerMode mode) { +#ifdef LITE_WITH_ARM + lite::DeviceInfo::Global().SetRunMode(mode, threads_); + mode_ = lite::DeviceInfo::Global().mode(); + threads_ = lite::DeviceInfo::Global().threads(); +#endif +} + +void ConfigBase::set_threads(int threads) { +#ifdef LITE_WITH_ARM + lite::DeviceInfo::Global().SetRunMode(mode_, threads); + mode_ = lite::DeviceInfo::Global().mode(); + threads_ = lite::DeviceInfo::Global().threads(); +#endif +} + } // namespace lite_api } // namespace paddle diff --git a/lite/api/paddle_api.h b/lite/api/paddle_api.h index 36a6a16ee5..e4da4af4a9 100644 --- a/lite/api/paddle_api.h +++ b/lite/api/paddle_api.h @@ -90,11 +90,20 @@ class LITE_API PaddlePredictor { /// Base class for all the configs. class LITE_API ConfigBase { std::string model_dir_; + int threads_{1}; + PowerMode mode_{LITE_POWER_NO_BIND}; public: + explicit ConfigBase(PowerMode mode = LITE_POWER_NO_BIND, int threads = 1); + // set Model_dir void set_model_dir(const std::string& x) { model_dir_ = x; } - const std::string& model_dir() const { return model_dir_; } + // set Power_mode + void set_power_mode(PowerMode mode); + PowerMode power_mode() const { return mode_; } + // set Thread + void set_threads(int threads); + int threads() const { return threads_; } }; /// CxxConfig is the config for the Full feature predictor. @@ -129,16 +138,11 @@ class LITE_API CxxConfig : public ConfigBase { /// MobileConfig is the config for the light weight predictor, it will skip /// IR optimization or other unnecessary stages. class LITE_API MobileConfig : public ConfigBase { - PowerMode mode_{LITE_POWER_HIGH}; - int threads_{1}; std::string model_buffer_; std::string param_buffer_; bool model_from_memory_{false}; public: - explicit MobileConfig(PowerMode mode = LITE_POWER_NO_BIND, int threads = 1); - void set_power_mode(PowerMode mode); - void set_threads(int threads); void set_model_buffer(const char* model_buffer, size_t model_buffer_size, const char* param_buffer, @@ -148,8 +152,6 @@ class LITE_API MobileConfig : public ConfigBase { model_from_memory_ = true; } - PowerMode power_mode() const { return mode_; } - int threads() const { return threads_; } bool model_from_memory() const { return model_from_memory_; } const std::string& model_buffer() const { return model_buffer_; } const std::string& param_buffer() const { return param_buffer_; } -- GitLab