Skip to content

Latest commit

 

History

History
260 lines (198 loc) · 11.8 KB

File metadata and controls

260 lines (198 loc) · 11.8 KB

FastDeploy ERNIE 3.0 模型 Python 部署示例

在部署前,参考 FastDeploy SDK 安装文档安装 FastDeploy Python SDK。

本目录下分别提供 seq_cls_infer.py 以及 token_cls_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 库完成 ERNIE 3.0 Medium 模型在 CLUE Benchmark 的 AFQMC 数据集上进行文本分类任务的 Python 预测部署,可通过命令行参数--device以及--backend指定运行在不同的硬件以及推理引擎后端,并使用--model_dir参数指定运行的模型,具体参数设置可查看下面参数说明。示例中的模型是按照 ERNIE 3.0 训练文档导出得到的部署模型,其模型目录为model_zoo/ernie-3.0/best_models/afqmc/export(用户可按实际情况设置)。

# CPU 推理
python seq_cls_infer.py --model_dir ../../best_models/afqmc/export --device cpu --backend paddle

# GPU 推理
python seq_cls_infer.py --model_dir ../../best_models/afqmc/export --device gpu --backend paddle

运行完成后返回的结果如下:

[INFO] fastdeploy/runtime.cc(596)::Init    Runtime initialized with Backend::PDINFER in Device::CPU.
Batch id:0, example id:0, sentence1:花呗收款额度限制, sentence2:收钱码,对花呗支付的金额有限制吗, label:0, similarity:0.5099
Batch id:1, example id:0, sentence1:花呗支持高铁票支付吗, sentence2:为什么友付宝不支持花呗付款, label:0, similarity:0.9862

量化模型部署

该示例支持部署 Paddle INT8 新格式量化模型,仅需在--model_dir参数传入量化模型路径,并且在对应硬件上选择可用的推理引擎后端,即可完成量化模型部署。在 GPU 上部署量化模型时,可选后端为paddle_tensorrttensorrt;在CPU上部署量化模型时,可选后端为paddleonnx_runtime。下面将展示如何使用该示例完成量化模型部署,示例中的模型是按照 ERNIE 3.0 训练文档 压缩量化后导出得到的量化模型。

# 在GPU上使用 tensorrt 后端,模型目录可按照实际模型路径设置
python seq_cls_infer.py --model_dir ../../best_models/afqmc/width_mult_0.75/mse16_1/ --device gpu --backend tensorrt --model_prefix int8

# 在CPU上使用paddle_inference后端,模型目录可按照实际模型路径设置
python seq_cls_infer.py --model_dir ../../best_models/afqmc/width_mult_0.75/mse16_1/ --device cpu --backend paddle --model_prefix int8

运行完成后返回的结果如下:

[INFO] fastdeploy/runtime/runtime.cc(101)::Init    Runtime initialized with Backend::PDINFER in Device::GPU.
Batch id:0, example id:0, sentence1:花呗收款额度限制, sentence2:收钱码,对花呗支付的金额有限制吗, label:0, similarity:0.5224
Batch id:1, example id:0, sentence1:花呗支持高铁票支付吗, sentence2:为什么友付宝不支持花呗付款, label:0, similarity:0.9856

参数说明

seq_cls_infer.py 除了以上示例的命令行参数,还支持更多命令行参数的设置。以下为各命令行参数的说明。

参数 参数说明
--model_dir 指定部署模型的目录,
--batch_size 输入的batch size,默认为 1
--max_length 最大序列长度,默认为 128
--device 运行的设备,可选范围: ['cpu', 'gpu'],默认为'cpu'
--backend 支持的推理后端,可选范围: ['onnx_runtime', 'paddle', 'openvino', 'tensorrt', 'paddle_tensorrt'],默认为'paddle'
--use_fp16 是否使用FP16模式进行推理。使用tensorrt和paddle_tensorrt后端时可开启,默认为False
--use_fast 是否使用FastTokenizer加速分词阶段。默认为True

序列标注任务

快速开始

以下示例展示如何基于 FastDeploy 库完成 ERNIE 3.0 Medium 模型在 CLUE Benchmark 的 MSRA_NER 数据集上进行序列标注任务的Python预测部署,可通过命令行参数--device以及--backend指定运行在不同的硬件以及推理引擎后端,并使用--model_dir参数指定运行的模型,具体参数设置可查看下面参数说明。示例中的模型是按照 ERNIE 3.0 训练文档导出得到的部署模型,其模型目录为model_zoo/ernie-3.0/best_models/msra_ner/export(用户可按实际情况设置)。

# CPU 推理
python token_cls_infer.py --model_dir ../../best_models/msra_ner/export/ --device cpu --backend paddle

