[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
This commit is contained in:
DefTruth
2023-06-25 19:27:36 +08:00
committed by GitHub
parent ba8649a69d
commit dea3795930
2 changed files with 51 additions and 2 deletions

View File

@@ -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()

View File

@@ -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<std::string, std::vector<int64_t>> input_shapes;
std::map<std::string, std::vector<int64_t>> output_shapes;
std::map<std::string, paddle_infer::DataType> output_dtypes;
// Get the all the input shape info.
for (size_t i = 0; i < input_names.size(); ++i) {
std::vector<int64_t> shape;
auto handle = predictor_->GetInputHandle(input_names[i]);
for (int j = 0; j < handle->shape().size(); ++j) {
shape.push_back(static_cast<int64_t>(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<int64_t> shape;
auto handle = predictor_->GetOutputHandle(output_names[i]);
for (int j = 0; j < handle->shape().size(); ++j) {
shape.push_back(static_cast<int64_t>(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) {