diff --git a/paddle/fluid/inference/capi/CMakeLists.txt b/paddle/fluid/inference/capi/CMakeLists.txt index 68e4fb4b1a3fa66c99bde411d97fdafacf286a8c..c910074f4fc7b8a2dc5d777ed47e3d3c843c19f4 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 07f58bf23685e1ed6d61aa18542cf59be15f8b2e..56ae10614157af3724c70e14ef34f3a89e513e8e 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 58380c9e42f780a4b9110176e13a5c3030c8e48f..13336cbd19cb453fae9226bc9db2bcf3e2779c3d 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 22dfab2f248426415de7057e727e1af06869e348..657add6d6643daf21f409305b6c8c01c16abd277 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 52b249f3bb28cd5300f485e0c35b64d462f169b0..89d4c415373b44d6848cb02be34e74c569fd6876 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 8c797fd31d0f0aafede04e310a7b3192e763ece2..1df7166223a59692c8433579572d779ec56e6a2c 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 3c62984c0e3d710fe04239d0478c4d389f196a86..58d0053662e2c72e5e26a3ad1422c1c0d6e6e7ef 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 e491ae70095e4d2c50cefbcf9f0c09fefa00899d..7aa85cec14eac152db99a791466a4f4ee4207ae1 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(); }