Skip to content

Commit

Permalink
[Serving]ppcls preprocessor support gpu (PaddlePaddle#615)
Browse files Browse the repository at this point in the history
* serving ppcls support gpu

* serving ppcls preprocessor use cpu
  • Loading branch information
heliqi authored Nov 17, 2022
1 parent 1694d9a commit 6ebe612
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/vision/classification/paddleclas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ PaddleClas模型导出,请参考其文档说明[模型导出](https://github.c

- [Python部署](python)
- [C++部署](cpp)
- [服务化部署](serving)
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def initialize(self, args):
__file__)) + "/inference_cls.yaml"
self.preprocess_ = fd.vision.classification.PaddleClasPreprocessor(
yaml_path)
if args['model_instance_kind'] == 'GPU':
device_id = int(args['model_instance_device_id'])
self.preprocess_.use_gpu(device_id)

def execute(self, requests):
"""`execute` must be implemented in every Python model. `execute`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ output [

instance_group [
{
count: 1
kind: KIND_CPU
# The number of instances is 1
count: 1
# Use CPU, GPU inference option is:KIND_GPU
kind: KIND_CPU
# The instance is deployed on the 0th GPU card
# gpus: [0]
}
]
3 changes: 1 addition & 2 deletions fastdeploy/pybind/fd_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ pybind11::capsule FDTensorToDLPack(FDTensor& fd_tensor) {

dlpack_tensor->dl_tensor.dtype = FDToDlpackType(fd_tensor.dtype);

// TODO(liqi): FDTensor add device_id
dlpack_tensor->dl_tensor.device.device_id = 0;
dlpack_tensor->dl_tensor.device.device_id = fd_tensor.device_id;
if(fd_tensor.device == Device::GPU) {
if (fd_tensor.is_pinned_memory) {
dlpack_tensor->dl_tensor.device.device_type = DLDeviceType::kDLCUDAHost;
Expand Down
6 changes: 4 additions & 2 deletions fastdeploy/vision/classification/ppcls/ppcls_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ void BindPaddleClas(pybind11::module& m) {
if (!self.Run(&images, &outputs)) {
pybind11::eval("raise Exception('Failed to preprocess the input data in PaddleClasPreprocessor.')");
}
for (size_t i = 0; i < outputs.size(); ++i) {
outputs[i].StopSharing();
if (!self.WithGpu()) {
for (size_t i = 0; i < outputs.size(); ++i) {
outputs[i].StopSharing();
}
}
return outputs;
})
Expand Down
2 changes: 2 additions & 0 deletions fastdeploy/vision/classification/ppcls/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class FASTDEPLOY_DECL PaddleClasPreprocessor {
*/
void UseGpu(int gpu_id = -1);

bool WithGpu() { return use_cuda_; }

private:
bool BuildPreprocessPipelineFromConfig(const std::string& config_file);
std::vector<std::shared_ptr<Processor>> processors_;
Expand Down

0 comments on commit 6ebe612

Please sign in to comment.