在部署前,参考 FastDeploy SDK 安装文档安装 FastDeploy Python SDK。
本目录下提供 infer.py
快速完成在 CPU/GPU 的通用文本分类任务的 Python 部署示例。
直接执行以下命令安装部署示例的依赖。
# 安装 fast_tokenizer 以及 GPU 版本 fastdeploy
pip install fast-tokenizer-python fastdeploy-gpu-python -f https://www.paddlepaddle.org.cn/whl/fastdeploy.html
以下示例展示如何基于 FastDeploy 库完成 UTC 模型进行文本分类任务的 Python 预测部署,可通过命令行参数--device
以及--backend
指定运行在不同的硬件以及推理引擎后端,并使用--model_dir
参数指定运行的模型,具体参数设置可查看下面参数说明。示例中的模型是按照 UTC 训练文档导出得到的部署模型,其模型目录为 application/zero_shot_text_classification/checkpoint/model_best
(用户可按实际情况设置)。
# CPU 推理
python infer.py --model_dir ../../checkpoint/model_best --device cpu
# GPU 推理
python infer.py --model_dir ../../checkpoint/model_best --device gpu
运行完成后返回的结果如下:
[2023-03-02 06:32:47,528] [ INFO] - We are using <class 'paddlenlp.transformers.ernie.tokenizer.ErnieTokenizer'> to load './checkpoint/model_best'.
[INFO] fastdeploy/runtime/runtime.cc(266)::CreatePaddleBackend Runtime initialized with Backend::PDINFER in Device::GPU.
[2023-03-02 06:33:18,120] [ INFO] - Assigning ['[O-MASK]'] to the additional_special_tokens key of the tokenizer
[{'predictions': [{'label': '这是一条好评', 'score': 0.9073}], 'text_a': '房间干净明亮,非常不错'}]
参数 | 参数说明 |
---|---|
--model_dir | 指定部署模型的目录, |
--batch_size | 输入的batch size,默认为 1 |
--max_length | 最大序列长度,默认为 128 |
--num_omask_tokens | 最大标签数量,默认为64 |
--device | 运行的设备,可选范围: ['cpu', 'gpu'],默认为'cpu' |
--device_id | 运行设备的id。默认为0。 |
--cpu_threads | 当使用cpu推理时,指定推理的cpu线程数,默认为1。 |
--backend | 支持的推理后端,可选范围: ['onnx_runtime', 'paddle', 'tensorrt', 'paddle_tensorrt'],默认为'paddle' |
--use_fp16 | 是否使用FP16模式进行推理。使用tensorrt和paddle_tensorrt后端时可开启,默认为False |
FastDeploy 在 Python 端上,提供 fastdeploy.RuntimeOption.use_xxx()
以及 fastdeploy.RuntimeOption.use_xxx_backend()
接口支持开发者选择不同的硬件、不同的推理引擎进行部署。在不同的硬件上部署 UTC 模型,需要选择硬件所支持的推理引擎进行部署,下表展示如何在不同的硬件上选择可用的推理引擎部署 UTC 模型。
符号说明: (1) ✅: 已经支持; (2) ❔: 正在进行中; (3) N/A: 暂不支持;
硬件 | 硬件对应的接口 | 可用的推理引擎 | 推理引擎对应的接口 | 是否支持 Paddle 新格式量化模型 | 是否支持 FP16 模式 |
CPU | use_cpu() | Paddle Inference | use_paddle_infer_backend() | ✅ | N/A |
ONNX Runtime | use_ort_backend() | ✅ | N/A | ||
GPU | use_gpu() | Paddle Inference | use_paddle_infer_backend() | ✅ | N/A |
ONNX Runtime | use_ort_backend() | ✅ | ❔ | ||
Paddle TensorRT | use_paddle_infer_backend() + paddle_infer_option.enable_trt = True | ✅ | ✅ | ||
TensorRT | use_trt_backend() | ✅ | ✅ | ||
昆仑芯 XPU | use_kunlunxin() | Paddle Lite | use_paddle_lite_backend() | N/A | ✅ |
华为 昇腾 | use_ascend() | Paddle Lite | use_paddle_lite_backend() | ❔ | ✅ |
Graphcore IPU | use_ipu() | Paddle Inference | use_paddle_infer_backend() | ❔ | N/A |
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》