Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c86f33

Browse files
authored
Add deploy for UIE (PaddlePaddle#2307)
* Add deploy for UIE * add paddle inference * add infer * add onnxruntime predictor for taskflow * remove unused file * Update predictor * Update predictor * Update README.md * fix codestyle * fix codestyle * update num_threads default value * update tokenizer * update tokenizer * Update README.md * Update README.md and remove num_threads
1 parent d0bbb90 commit 8c86f33

15 files changed

Lines changed: 1045 additions & 78 deletions

File tree

docs/model_zoo/taskflow.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,25 +630,25 @@ from paddlenlp import Taskflow
630630
```python
631631
>>> schema = ['法院', {'原告': '委托代理人'}, {'被告': '委托代理人'}]
632632
>>> ie.set_schema(schema)
633-
>>> pprint(ie("北京市海淀区人民法院\n民事判决书\n(199x)建初字第xxx号\n原告:张三。\n委托代理人李四,北京市 A律师事务所律师。\n被告:B公司,法定代表人王五,开发公司总经理。")) # Better print results using pprint
633+
>>> pprint(ie("北京市海淀区人民法院\n民事判决书\n(199x)建初字第xxx号\n原告:张三。\n委托代理人李四,北京市 A律师事务所律师。\n被告:B公司,法定代表人王五,开发公司总经理。\n委托代理人赵六,北京市 C律师事务所律师。")) # Better print results using pprint
634634
[{'原告': [{'end': 37,
635-
'probability': 0.9955972637653154,
635+
'probability': 0.9949814024296764,
636636
'relations': {'委托代理人': [{'end': 46,
637-
'probability': 0.9835957661618089,
637+
'probability': 0.7956844697990384,
638638
'start': 44,
639639
'text': '李四'}]},
640640
'start': 35,
641641
'text': '张三'}],
642642
'法院': [{'end': 10,
643-
'probability': 0.9245885500450299,
643+
'probability': 0.9221074192336651,
644644
'start': 0,
645645
'text': '北京市海淀区人民法院'}],
646646
'被告': [{'end': 67,
647-
'probability': 0.9033652934762237,
648-
'relations': {'委托代理人': [{'end': 46,
649-
'probability': 0.3863244074945271,
650-
'start': 44,
651-
'text': '李四'}]},
647+
'probability': 0.8437349536631089,
648+
'relations': {'委托代理人': [{'end': 92,
649+
'probability': 0.7267121388225029,
650+
'start': 90,
651+
'text': '赵六'}]},
652652
'start': 64,
653653
'text': 'B公司'}]}]
654654
```
@@ -695,6 +695,7 @@ from paddlenlp import Taskflow
695695
* `model`:选择任务使用的模型,默认为`uie-base`,可选有`uie-tiny``uie-base``uie-medical-base`
696696
* `schema`:定义任务抽取目标,可参考示例中对于不同信息抽取任务的schema配置自定义抽取目标。
697697
* `position_prob`:模型对于span的起始位置/终止位置的结果概率0~1之间,返回结果去掉小于这个阈值的结果,默认为0.5,span的最终概率输出为起始位置概率和终止位置概率的乘积。
698+
* `precision`:选择模型精度,默认为`fp32`,可选有`fp16``fp32``fp16`推理速度更快。如果选择`fp16`,请先确保机器正确安装NVIDIA相关驱动和基础软件,**确保CUDA>=11.2,cuDNN>=8.1.1**,初次使用需按照提示安装相关依赖。其次,需要确保GPU设备的CUDA计算能力(CUDA Compute Capability)大于7.0,典型的设备包括V100、T4、A10、A100、GTX 20系列和30系列显卡等。更多关于CUDA Compute Capability和精度支持情况请参考NVIDIA文档:[GPU硬件与支持精度对照表](https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-840-ea/support-matrix/index.html#hardware-precision-matrix)
698699
</div></details>
699700

700701
### 解语知识标注

model_zoo/uie/README.md

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
**目录**
44

5-
* [1. 模型简介](#模型简介)
6-
* [2. 应用示例](#应用示例)
7-
* [3. 开箱即用](#开箱即用)
8-
* [4. 轻定制功能](#轻定制功能)
9-
* [5. CCKS比赛](#CCKS比赛)
5+
- [1. 模型简介](#模型简介)
6+
- [2. 应用示例](#应用示例)
7+
- [3. 开箱即用](#开箱即用)
8+
- [4. 轻定制功能](#轻定制功能)
9+
- [5. CCKS比赛](#CCKS比赛)
1010

1111
<a name="模型简介"></a>
1212

@@ -273,16 +273,10 @@ UIE不限定行业领域和抽取目标,以下是一些零样本行业示例
273273
[
274274
"法院",
275275
{
276-
"原告": [
277-
"委托代理人",
278-
"法定代表人"
279-
]
276+
"原告": "委托代理人"
280277
},
281278
{
282-
"被告": [
283-
"委托代理人",
284-
"法定代表人"
285-
]
279+
"被告": "委托代理人"
286280
}
287281
]
288282
```
@@ -292,25 +286,25 @@ UIE不限定行业领域和抽取目标,以下是一些零样本行业示例
292286
```python
293287
>>> schema = ['法院', {'原告': '委托代理人'}, {'被告': '委托代理人'}]
294288
>>> ie.set_schema(schema)
295-
>>> pprint(ie("北京市海淀区人民法院\n民事判决书\n(199x)建初字第xxx号\n原告:张三。\n委托代理人李四,北京市 A律师事务所律师。\n被告:B公司,法定代表人王五,开发公司总经理。")) # Better print results using pprint
289+
>>> pprint(ie("北京市海淀区人民法院\n民事判决书\n(199x)建初字第xxx号\n原告:张三。\n委托代理人李四,北京市 A律师事务所律师。\n被告:B公司,法定代表人王五,开发公司总经理。\n委托代理人赵六,北京市 C律师事务所律师。")) # Better print results using pprint
296290
[{'原告': [{'end': 37,
297-
'probability': 0.9955972637653154,
291+
'probability': 0.9949814024296764,
298292
'relations': {'委托代理人': [{'end': 46,
299-
'probability': 0.9835957661618089,
293+
'probability': 0.7956844697990384,
300294
'start': 44,
301295
'text': '李四'}]},
302296
'start': 35,
303297
'text': '张三'}],
304298
'法院': [{'end': 10,
305-
'probability': 0.9245885500450299,
299+
'probability': 0.9221074192336651,
306300
'start': 0,
307301
'text': '北京市海淀区人民法院'}],
308302
'被告': [{'end': 67,
309-
'probability': 0.9033652934762237,
310-
'relations': {'委托代理人': [{'end': 46,
311-
'probability': 0.3863244074945271,
312-
'start': 44,
313-
'text': '李四'}]},
303+
'probability': 0.8437349536631089,
304+
'relations': {'委托代理人': [{'end': 92,
305+
'probability': 0.7267121388225029,
306+
'start': 90,
307+
'text': '赵六'}]},
314308
'start': 64,
315309
'text': 'B公司'}]}]
316310
```
@@ -342,6 +336,7 @@ UIE不限定行业领域和抽取目标,以下是一些零样本行业示例
342336
* `model`:选择任务使用的模型,默认为`uie-base`,可选有`uie-tiny``uie-base``uie-medical-base`
343337
* `schema`:定义任务抽取目标,可参考示例中对于不同信息抽取任务的schema配置自定义抽取目标。
344338
* `position_prob`:模型对于span的起始位置/终止位置的结果概率0~1之间,返回结果去掉小于这个阈值的结果,默认为0.5,span的最终概率输出为起始位置概率和终止位置概率的乘积。
339+
* `precision`:选择模型精度,默认为`fp32`,可选有`fp16``fp32``fp16`推理速度更快。如果选择`fp16`,请先确保机器正确安装NVIDIA相关驱动和基础软件,**确保CUDA>=11.2,cuDNN>=8.1.1**,初次使用需按照提示安装相关依赖。其次,需要确保GPU设备的CUDA计算能力(CUDA Compute Capability)大于7.0,典型的设备包括V100、T4、A10、A100、GTX 20系列和30系列显卡等。更多关于CUDA Compute Capability和精度支持情况请参考NVIDIA文档:[GPU硬件与支持精度对照表](https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-840-ea/support-matrix/index.html#hardware-precision-matrix)
345340

346341
<a name="轻定制功能"></a>
347342

@@ -404,7 +399,7 @@ python doccano.py \
404399

405400
- ``doccano_file``: 从doccano导出的数据标注文件。
406401
- ``save_dir``: 训练数据的保存目录,默认存储在``data``目录下。
407-
- ``negative_ratio``: 最大负例比例,该参数只对抽取类型任务有效,适当构造负例可提升模型效果。负例数量和实际的标签数量有关,最大负例数量 = negative_ratio * 正例数量。该参数只对训练集有效,为了保证评估指标的准确性,验证集和测试集默认构造全负例。默认为5
402+
- ``negative_ratio``: 最大负例比例,该参数只对抽取类型任务有效,适当构造负例可提升模型效果。负例数量和实际的标签数量有关,最大负例数量 = negative_ratio * 正例数量。该参数只对训练集有效,默认为5。为了保证评估指标的准确性,验证集和测试集默认构造全负例。
408403
- ``splits``: 划分数据集时训练集、验证集所占的比例。默认为[0.8, 0.1, 0.1]表示按照``8:1:1``的比例将数据划分为训练集、验证集和测试集。
409404
- ``task_type``: 选择任务类型,可选有抽取和分类两种类型的任务。
410405
- ``options``: 指定分类任务的类别标签,该参数只对分类类型任务有效。
@@ -418,7 +413,7 @@ python doccano.py \
418413
- 在模型训练阶段我们推荐构造一些负例以提升模型效果,在数据转换阶段我们内置了这一功能。可通过`negative_ratio`控制自动构造的负样本比例;负样本数量 = negative_ratio * 正样本数量。
419414
- 对于从doccano导出的文件,默认文件中的每条数据都是经过人工正确标注的。
420415

421-
更多不同类型任务(关系抽取、事件抽取、评价观点抽取等)的标注规则及参数说明,请参考[doccano数据标注指南](doccano.md)
416+
更多**不同类型任务(关系抽取、事件抽取、评价观点抽取等)的标注规则及参数说明**,请参考[doccano数据标注指南](doccano.md)
422417

423418
#### 模型微调
424419

@@ -520,6 +515,77 @@ python evaluate.py \
520515

521516
0-shot表示无训练数据直接通过```paddlenlp.Taskflow```进行预测,5-shot表示基于5条标注数据进行模型微调。实验表明UIE在垂类场景可以通过少量数据(few-shot)进一步提升效果。
522517

518+
#### Python部署
519+
520+
以下是UIE Python端基于ONNXRuntime的部署流程,包括环境准备、模型导出和使用示例。
521+
522+
- 环境准备
523+
UIE的部署分为CPU和GPU两种情况,请根据你的部署环境安装对应的依赖。
524+
525+
- CPU端
526+
527+
CPU端的部署请使用如下命令安装所需依赖
528+
529+
```shell
530+
pip install -r deploy/python/requirements_cpu.txt
531+
```
532+
533+
- GPU端
534+
535+
为了在GPU上获得最佳的推理性能和稳定性,请先确保机器已正确安装NVIDIA相关驱动和基础软件,确保**CUDA >= 11.2,cuDNN >= 8.1.1**,并使用以下命令安装所需依赖
536+
537+
```shell
538+
pip install -r deploy/python/requirements_gpu.txt
539+
```
540+
541+
如需使用半精度(FP16)部署,请确保GPU设备的CUDA计算能力 (CUDA Compute Capability) 大于7.0,典型的设备包括V100、T4、A10、A100、GTX 20系列和30系列显卡等。
542+
更多关于CUDA Compute Capability和精度支持情况请参考NVIDIA文档:[GPU硬件与支持精度对照表](https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-840-ea/support-matrix/index.html#hardware-precision-matrix)
543+
544+
545+
- 模型导出
546+
547+
将训练后的动态图参数导出为静态图参数:
548+
549+
```shell
550+
python export_model.py --model_path=./checkpoint/model_best --output_path=./export
551+
```
552+
553+
可配置参数说明:
554+
555+
- `model_path`: 动态图训练保存的参数路径,路径下包含模型参数文件`model_state.pdparams`和模型配置文件`model_config.json`
556+
- `output_path`: 静态图参数导出路径,默认导出路径为`./export`
557+
558+
- 推理
559+
560+
- CPU端推理样例
561+
562+
在CPU端,请使用如下命令进行部署
563+
564+
```shell
565+
python deploy/python/infer_cpu.py --model_path_prefix export/inference
566+
```
567+
568+
可配置参数说明:
569+
570+
- `model_path_prefix`: 用于推理的Paddle模型文件路径,需加上文件前缀名称。例如模型文件路径为`./export/inference.pdiparams`,则传入`./export/inference`
571+
- `position_prob`:模型对于span的起始位置/终止位置的结果概率0~1之间,返回结果去掉小于这个阈值的结果,默认为0.5,span的最终概率输出为起始位置概率和终止位置概率的乘积。
572+
- `max_seq_len`: 文本最大切分长度,输入超过最大长度时会对输入文本进行自动切分,默认为512。
573+
574+
- GPU端推理样例
575+
576+
在GPU端,请使用如下命令进行部署
577+
578+
```shell
579+
python deploy/python/infer_gpu.py --model_path_prefix export/inference --use_fp16
580+
```
581+
582+
可配置参数说明:
583+
584+
- `model_path_prefix`: 用于推理的Paddle模型文件路径,需加上文件前缀名称。例如模型文件路径为`./export/inference.pdiparams`,则传入`./export/inference`
585+
- `use_fp16`: 是否使用FP16进行加速,默认关闭。
586+
- `position_prob`:模型对于span的起始位置/终止位置的结果概率0~1之间,返回结果去掉小于这个阈值的结果,默认为0.5,span的最终概率输出为起始位置概率和终止位置概率的乘积。
587+
- `max_seq_len`: 文本最大切分长度,输入超过最大长度时会对输入文本进行自动切分,默认为512。
588+
523589
<a name="CCKS比赛"></a>
524590

525591
## 5.CCKS比赛
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import argparse
16+
import math
17+
from pprint import pprint
18+
19+
import paddle
20+
from uie_predictor import UIEPredictor
21+
22+
23+
def parse_args():
24+
parser = argparse.ArgumentParser()
25+
# Required parameters
26+
parser.add_argument(
27+
"--model_path_prefix",
28+
type=str,
29+
required=True,
30+
help="The path prefix of inference model to be used.", )
31+
parser.add_argument(
32+
"--position_prob",
33+
default=0.5,
34+
type=float,
35+
help="Probability threshold for start/end index probabiliry.", )
36+
parser.add_argument(
37+
"--max_seq_len",
38+
default=512,
39+
type=int,
40+
help="The maximum input sequence length. Sequences longer than this will be split automatically.",
41+
)
42+
args = parser.parse_args()
43+
return args
44+
45+
46+
def main():
47+
args = parse_args()
48+
49+
texts = [
50+
'"北京市海淀区人民法院\n民事判决书\n(199x)建初字第xxx号\n原告:张三。\n委托代理人李四,北京市 A律师事务所律师。\n被告:B公司,法定代表人王五,开发公司总经理。\n委托代理人赵六,北京市 C律师事务所律师。"',
51+
'原告赵六,2022年5月29日生\n委托代理人孙七,深圳市C律师事务所律师。\n被告周八,1990年7月28日出生\n委托代理人吴九,山东D律师事务所律师'
52+
]
53+
schema1 = ['法院', {'原告': '委托代理人'}, {'被告': '委托代理人'}]
54+
schema2 = [{'原告': ['出生日期', '委托代理人']}, {'被告': ['出生日期', '委托代理人']}]
55+
56+
args.device = 'cpu'
57+
args.schema = schema1
58+
predictor = UIEPredictor(args)
59+
60+
print("-----------------------------")
61+
outputs = predictor.predict(texts)
62+
for text, output in zip(texts, outputs):
63+
print("1. Input text: ")
64+
print(text)
65+
print("2. Input schema: ")
66+
print(schema1)
67+
print("3. Result: ")
68+
pprint(output)
69+
print("-----------------------------")
70+
71+
# Reset schema
72+
predictor.set_schema(schema2)
73+
outputs = predictor.predict(texts)
74+
for text, output in zip(texts, outputs):
75+
print("1. Input text: ")
76+
print(text)
77+
print("2. Input schema: ")
78+
print(schema2)
79+
print("3. Result: ")
80+
pprint(output)
81+
print("-----------------------------")
82+
83+
84+
if __name__ == "__main__":
85+
main()
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import argparse
16+
from pprint import pprint
17+
18+
import paddle
19+
from uie_predictor import UIEPredictor
20+
21+
22+
def parse_args():
23+
parser = argparse.ArgumentParser()
24+
# Required parameters
25+
parser.add_argument(
26+
"--model_path_prefix",
27+
type=str,
28+
required=True,
29+
help="The path prefix of inference model to be used.", )
30+
parser.add_argument(
31+
"--position_prob",
32+
default=0.5,
33+
type=float,
34+
help="Probability threshold for start/end index probabiliry.", )
35+
parser.add_argument(
36+
"--use_fp16",
37+
action='store_true',
38+
help="Whether to use fp16 inference, only takes effect when deploying on gpu.",
39+
)
40+
parser.add_argument(
41+
"--max_seq_len",
42+
default=512,
43+
type=int,
44+
help="The maximum input sequence length. Sequences longer than this will be split automatically.",
45+
)
46+
args = parser.parse_args()
47+
return args
48+
49+
50+
def main():
51+
args = parse_args()
52+
53+
texts = [
54+
'"北京市海淀区人民法院\n民事判决书\n(199x)建初字第xxx号\n原告:张三。\n委托代理人李四,北京市 A律师事务所律师。\n被告:B公司,法定代表人王五,开发公司总经理。\n委托代理人赵六,北京市 C律师事务所律师。"',
55+
'原告赵六,2022年5月29日生\n委托代理人孙七,深圳市C律师事务所律师。\n被告周八,1990年7月28日出生\n委托代理人吴九,山东D律师事务所律师'
56+
]
57+
schema1 = ['法院', {'原告': '委托代理人'}, {'被告': '委托代理人'}]
58+
schema2 = [{'原告': ['出生日期', '委托代理人']}, {'被告': ['出生日期', '委托代理人']}]
59+
60+
args.device = 'gpu'
61+
args.schema = schema1
62+
predictor = UIEPredictor(args)
63+
64+
print("-----------------------------")
65+
outputs = predictor.predict(texts)
66+
for text, output in zip(texts, outputs):
67+
print("1. Input text: ")
68+
print(text)
69+
print("2. Input schema: ")
70+
print(schema1)
71+
print("3. Result: ")
72+
pprint(output)
73+
print("-----------------------------")
74+
75+
# Reset schema
76+
predictor.set_schema(schema2)
77+
outputs = predictor.predict(texts)
78+
for text, output in zip(texts, outputs):
79+
print("1. Input text: ")
80+
print(text)
81+
print("2. Input schema: ")
82+
print(schema2)
83+
print("3. Result: ")
84+
pprint(output)
85+
print("-----------------------------")
86+
87+
88+
if __name__ == "__main__":
89+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
onnx
2+
onnxruntime

0 commit comments

Comments
 (0)