1
- # ERNIE-3.0服务化部署
1
+ # 基于Paddle Serving的服务化部署
2
+
3
+ 本文档将介绍如何使用[ Paddle Serving] ( https://github.com/PaddlePaddle/Serving/blob/develop/README_CN.md ) 工具部署ERNIE 3.0新闻分类和命名实体识别模型的pipeline在线服务。
4
+
5
+ ## 目录
6
+ - [ 环境准备] ( #环境准备 )
7
+ - [ 模型转换] ( #模型转换 )
8
+ - [ 部署模型] ( #部署模型 )
2
9
3
10
## 环境准备
11
+ 需要[ 准备PaddleNLP的运行环境] ( ) 和Paddle Serving的运行环境。
4
12
5
- ### 安装Paddle Serving,
13
+ ### 安装Paddle Serving
6
14
安装指令如下,更多wheel包请参考[ serving官网文档] ( https://github.com/PaddlePaddle/Serving/blob/develop/doc/Latest_Packages_CN.md )
7
15
```
16
+ # 安装client和serving app,用于向服务发送请求
8
17
pip install paddle_serving_app paddle_serving_clinet
9
18
19
+ # 安装serving,用于启动服务
10
20
# CPU server
11
21
pip install paddle_serving_server
12
22
13
- # GPU server, 需要确认环境再选择执行哪一条 :
23
+ # GPU server, 选择跟本地环境一致的命令 :
14
24
# CUDA10.2 + Cudnn7 + TensorRT6
15
25
pip install paddle-serving-server-gpu==0.8.3.post102 -i https://pypi.tuna.tsinghua.edu.cn/simple
16
26
# CUDA10.1 + TensorRT6
@@ -22,41 +32,67 @@ pip install paddle-serving-server-gpu==0.8.3.post112 -i https://pypi.tuna.tsingh
22
32
默认开启国内清华镜像源来加速下载,如果您使用 HTTP 代理可以关闭(-i https://pypi.tuna.tsinghua.edu.cn/simple )
23
33
24
34
25
- ### 安装Paddle库
26
- 更多Paddle库下载安装可参考 [ Paddle官网文档 ] ( https://www.paddlepaddle.org.cn/documentation/docs/zh/install/index_cn.html )
35
+ ### 安装FasterTokenizer文本处理加速库(可选)
36
+ 如果部署环境是Linux,推荐安装faster_tokenizer可以得到更极致的文本处理效率,进一步提升服务性能。目前暂不支持Windows设备安装,将会在下个版本支持。
27
37
```
28
- # CPU 环境请执行
29
- pip3 install paddlepaddle
30
-
31
- # GPU CUDA 环境(默认CUDA10.2)
32
- pip3 install paddlepaddle-gpu
38
+ pip install faster_tokenizers
33
39
```
34
40
35
- ## 准备模型和数据
36
- 下载[ Erine-3.0模型] ( TODO )
37
41
38
- ### 转换模型
39
- 如果是链接中下载的部署模型或训练导出的静态图推理模型(含` xx.pdmodel ` 和` xx.pdiparams ` ),需要转换成serving模型
42
+ ## 模型转换
43
+
44
+ 使用Paddle Serving做服务化部署时,需要将保存的inference模型转换为serving易于部署的模型。
45
+
46
+ 下载ERNIE 3.0的新闻分类、命名实体识别模型:
47
+
48
+ ``` bash
49
+ # 下载并解压新闻分类模型
50
+ wget https://paddlenlp.bj.bcebos.com/models/transformers/ernie_3.0/tnews_pruned_infer_model.zip
51
+ unzip tnews_pruned_infer_model.zip
52
+ # 下载并解压命名实体识别模型
53
+ wget https://paddlenlp.bj.bcebos.com/models/transformers/ernie_3.0/msra_ner_pruned_infer_model.zip
54
+ unzip msra_ner_pruned_infer_model.zip
40
55
```
56
+
57
+ 用已安装的paddle_serving_client将inference模型转换成serving格式。
58
+
59
+ ``` bash
41
60
# 模型地址根据实际填写即可
42
- python -m paddle_serving_client.convert --dirname models/erinie-3.0 --model_filename infer.pdmodel --params_filename infer.pdiparams
61
+ # 转换新闻分类模型
62
+ python -m paddle_serving_client.convert --dirname tnews_pruned_infer_model --model_filename float32.pdmodel --params_filename float32.pdiparams
63
+
64
+ # 转换命名实体识别模型
65
+ python -m paddle_serving_client.convert --dirname msra_ner_pruned_infer_model --model_filename float32.pdmodel --params_filename float32.pdiparams
43
66
44
- # 可通过指令查看参数含义
67
+ # 可通过命令查参数含义
45
68
python -m paddle_serving_client.convert --help
46
69
```
47
70
转换成功后的目录如下:
48
71
```
49
72
serving_server
50
- ├── infer .pdiparams
51
- ├── infer .pdmodel
73
+ ├── float32 .pdiparams
74
+ ├── float32 .pdmodel
52
75
├── serving_server_conf.prototxt
53
76
└── serving_server_conf.stream.prototxt
54
77
```
55
78
79
+ ## 部署模型
80
+
81
+ serving目录包含启动pipeline服务和发送预测请求的代码,包括:
82
+
83
+ ```
84
+ seq_cls_config.yml # 新闻分类任务启动服务端的配置文件
85
+ seq_cls_rpc_client.py # 新闻分类任务发送pipeline预测请求的脚本
86
+ seq_cls_service.py # 新闻分类任务启动服务端的脚本
87
+
88
+ token_cls_config.yml # 命名实体识别任务启动服务端的配置文件
89
+ token_cls_rpc_client.py # 命名实体识别任务发送pipeline预测请求的脚本
90
+ token_cls_service.py # 命名实体识别任务启动服务端的脚本
91
+ ```
92
+
56
93
57
- ## 服务化部署模型
58
94
### 修改配置文件
59
- 目录中的` xx_config .yml` 文件解释了每一个参数的含义,可以根据实际需要修改其中的配置。比如:
95
+ 目录中的` seq_cls_config.yml ` 和 ` token_cls_config .yml` 文件解释了每一个参数的含义,可以根据实际需要修改其中的配置。比如:
60
96
```
61
97
# 修改模型目录为下载的模型目录或自己的模型目录:
62
98
model_config: no_task_emb/serving_server => model_config: erine-3.0-tiny/serving_server
@@ -70,10 +106,25 @@ device_type: 1 => device_type: 0
70
106
71
107
### 分类任务
72
108
#### 启动服务
73
- 修改好配置文件后,执行下面指令启动服务 :
109
+ 修改好配置文件后,执行下面命令启动服务 :
74
110
```
75
111
python seq_cls_service.py
76
112
```
113
+ 输出打印如下:
114
+ ```
115
+ [DAG] Succ init
116
+ [PipelineServicer] succ init
117
+ --- Running analysis [ir_graph_build_pass]
118
+ ......
119
+ --- Running analysis [ir_graph_to_program_pass]
120
+ I0515 05:36:48.316895 62364 analysis_predictor.cc:714] ======= optimize end =======
121
+ I0515 05:36:48.320442 62364 naive_executor.cc:98] --- skip [feed], feed -> token_type_ids
122
+ I0515 05:36:48.320463 62364 naive_executor.cc:98] --- skip [feed], feed -> input_ids
123
+ I0515 05:36:48.321842 62364 naive_executor.cc:98] --- skip [linear_113.tmp_1], fetch -> fetch
124
+ [2022-05-15 05:36:49,316] [ INFO] - We are using <class 'paddlenlp.transformers.ernie.tokenizer.ErnieTokenizer'> to load 'ernie-3.0-medium-zh'.
125
+ [2022-05-15 05:36:49,317] [ INFO] - Already cached /vdb1/home/heliqi/.paddlenlp/models/ernie-3.0-medium-zh/ernie_3.0_medium_zh_vocab.txt
126
+ [OP Object] init success
127
+ ```
77
128
78
129
#### 启动client测试
79
130
注意执行客户端请求时关闭代理,并根据实际情况修改init_client函数中的ip地址(启动服务所在的机器)
@@ -82,32 +133,51 @@ python seq_cls_rpc_client.py
82
133
```
83
134
输出打印如下:
84
135
```
85
- {'label': array([6, 2]), 'confidence': array([4.9473147, 5.7493963], dtype=float32)}
86
- acc: 0.5745
136
+ {'label': array([6, 2]), 'confidence': array([0.5543532, 0.9495907], dtype=float32)}acc: 0.5745
87
137
```
88
138
89
139
### 实体识别任务
90
140
#### 启动服务
91
- 修改好配置文件后,执行下面指令启动服务 :
141
+ 修改好配置文件后,执行下面命令启动服务 :
92
142
```
93
143
python token_cls_service.py
94
144
```
145
+ 输出打印如下:
146
+ ```
147
+ [DAG] Succ init
148
+ [PipelineServicer] succ init
149
+ --- Running analysis [ir_graph_build_pass]
150
+ ......
151
+ --- Running analysis [ir_graph_to_program_pass]
152
+ I0515 05:36:48.316895 62364 analysis_predictor.cc:714] ======= optimize end =======
153
+ I0515 05:36:48.320442 62364 naive_executor.cc:98] --- skip [feed], feed -> token_type_ids
154
+ I0515 05:36:48.320463 62364 naive_executor.cc:98] --- skip [feed], feed -> input_ids
155
+ I0515 05:36:48.321842 62364 naive_executor.cc:98] --- skip [linear_113.tmp_1], fetch -> fetch
156
+ [2022-05-15 05:36:49,316] [ INFO] - We are using <class 'paddlenlp.transformers.ernie.tokenizer.ErnieTokenizer'> to load 'ernie-3.0-medium-zh'.
157
+ [2022-05-15 05:36:49,317] [ INFO] - Already cached /vdb1/home/heliqi/.paddlenlp/models/ernie-3.0-medium-zh/ernie_3.0_medium_zh_vocab.txt
158
+ [OP Object] init success
159
+ ```
95
160
96
161
#### 启动client测试
97
162
注意执行客户端请求时关闭代理,并根据实际情况修改init_client函数中的ip地址(启动服务所在的机器)
98
163
```
99
- python seq_cls_rpc_client .py
164
+ python token_cls_rpc_client .py
100
165
```
101
166
输出打印如下:
102
167
```
103
- input data: 古老的文明,使我们引以为豪,彼此钦佩 。
168
+ input data: 北京的涮肉,重庆的火锅,成都的小吃都是极具特色的美食 。
104
169
The model detects all entities:
170
+ entity: 北京 label: LOC pos: [0, 1]
171
+ entity: 重庆 label: LOC pos: [6, 7]
172
+ entity: 成都 label: LOC pos: [12, 13]
105
173
-----------------------------
106
174
input data: 原产玛雅故国的玉米,早已成为华夏大地主要粮食作物之一。
107
175
The model detects all entities:
108
176
entity: 玛雅 label: LOC pos: [2, 3]
109
177
entity: 华夏 label: LOC pos: [14, 15]
110
178
-----------------------------
179
+ PipelineClient::predict pack_data time:1652593013.713769
180
+ PipelineClient::predict before time:1652593013.7141528
111
181
input data: ['从', '首', '都', '利', '隆', '圭', '乘', '车', '向', '湖', '边', '小', '镇', '萨', '利', '马', '进', '发', '时', ',', '不', '到', '1', '0', '0', '公', '里', '的', '道', '路', '上', '坑', '坑', '洼', '洼', ',', '又', '逢', '阵', '雨', '迷', '蒙', ',', '令', '人', '不', '时', '发', '出', '路', '难', '行', '的', '慨', '叹', '。']
112
182
The model detects all entities:
113
183
entity: 利隆圭 label: LOC pos: [3, 5]
0 commit comments