Skip to content

Commit cfa34df

Browse files
authored
[Cherry-pick] Fix inference c api PD_GetZeroCopyOutput lod and c api encapsulation (#22826)
* Fix pointer and c-api encapsulation (#22663) * refine pointer and c-api prototype, test=develop * fix new c api profile bug, test=develop * add unit tests, test=develop * Fix inference c api PD_GetZeroCopyOutput lod (#22768) * fix inference c api lod, test=develop * fix capi lod problem and enrich tests, test=develop * delete useless header files and alter const_cast, test=develop
1 parent 7244b2a commit cfa34df

9 files changed

+162
-26
lines changed

paddle/fluid/inference/capi/paddle_c_api.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ PADDLE_CAPI_EXPORT extern PD_DataType PD_GetPaddleTensorDType(
117117
PADDLE_CAPI_EXPORT extern PD_PaddleBuf* PD_GetPaddleTensorData(
118118
const PD_Tensor* tensor);
119119

120-
PADDLE_CAPI_EXPORT extern int* PD_GetPaddleTensorShape(const PD_Tensor* tensor,
121-
int** size);
120+
PADDLE_CAPI_EXPORT extern const int* PD_GetPaddleTensorShape(
121+
const PD_Tensor* tensor, int* size);
122122

123123
// AnalysisPredictor
124124
PADDLE_CAPI_EXPORT extern bool PD_PredictorRun(const PD_AnalysisConfig* config,
@@ -262,22 +262,32 @@ PADDLE_CAPI_EXPORT extern bool PD_ProfileEnabled(
262262
PADDLE_CAPI_EXPORT extern void PD_SetInValid(PD_AnalysisConfig* config);
263263

264264
PADDLE_CAPI_EXPORT extern bool PD_IsValid(const PD_AnalysisConfig* config);
265+
265266
PADDLE_CAPI_EXPORT extern void PD_DisableGlogInfo(PD_AnalysisConfig* config);
267+
266268
PADDLE_CAPI_EXPORT extern void PD_DeletePass(PD_AnalysisConfig* config,
267269
char* pass_name);
268270

269271
PADDLE_CAPI_EXPORT extern PD_Predictor* PD_NewPredictor(
270272
const PD_AnalysisConfig* config);
273+
271274
PADDLE_CAPI_EXPORT extern void PD_DeletePredictor(PD_Predictor* predictor);
275+
272276
PADDLE_CAPI_EXPORT extern int PD_GetInputNum(const PD_Predictor*);
277+
273278
PADDLE_CAPI_EXPORT extern int PD_GetOutputNum(const PD_Predictor*);
279+
274280
PADDLE_CAPI_EXPORT extern const char* PD_GetInputName(const PD_Predictor*, int);
281+
275282
PADDLE_CAPI_EXPORT extern const char* PD_GetOutputName(const PD_Predictor*,
276283
int);
284+
277285
PADDLE_CAPI_EXPORT extern void PD_SetZeroCopyInput(
278286
PD_Predictor* predictor, const PD_ZeroCopyTensor* tensor);
287+
279288
PADDLE_CAPI_EXPORT extern void PD_GetZeroCopyOutput(PD_Predictor* predictor,
280289
PD_ZeroCopyTensor* tensor);
290+
281291
PADDLE_CAPI_EXPORT extern void PD_ZeroCopyRun(PD_Predictor* predictor);
282292

283293
#ifdef __cplusplus

paddle/fluid/inference/capi/pd_predictor.cc

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ PD_Predictor* PD_NewPredictor(const PD_AnalysisConfig* config) {
180180
}
181181

182182
void PD_DeletePredictor(PD_Predictor* predictor) {
183-
if (predictor == nullptr) {
183+
if (predictor) {
184+
predictor->predictor = nullptr;
184185
delete predictor;
185186
predictor = nullptr;
186187
}
@@ -232,7 +233,8 @@ void PD_SetZeroCopyInput(PD_Predictor* predictor,
232233

233234
if (tensor->lod.length) {
234235
auto* lod_ptr = reinterpret_cast<size_t*>(tensor->lod.data);
235-
std::vector<size_t> lod(lod_ptr, lod_ptr + tensor->lod.length);
236+
std::vector<size_t> lod;
237+
lod.assign(lod_ptr, lod_ptr + tensor->lod.length / sizeof(size_t));
236238
input->SetLoD({std::move(lod)});
237239
}
238240
}
@@ -265,17 +267,19 @@ void PD_GetZeroCopyOutput(PD_Predictor* predictor, PD_ZeroCopyTensor* tensor) {
265267
tensor->data.length = length;
266268

267269
auto lod = output->lod();
268-
tensor->lod.length = lod.front().size() * sizeof(size_t);
269-
if (tensor->lod.capacity < lod.front().size()) {
270-
if (tensor->lod.data) {
271-
std::free(tensor->lod.data);
272-
}
270+
if (!lod.empty()) {
271+
tensor->lod.length = lod.front().size() * sizeof(size_t);
272+
if (tensor->lod.capacity < lod.front().size()) {
273+
if (tensor->lod.data) {
274+
std::free(tensor->lod.data);
275+
}
273276

274-
tensor->lod.data = std::malloc(lod.front().size() * sizeof(size_t));
275-
tensor->lod.capacity = lod.front().size() * sizeof(size_t);
277+
tensor->lod.data = std::malloc(lod.front().size() * sizeof(size_t));
278+
tensor->lod.capacity = lod.front().size() * sizeof(size_t);
279+
}
280+
std::copy(lod.front().begin(), lod.front().end(),
281+
reinterpret_cast<size_t*>(tensor->lod.data));
276282
}
277-
std::copy(lod.front().begin(), lod.front().end(),
278-
reinterpret_cast<size_t*>(tensor->lod.data));
279283
switch (tensor->dtype) {
280284
case PD_FLOAT32:
281285
output->copy_to_cpu(reinterpret_cast<float*>(tensor->data.data));

paddle/fluid/inference/capi/pd_tensor.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ PD_PaddleBuf* PD_GetPaddleTensorData(const PD_Tensor* tensor) {
7373
return ret;
7474
}
7575

76-
int* PD_GetPaddleTensorShape(const PD_Tensor* tensor, int** size) {
76+
const int* PD_GetPaddleTensorShape(const PD_Tensor* tensor, int* size) {
7777
PADDLE_ENFORCE_NOT_NULL(tensor);
78-
std::vector<int> shape = tensor->tensor.shape;
79-
int s = shape.size();
80-
*size = &s;
78+
const std::vector<int>& shape = tensor->tensor.shape;
79+
*size = shape.size();
8180
return shape.data();
8281
}
8382

paddle/fluid/inference/tests/api/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,7 @@ if(WITH_MKLDNN)
387387
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS} paddle_fluid_c
388388
ARGS --infer_model=${INT8_DATA_DIR}/resnet50/model)
389389
endif()
390+
391+
inference_analysis_test(test_analyzer_capi_ner SRCS analyzer_capi_ner_tester.cc
392+
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS} paddle_fluid_c
393+
ARGS --infer_model=${CHINESE_NER_INSTALL_DIR}/model)

paddle/fluid/inference/tests/api/analyzer_capi_gpu_tester.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ limitations under the License. */
1515
#include <stddef.h>
1616
#include <stdint.h>
1717
#include <stdio.h>
18-
#include <fstream>
19-
#include <iostream>
2018
#include <string>
2119
#include <vector>
2220
#include "paddle/fluid/inference/capi/paddle_c_api.h"
@@ -93,6 +91,8 @@ TEST(PD_AnalysisConfig, trt_fp16) {
9391
false);
9492
bool trt_enable = PD_TensorrtEngineEnabled(config);
9593
CHECK(trt_enable) << "NO";
94+
PD_Predictor *predictor = PD_NewPredictor(config);
95+
PD_DeletePredictor(predictor);
9696
PD_DeleteAnalysisConfig(config);
9797
}
9898

paddle/fluid/inference/tests/api/analyzer_capi_int_tester.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ limitations under the License. */
1515
#include <stddef.h>
1616
#include <stdint.h>
1717
#include <stdio.h>
18-
#include <fstream>
19-
#include <iostream>
2018
#include <string>
2119
#include <vector>
2220
#include "paddle/fluid/inference/capi/paddle_c_api.h"
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <stddef.h>
16+
#include <stdint.h>
17+
#include <stdio.h>
18+
#include <string>
19+
#include <vector>
20+
#include "paddle/fluid/inference/capi/paddle_c_api.h"
21+
#include "paddle/fluid/inference/tests/api/tester_helper.h"
22+
23+
namespace paddle {
24+
namespace inference {
25+
namespace analysis {
26+
27+
void SetConfig(PD_AnalysisConfig *config) {
28+
auto model_dir = FLAGS_infer_model;
29+
PD_SetModel(config, (model_dir + "/__model__").c_str(),
30+
(model_dir + "/param").c_str());
31+
PD_SwitchUseFeedFetchOps(config, false);
32+
PD_SwitchSpecifyInputNames(config, true);
33+
PD_DisableGpu(config);
34+
}
35+
36+
TEST(PD_ZeroCopyRun, zero_copy_run) {
37+
PD_AnalysisConfig *config = PD_NewAnalysisConfig();
38+
SetConfig(config);
39+
PD_Predictor *predictor = PD_NewPredictor(config);
40+
41+
int input_num = PD_GetInputNum(predictor);
42+
printf("Input num: %d\n", input_num);
43+
int output_num = PD_GetOutputNum(predictor);
44+
printf("Output num: %d\n", output_num);
45+
46+
PD_ZeroCopyTensor inputs[2];
47+
48+
// inputs[0]: word
49+
PD_InitZeroCopyTensor(&inputs[0]);
50+
inputs[0].name = new char[5];
51+
snprintf(inputs[0].name, strlen(PD_GetInputName(predictor, 0)) + 1, "%s",
52+
PD_GetInputName(predictor, 0));
53+
54+
inputs[0].data.capacity = sizeof(int64_t) * 11 * 1;
55+
inputs[0].data.length = inputs[0].data.capacity;
56+
inputs[0].data.data = malloc(inputs[0].data.capacity);
57+
std::vector<int64_t> ref_word(
58+
{12673, 9763, 905, 284, 45, 7474, 20, 17, 1, 4, 9});
59+
inputs[0].data.data = reinterpret_cast<void *>(ref_word.data());
60+
61+
int shape0[] = {11, 1};
62+
inputs[0].shape.data = reinterpret_cast<void *>(shape0);
63+
inputs[0].shape.capacity = sizeof(shape0);
64+
inputs[0].shape.length = sizeof(shape0);
65+
inputs[0].dtype = PD_INT64;
66+
67+
size_t lod0[] = {0, 11};
68+
inputs[0].lod.data = reinterpret_cast<void *>(lod0);
69+
inputs[0].lod.capacity = sizeof(size_t) * 2;
70+
inputs[0].lod.length = sizeof(size_t) * 2;
71+
72+
PD_SetZeroCopyInput(predictor, &inputs[0]);
73+
74+
// inputs[1]: mention
75+
PD_InitZeroCopyTensor(&inputs[1]);
76+
inputs[1].name = new char[8];
77+
snprintf(inputs[1].name, strlen(PD_GetInputName(predictor, 1)) + 1, "%s",
78+
PD_GetInputName(predictor, 1));
79+
80+
inputs[1].data.capacity = sizeof(int64_t) * 11 * 1;
81+
inputs[1].data.length = inputs[1].data.capacity;
82+
inputs[1].data.data = malloc(inputs[1].data.capacity);
83+
std::vector<int64_t> ref_mention({27, 0, 0, 33, 34, 33, 0, 0, 0, 1, 2});
84+
inputs[1].data.data = reinterpret_cast<void *>(ref_mention.data());
85+
86+
int shape1[] = {11, 1};
87+
inputs[1].shape.data = reinterpret_cast<void *>(shape1);
88+
inputs[1].shape.capacity = sizeof(shape1);
89+
inputs[1].shape.length = sizeof(shape1);
90+
inputs[1].dtype = PD_INT64;
91+
92+
size_t lod1[] = {0, 11};
93+
inputs[1].lod.data = reinterpret_cast<void *>(lod1);
94+
inputs[1].lod.capacity = sizeof(size_t) * 2;
95+
inputs[1].lod.length = sizeof(size_t) * 2;
96+
97+
PD_SetZeroCopyInput(predictor, &inputs[1]);
98+
99+
PD_ZeroCopyRun(predictor);
100+
PD_ZeroCopyTensor output;
101+
PD_InitZeroCopyTensor(&output);
102+
output.name = new char[21];
103+
snprintf(output.name, strlen(PD_GetOutputName(predictor, 0)) + 1, "%s",
104+
PD_GetOutputName(predictor, 0));
105+
106+
// not necessary, just for converage tests
107+
output.lod.data = std::malloc(sizeof(size_t));
108+
109+
PD_GetZeroCopyOutput(predictor, &output);
110+
PD_DestroyZeroCopyTensor(&output);
111+
PD_DeleteAnalysisConfig(config);
112+
PD_DeletePredictor(predictor);
113+
}
114+
115+
} // namespace analysis
116+
} // namespace inference
117+
} // namespace paddle

paddle/fluid/inference/tests/api/analyzer_capi_pd_tensor_tester.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,14 @@ void PD_run() {
6767
float* result = static_cast<float*>(PD_PaddleBufData(b));
6868
LOG(INFO) << *result;
6969
PD_DeletePaddleTensor(input);
70-
int* size;
71-
PD_GetPaddleTensorShape(out_data, &size);
70+
int size;
71+
const int* out_shape = PD_GetPaddleTensorShape(out_data, &size);
72+
CHECK(size == 2) << "The Output shape's size is NOT match.";
73+
std::vector<int> ref_outshape_size({9, 6});
74+
for (int i = 0; i < 2; ++i) {
75+
CHECK(out_shape[i] == ref_outshape_size[i])
76+
<< "The Output's shape is NOT match.";
77+
}
7278
PD_DeletePaddleBuf(buf);
7379
}
7480

paddle/fluid/inference/tests/api/analyzer_capi_tester.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ limitations under the License. */
1515
#include <stddef.h>
1616
#include <stdint.h>
1717
#include <stdio.h>
18-
#include <fstream>
19-
#include <iostream>
2018
#include <string>
2119
#include <vector>
2220
#include "paddle/fluid/inference/capi/paddle_c_api.h"
@@ -71,7 +69,7 @@ void zero_copy_run() {
7169
delete[] outputs;
7270
}
7371

74-
TEST(PD_ZeroCopyRun, zero_copy_run) { zero_copy_run(); }
72+
TEST(PD_PredictorZeroCopyRun, zero_copy_run) { zero_copy_run(); }
7573

7674
#ifdef PADDLE_WITH_MKLDNN
7775
TEST(PD_AnalysisConfig, profile_mkldnn) {

0 commit comments

Comments
 (0)