# GPU 推理
python token_cls_infer.py --model_dir ../../best_models/msra_ner/export/ --device gpu --backend paddle

运行完成后返回的结果如下:

[INFO] fastdeploy/runtime.cc(500)::Init    Runtime initialized with Backend::PDINFER in Device::CPU.
input data: 北京的涮肉,重庆的火锅,成都的小吃都是极具特色的美食。
The model detects all entities:
entity: 北京   label: LOC   pos: [0, 1]
entity: 重庆   label: LOC   pos: [6, 7]
entity: 成都   label: LOC   pos: [12, 13]
-----------------------------
input data: 乔丹、科比、詹姆斯和姚明都是篮球界的标志性人物。
The model detects all entities:
entity: 乔丹   label: PER   pos: [0, 1]
entity: 科比   label: PER   pos: [3, 4]
entity: 詹姆斯   label: PER   pos: [6, 8]
entity: 姚明   label: PER   pos: [10, 11]
-----------------------------

量化模型部署

该示例支持部署 Paddle INT8 新格式量化模型,仅需在--model_dir参数传入量化模型路径,并且在对应硬件上选择可用的推理引擎后端,即可完成量化模型部署。在 GPU 上部署量化模型时,可选后端为paddle_tensorrttensorrt;在CPU上部署量化模型时,可选后端为paddleonnx_runtime。下面将展示如何使用该示例完成量化模型部署,示例中的模型是按照 ERNIE 3.0 训练文档 压缩量化后导出得到的量化模型。

# 在GPU上使用 tensorrt 后端,模型目录可按照实际模型路径设置
python token_cls_infer.py --model_dir ../../best_models/msra_ner/width_mult_0.75/mse16_1/ --device gpu --backend tensorrt --model_prefix int8

# 在CPU上使用paddle_inference后端,模型目录可按照实际模型路径设置
python token_cls_infer.py --model_dir ../../best_models/msra_ner/width_mult_0.75/mse16_1/ --device cpu --backend paddle --model_prefix int8

运行完成后返回的结果如下:

[INFO] fastdeploy/runtime.cc(500)::Init    Runtime initialized with Backend::PDINFER in Device::CPU.
input data: 北京的涮肉,重庆的火锅,成都的小吃都是极具特色的美食。
The model detects all entities:
entity: 北京   label: LOC   pos: [0, 1]
entity: 重庆   label: LOC   pos: [6, 7]
entity: 成都   label: LOC   pos: [12, 13]
-----------------------------
input data: 乔丹、科比、詹姆斯和姚明都是篮球界的标志性人物。
The model detects all entities:
entity: 乔丹   label: PER   pos: [0, 1]
entity: 科比   label: PER   pos: [3, 4]
entity: 詹姆斯   label: PER   pos: [6, 8]
entity: 姚明   label: PER   pos: [10, 11]
-----------------------------

参数说明

token_cls_infer.py 除了以上示例的命令行参数,还支持更多命令行参数的设置。以下为各命令行参数的说明。

参数 参数说明
--model_dir 指定部署模型的目录,
--batch_size 输入的batch size,默认为 1
--max_length 最大序列长度,默认为 128
--device 运行的设备,可选范围: ['cpu', 'gpu'],默认为'cpu'
--backend 支持的推理后端,可选范围: ['onnx_runtime', 'paddle', 'openvino', 'tensorrt', 'paddle_tensorrt'],默认为'paddle'
--use_fp16 是否使用FP16模式进行推理。使用tensorrt和paddle_tensorrt后端时可开启,默认为False
--use_fast 是否使用FastTokenizer加速分词阶段。默认为True
--model_prefix 模型文件前缀。前缀会分别与'.pdmodel'和'.pdiparams'拼接得到模型文件名和参数文件名。默认为 'model'

FastDeploy 高阶用法

FastDeploy 在 Python 端上,提供 fastdeploy.RuntimeOption.use_xxx() 以及 fastdeploy.RuntimeOption.use_xxx_backend() 接口支持开发者选择不同的硬件、不同的推理引擎进行部署。在不同的硬件上部署 ERNIE 3.0 模型,需要选择硬件所支持的推理引擎进行部署,下表展示如何在不同的硬件上选择可用的推理引擎部署 ERNIE 3.0 模型。

符号说明: (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
OpenVINO use_openvino_backend() N/A
GPU use_gpu() Paddle Inference use_paddle_infer_backend() N/A
ONNX Runtime use_ort_backend()
Paddle TensorRT use_trt_backend() + enable_paddle_to_trt()
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

相关文档

ERNIE 3.0模型详细介绍

ERNIE 3.0模型C++部署方法