Skip to content

【PaddleNLP No.19】Create ernie1.0 infer example to fit pd3.0.0 #10426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions slm/model_zoo/ernie-1.0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ python run_qa.py \
<a name="预测部署"></a>

## 4. 预测部署
以中文文本情感分类问题为例,介绍一下从模型 finetune 到部署的过程。
以中文文本情感分类问题为例,介绍一下从模型 finetune 到部署的过程(已经在 PaddlePaddle 3.0.0版本验证)

与之前的 finetune 参数配置稍有区别,此处加入了一些配置选项。

Expand Down Expand Up @@ -644,20 +644,24 @@ python run_seq_cls.py \
--save_total_limit 3 \

```
训练完导出模型之后,可以用于部署,`deploy/seq_cls_infer.py`文件提供了 python 部署预测示例。可执行以下命令运行部署示例:
训练完导出模型之后,可以用于部署,`deploy/infer.py`文件提供了 python 部署预测示例。可执行以下命令运行部署示例:

```shell
python deploy/seq_cls_infer.py --model_dir tmp/chnsenticorp_v2/export/ --device cpu --backend paddle
python deploy/infer.py --model_dir tmp/chnsenticorp_v2/export/ --device gpu
```

运行后预测结果打印如下:
```text
WARNING: Logging before InitGoogleLogging() is written to STDERR
W0301 08:25:37.617117 58742 analysis_config.cc:958] It is detected that mkldnn and memory_optimize_pass are enabled at the same time, but they are not supported yet. Currently, memory_optimize_pass is explicitly disabled
[INFO] fastdeploy/runtime/runtime.cc(266)::CreatePaddleBackend Runtime initialized with Backend::PDINFER in Device::CPU.
Batch id: 0, example id: 0, sentence: 这个宾馆比较陈旧了,特价的房间也很一般。总体来说一般, label: negative, negative prob: 0.9999, positive prob: 0.0001.
Batch id: 1, example id: 0, sentence: 怀着十分激动的心情放映,可是看着看着发现,在放映完毕后,出现一集米老鼠的动画片!开始还怀疑是不是赠送的个别现象,可是后来发现每张DVD后面都有!真不知道生产商怎么想的,我想看的是猫和老鼠,不是米老鼠!如果厂家是想赠送的话,那就全套米老鼠和唐老鸭都赠送,只在每张DVD后面添加一集算什么??简直是画蛇添足!!, label: negative, negative prob: 0.9998, positive prob: 0.0002.
Batch id: 2, example id: 0, sentence: 还稍微重了点,可能是硬盘大的原故,还要再轻半斤就好了。其他要进一步验证。贴的几种膜气泡较多,用不了多久就要更换了,屏幕膜稍好点,但比没有要强多了。建议配赠几张膜让用用户自己贴。, label: negative, negative prob: 0.9999, positive prob: 0.0001.
Batch id: 1189, example id: 0, sentence: 作为五星级 酒店的硬件是差了点 装修很久 电视很小 只是位置很好 楼下是DFS 对面是海港城 但性价比不高, label: positive, negative prob: 0.0001, positive prob: 0.9999.
Batch id: 1190, example id: 0, sentence: 最好别去,很差,看完很差想换酒店,他们竟跟我要服务费.也没待那房间2分种,居然解决了问题,可觉的下次不能去的,, label: negative, negative prob: 1.0000, positive prob: 0.0000.
Batch id: 1191, example id: 0, sentence: 看了一半就看不下去了,后半本犹豫几次都放下没有继续看的激情,故事平淡的连个波折起伏都没有,职场里那点事儿也学得太模糊,没有具体描述,而且杜拉拉就做一个行政而已,是个人都会做的没有技术含量的工作 也能描写的这么有技术含量 真是为难作者了本来冲着畅销排行第一买来看看,觉得总不至于大部分人都没品味吧?结果证明这个残酷的事实,一本让人如同嚼蜡的“畅销书”......, label: negative, negative prob: 0.9999, positive prob: 0.0001.
Batch id: 1192, example id: 0, sentence: 酒店环境很好 就是有一点点偏 交通不是很便利 去哪都需要达车 关键是不好打 酒店应该想办法解决一下, label: positive, negative prob: 0.0003, positive prob: 0.9997.
Batch id: 1193, example id: 0, sentence: 价格在这个地段属于适中, 附近有早餐店,小饭店, 比较方便,无早也无所, label: positive, negative prob: 0.1121, positive prob: 0.8879.
Batch id: 1194, example id: 0, sentence: 酒店的位置不错,附近都靠近购物中心和写字楼区。以前来大连一直都住,但感觉比较陈旧了。住的期间,酒店在进行装修,翻新和升级房间设备。好是好,希望到时房价别涨太多了。, label: positive, negative prob: 0.0000, positive prob: 1.0000.
Batch id: 1195, example id: 0, sentence: 位置不很方便,周围乱哄哄的,卫生条件也不如其他如家的店。以后绝不会再住在这里。, label: negative, negative prob: 1.0000, positive prob: 0.0000.
Batch id: 1196, example id: 0, sentence: 抱着很大兴趣买的,买来粗粗一翻排版很不错,姐姐还说快看吧,如果好我也买一本。可是真的看了,实在不怎么样。就是中文里夹英文单词说话,才翻了2页实在不想勉强自己了。我想说的是,练习英文单词,靠这本书肯定没有效果,其它好的方法比这强多了。, label: negative, negative prob: 1.0000, positive prob: 0.0000.
Batch id: 1197, example id: 0, sentence: 东西不错,不过有人不太喜欢镜面的,我个人比较喜欢,总之还算满意。, label: positive, negative prob: 0.0001, positive prob: 0.9999.
Batch id: 1198, example id: 0, sentence: 房间不错,只是上网速度慢得无法忍受,打开一个网页要等半小时,连邮件都无法收。另前台工作人员服务态度是很好,只是效率有得改善。, label: positive, negative prob: 0.0001, positive prob: 0.9999.
......
```

Expand Down
116 changes: 16 additions & 100 deletions slm/model_zoo/ernie-1.0/finetune/deploy/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# FastDeploy ERNIE 1.0 模型 Python 部署示例
# ERNIE 1.0 模型 Python 推理示例

在部署前,参考 [FastDeploy SDK 安装文档](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/download_prebuilt_libraries.md)安装 FastDeploy Python SDK。

本目录下提供 `seq_cls_infer.py` 快速完成在 CPU/GPU 的中文情感分类任务的 Python 部署示例。
本目录下提供 `infer.py` 快速完成在 CPU/GPU 的中文情感分类任务的 Python 推理示例。

## 快速开始

以下示例展示如何基于 FastDeploy 库完成 ERNIE 1.0 模型在 ChnSenticorp 数据集上进行文本分类任务的 Python 预测部署,可通过命令行参数`--device`以及`--backend`指定运行在不同的硬件以及推理引擎后端,并使用`--model_dir`参数指定运行的模型,具体参数设置可查看下面[参数说明](#参数说明)。示例中的模型是按照 [ERNIE 1.0 训练文档](../../README.md)导出得到的部署模型,其模型目录为`model_zoo/ernie-1.0/finetune/tmp/export`(用户可按实际情况设置)。
以下示例展示 ERNIE 1.0 模型在 ChnSenticorp 数据集上进行文本分类任务的 Python 预测部署,可通过命令行参数`--device`指定运行在不同的硬件,并使用`--model_dir`参数指定运行的模型,具体参数设置可查看下面[参数说明](#参数说明)。示例中的模型是按照 [ERNIE 1.0 训练文档](../../README.md)导出得到的部署模型,其模型目录为`model_zoo/ernie-1.0/finetune/tmp/export`(用户可按实际情况设置)。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

赞👍


```bash
# CPU 推理
python seq_cls_infer.py --model_dir ../tmp/chnsenticorp_v2/export/ --device cpu --backend paddle
python infer.py --model_dir ../tmp/chnsenticorp_v2/export/ --device cpu
# GPU 推理
python seq_cls_infer.py --model_dir ../tmp/chnsenticorp_v2/export/ --device gpu --backend paddle
python infer.py --model_dir ../tmp/chnsenticorp_v2/export/ --device gpu
```

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

```bash
[INFO] fastdeploy/runtime/runtime.cc(266)::CreatePaddleBackend Runtime initialized with Backend::PDINFER in Device::GPU.
Batch id: 0, example id: 0, sentence: 这个宾馆比较陈旧了,特价的房间也很一般。总体来说一般, label: negative, negative prob: 0.9999, positive prob: 0.0001.
Batch id: 1, example id: 0, sentence: 怀着十分激动的心情放映,可是看着看着发现,在放映完毕后,出现一集米老鼠的动画片!开始还怀疑是不是赠送的个别现象,可是后来发现每张DVD后面都有!真不知道生产商怎么想的,我想看的是猫和老鼠,不是米老鼠!如果厂家是想赠送的话,那就全套米老鼠和唐老鸭都赠送,只在每张DVD后面添加一集算什么??简直是画蛇添足!!, label: negative, negative prob: 0.9998, positive prob: 0.0002.
Batch id: 2, example id: 0, sentence: 还稍微重了点,可能是硬盘大的原故,还要再轻半斤就好了。其他要进一步验证。贴的几种膜气泡较多,用不了多久就要更换了,屏幕膜稍好点,但比没有要强多了。建议配赠几张膜让用用户自己贴。, label: negative, negative prob: 0.9999, positive prob: 0.0001.
......
Batch id: 1189, example id: 0, sentence: 作为五星级 酒店的硬件是差了点 装修很久 电视很小 只是位置很好 楼下是DFS 对面是海港城 但性价比不高, label: positive, negative prob: 0.0001, positive prob: 0.9999.
Batch id: 1190, example id: 0, sentence: 最好别去,很差,看完很差想换酒店,他们竟跟我要服务费.也没待那房间2分种,居然解决了问题,可觉的下次不能去的,, label: negative, negative prob: 1.0000, positive prob: 0.0000.
Batch id: 1191, example id: 0, sentence: 看了一半就看不下去了,后半本犹豫几次都放下没有继续看的激情,故事平淡的连个波折起伏都没有,职场里那点事儿也学得太模糊,没有具体描述,而且杜拉拉就做一个行政而已,是个人都会做的没有技术含量的工作 也能描写的这么有技术含量 真是为难作者了本来冲着畅销排行第一买来看看,觉得总不至于大部分人都没品味吧?结果证明这个残酷的事实,一本让人如同嚼蜡的“畅销书”......, label: negative, negative prob: 0.9999, positive prob: 0.0001.
Batch id: 1192, example id: 0, sentence: 酒店环境很好 就是有一点点偏 交通不是很便利 去哪都需要达车 关键是不好打 酒店应该想办法解决一下, label: positive, negative prob: 0.0003, positive prob: 0.9997.
Batch id: 1193, example id: 0, sentence: 价格在这个地段属于适中, 附近有早餐店,小饭店, 比较方便,无早也无所, label: positive, negative prob: 0.1121, positive prob: 0.8879.
Batch id: 1194, example id: 0, sentence: 酒店的位置不错,附近都靠近购物中心和写字楼区。以前来大连一直都住,但感觉比较陈旧了。住的期间,酒店在进行装修,翻新和升级房间设备。好是好,希望到时房价别涨太多了。, label: positive, negative prob: 0.0000, positive prob: 1.0000.
Batch id: 1195, example id: 0, sentence: 位置不很方便,周围乱哄哄的,卫生条件也不如其他如家的店。以后绝不会再住在这里。, label: negative, negative prob: 1.0000, positive prob: 0.0000.
Batch id: 1196, example id: 0, sentence: 抱着很大兴趣买的,买来粗粗一翻排版很不错,姐姐还说快看吧,如果好我也买一本。可是真的看了,实在不怎么样。就是中文里夹英文单词说话,才翻了2页实在不想勉强自己了。我想说的是,练习英文单词,靠这本书肯定没有效果,其它好的方法比这强多了。, label: negative, negative prob: 1.0000, positive prob: 0.0000.
Batch id: 1197, example id: 0, sentence: 东西不错,不过有人不太喜欢镜面的,我个人比较喜欢,总之还算满意。, label: positive, negative prob: 0.0001, positive prob: 0.9999.
Batch id: 1198, example id: 0, sentence: 房间不错,只是上网速度慢得无法忍受,打开一个网页要等半小时,连邮件都无法收。另前台工作人员服务态度是很好,只是效率有得改善。, label: positive, negative prob: 0.0001, positive prob: 0.9999.
......
```

Expand All @@ -36,92 +41,3 @@ Batch id: 2, example id: 0, sentence: 还稍微重了点,可能是硬盘大的
|--device | 运行的设备,可选范围: ['cpu', 'gpu'],默认为'cpu' |
|--device_id | 运行设备的 id。默认为0。 |
|--cpu_threads | 当使用 cpu 推理时,指定推理的 cpu 线程数,默认为1。|
|--backend | 支持的推理后端,可选范围: ['onnx_runtime', 'paddle', 'openvino', 'tensorrt', 'paddle_tensorrt'],默认为'paddle' |
|--use_fp16 | 是否使用 FP16模式进行推理。使用 tensorrt 和 paddle_tensorrt 后端时可开启,默认为 False |

## FastDeploy 高阶用法

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

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

<table>
<tr>
<td align=center> 硬件</td>
<td align=center> 硬件对应的接口</td>
<td align=center> 可用的推理引擎 </td>
<td align=center> 推理引擎对应的接口 </td>
<td align=center> 是否支持 Paddle 新格式量化模型 </td>
<td align=center> 是否支持 FP16 模式 </td>
</tr>
<tr>
<td rowspan=3 align=center> CPU </td>
<td rowspan=3 align=center> use_cpu() </td>
<td align=center> Paddle Inference </td>
<td align=center> use_paddle_infer_backend() </td>
<td align=center> ✅ </td>
<td align=center> N/A </td>
</tr>
<tr>
<td align=center> ONNX Runtime </td>
<td align=center> use_ort_backend() </td>
<td align=center> ✅ </td>
<td align=center> N/A </td>
</tr>
<tr>
<td align=center> OpenVINO </td>
<td align=center> use_openvino_backend() </td>
<td align=center> ❔ </td>
<td align=center> N/A </td>
</tr>
<tr>
<td rowspan=4 align=center> GPU </td>
<td rowspan=4 align=center> use_gpu() </td>
<td align=center> Paddle Inference </td>
<td align=center> use_paddle_infer_backend() </td>
<td align=center> ✅ </td>
<td align=center> N/A </td>
</tr>
<tr>
<td align=center> ONNX Runtime </td>
<td align=center> use_ort_backend() </td>
<td align=center> ✅ </td>
<td align=center> ❔ </td>
</tr>
<tr>
<td align=center> Paddle TensorRT </td>
<td align=center> use_paddle_infer_backend() + paddle_infer_option.enable_trt = True </td>
<td align=center> ✅ </td>
<td align=center> ✅ </td>
</tr>
<tr>
<td align=center> TensorRT </td>
<td align=center> use_trt_backend() </td>
<td align=center> ✅ </td>
<td align=center> ✅ </td>
</tr>
<tr>
<td align=center> 昆仑芯 XPU </td>
<td align=center> use_kunlunxin() </td>
<td align=center> Paddle Lite </td>
<td align=center> use_paddle_lite_backend() </td>
<td align=center> N/A </td>
<td align=center> ✅ </td>
</tr>
<tr>
<td align=center> 华为 昇腾 </td>
<td align=center> use_ascend() </td>
<td align=center> Paddle Lite </td>
<td align=center> use_paddle_lite_backend() </td>
<td align=center> ❔ </td>
<td align=center> ✅ </td>
</tr>
<tr>
<td align=center> Graphcore IPU </td>
<td align=center> use_ipu() </td>
<td align=center> Paddle Inference </td>
<td align=center> use_paddle_infer_backend() </td>
<td align=center> ❔ </td>
<td align=center> N/A </td>
</tr>
</table>
Loading
Loading