From dea3795930ef61a60d6fd00277b59f9bdff42014 Mon Sep 17 00:00:00 2001 From: DefTruth <31974251+DefTruth@users.noreply.github.com> Date: Sun, 25 Jun 2023 19:27:36 +0800 Subject: [PATCH] [Backend] Add backward compatible for paddle inference 2.4.x (#2062) * [Backend] Add backward compatiable for paddle infernence 2.4.x * [Backend] Add backward compatiable for paddle infernence 2.4.x --- cmake/paddle_inference.cmake | 16 ++++++++ .../runtime/backends/paddle/paddle_backend.cc | 37 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/cmake/paddle_inference.cmake b/cmake/paddle_inference.cmake index bf79ad820..637256971 100755 --- a/cmake/paddle_inference.cmake +++ b/cmake/paddle_inference.cmake @@ -22,6 +22,7 @@ endif() # Custom options for Paddle Inference backend option(PADDLEINFERENCE_DIRECTORY "Directory of custom Paddle Inference library" OFF) +option(PADDLEINFERENCE_API_COMPAT_2_4_x "Whether using Paddle Inference 2.4.x" OFF) set(PADDLEINFERENCE_PROJECT "extern_paddle_inference") set(PADDLEINFERENCE_PREFIX_DIR ${THIRD_PARTY_PATH}/paddle_inference) @@ -247,3 +248,18 @@ function(set_paddle_encrypt_auth_link_policy LIBRARY_NAME) endif() endif() endfunction() + +# Backward compatible for 2.4.x +string(FIND ${PADDLEINFERENCE_VERSION} "2.4" PADDLEINFERENCE_USE_2_4_x) +string(FIND ${PADDLEINFERENCE_VERSION} "2.5" PADDLEINFERENCE_USE_2_5_x) +string(FIND ${PADDLEINFERENCE_VERSION} "0.0.0" PADDLEINFERENCE_USE_DEV) + +if((NOT (PADDLEINFERENCE_USE_2_4_x EQUAL -1)) + AND (PADDLEINFERENCE_USE_2_5_x EQUAL -1) AND (PADDLEINFERENCE_USE_DEV EQUAL -1)) + set(PADDLEINFERENCE_API_COMPAT_2_4_x ON CACHE BOOL "" FORCE) + message(WARNING "You are using PADDLEINFERENCE_USE_2_4_x:${PADDLEINFERENCE_VERSION}, force PADDLEINFERENCE_API_COMPAT_2_4_x=ON") +endif() + +if(PADDLEINFERENCE_API_COMPAT_2_4_x) + add_definitions(-DPADDLEINFERENCE_API_COMPAT_2_4_x) +endif() diff --git a/fastdeploy/runtime/backends/paddle/paddle_backend.cc b/fastdeploy/runtime/backends/paddle/paddle_backend.cc index 81d8a1f37..9965bae73 100644 --- a/fastdeploy/runtime/backends/paddle/paddle_backend.cc +++ b/fastdeploy/runtime/backends/paddle/paddle_backend.cc @@ -48,9 +48,14 @@ void PaddleBackend::BuildOption(const PaddleBackendOption& option) { FDINFO << "Will Enable ir_debug for Paddle Backend." << std::endl; config_.SwitchIrDebug(); } - if (option_.enable_inference_cutlass){ + if (option_.enable_inference_cutlass) { +#ifdef PADDLEINFERENCE_API_COMPAT_2_4_x + FDWARNING << "Your are using Paddle infernence 2.4.x, cutlass is not supported!" + << std::endl; +#else FDINFO << "Will enable_inference_cutlass" << std::endl; config_.Exp_EnableUseCutlass(); +#endif } if (option_.external_stream_) { FDINFO << "Will use external stream for Paddle Backend." << std::endl; @@ -291,9 +296,37 @@ bool PaddleBackend::InitFromPaddle(const std::string& model, auto input_names = predictor_->GetInputNames(); auto output_names = predictor_->GetOutputNames(); auto input_dtypes = predictor_->GetInputTypes(); - auto output_dtypes = predictor_->GetOutputTypes(); + +#ifdef PADDLEINFERENCE_API_COMPAT_2_4_x + // Note: GetInputTensorShape, GetOutputTensorShape and GetOutputTypes + // are not supported when Paddle Inference API version is 2.4.x. + std::map> input_shapes; + std::map> output_shapes; + std::map output_dtypes; + // Get the all the input shape info. + for (size_t i = 0; i < input_names.size(); ++i) { + std::vector shape; + auto handle = predictor_->GetInputHandle(input_names[i]); + for (int j = 0; j < handle->shape().size(); ++j) { + shape.push_back(static_cast(handle->shape()[j])); // int32 -> int64 + } + input_shapes[input_names[i]] = shape; + } + // Get the all the output shape and dtype info. + for (size_t i = 0; i < output_names.size(); ++i) { + std::vector shape; + auto handle = predictor_->GetOutputHandle(output_names[i]); + for (int j = 0; j < handle->shape().size(); ++j) { + shape.push_back(static_cast(handle->shape()[j])); // int32 -> int64 + } + output_shapes[output_names[i]] = shape; + output_dtypes[output_names[i]] = handle->type(); + } +#else auto input_shapes = predictor_->GetInputTensorShape(); auto output_shapes = predictor_->GetOutputTensorShape(); + auto output_dtypes = predictor_->GetOutputTypes(); +#endif inputs_desc_.resize(input_names.size()); for (int i = 0; i < input_names.size(); ++i) {