diff --git a/cmake/anakin_subgraph.cmake b/cmake/anakin_subgraph.cmake index eb7bce9f3b7a98cb5244ea8d1ce595d5d374cda0..177f34438d6ac8731390e44255072718039bf01c 100644 --- a/cmake/anakin_subgraph.cmake +++ b/cmake/anakin_subgraph.cmake @@ -26,6 +26,19 @@ if(ANAKIN_FOUND) add_definitions(-DPADDLE_WITH_ANAKIN) endif() +if(ANAKIN_FOUND) + if (ANAKIN_MLU AND NOT WITH_GPU AND NOT ANAKIN_X86) + message(STATUS "Compile with anakin mlu place.") + add_definitions(-DANAKIN_MLU_PLACE) + elseif(ANAKIN_BM AND NOT WITH_GPU AND NOT ANAKIN_X86) + message(STATUS "Compile with anakin bm place.") + add_definitions(-DANAKIN_BM_PLACE) + elseif(ANAKIN_X86) + message(STATUS "Compile with anakin x86 place.") + add_definitions(-DANAKIN_X86_PLACE) + endif() +endif() + if(ANAKIN_FOUND AND WITH_GPU AND WITH_DSO) message(STATUS "Compile with anakin subgraph.") set(ANAKIN_SUBGRAPH ON) diff --git a/paddle/fluid/inference/api/CMakeLists.txt b/paddle/fluid/inference/api/CMakeLists.txt index 1921e419383a7b3b657bd4b2ce1b04759d472c65..033224c0f59cbbcd0e8ef2d346ad0aaf4f1d3370 100644 --- a/paddle/fluid/inference/api/CMakeLists.txt +++ b/paddle/fluid/inference/api/CMakeLists.txt @@ -68,13 +68,8 @@ cc_test(test_analysis_predictor SRCS analysis_predictor_tester.cc DEPS analysis_ ARGS --dirname=${WORD2VEC_MODEL_DIR}) if(ANAKIN_FOUND) - if (ANAKIN_MLU AND NOT WITH_GPU AND NOT ANAKIN_X86) - message(STATUS "Compile with anakin mlu place.") - add_definitions(-DANAKIN_MLU_PLACE) - elseif(ANAKIN_X86) - message(STATUS "Compile with anakin x86 place.") - add_definitions(-DANAKIN_X86_PLACE) - endif() + # Do not turn warnings into errors. + set_source_files_properties(api.cc api_anakin_engine.cc PROPERTIES COMPILE_FLAGS "-Wno-error") cc_library(inference_anakin_api SRCS api.cc api_anakin_engine.cc) target_link_libraries(inference_anakin_api anakin anakin_saber_common) cc_library(inference_anakin_api_shared SHARED SRCS api.cc api_anakin_engine.cc) diff --git a/paddle/fluid/inference/api/api_anakin_engine.cc b/paddle/fluid/inference/api/api_anakin_engine.cc index 63d23321ab41eb9f26a4cd79f3ffa799e3795e72..3a7ad2d2db897de2376c8dfe8e6d76ce0bb00a97 100644 --- a/paddle/fluid/inference/api/api_anakin_engine.cc +++ b/paddle/fluid/inference/api/api_anakin_engine.cc @@ -34,10 +34,10 @@ extern std::once_flag PaddleInferenceAnakinPredictor::init_anakin_; template void PaddleInferenceAnakinPredictor::InitEnv() { - anakin::TargetWrapper::set_device(this->config_.device_id); std::call_once(this->init_anakin_, [this]() { anakin::Env::env_init(this->config_.max_stream); }); + anakin::TargetWrapper::set_device(this->config_.device_id); } template void PaddleInferenceAnakinPredictor::InitNet() { @@ -54,14 +54,19 @@ template void PaddleInferenceAnakinPredictor::InitGraph() { this->graph_p_ = std::make_shared>(); - if (!(this->graph_p_->load(this->config_.model_file))) { - LOG(FATAL) << "fail to load graph from " << this->config_.model_file; + if (!this->config_.model_file.empty()) { + this->graph_p_->load(this->config_.model_file); + } else if (this->config_.model_buf_p) { + this->graph_p_->load(this->config_.model_buf_p, + this->config_.model_buf_len); + } else { + LOG(FATAL) << "Model load error."; } auto inputs = this->graph_p_->get_ins(); for (auto &input_str : inputs) { if (this->config_.init_inputs_shape.find(input_str) == this->config_.init_inputs_shape.end()) { - LOG(FATAL) << input_str << " is not implemented."; + LOG(FATAL) << input_str << " should be set in init_inputs_shape."; } std::vector shape = this->config_.init_inputs_shape.find(input_str)->second; @@ -189,6 +194,7 @@ template bool PaddleInferenceAnakinPredictor::RunImpl( const std::vector &inputs, std::vector *output_data) { + anakin::TargetWrapper::set_device(this->config_.device_id); for (const auto &input : inputs) { if (input.dtype != PaddleDType::FLOAT32) { LOG(FATAL) << "Only support float type inputs. " << input.name @@ -321,6 +327,27 @@ void PaddleInferenceAnakinMLUPredictor::Predict() { } #endif +#ifdef ANAKIN_BM_PLACE +template +void PaddleInferenceAnakinBMPredictor::OptimizeGraph() { + if (!this->graph_p_->fusion_optimize()) { + LOG(FATAL) << "Graph optimization error."; + } +} +template +void PaddleInferenceAnakinBMPredictor::InitNet() { + std::unique_lock lock(this->mutex_); + this->executor_p_ = new anakin::Net(); + this->executor_p_->fusion_init(*this->graph_p_, this->ctx_p_, true); +} +template +void PaddleInferenceAnakinBMPredictor::Predict() { + anakin::TargetWrapper::device_sync(); + this->executor_p_->fusion_prediction(); + anakin::TargetWrapper::device_sync(); +} +#endif + #ifdef PADDLE_WITH_CUDA template class PaddleInferenceAnakinPredictor< anakin::NV, anakin::Precision::FP32, ::anakin::OpRunType::ASYNC>; @@ -333,6 +360,10 @@ template class PaddleInferenceAnakinPredictor< template class PaddleInferenceAnakinMLUPredictor; #endif +#ifdef ANAKIN_BM_PLACE +template class PaddleInferenceAnakinBMPredictor; +#endif // A factory to help create difference predictor. template <> @@ -361,7 +392,16 @@ CreatePaddlePredictor( config)); } #endif - LOG(FATAL) << "Anakin Predictor create on unknown platform."; +#ifdef ANAKIN_BM_PLACE + if (config.target_type == contrib::AnakinConfig::BM) { + return std::unique_ptr( + new PaddleInferenceAnakinBMPredictor( + config)); + } +#endif + LOG(FATAL) << "Anakin Predictor create on unknown platform: " + << config.target_type; return nullptr; } template diff --git a/paddle/fluid/inference/api/api_anakin_engine.h b/paddle/fluid/inference/api/api_anakin_engine.h index 0f0d7febe2ed7331830227c95fed31ac496fa129..17a4c9fa4f6a961db1bcc0a35cc12613f7a8be48 100644 --- a/paddle/fluid/inference/api/api_anakin_engine.h +++ b/paddle/fluid/inference/api/api_anakin_engine.h @@ -92,4 +92,19 @@ class PaddleInferenceAnakinMLUPredictor final void Predict() override; }; #endif + +#ifdef ANAKIN_BM_PLACE +template +class PaddleInferenceAnakinBMPredictor final + : public PaddleInferenceAnakinPredictor { + public: + explicit PaddleInferenceAnakinBMPredictor(const AnakinConfig& config) { + this->ResetConfig(config); + this->InitPredictor(); + } + void OptimizeGraph() override; + void InitNet() override; + void Predict() override; +}; +#endif } // namespace paddle diff --git a/paddle/fluid/inference/api/paddle_anakin_config.h b/paddle/fluid/inference/api/paddle_anakin_config.h index 7c0e2f06ff417d22b2adfec3387e9bcbc7e5e81e..e9af13f526a2341a6e02c6979493e995c72dfa98 100644 --- a/paddle/fluid/inference/api/paddle_anakin_config.h +++ b/paddle/fluid/inference/api/paddle_anakin_config.h @@ -25,7 +25,7 @@ namespace paddle { namespace contrib { // Configurations for Anakin engine. struct AnakinConfig : public PaddlePredictor::Config { - enum TargetType { NVGPU = 0, X86, MLU }; + enum TargetType { NVGPU = 0, X86, MLU, BM }; int device_id{0}; std::string model_file; std::map> init_inputs_shape; @@ -34,6 +34,8 @@ struct AnakinConfig : public PaddlePredictor::Config { int max_stream{4}; int data_stream_id{0}; int compute_stream_id{0}; + char* model_buf_p{nullptr}; + size_t model_buf_len{0}; TargetType target_type; #ifdef ANAKIN_MLU_PLACE int model_parallel{8};