From d45be4c1a64ea44965bf0eeeb465f1c3fcea7535 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Wed, 14 Oct 2020 08:16:44 +0000 Subject: [PATCH 1/3] fix xpu compile.. test=xpu --- paddle/fluid/inference/lite/CMakeLists.txt | 2 +- paddle/fluid/inference/tests/api/lite_resnet50_test.cc | 8 ++++++-- paddle/fluid/operators/metrics/accuracy_op_xpu.cc | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/inference/lite/CMakeLists.txt b/paddle/fluid/inference/lite/CMakeLists.txt index fd513b59588f82..924d273a9fccd8 100644 --- a/paddle/fluid/inference/lite/CMakeLists.txt +++ b/paddle/fluid/inference/lite/CMakeLists.txt @@ -4,6 +4,6 @@ endif() cc_library(lite_op_teller SRCS op_teller.cc DEPS lite_full_static framework_proto device_context boost xxhash) cc_library(lite_engine SRCS engine.cc DEPS lite_full_static framework_proto ${XPU_DEPS}) -cc_library(lite_tensor_utils SRCS tensor_utils.cc DEPS memcpy lite_full_static framework_proto boost device_context) +cc_library(lite_tensor_utils SRCS tensor_utils.cc DEPS memcpy lite_full_static framework_proto boost device_context ${XPU_DEPS}) cc_test(test_lite_engine SRCS test_engine.cc DEPS lite_engine protobuf framework_proto glog gtest analysis) cc_test(test_lite_tensor_utils SRCS test_tensor_utils.cc DEPS lite_engine lite_tensor_utils) diff --git a/paddle/fluid/inference/tests/api/lite_resnet50_test.cc b/paddle/fluid/inference/tests/api/lite_resnet50_test.cc index d68065553a9cff..a2a374e22d4d50 100644 --- a/paddle/fluid/inference/tests/api/lite_resnet50_test.cc +++ b/paddle/fluid/inference/tests/api/lite_resnet50_test.cc @@ -26,7 +26,11 @@ namespace inference { TEST(AnalysisPredictor, use_gpu) { std::string model_dir = FLAGS_infer_model + "/" + "model"; AnalysisConfig config; +#if defined(PADDLE_WITH_CUDA) config.EnableUseGpu(100, 0); +#elif defined(PADDLE_WITH_XPU) + config.EnableXpu(100); +#endif config.SetModel(model_dir + "/model", model_dir + "/params"); config.EnableLiteEngine(paddle::AnalysisConfig::Precision::kFloat32, true); @@ -40,7 +44,7 @@ TEST(AnalysisPredictor, use_gpu) { std::vector input(input_num, 1); PaddleTensor in; - in.shape = {1, 3, 318, 318}; + in.shape = {batch, channel, height, width}; in.data = PaddleBuf(static_cast(input.data()), input_num * sizeof(float)); in.dtype = PaddleDType::FLOAT32; @@ -92,7 +96,7 @@ TEST(Predictor, use_gpu) { auto input_names = predictor->GetInputNames(); auto input_t = predictor->GetInputHandle(input_names[0]); - input_t->Reshape({1, 3, 318, 318}); + input_t->Reshape({batch, channel, height, width}); input_t->CopyFromCpu(input.data()); predictor->Run(); diff --git a/paddle/fluid/operators/metrics/accuracy_op_xpu.cc b/paddle/fluid/operators/metrics/accuracy_op_xpu.cc index 294539b696f632..d73e46df3491be 100644 --- a/paddle/fluid/operators/metrics/accuracy_op_xpu.cc +++ b/paddle/fluid/operators/metrics/accuracy_op_xpu.cc @@ -81,9 +81,9 @@ class AccuracyXPUKernel : public framework::OpKernel { memory::Copy(platform::CPUPlace(), label_int64_host, BOOST_GET_CONST(platform::XPUPlace, ctx.GetPlace()), label_data, label_int64_size); - for (int i = 0; i < num_samples; ++i) { + for (size_t i = 0; i < num_samples; ++i) { label_int32_host[i] = label_int64_host[i]; - for (int j = 0; j < class_dim; ++j) { + for (size_t j = 0; j < class_dim; ++j) { indices_int32_host[i * class_dim + j] = indices_int64_host[i * class_dim + j]; } From a6df5b269fd322942f05d3df9ab35cb646043844 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Wed, 21 Oct 2020 07:17:02 +0000 Subject: [PATCH 2/3] update. test=kunlun --- cmake/external/lite.cmake | 2 +- paddle/fluid/inference/api/analysis_config.cc | 2 +- paddle/fluid/inference/lite/engine.cc | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmake/external/lite.cmake b/cmake/external/lite.cmake index 1da47bba7b6a53..9781d339666794 100644 --- a/cmake/external/lite.cmake +++ b/cmake/external/lite.cmake @@ -22,7 +22,7 @@ if(XPU_SDK_ROOT) set(LITE_WITH_XPU ON) include_directories("${XPU_SDK_ROOT}/XTDK/include") include_directories("${XPU_SDK_ROOT}/XTCL/include") - add_definitions(-DPADDLE_WITH_XPU) + add_definitions(-DLITE_SUBGRAPH_WITH_XPU) LINK_DIRECTORIES("${XPU_SDK_ROOT}/XTDK/shlib/") LINK_DIRECTORIES("${XPU_SDK_ROOT}/XTDK/runtime/shlib/") endif() diff --git a/paddle/fluid/inference/api/analysis_config.cc b/paddle/fluid/inference/api/analysis_config.cc index 6965a0c9041058..009ebd520c2b66 100644 --- a/paddle/fluid/inference/api/analysis_config.cc +++ b/paddle/fluid/inference/api/analysis_config.cc @@ -381,7 +381,7 @@ void AnalysisConfig::Update() { } if (use_xpu_) { -#ifndef PADDLE_WITH_XPU +#ifndef LITE_SUBGRAPH_WITH_XPU PADDLE_THROW(platform::errors::Unavailable( "You tried to use an XPU device, but Paddle was not compiled " "with XPU-runtime.")); diff --git a/paddle/fluid/inference/lite/engine.cc b/paddle/fluid/inference/lite/engine.cc index 5f24ef00bce59e..b8f6104780f1ee 100644 --- a/paddle/fluid/inference/lite/engine.cc +++ b/paddle/fluid/inference/lite/engine.cc @@ -16,7 +16,7 @@ #define LITE_WITH_CUDA 1 #endif -#ifdef PADDLE_WITH_XPU +#ifdef LITE_SUBGRAPH_WITH_XPU #define LITE_WITH_XPU 1 #endif @@ -59,7 +59,7 @@ paddle::lite_api::PaddlePredictor* EngineManager::Create( cfg.cpu_math_library_num_threads); #endif -#ifdef PADDLE_WITH_XPU +#ifdef LITE_SUBGRAPH_WITH_XPU lite_cxx_config.set_xpu_workspace_l3_size_per_thread( cfg.xpu_l3_workspace_size); #endif From 1f58a3ad5095146ac4a5b3f4babcae77b3b86049 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Wed, 21 Oct 2020 07:30:15 +0000 Subject: [PATCH 3/3] update. test=kunlun --- paddle/fluid/inference/tests/api/lite_resnet50_test.cc | 2 +- paddle/fluid/pybind/inference_api.cc | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/inference/tests/api/lite_resnet50_test.cc b/paddle/fluid/inference/tests/api/lite_resnet50_test.cc index a2a374e22d4d50..b88f09ae6a6a86 100644 --- a/paddle/fluid/inference/tests/api/lite_resnet50_test.cc +++ b/paddle/fluid/inference/tests/api/lite_resnet50_test.cc @@ -28,7 +28,7 @@ TEST(AnalysisPredictor, use_gpu) { AnalysisConfig config; #if defined(PADDLE_WITH_CUDA) config.EnableUseGpu(100, 0); -#elif defined(PADDLE_WITH_XPU) +#elif defined(LITE_SUBGRAPH_WITH_XPU) config.EnableXpu(100); #endif config.SetModel(model_dir + "/model", model_dir + "/params"); diff --git a/paddle/fluid/pybind/inference_api.cc b/paddle/fluid/pybind/inference_api.cc index ac615a2320daa0..e503ca31cdd744 100644 --- a/paddle/fluid/pybind/inference_api.cc +++ b/paddle/fluid/pybind/inference_api.cc @@ -443,6 +443,8 @@ void BindAnalysisConfig(py::module *m) { .def("params_file", &AnalysisConfig::params_file) .def("enable_use_gpu", &AnalysisConfig::EnableUseGpu, py::arg("memory_pool_init_size_mb"), py::arg("device_id") = 0) + .def("enable_xpu", &AnalysisConfig::EnableXpu, + py::arg("l3_workspace_size")) .def("disable_gpu", &AnalysisConfig::DisableGpu) .def("use_gpu", &AnalysisConfig::use_gpu) .def("gpu_device_id", &AnalysisConfig::gpu_device_id)