Skip to content

Commit 1d5591b

Browse files
authored
[PIR] Fix ernie-1.0 infer example and make it compatible with pd3.0.0 (#10426)
1 parent e6e4928 commit 1d5591b

File tree

4 files changed

+141
-258
lines changed

4 files changed

+141
-258
lines changed

slm/model_zoo/ernie-1.0/README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ python run_qa.py \
616616
<a name="预测部署"></a>
617617

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

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

@@ -644,20 +644,24 @@ python run_seq_cls.py \
644644
--save_total_limit 3 \
645645

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

649649
```shell
650-
python deploy/seq_cls_infer.py --model_dir tmp/chnsenticorp_v2/export/ --device cpu --backend paddle
650+
python deploy/infer.py --model_dir tmp/chnsenticorp_v2/export/ --device gpu
651651
```
652652

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

Lines changed: 16 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
# FastDeploy ERNIE 1.0 模型 Python 部署示例
1+
# ERNIE 1.0 模型 Python 推理示例
22

3-
在部署前,参考 [FastDeploy SDK 安装文档](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/download_prebuilt_libraries.md)安装 FastDeploy Python SDK。
4-
5-
本目录下提供 `seq_cls_infer.py` 快速完成在 CPU/GPU 的中文情感分类任务的 Python 部署示例。
3+
本目录下提供 `infer.py` 快速完成在 CPU/GPU 的中文情感分类任务的 Python 推理示例。
64

75
## 快速开始
86

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

119

1210
```bash
1311
# CPU 推理
14-
python seq_cls_infer.py --model_dir ../tmp/chnsenticorp_v2/export/ --device cpu --backend paddle
12+
python infer.py --model_dir ../tmp/chnsenticorp_v2/export/ --device cpu
1513
# GPU 推理
16-
python seq_cls_infer.py --model_dir ../tmp/chnsenticorp_v2/export/ --device gpu --backend paddle
14+
python infer.py --model_dir ../tmp/chnsenticorp_v2/export/ --device gpu
1715
```
1816

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

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

@@ -36,92 +41,3 @@ Batch id: 2, example id: 0, sentence: 还稍微重了点,可能是硬盘大的
3641
|--device | 运行的设备,可选范围: ['cpu', 'gpu'],默认为'cpu' |
3742
|--device_id | 运行设备的 id。默认为0。 |
3843
|--cpu_threads | 当使用 cpu 推理时,指定推理的 cpu 线程数,默认为1。|
39-
|--backend | 支持的推理后端,可选范围: ['onnx_runtime', 'paddle', 'openvino', 'tensorrt', 'paddle_tensorrt'],默认为'paddle' |
40-
|--use_fp16 | 是否使用 FP16模式进行推理。使用 tensorrt 和 paddle_tensorrt 后端时可开启,默认为 False |
41-
42-
## FastDeploy 高阶用法
43-
44-
FastDeploy 在 Python 端上,提供 `fastdeploy.RuntimeOption.use_xxx()` 以及 `fastdeploy.RuntimeOption.use_xxx_backend()` 接口支持开发者选择不同的硬件、不同的推理引擎进行部署。在不同的硬件上部署 ERNIE 1.0 模型,需要选择硬件所支持的推理引擎进行部署,下表展示如何在不同的硬件上选择可用的推理引擎部署 ERNIE 1.0 模型。
45-
46-
符号说明: (1) ✅: 已经支持; (2) ❔: 正在进行中; (3) N/A: 暂不支持;
47-
48-
<table>
49-
<tr>
50-
<td align=center> 硬件</td>
51-
<td align=center> 硬件对应的接口</td>
52-
<td align=center> 可用的推理引擎 </td>
53-
<td align=center> 推理引擎对应的接口 </td>
54-
<td align=center> 是否支持 Paddle 新格式量化模型 </td>
55-
<td align=center> 是否支持 FP16 模式 </td>
56-
</tr>
57-
<tr>
58-
<td rowspan=3 align=center> CPU </td>
59-
<td rowspan=3 align=center> use_cpu() </td>
60-
<td align=center> Paddle Inference </td>
61-
<td align=center> use_paddle_infer_backend() </td>
62-
<td align=center> ✅ </td>
63-
<td align=center> N/A </td>
64-
</tr>
65-
<tr>
66-
<td align=center> ONNX Runtime </td>
67-
<td align=center> use_ort_backend() </td>
68-
<td align=center> ✅ </td>
69-
<td align=center> N/A </td>
70-
</tr>
71-
<tr>
72-
<td align=center> OpenVINO </td>
73-
<td align=center> use_openvino_backend() </td>
74-
<td align=center> ❔ </td>
75-
<td align=center> N/A </td>
76-
</tr>
77-
<tr>
78-
<td rowspan=4 align=center> GPU </td>
79-
<td rowspan=4 align=center> use_gpu() </td>
80-
<td align=center> Paddle Inference </td>
81-
<td align=center> use_paddle_infer_backend() </td>
82-
<td align=center> ✅ </td>
83-
<td align=center> N/A </td>
84-
</tr>
85-
<tr>
86-
<td align=center> ONNX Runtime </td>
87-
<td align=center> use_ort_backend() </td>
88-
<td align=center> ✅ </td>
89-
<td align=center> ❔ </td>
90-
</tr>
91-
<tr>
92-
<td align=center> Paddle TensorRT </td>
93-
<td align=center> use_paddle_infer_backend() + paddle_infer_option.enable_trt = True </td>
94-
<td align=center> ✅ </td>
95-
<td align=center> ✅ </td>
96-
</tr>
97-
<tr>
98-
<td align=center> TensorRT </td>
99-
<td align=center> use_trt_backend() </td>
100-
<td align=center> ✅ </td>
101-
<td align=center> ✅ </td>
102-
</tr>
103-
<tr>
104-
<td align=center> 昆仑芯 XPU </td>
105-
<td align=center> use_kunlunxin() </td>
106-
<td align=center> Paddle Lite </td>
107-
<td align=center> use_paddle_lite_backend() </td>
108-
<td align=center> N/A </td>
109-
<td align=center> ✅ </td>
110-
</tr>
111-
<tr>
112-
<td align=center> 华为 昇腾 </td>
113-
<td align=center> use_ascend() </td>
114-
<td align=center> Paddle Lite </td>
115-
<td align=center> use_paddle_lite_backend() </td>
116-
<td align=center> ❔ </td>
117-
<td align=center> ✅ </td>
118-
</tr>
119-
<tr>
120-
<td align=center> Graphcore IPU </td>
121-
<td align=center> use_ipu() </td>
122-
<td align=center> Paddle Inference </td>
123-
<td align=center> use_paddle_infer_backend() </td>
124-
<td align=center> ❔ </td>
125-
<td align=center> N/A </td>
126-
</tr>
127-
</table>

0 commit comments

Comments
 (0)