Skip to content

Latest commit

 

History

History
289 lines (234 loc) · 12.3 KB

File metadata and controls

289 lines (234 loc) · 12.3 KB

FastDeploy ERNIE 3.0 Tiny 模型 Python 部署示例

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

本目录下分别提供 infer_demo.py 快速完成在 CPU/GPU 的车载语音场景下的口语理解(Spoken Language Understanding,SLU)任务的 Python 部署示例,并展示使用 FastTokenizer 后,端到端预测性能的 Benchmark。

依赖安装

直接执行以下命令安装部署示例的依赖。

# 安装fast_tokenizer以及GPU版本fastdeploy
pip install fast-tokenizer-python fastdeploy-gpu-python -f https://www.paddlepaddle.org.cn/whl/fastdeploy.html

快速开始

以下示例可通过命令行参数--device以及--backend指定运行在不同的硬件以及推理引擎后端,并使用--model_dir参数指定运行的模型,具体参数设置可查看下面参数说明。示例中的模型是按照ERNIE 3.0 Tiny 训练文档导出得到的部署模型,其模型目录为model_zoo/ernie-tiny/output/BS64_LR5e-5_20EPOCHS_WD0.01_WR0.1/(用户可按实际情况设置)。

# 在GPU上使用paddle_inference后端,模型目录可按照实际模型路径设置
python infer_demo.py --device gpu --backend paddle --model_dir ../../output/BS64_LR5e-5_20EPOCHS_WD0.01_WR0.1 --slot_label_path ../../data/slot_label.txt --intent_label_path ../../data/intent_label.txt

# 在CPU上使用paddle_inference后端,模型目录可按照实际模型路径设置
python infer_demo.py --device cpu --backend paddle --model_dir ../../output/BS64_LR5e-5_20EPOCHS_WD0.01_WR0.1 --slot_label_path ../../data/slot_label.txt --intent_label_path ../../data/intent_label.txt

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

[INFO] fastdeploy/runtime.cc(596)::Init    Runtime initialized with Backend::PDINFER in Device::GPU.
No. 0 text = 来一首周华健的花心
{'intent': 'music.play', 'confidence': 0.99833965, 'slot': [{'slot': 'singer', 'entity': '周华健', 'pos': [3, 5]}, {'slot': 'song', 'entity': '花心', 'pos': [7, 8]}]}
No. 1 text = 播放我们都一样
{'intent': 'music.play', 'confidence': 0.9985164, 'slot': [{'slot': 'song', 'entity': '我们都一样', 'pos': [2, 6]}]}
No. 2 text = 到信阳市汽车配件城
{'intent': 'navigation.navigation', 'confidence': 0.998626, 'slot': [{'slot': 'destination', 'entity': '信阳市汽车配件城', 'pos': [1, 8]}]}

量化模型部署

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

# 在 GPU 上使用 tensorrt 后端,模型目录可按照实际模型路径设置
python infer_demo.py --device gpu --backend tensorrt --model_prefix int8 --model_dir ../../output/BS64_LR5e-5_20EPOCHS_WD0.01_WR0.1 --slot_label_path ../../data/slot_label.txt --intent_label_path ../../data/intent_label.txt

# 在 CPU 上使用 paddle_inference 后端,模型目录可按照实际模型路径设置
python infer_demo.py --device cpu --backend paddle --model_prefix int8 --model_dir ../../output/BS64_LR5e-5_20EPOCHS_WD0.01_WR0.1_quant --slot_label_path ../../data/slot_label.txt --intent_label_path ../../data/intent_label.txt

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

[INFO] fastdeploy/runtime.cc(517)::Init    Runtime initialized with Backend::TRT in Device::GPU.
No. 0 text = 来一首周华健的花心
{'intent': 'music.play', 'confidence': 0.99706995, 'slot': [{'slot': 'singer', 'entity': '周华健', 'pos': [3, 5]}, {'slot': 'song', 'entity': '花心', 'pos': [7, 8]}]}
No. 1 text = 播放我们都一样
{'intent': 'music.play', 'confidence': 0.9973666, 'slot': [{'slot': 'song', 'entity': '我们都一样', 'pos': [2, 6]}]}
No. 2 text = 到信阳市汽车配件城
{'intent': 'navigation.navigation', 'confidence': 0.99799216, 'slot': [{'slot': 'destination', 'entity': '信阳市汽车配件城', 'pos': [1, 8]}]}

参数说明

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

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

FastDeploy 高阶用法

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

符号说明: (1) ✅: 已经支持; (2) ❔: 正在进行中; (3) N/A: 暂不支持;

硬件 硬件对应的接口 可用的推理引擎 推理引擎对应的接口 是否支持 ERNIE 3.0 Tiny 模型 是否支持 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

性能 Benchmark

在 Python 端上,以往会使用纯 Python 实现的 Tokenizer 进行分词,在处理大规模文本下往往会显得十分低效。为了解决这个问题,我们使用 PaddleNLP 的 FastTokenizer 工具,该工具使用 C++ 实现,并集成了 Google 提出的 Fast WordPiece Tokenization 快速分词算法,可以大大提升分词阶段性能。开发者安装 FastTokenizer 后,可以使用 PaddleNLP 提供的 AutoTokenizer.from_pretrained 加载 Tokenizer,并通过传入 use_fast=True 的参数使用 FastTokenizer。下面对比使用 FastTokenizer 前后,FP32 模型与量化 INT8 模型在 GPU 上使用 Paddle Inference 以及 Paddle TensorRT 后端预测的预测性能。

实验环境

GPU型号 A10
CUDA版本 11.6
cuDNN版本 8.4.0
CPU型号 Intel(R) Xeon(R) Gold 6271C CPU @ 2.60GHz

参数设置

batch size = 32,max length = 16。

测试文本长度15。

性能对比

FP32 模型

使用 Paddle Inference 后端预测

切词方式 端到端延时(ms) Runtime延时(ms) Tokenizer延时(ms) PostProcess延时(ms)
FastTokenizer 2.5047 0.9702 1.1807 0.3538
Python Tokenizer 8.9028 0.9987 7.5499 0.3541

INT8 模型

使用 Paddle TensorRT 后端预测

切词方式 端到端延时(ms) Runtime延时(ms) Tokenizer延时(ms) PostProcess延时(ms)
FastTokenizer 2.5707 1.0858 1.1233 0.3616
Python Tokenizer 9.2509 1.0543 7.8407 0.3559

结论:在此 ERNIE 3.0 Tiny 模型部署场景下,使用 FastTokenizer 可以大大加速分词阶段,分词阶段性能加速比为 6.39x~6.98x,端到端性能加速比为 3.56x~3.59x

相关文档

ERNIE 3.0 Tiny模型详细介绍

ERNIE 3.0 Tiny模型C++部署方法

ERNIE 3.0 Tiny模型Android部署方法

FastDeploy SDK安装文档