From 922d4324772380f066311b0d5dc1946292c4cb6d Mon Sep 17 00:00:00 2001 From: liu zhengxi <380185688@qq.com> Date: Tue, 15 Oct 2019 08:23:37 +0800 Subject: [PATCH] fix the PD_ZeroCopyPredictorRun output problem (#20612) * fix the PD_ZeroCopyPredictorRun output problem and add some checks and logs for users * modify the cmakelists depends and fix the cmakelists problem --- paddle/fluid/inference/capi/CMakeLists.txt | 10 +++--- paddle/fluid/inference/capi/c_api.cc | 18 +++++++++-- paddle/fluid/inference/capi/c_api.h | 2 +- paddle/fluid/inference/capi/pd_config.cc | 1 + paddle/fluid/inference/capi/pd_predictor.cc | 32 +++++++++++-------- paddle/fluid/inference/capi/pd_tensor.cc | 9 ++++++ .../tests/api/analyzer_capi_int_tester.cc | 5 ++- .../tests/api/analyzer_capi_tester.cc | 2 +- 8 files changed, 53 insertions(+), 26 deletions(-) diff --git a/paddle/fluid/inference/capi/CMakeLists.txt b/paddle/fluid/inference/capi/CMakeLists.txt index 68e4fb4b1a..c910074f4f 100644 --- a/paddle/fluid/inference/capi/CMakeLists.txt +++ b/paddle/fluid/inference/capi/CMakeLists.txt @@ -1,10 +1,8 @@ -cc_library(pd_config SRCS pd_config.cc) -cc_library(pd_predictor SRCS pd_predictor.cc) -cc_library(pd_tensor SRCS pd_tensor.cc) -cc_library(pd_c_api SRCS c_api.cc) -cc_library(paddle_fluid_c SRCS c_api.cc DEPS paddle_fluid pd_config pd_predictor pd_tensor pd_c_api) -cc_library(paddle_fluid_c_shared SHARED SRCS c_api.cc DEPS paddle_fluid pd_config pd_predictor pd_tensor pd_c_api) +set(C_API_SRCS pd_config.cc pd_predictor.cc pd_tensor.cc c_api.cc) + +cc_library(paddle_fluid_c SRCS ${C_API_SRCS} DEPS paddle_fluid) +cc_library(paddle_fluid_c_shared SHARED SRCS ${C_API_SRCS} DEPS paddle_fluid) set_target_properties(paddle_fluid_c_shared PROPERTIES OUTPUT_NAME paddle_fluid_c) if(WIN32) target_link_libraries(paddle_fluid_c_shared shlwapi.lib) diff --git a/paddle/fluid/inference/capi/c_api.cc b/paddle/fluid/inference/capi/c_api.cc index 07f58bf236..56ae106141 100644 --- a/paddle/fluid/inference/capi/c_api.cc +++ b/paddle/fluid/inference/capi/c_api.cc @@ -29,22 +29,34 @@ void PD_DeletePaddleBuf(PD_PaddleBuf* buf) { if (buf) { delete buf; buf = nullptr; + VLOG(3) << "PD_PaddleBuf delete successfully. "; } } void PD_PaddleBufResize(PD_PaddleBuf* buf, size_t length) { + PADDLE_ENFORCE_NOT_NULL(buf); buf->buf.Resize(length); } void PD_PaddleBufReset(PD_PaddleBuf* buf, void* data, size_t length) { + PADDLE_ENFORCE_NOT_NULL(buf); buf->buf.Reset(data, length); } -bool PD_PaddleBufEmpty(PD_PaddleBuf* buf) { return buf->buf.empty(); } +bool PD_PaddleBufEmpty(PD_PaddleBuf* buf) { + PADDLE_ENFORCE_NOT_NULL(buf); + return buf->buf.empty(); +} -void* PD_PaddleBufData(PD_PaddleBuf* buf) { return buf->buf.data(); } +void* PD_PaddleBufData(PD_PaddleBuf* buf) { + PADDLE_ENFORCE_NOT_NULL(buf); + return buf->buf.data(); +} -size_t PD_PaddleBufLength(PD_PaddleBuf* buf) { return buf->buf.length(); } +size_t PD_PaddleBufLength(PD_PaddleBuf* buf) { + PADDLE_ENFORCE_NOT_NULL(buf); + return buf->buf.length(); +} } // extern "C" diff --git a/paddle/fluid/inference/capi/c_api.h b/paddle/fluid/inference/capi/c_api.h index 58380c9e42..13336cbd19 100644 --- a/paddle/fluid/inference/capi/c_api.h +++ b/paddle/fluid/inference/capi/c_api.h @@ -104,7 +104,7 @@ PADDLE_CAPI_EXPORT extern bool PD_PredictorRun(const PD_AnalysisConfig* config, PADDLE_CAPI_EXPORT extern bool PD_PredictorZeroCopyRun( const PD_AnalysisConfig* config, PD_ZeroCopyData* inputs, int in_size, - PD_ZeroCopyData* output, int** out_size); + PD_ZeroCopyData** output, int** out_size); // AnalysisConfig enum Precision { kFloat32 = 0, kInt8, kHalf }; diff --git a/paddle/fluid/inference/capi/pd_config.cc b/paddle/fluid/inference/capi/pd_config.cc index 22dfab2f24..657add6d66 100644 --- a/paddle/fluid/inference/capi/pd_config.cc +++ b/paddle/fluid/inference/capi/pd_config.cc @@ -33,6 +33,7 @@ void PD_DeleteAnalysisConfig(PD_AnalysisConfig* config) { if (config) { delete config; config = nullptr; + VLOG(3) << "PD_AnalysisConfig delete successfully. "; } } diff --git a/paddle/fluid/inference/capi/pd_predictor.cc b/paddle/fluid/inference/capi/pd_predictor.cc index 52b249f3bb..89d4c41537 100644 --- a/paddle/fluid/inference/capi/pd_predictor.cc +++ b/paddle/fluid/inference/capi/pd_predictor.cc @@ -28,6 +28,7 @@ extern "C" { bool PD_PredictorRun(const PD_AnalysisConfig* config, PD_Tensor* inputs, int in_size, PD_Tensor* output_data, int** out_size, int batch_size) { + PADDLE_ENFORCE_NOT_NULL(config); auto predictor = paddle::CreatePaddlePredictor(config->config); std::vector in; for (int i = 0; i < in_size; ++i) { @@ -47,9 +48,11 @@ bool PD_PredictorRun(const PD_AnalysisConfig* config, PD_Tensor* inputs, bool PD_PredictorZeroCopyRun(const PD_AnalysisConfig* config, PD_ZeroCopyData* inputs, int in_size, - PD_ZeroCopyData* output, int** out_size) { + PD_ZeroCopyData** output, int** out_size) { + PADDLE_ENFORCE_NOT_NULL(config); auto predictor = paddle::CreatePaddlePredictor(config->config); auto input_names = predictor->GetInputNames(); + VLOG(3) << "The inputs' size is " << input_names.size(); PADDLE_ENFORCE_EQ( input_names.size(), in_size, "The number of input and the number of model's input must match. "); @@ -81,26 +84,27 @@ bool PD_PredictorZeroCopyRun(const PD_AnalysisConfig* config, auto output_names = predictor->GetOutputNames(); int osize = output_names.size(); *out_size = &osize; - output = new PD_ZeroCopyData[osize]; + *output = new PD_ZeroCopyData[osize]; + VLOG(3) << "The output size is " << osize; for (int i = 0; i < osize; ++i) { - LOG(INFO) << 1; - output[i].name = new char[output_names[i].length() + 1]; - snprintf(output[i].name, output_names[i].length() + 1, "%s", + auto& output_i = (*output)[i]; + output_i.name = new char[output_names[i].length() + 1]; + snprintf(output_i.name, output_names[i].length() + 1, "%s", output_names[i].c_str()); auto output_t = predictor->GetOutputTensor(output_names[i]); - output[i].dtype = ConvertToPDDataType(output_t->type()); + output_i.dtype = ConvertToPDDataType(output_t->type()); std::vector output_shape = output_t->shape(); - output[i].shape = new int[output_shape.size()]; - output[i].shape = output_shape.data(); - output[i].shape_size = output_shape.size(); - switch (output[i].dtype) { + output_i.shape = new int[output_shape.size()]; + output_i.shape = output_shape.data(); + output_i.shape_size = output_shape.size(); + switch (output_i.dtype) { case PD_FLOAT32: { std::vector out_data; int out_num = std::accumulate(output_shape.begin(), output_shape.end(), 1, std::multiplies()); out_data.resize(out_num); output_t->copy_to_cpu(out_data.data()); - output[i].data = static_cast(out_data.data()); + output_i.data = static_cast(out_data.data()); } break; case PD_INT32: { std::vector out_data; @@ -108,7 +112,7 @@ bool PD_PredictorZeroCopyRun(const PD_AnalysisConfig* config, 1, std::multiplies()); out_data.resize(out_num); output_t->copy_to_cpu(out_data.data()); - output[i].data = static_cast(out_data.data()); + output_i.data = static_cast(out_data.data()); } break; case PD_INT64: { std::vector out_data; @@ -116,7 +120,7 @@ bool PD_PredictorZeroCopyRun(const PD_AnalysisConfig* config, 1, std::multiplies()); out_data.resize(out_num); output_t->copy_to_cpu(out_data.data()); - output[i].data = static_cast(out_data.data()); + output_i.data = static_cast(out_data.data()); } break; case PD_UINT8: { std::vector out_data; @@ -124,7 +128,7 @@ bool PD_PredictorZeroCopyRun(const PD_AnalysisConfig* config, 1, std::multiplies()); out_data.resize(out_num); output_t->copy_to_cpu(out_data.data()); - output[i].data = static_cast(out_data.data()); + output_i.data = static_cast(out_data.data()); } break; default: CHECK(false) << "Unsupport data type."; diff --git a/paddle/fluid/inference/capi/pd_tensor.cc b/paddle/fluid/inference/capi/pd_tensor.cc index 8c797fd31d..1df7166223 100644 --- a/paddle/fluid/inference/capi/pd_tensor.cc +++ b/paddle/fluid/inference/capi/pd_tensor.cc @@ -29,40 +29,49 @@ void PD_DeletePaddleTensor(PD_Tensor* tensor) { if (tensor) { delete tensor; tensor = nullptr; + VLOG(3) << "PD_Tensor delete successfully. "; } } void PD_SetPaddleTensorName(PD_Tensor* tensor, char* name) { + PADDLE_ENFORCE_NOT_NULL(tensor); tensor->tensor.name = std::string(name); } void PD_SetPaddleTensorDType(PD_Tensor* tensor, PD_DataType dtype) { + PADDLE_ENFORCE_NOT_NULL(tensor); tensor->tensor.dtype = paddle::ConvertToPaddleDType(dtype); } void PD_SetPaddleTensorData(PD_Tensor* tensor, PD_PaddleBuf* buf) { + PADDLE_ENFORCE_NOT_NULL(tensor); tensor->tensor.data = buf->buf; } void PD_SetPaddleTensorShape(PD_Tensor* tensor, int* shape, int size) { + PADDLE_ENFORCE_NOT_NULL(tensor); tensor->tensor.shape.assign(shape, shape + size); } const char* PD_GetPaddleTensorName(const PD_Tensor* tensor) { + PADDLE_ENFORCE_NOT_NULL(tensor); return tensor->tensor.name.c_str(); } PD_DataType PD_GetPaddleTensorDType(const PD_Tensor* tensor) { + PADDLE_ENFORCE_NOT_NULL(tensor); return ConvertToPDDataType(tensor->tensor.dtype); } PD_PaddleBuf* PD_GetPaddleTensorData(const PD_Tensor* tensor) { + PADDLE_ENFORCE_NOT_NULL(tensor); PD_PaddleBuf* ret = PD_NewPaddleBuf(); ret->buf = tensor->tensor.data; return ret; } int* PD_GetPaddleTensorShape(const PD_Tensor* tensor, int** size) { + PADDLE_ENFORCE_NOT_NULL(tensor); std::vector shape = tensor->tensor.shape; int s = shape.size(); *size = &s; diff --git a/paddle/fluid/inference/tests/api/analyzer_capi_int_tester.cc b/paddle/fluid/inference/tests/api/analyzer_capi_int_tester.cc index 3c62984c0e..58d0053662 100644 --- a/paddle/fluid/inference/tests/api/analyzer_capi_int_tester.cc +++ b/paddle/fluid/inference/tests/api/analyzer_capi_int_tester.cc @@ -92,7 +92,10 @@ void zero_copy_run() { inputs[1].shape = label_shape; inputs[1].shape_size = label_shape_size; - PD_PredictorZeroCopyRun(config, inputs, in_size, outputs, &out_size); + PD_PredictorZeroCopyRun(config, inputs, in_size, &outputs, &out_size); + + LOG(INFO) << outputs[0].name; + LOG(INFO) << outputs[0].shape_size; } TEST(PD_ZeroCopyRun, zero_copy_run) { diff --git a/paddle/fluid/inference/tests/api/analyzer_capi_tester.cc b/paddle/fluid/inference/tests/api/analyzer_capi_tester.cc index e491ae7009..7aa85cec14 100644 --- a/paddle/fluid/inference/tests/api/analyzer_capi_tester.cc +++ b/paddle/fluid/inference/tests/api/analyzer_capi_tester.cc @@ -74,7 +74,7 @@ void zero_copy_run() { inputs->shape = shape; inputs->shape_size = shape_size; - PD_PredictorZeroCopyRun(config, inputs, in_size, outputs, &out_size); + PD_PredictorZeroCopyRun(config, inputs, in_size, &outputs, &out_size); } TEST(PD_ZeroCopyRun, zero_copy_run) { zero_copy_run(); } -- GitLab