Skip to content

Commit 43ee4a3

Browse files
authored
[cherry-pick] add python share_date interface (#41627)
* add python share_date interface * Update inference_api.cc * add python share_data interface
1 parent 47b6e5f commit 43ee4a3

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

paddle/fluid/inference/api/paddle_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace paddle {
3636

3737
using PaddleDType = paddle_infer::DataType;
3838
using PaddlePlace = paddle_infer::PlaceType;
39+
using PaddleDataLayout = paddle_infer::DataLayout;
3940

4041
/// \brief Memory manager for PaddleTensor.
4142
///

paddle/fluid/pybind/inference_api.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ using paddle::NativeConfig;
7676
using paddle::NativePaddlePredictor;
7777
using paddle::PaddleBuf;
7878
using paddle::PaddleDType;
79+
using paddle::PaddleDataLayout;
7980
using paddle::PaddlePassBuilder;
8081
using paddle::PaddlePlace;
8182
using paddle::PaddlePredictor;
@@ -85,6 +86,7 @@ using paddle::ZeroCopyTensor;
8586

8687
namespace {
8788
void BindPaddleDType(py::module *m);
89+
void BindPaddleDataLayout(py::module *m);
8890
void BindPaddleBuf(py::module *m);
8991
void BindPaddleTensor(py::module *m);
9092
void BindPaddlePlace(py::module *m);
@@ -211,6 +213,34 @@ void PaddleInferTensorCreate(
211213
tensor.CopyFromCpu(static_cast<const T *>(data.data()));
212214
}
213215

216+
paddle_infer::PlaceType ToPaddleInferPlace(
217+
phi::AllocationType allocation_type) {
218+
if (allocation_type == phi::AllocationType::CPU) {
219+
return paddle_infer::PlaceType::kCPU;
220+
} else if (allocation_type == phi::AllocationType::GPU) {
221+
return paddle_infer::PlaceType::kGPU;
222+
} else {
223+
return paddle_infer::PlaceType::kCPU;
224+
}
225+
}
226+
227+
void PaddleInferShareExternalData(paddle_infer::Tensor &tensor, // NOLINT
228+
framework::Tensor input_tensor) {
229+
std::vector<int> shape;
230+
for (int i = 0; i < input_tensor.dims().size(); ++i) {
231+
shape.push_back(input_tensor.dims()[i]);
232+
}
233+
if (input_tensor.dtype() == phi::DataType::FLOAT32) {
234+
tensor.ShareExternalData(
235+
static_cast<float *>(input_tensor.data()), shape,
236+
ToPaddleInferPlace(input_tensor.place().GetType()));
237+
} else if (input_tensor.dtype() == phi::DataType::FLOAT16) {
238+
tensor.ShareExternalData(
239+
static_cast<paddle::platform::float16 *>(input_tensor.data()), shape,
240+
ToPaddleInferPlace(input_tensor.place().GetType()));
241+
}
242+
}
243+
214244
/// \brief Experimental interface.
215245
/// Create the Strings tensor from data.
216246
/// \param tensor The tensor will be created and
@@ -327,6 +357,7 @@ void CopyPaddleInferTensor(paddle_infer::Tensor &dst, // NOLINT
327357

328358
void BindInferenceApi(py::module *m) {
329359
BindPaddleDType(m);
360+
BindPaddleDataLayout(m);
330361
BindPaddleBuf(m);
331362
BindPaddleTensor(m);
332363
BindPaddlePlace(m);
@@ -372,6 +403,14 @@ void BindPaddleDType(py::module *m) {
372403
.value("INT32", PaddleDType::INT32);
373404
}
374405

406+
void BindPaddleDataLayout(py::module *m) {
407+
py::enum_<PaddleDataLayout>(*m, "PaddleDataLayout")
408+
.value("UNK", PaddleDataLayout::kUNK)
409+
.value("Any", PaddleDataLayout::kAny)
410+
.value("NHWC", PaddleDataLayout::kNHWC)
411+
.value("NCHW", PaddleDataLayout::kNCHW);
412+
}
413+
375414
void BindPaddleBuf(py::module *m) {
376415
py::class_<PaddleBuf>(*m, "PaddleBuf")
377416
.def(py::init<size_t>())
@@ -817,6 +856,7 @@ void BindPaddleInferTensor(py::module *m) {
817856
.def("copy_from_cpu_bind",
818857
&PaddleInferTensorCreate<paddle_infer::float16>)
819858
.def("copy_from_cpu_bind", &PaddleInferStringTensorCreate)
859+
.def("share_external_data_bind", &PaddleInferShareExternalData)
820860
.def("copy_to_cpu", &PaddleInferTensorToNumpy)
821861
.def("shape", &paddle_infer::Tensor::shape)
822862
.def("set_lod", &paddle_infer::Tensor::SetLoD)

python/paddle/fluid/inference/wrapper.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from ..core import AnalysisConfig, PaddleDType, PaddlePlace
1616
from ..core import PaddleInferPredictor, PaddleInferTensor
17+
from .. import core
1718

1819
import numpy as np
1920

@@ -39,4 +40,16 @@ def tensor_copy_from_cpu(self, data):
3940
)
4041

4142

43+
def tensor_share_external_data(self, data):
44+
'''
45+
Support input type check based on tensor.share_external_data.
46+
'''
47+
if isinstance(data, core.LoDTensor):
48+
self.share_external_data_bind(data)
49+
else:
50+
raise TypeError(
51+
"In share_external_data, we only support LoDTensor data type.")
52+
53+
4254
Tensor.copy_from_cpu = tensor_copy_from_cpu
55+
Tensor.share_external_data = tensor_share_external_data

0 commit comments

Comments
 (0)