1
1
# ERNIE 3.0 Python部署指南
2
-
3
- ## 安装
2
+ 本文介绍ERNIE 3.0 Python端的部署,包括部署环境的准备,命名实体识别和分类两大场景下的使用示例。
3
+ - [ ERNIE 3.0 Python部署指南] ( #ERNIE3.0Python部署指南 )
4
+ - [ 1. 环境准备] ( #1-环境准备 )
5
+ - [ 1.1 CPU端] ( #11-CPU端 )
6
+ - [ 1.2 GPU端] ( #12-GPU端 )
7
+ - [ 2. 命名实体识别模型推理] ( #2-命名实体识别模型推理 )
8
+ - [ 2.1 模型获取] ( #21-模型获取 )
9
+ - [ 2.2 CPU端推理样例] ( #22-CPU端推理样例 )
10
+ - [ 2.3 GPU端推理样例] ( #23-GPU端推理样例 )
11
+ - [ 3. 分类模型推理] ( #3-分类模型推理 )
12
+ - [ 3.1 模型获取] ( #31-模型获取 )
13
+ - [ 3.2 CPU端推理样例] ( #32-CPU端推理样例 )
14
+ - [ 3.3 GPU端推理样例] ( #33-GPU端推理样例 )
15
+ ## 1. 环境准备
4
16
ERNIE 3.0的部署分为CPU和GPU两种情况,请根据你的部署环境安装对应的依赖。
5
- ### CPU端
6
- CPU端的部署请使用如下指令安装所需依赖
17
+ ### 1.1 CPU端
18
+ CPU端的部署请使用如下命令安装所需依赖
7
19
```
8
20
pip install -r requirements_cpu.txt
9
21
```
10
- ### GPU端
11
- 在进行GPU部署之前请先确保机器已经安装好CUDA >= 11.2,CuDNN >= 8.2,然后请使用如下指令安装所需依赖
22
+ ### 1.2 GPU端
23
+ 为了在GPU上获得最佳的推理性能和稳定性,请先确保机器已正确安装NVIDIA相关驱动和基础软件,确保CUDA >= 11.2,CuDNN >= 8.2,并使用以下命令安装所需依赖
12
24
```
13
25
pip install -r requirements_gpu.txt
14
26
```
15
- 在计算能力(Compute Capability)大于7.0的GPU硬件上,比如T4,如需FP16或者Int8量化推理加速,还需安装TensorRT和Paddle Inference,计算能力(Compute Capability)和精度支持情况请参考:[ GPU算力和支持精度对照表] ( https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-840-ea/support-matrix/index.html#hardware-precision-matrix )
27
+ 如需使用半精度(FP16)或量化(INT8)部署,请确保GPU设备的CUDA计算能力 (CUDA Compute Capability) 大于7.0,典型的设备包括V100、T4、A10、A100、GTX 20系列和30系列显卡等。同时需额外安装TensorRT和Paddle Inference。
28
+ 更多关于CUDA Compute Capability和精度支持情况请参考NVIDIA文档:[ GPU硬件与支持精度对照表] ( https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-840-ea/support-matrix/index.html#hardware-precision-matrix )
16
29
1 . TensorRT安装请参考:[ TensorRT安装说明] ( https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-840-ea/install-guide/index.html#overview ) ,Linux端简要步骤如下:
17
30
(1)下载TensorRT8.2版本,文件名TensorRT-XXX.tar.gz,[ 下载链接] ( https://developer.nvidia.com/tensorrt )
18
31
(2)解压得到TensorRT-XXX文件夹
@@ -23,88 +36,200 @@ pip install -r requirements_gpu.txt
23
36
(2)使用pip install安装下载好的Paddle Inference安装包
24
37
25
38
26
- ## 使用说明
27
- 如果使用CPU,请运行infer_cpu.py进行部署,如果使用gpu,请运行infer_gpu.py进行部署,请根据你的部署设备选择相应的部署脚本
28
- ### CPU端
29
- 在CPU端,请使用如下指令进行部署
39
+ ## 2. 命名实体识别模型推理
40
+ ### 2.1 模型获取
41
+ 用户可使用自己训练的模型进行推理,具体训练调优方法可参考[ 模型训练调优] ( ./../../README.md#微调 ) ,也可以使用我们提供的msra_ner数据集训练的ERNIE 3.0模型,请执行如下命令获取模型:
42
+ ```
43
+ # 获取命名实体识别FP32模型
44
+ wget https://paddlenlp.bj.bcebos.com/models/transformers/ernie_3.0/msra_ner_pruned_infer_model.zip
45
+ unzip msra_ner_pruned_infer_model.zip
30
46
```
31
- python infer_cpu.py --task_name token_cls --model_path ./ner_model/infer
47
+ ### 2.2 CPU端推理样例
48
+ 在CPU端,请使用如下命令进行部署
49
+ ```
50
+ python infer_cpu.py --task_name token_cls --model_path ./msra_ner_pruned_infer_model/float32
32
51
```
33
52
输出打印如下:
34
53
```
35
- input data: 古老的文明,使我们引以为豪,彼此钦佩 。
54
+ input data: 北京的涮肉,重庆的火锅,成都的小吃都是极具特色的美食 。
36
55
The model detects all entities:
56
+ entity: 北京 label: LOC pos: [0, 1]
57
+ entity: 重庆 label: LOC pos: [6, 7]
58
+ entity: 成都 label: LOC pos: [12, 13]
37
59
-----------------------------
38
- input data: 原产玛雅故国的玉米,早已成为华夏大地主要粮食作物之一 。
60
+ input data: 乔丹、科比、詹姆斯和姚明都是篮球界的标志性人物 。
39
61
The model detects all entities:
40
- entity: 玛雅 label: LOC pos: [2, 3]
41
- entity: 华夏 label: LOC pos: [14, 15]
62
+ entity: 乔丹 label: PER pos: [0, 1]
63
+ entity: 科比 label: PER pos: [3, 4]
64
+ entity: 詹姆斯 label: PER pos: [6, 8]
65
+ entity: 姚明 label: PER pos: [10, 11]
42
66
-----------------------------
43
67
```
44
- 参数说明 :
68
+ infer_cpu.py脚本中的参数说明 :
45
69
| 参数 | 参数说明 |
46
70
| ----------| --------------|
47
71
| --task_name | 配置任务名称,可选seq_cls和token_cls,默认为seq_cls|
48
72
| --model_name_or_path | 模型的路径或者名字,默认为ernie-3.0-medium-zh|
49
73
| --model_path | 用于推理的Paddle模型的路径|
50
74
| --max_seq_length | 最大序列长度,默认为128|
51
- | --enable_quantize | 是否使用动态量化进行加速,默认关闭 |
75
+ | --use_quantize | 是否使用动态量化进行加速,默认关闭 |
52
76
| --num_threads | 配置cpu的线程数,默认为cpu的最大线程数 |
53
77
54
- ** Note** :在支持avx512_vnni指令集或Intel® DL Boost的CPU设备上,可开启enable_quantize开关对FP32模型进行动态量化以获得更高的推理性能。
55
- batch size为32,max_seq_length为128时,推理加速情况如下表所示:
56
- 当线程数为1时
57
- | 数据集 | FP32模型推理时间 | INT8量化推理时间 | 加速比 |
58
- | ----------| --------------| -------------| -------------|
59
- | tnews | 1026.41| 425.89| 2.41|
60
- | msra_ner | 3218.55| 1442.10| 2.23|
61
- | cmrc2018 | 7833.57| 3680.89| 2.12|
62
-
63
- 当线程为10时
64
- | 数据集 | FP32模型推理时间 | INT8量化推理时间 | 加速比 |
65
- | ----------| --------------| -------------| -------------|
66
- | tnews | 105.97| 68.48| 1.73|
67
- | msra_ner | 392.42| 233.42| 1.68|
68
- | cmrc2018 | 999.76| 599.84| 1.67|
69
-
78
+ ** Note** :在支持avx512_vnni指令集或Intel® DL Boost的CPU设备上,可开启use_quantize开关对FP32模型进行动态量化以获得更高的推理性能,具体性能提升情况请查阅[ 量化性能提升情况] ( ../../README.md#压缩效果 ) 。
79
+ CPU端,开启动态量化的命令如下:
80
+ ```
81
+ python infer_cpu.py --task_name token_cls --model_path ./msra_ner_pruned_infer_model/float32 --use_quantize
82
+ ```
83
+ INT8的输出打印和FP32的输出打印一致。
70
84
71
- ### GPU端
72
- 在GPU端,请使用如下指令进行部署
85
+ ### 2.3 GPU端推理样例
86
+ 在GPU端,请使用如下命令进行部署
73
87
```
74
- python infer_gpu.py --task_name token_cls --model_path ./ner_model/infer
88
+ python infer_gpu.py --task_name token_cls --model_path ./msra_ner_pruned_infer_model/float32
75
89
```
76
90
输出打印如下:
77
91
```
78
- input data: 古老的文明,使我们引以为豪,彼此钦佩 。
92
+ input data: 北京的涮肉,重庆的火锅,成都的小吃都是极具特色的美食 。
79
93
The model detects all entities:
94
+ entity: 北京 label: LOC pos: [0, 1]
95
+ entity: 重庆 label: LOC pos: [6, 7]
96
+ entity: 成都 label: LOC pos: [12, 13]
80
97
-----------------------------
81
- input data: 原产玛雅故国的玉米,早已成为华夏大地主要粮食作物之一 。
98
+ input data: 乔丹、科比、詹姆斯和姚明都是篮球界的标志性人物 。
82
99
The model detects all entities:
83
- entity: 玛雅 label: LOC pos: [2, 3]
84
- entity: 华夏 label: LOC pos: [14, 15]
100
+ entity: 乔丹 label: PER pos: [0, 1]
101
+ entity: 科比 label: PER pos: [3, 4]
102
+ entity: 詹姆斯 label: PER pos: [6, 8]
103
+ entity: 姚明 label: PER pos: [10, 11]
85
104
-----------------------------
86
105
```
87
- 如果需要FP16进行加速,可以开启use_fp16开关,具体指令为
106
+ 如果需要FP16进行加速,可以开启use_fp16开关,具体命令为
88
107
```
89
- # 第一步,打开set_dynamic_shape开关,自动配置动态shape
90
- python infer_gpu.py --task_name token_cls --model_path ./ner_model/infer --use_fp16 --set_dynamic_shape
91
- # 第二步,开启预测
92
- python infer_gpu.py --task_name token_cls --model_path ./ner_model/infer --use_fp16
108
+ # 第一步,打开set_dynamic_shape开关,自动配置动态shape,在当前目录下生成dynamic_shape_info.txt文件
109
+ python infer_gpu.py --task_name token_cls --model_path ./msra_ner_pruned_infer_model/float32 --use_fp16 --shape_info_file dynamic_shape_info.txt --set_dynamic_shape
110
+ # 第二步,读取上一步中生成的dynamic_shape_info.txt文件, 开启预测
111
+ python infer_gpu.py --task_name token_cls --model_path ./msra_ner_pruned_infer_model/float32 --use_fp16 --shape_info_file dynamic_shape_info.txt
93
112
```
94
- 如果需要进行int8量化加速,还需要使用量化脚本对训练的FP32模型进行量化 ,然后使用量化后的模型进行部署,模型的量化请参考:[ 模型量化脚本使用说明] ( ./../../README.md ) ,量化模型的部署指令为
113
+ 如果需要进行INT8量化加速,还需要使用量化脚本对训练好的FP32模型进行量化 ,然后使用量化后的模型进行部署,模型的量化请参考:[ 模型量化脚本使用说明] ( ./../../README.md#模型压缩 ) ,也可下载我们量化后的INT8模型进行部署,请执行如下命令获取模型:
95
114
```
96
- # 第一步,打开set_dynamic_shape开关,自动配置动态shape
97
- python infer_gpu.py --task_name token_cls --model_path ./ner_quant_model/int --set_dynamic_shape
98
- # 第二步,开启预测
99
- python infer_gpu.py --task_name token_cls --model_path ./ner_quant_model/int8
115
+ # 获取命名实体识别INT8量化模型
116
+ wget https://paddlenlp.bj.bcebos.com/models/transformers/ernie_3.0/msra_ner_quant_infer_model.zip
117
+ unzip msra_ner_quant_infer_model.zip
100
118
```
101
- 参数说明:
119
+ 量化模型的部署命令为:
120
+ ```
121
+ # 第一步,打开set_dynamic_shape开关,自动配置动态shape,在当前目录下生成dynamic_shape_info.txt文件
122
+ python infer_gpu.py --task_name token_cls --model_path ./msra_ner_quant_infer_model/int8 --shape_info_file dynamic_shape_info.txt --set_dynamic_shape
123
+ # 第二步,读取上一步中生成的dynamic_shape_info.txt文件,开启预测
124
+ python infer_gpu.py --task_name token_cls --model_path ./msra_ner_quant_infer_model/int8 --shape_info_file dynamic_shape_info.txt
125
+ ```
126
+ FP16和INT8推理的运行结果和FP32的运行结果一致。
127
+ infer_gpu.py脚本中的参数说明:
102
128
| 参数 | 参数说明 |
103
129
| ----------| --------------|
104
130
| --task_name | 配置任务名称,可选seq_cls和token_cls,默认为seq_cls|
105
131
| --model_name_or_path | 模型的路径或者名字,默认为ernie-3.0-medium-zh|
106
132
| --model_path | 用于推理的Paddle模型的路径|
107
133
| --batch_size | 最大可测的batch size,默认为32|
108
134
| --max_seq_length | 最大序列长度,默认为128|
135
+ | --shape_info_file | 指定dynamic shape info的存储文件名,默认为shape_info.txt |
136
+ | --set_dynamic_shape | 配置是否自动配置TensorRT的dynamic shape,开启use_fp16或者进行INT8量化推理时需要先开启此选项进行dynamic shape配置,生成shape_info.txt后再关闭,默认关闭 |
109
137
| --use_fp16 | 是否使用FP16进行加速,默认关闭 |
110
- | --set_dynamic_shape | 配置是否自动配置TensorRT的dynamic shape,开启use_fp16或者进行int8量化推理时需要先开启此选项进行dynamic shape配置,生成shape_info.txt后再关闭,默认关闭 |
138
+
139
+ ## 3. 分类模型推理
140
+ ### 3.1 模型获取
141
+ 用户可使用自己训练的模型进行推理,具体训练调优方法可参考[ 模型训练调优] ( ./../../README.md#微调 ) ,也可以使用我们提供的tnews数据集训练的ERNIE 3.0模型,请执行如下命令获取模型:
142
+ ```
143
+ # 分类模型模型:
144
+ wget https://paddlenlp.bj.bcebos.com/models/transformers/ernie_3.0/tnews_pruned_infer_model.zip
145
+ unzip tnews_pruned_infer_model.zip
146
+ ```
147
+ ### 3.2 CPU端推理样例
148
+ 在CPU端,请使用如下命令进行部署
149
+ ```
150
+ python infer_cpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/float32
151
+ ```
152
+ 输出打印如下:
153
+ ```
154
+ input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
155
+ seq cls result:
156
+ label: news_car confidence: 0.9929346442222595
157
+ -----------------------------
158
+ input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
159
+ seq cls result:
160
+ label: news_entertainment confidence: 0.998711347579956
161
+ -----------------------------
162
+ ```
163
+ 和命名实体识别模型推理类似,开启动态量化的命令如下:
164
+ ```
165
+ python infer_cpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/float32 --use_quantize
166
+ ```
167
+ 输出打印如下:
168
+ ```
169
+ input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
170
+ seq cls result:
171
+ label: news_car confidence: 0.9910931587219238
172
+ -----------------------------
173
+ input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
174
+ seq cls result:
175
+ label: news_entertainment confidence: 0.9977021813392639
176
+ -----------------------------
177
+ ```
178
+ ### 3.3 GPU端推理样例
179
+ 在GPU端,请使用如下命令进行部署
180
+ ```
181
+ python infer_gpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/float32
182
+ ```
183
+ 输出打印如下:
184
+ ```
185
+ input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
186
+ seq cls result:
187
+ label: news_car confidence: 0.9929346442222595
188
+ -----------------------------
189
+ input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
190
+ seq cls result:
191
+ label: news_entertainment confidence: 0.998711347579956
192
+ -----------------------------
193
+ ```
194
+ 如果需要FP16进行加速,可以开启use_fp16开关,具体命令为
195
+ ```
196
+ # 第一步,打开set_dynamic_shape开关,自动配置动态shape,在当前目录下生成dynamic_shape_info.txt文件
197
+ python infer_gpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/float32 --use_fp16 --shape_info_file dynamic_shape_info.txt --set_dynamic_shape
198
+ # 第二步,读取上一步中生成的dynamic_shape_info.txt文件,开启预测
199
+ python infer_gpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/float32 --use_fp16 --shape_info_file dynamic_shape_info.txt
200
+ ```
201
+ 输出打印如下:
202
+ ```
203
+ input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
204
+ seq cls result:
205
+ label: news_car confidence: 0.9929342269897461
206
+ -----------------------------
207
+ input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
208
+ seq cls result:
209
+ label: news_entertainment confidence: 0.99870365858078
210
+ -----------------------------
211
+ ```
212
+ 如果需要进行INT8量化加速,还需要使用量化脚本对训练好的FP32模型进行量化,然后使用量化后的模型进行部署,模型的量化请参考:[ 模型量化脚本使用说明] ( ./../../README.md#模型压缩 ) ,也可下载我们量化后的INT8模型进行部署,请执行如下命令获取模型:
213
+ ```
214
+ # 获取命名实体识别INT8量化模型
215
+ wget https://paddlenlp.bj.bcebos.com/models/transformers/ernie_3.0/tnews_quant_infer_model.zip
216
+ unzip tnews_quant_infer_model.zip
217
+ ```
218
+ 量化模型的部署命令为:
219
+ ```
220
+ # 第一步,打开set_dynamic_shape开关,自动配置动态shape,在当前目录下生成dynamic_shape_info.txt文件
221
+ python infer_gpu.py --task_name seq_cls --model_path ./tnews_quant_infer_model/int8 --shape_info_file dynamic_shape_info.txt --set_dynamic_shape
222
+ # 第二步,读取上一步中生成的dynamic_shape_info.txt文件,开启预测
223
+ python infer_gpu.py --task_name seq_cls --model_path ./tnews_quant_infer_model/int8 --shape_info_file dynamic_shape_info.txt
224
+ ```
225
+ 输出打印如下:
226
+ ```
227
+ input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
228
+ seq cls result:
229
+ label: news_car confidence: 0.9922153353691101
230
+ -----------------------------
231
+ input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
232
+ seq cls result:
233
+ label: news_entertainment confidence: 0.9986827373504639
234
+ -----------------------------
235
+ ```
0 commit comments