UIESlim满足工业应用场景,解决推理部署耗时问题,提升效能
在UIE强大的抽取能力背后,同样需要较大的算力支持计算。在一些工业应用场景中对性能的要求较高,若不能有效压缩则无法实际应用。基于数据蒸馏技术构建了UIESlim数据蒸馏系统。其原理是通过数据作为桥梁,将UIE模型的知识迁移到封闭域信息抽取小模型,以达到精度损失较小的情况下却能达到大幅度预测速度提升的效果。
FasterTokenizer是一款简单易用、功能强大的跨平台高性能文本预处理库,集成业界多个常用的Tokenizer实现,支持不同NLP场景下的文本预处理功能,如文本分类、阅读理解,序列标注等。结合PaddleNLPTokenizer模块,为用户在训练、推理阶段提供高效通用的文本预处理能力。use_faster:使用C++实现的高性能分词算子FasterTokenizer进行文本预处理加速
UIE数据蒸馏三步
Step1:使用UIE模型对标注数据进行finetune,得到TeacherModel。Step2:用户提供大规模无标注数据,需与标注数据同源。使用TaskflowUIE对无监督数据进行预测。Step3:使用标注数据以及步骤2得到的合成数据训练出封闭域StudentModel。
效果展示:
测试硬件情况:
uie-base | 68.61049008s | 0.69277 | 0.72327 | 0.70769 |
uie-mini | 28.932519437s | 0.74138 | 0.54088 | 0.62545 |
uie-micro | 26.36701917 | 0.74757 | 0.48428 | 0.58779 |
uie-nano | 24.8937761 | 0.74286 | 0.49057 | 0.59091 |
蒸馏mini | 6.839258904s | 0.7732 | 0.75 | 0.76142 |
蒸馏micro | 6.776990s | 0.78261 | 0.72 | 0.75 |
蒸馏nano | 6.6231770s | 0.7957 | 0.74 | 0.76684 |
模型计算运行时间:
UIE base | 203.95947s | 1 |
UIE base + FasterTokenizer | 177.1798s | 1.15 |
UIE蒸馏mini | 21.97979s | 9.28 |
UIE蒸馏mini + FasterTokenizer | 20.1557s | 10.12 |
示例数据包含以下两部分:
doccano格式标注数据(doccano_ext.json) | 200 |
无标注数据(unlabeled_data.txt) | 1277 |
进行预训练微调,得到TeacherModel
Paddlenlp之UIE模型实战实体抽取任务【打车数据、快递单】
PaddleNLP之UIE信息抽取小样本进阶[含doccano详解]
Paddlenlp之UIE分类模型【以情感倾向分析新闻分类为例】含智能标注方案)
Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
UIE:模型
uie-base (默认) | 12-layers, 768-hidden, 12-heads | 中文 | 118M |
uie-base-en | 12-layers, 768-hidden, 12-heads | 英文 | 118M |
uie-medical-base | 12-layers, 768-hidden, 12-heads | 中文 | |
uie-medium | 6-layers, 768-hidden, 12-heads | 中文 | 75M |
uie-mini | 6-layers, 384-hidden, 12-heads | 中文 | 27M |
uie-micro | 4-layers, 384-hidden, 12-heads | 中文 | 23M |
uie-nano | 4-layers, 312-hidden, 12-heads | 中文 | 18M |
uie-m-large | 24-layers, 1024-hidden, 16-heads | 中、英文 | 实际大小2G |
uie-m-base | 12-layers, 768-hidden, 12-heads | 中、英文 | 实际大小1G |
实际模型大小解释:
base模型118Mparameters是指base模型的参数个数,因为同一个模型可以被不同的精度来表示,例如float1float3下载下来是450M左右,是因为下载的模型是float3118M*4大概是存储空间的量级。
!python finetune.py
--train_path "./data/train.txt"
--dev_path "./data/dev.txt"
--save_dir "./checkpoint"
--learning_rate 5e-6
--batch_size 16
--max_seq_len 512
--num_epochs 10
--model "uie-base"
--seed 1000
--logging_steps 10
--valid_steps 50
--device "gpu"
base模型部分结果展示:
[2022-09-08 17:26:55,701] [ INFO] - Evaluation precision: 0.69375, recall: 0.69811, F1: 0.69592
[2022-09-08 17:27:01,145] [ INFO] - global step 260, epoch: 9, loss: 0.00172, speed: 1.84 step/s
[2022-09-08 17:27:06,448] [ INFO] - global step 270, epoch: 9, loss: 0.00168, speed: 1.89 step/s
[2022-09-08 17:27:12,102] [ INFO] - global step 280, epoch: 10, loss: 0.00165, speed: 1.77 step/s
[2022-09-08 17:27:17,607] [ INFO] - global step 290, epoch: 10, loss: 0.00162, speed: 1.82 step/s
[2022-09-08 17:27:22,899] [ INFO] - global step 300, epoch: 10, loss: 0.00159, speed: 1.89 step/s
[2022-09-08 17:27:26,577] [ INFO] - Evaluation precision: 0.69277, recall: 0.72327, F1: 0.70769
[2022-09-08 17:27:26,577] [ INFO] - best F1 performence has been updated: 0.69841 --> 0.70769
离线蒸馏
1通过训练好的UIE定制模型预测无监督数据的标签
用户提供大规模无标注数据,需与标注数据同源。使用TaskflowUIE对无监督数据进行预测。
GlobalPointer:用统一的方式处理嵌套和非嵌套NER:
GPLinker:基于GlobalPointer的实体关系联合抽取
%cd /home/aistudio/data_distill
!python data_distill.py
--data_path /home/aistudio/data
--save_dir student_data
--task_type relation_extraction
--synthetic_ratio 10
--model_path /home/aistudio/checkpoint/model_best
data_path:标注数据及无监督文本路径。model_path:训练好的UIE定制模型路径。save_dir:学生模型训练数据保存路径。synthetic_ratio:控制合成数据的比例。最大合成数据数量=synthetic_ratio*标注数据数量。task_type:选择任务类型,可选有entity_extraction,relation_extraction,event_extraction和opinion_extraction。因为是封闭域信息抽取,需指定任务类型。seed:随机种子,默认为1000。
parser.add_argument("--data_path", default="../data", type=str, help="The directory for labeled data with doccano format and the large scale unlabeled data.")
parser.add_argument("--model_path", type=str, default="../checkpoint/model_best", help="The path of saved model that you want to load.")
parser.add_argument("--save_dir", default="./distill_task", type=str, help="The path of data that you wanna save.")
parser.add_argument("--synthetic_ratio", default=10, type=int, help="The ratio of labeled and synthetic samples.")
parser.add_argument("--task_type", choices=["relation_extraction", "event_extraction", "entity_extraction", "opinion_extraction"], default="entity_extraction", type=str, help="Select the training task type.")
parser.add_argument("--seed", type=int, default=1000, help="Random seed for initialization")
model_path:训练好的UIE定制模型路径。test_path:测试数据集路径。label_maps_path:学生模型标签字典。batch_size:批处理大小,默认为max_seq_len:最大文本长度,默认为25task_type:选择任务类型,可选有entity_extraction,relation_extraction,event_extraction和opinion_extraction。因为是封闭域信息抽取的评估,需指定任务类型。
parser.add_argument("--model_path", type=str, default=None, help="The path of saved model that you want to load.")
parser.add_argument("--test_path", type=str, default=None, help="The path of test set.")
parser.add_argument("--encoder", default="ernie-3.0-base-zh", type=str, help="Select the pretrained encoder model for GP.")
parser.add_argument("--label_maps_path", default="./ner_data/label_maps.json", type=str, help="The file path of the labels dictionary.")
parser.add_argument("--batch_size", type=int, default=16, help="Batch size per GPU/CPU for training.")
parser.add_argument("--max_seq_len", type=int, default=128, help="The maximum total input sequence length after tokenization.")
parser.add_argument("--task_type", choices=["relation_extraction", "event_extraction", "entity_extraction", "opinion_extraction"], default="entity_extraction",
3学生模型训练
!python train.py
--task_type relation_extraction
--train_path student_data/train_data.json
--dev_path student_data/dev_data.json
--label_maps_path student_data/label_maps.json
--num_epochs 200
--encoder ernie-3.0-mini-zh
# %cd /home/aistudio/data_distill
!python train.py
--task_type relation_extraction
--train_path student_data/train_data.json
--dev_path student_data/dev_data.json
--label_maps_path student_data/label_maps.json
--num_epochs 100
--encoder ernie-3.0-mini-zh
--device "gpu"
--valid_steps 100
--logging_steps 10
--save_dir "./checkpoint2"
--batch_size 16
Taskflow部署学生模型以及性能测试
通过Taskflow一键部署封闭域信息抽取模型,task_path为学生模型路径。
demo测试
from pprint import pprint
from paddlenlp import Taskflow
ie = Taskflow("information_extraction", model="uie-data-distill-gp", task_path="checkpoint2/model_best/") # Schema 在闭域信息抽取中是固定的
pprint(ie("登革热@结果 升高 ### 血清白蛋白水平 检查 结果 检查 在资源匮乏地区和富足地区,对有症状患者均应早期检测。"))
[{"疾病": [{"end": 3,
"probability": 0.9995957,
"relations": {"实验室检查": [{"end": 21,
"probability": 0.99892455,
"relations": {},
"start": 14,
"text": "血清白蛋白水平"}],
"影像学检查": [{"end": 21,
"probability": 0.99832386,
"relations": {},
"start": 14,
"text": "血清白蛋白水平"}]},
"start": 0,
"text": "登革热"}]}]
from pprint import pprint
import json
from paddlenlp.taskflow import Taskflow
import pandas as pd
#运行时间
import time
def openreadtxt(file_name):
data = []
file = open(file_name,"r",encoding="UTF-8") #打开文件
file_data = file.readlines() #读取所有行
for row in file_data:
data.append(row) #将每行数据插入data中
return data
# 时间1
old_time = time.time()
data_input=openreadtxt("/home/aistudio/数据集/unlabeled_data.txt")
few_ie = Taskflow("information_extraction", model="uie-data-distill-gp", task_path="/home/aistudio/data_distill/checkpoint2/model_best",batch_size=32) # Schema 在闭域信息抽取中是固定的
# 时间1
current_time = time.time()
print("数据模型载入运行时间为" + str(current_time - old_time) + "s")
#时间2
old_time1 = time.time()
results=few_ie(data_input)
current_time1 = time.time()
print("模型计算运行时间为" + str(current_time1 - old_time1) + "s")
#时间2
#时间三
old_time3 = time.time()
test = pd.DataFrame(data=results)
test.to_csv("/home/aistudio/output/reslut.txt", sep=" ", index=False,header=False) #本地
# with open("/home/aistudio/output/reslut.txt", "w+",encoding="UTF-8") as f: #a : 写入文件,若文件不存在则会先创建再写入,但不会覆盖原文件,而是追加在文件末尾
# for result in results:
# line = json.dumps(result, ensure_ascii=False) #对中文默认使用的ascii编码.想输出真正的中文需要指定ensure_ascii=False
# f.write(line + "
")
current_time3 = time.time()
print("数据导出运行时间为" + str(current_time3 - old_time3) + "s")
# for idx, text in enumerate(data):
# print("Data: {} Lable: {}".format(text[0], results[idx]))
print("数据结果已导出")
**mini运行时间:**
数据模型载入运行时间为0.8430757522583008s
模型计算运行时间为6.839258909225464s
数据导出运行时间为0.008304595947265625s
**nano运行时间:**
数据模型载入运行时间为0.5164840221405029s
模型计算运行时间为6.6231770515441895s
数据导出运行时间为0.023623943328857422s
**micro运行时间:**
数据模型载入运行时间为0.5323500633239746s
模型计算运行时间为6.77699007987976s
数据导出运行时间为0.04320549964904785s
4进行预训练模型UIE-mini并测试推理时间
封闭域UIE的schema是固定的,可以在label_maps.json查看
0:"手术治疗"
1:"实验室检查"
2:"影像学检查"
from pprint import pprint
import json
from paddlenlp import Taskflow
import pandas as pd
#运行时间
import time
def openreadtxt(file_name):
data = []
file = open(file_name,"r",encoding="UTF-8") #打开文件
file_data = file.readlines() #读取所有行
for row in file_data:
data.append(row) #将每行数据插入data中
return data
# 时间1
old_time = time.time()
data_input=openreadtxt("/home/aistudio/数据集/unlabeled_data.txt")
schema = {"疾病": ["手术治疗", "实验室检查", "影像学检查"]}
# few_ie = Taskflow("information_extraction", schema=schema, batch_size=32,task_path="/home/aistudio/checkpoint_mini/model_best") #自行切换
few_ie = Taskflow("information_extraction", schema=schema, batch_size=32,task_path="/home/aistudio/checkpoint_micro/model_best")
# 时间1
current_time = time.time()
print("数据模型载入运行时间为" + str(current_time - old_time) + "s")
#时间2
old_time1 = time.time()
results=few_ie(data_input)
current_time1 = time.time()
print("模型计算运行时间为" + str(current_time1 - old_time1) + "s")
#时间2
#时间三
old_time3 = time.time()
test = pd.DataFrame(data=results)
test.to_csv("/home/aistudio/output/reslut.txt", sep=" ", index=False,header=False) #本地
# with open("/home/aistudio/output/reslut.txt", "w+",encoding="UTF-8") as f: #a : 写入文件,若文件不存在则会先创建再写入,但不会覆盖原文件,而是追加在文件末尾
# for result in results:
# line = json.dumps(result, ensure_ascii=False) #对中文默认使用的ascii编码.想输出真正的中文需要指定ensure_ascii=False
# f.write(line + "
")
current_time3 = time.time()
print("数据导出运行时间为" + str(current_time3 - old_time3) + "s")
# for idx, text in enumerate(data):
# print("Data: {} Lable: {}".format(text[0], results[idx]))
print("数据结果已导出")
通过上述程序自行切换:加载对应模型
记录推理时间:
**uie-nano**
数据模型载入运行时间为0.3770780563354492s
模型计算运行时间为24.893776178359985s
数据导出运行时间为0.01157689094543457s
**uie-micro**
数据模型载入运行时间为0.39632749557495117s
模型计算运行时间为26.367019176483154s
数据导出运行时间为0.012260198593139648s
**uie-mini**
数据模型载入运行时间为0.5642790794372559s
模型计算运行时间为28.93251943588257s
数据导出运行时间为0.01435089111328125s
**uie-base**
数据模型载入运行时间为1.4756040573120117s
模型计算运行时间为68.61049008369446s
数据导出运行时间为0.02205801010131836s
提前尝鲜UIEFasterTokenizer加速,提升推理性能
FasterTokenizer是一款简单易用、功能强大的跨平台高性能文本预处理库,集成业界多个常用的Tokenizer实现,支持不同NLP场景下的文本预处理功能,如文本分类、阅读理解,序列标注等。结合PaddleNLPTokenizer模块,为用户在训练、推理阶段提供高效通用的文本预处理能力。
高性能。由于底层采用C++实现,所以其性能远高于目前常规Python实现的Tokenizer。在文本分类任务上,FasterTokenizer对比Python版本Tokenizer加速比最高可达20倍。跨平台。FasterTokenizer可在不同的系统平台上使用,目前已支持Windowsx6Linuxx64以及MacOS14+平台上使用。多编程语言支持。FasterTokenizer提供在C++、Python语言上开发的能力。灵活性强。用户可以通过指定不同的FasterTokenizer组件定制满足需求的Tokenizer。
Q:我在AutoTokenizer.from_pretrained接口上已经打开use_faster=True开关,为什么文本预处理阶段性能上好像没有任何变化?
A:在有三种情况下,打开use_faster=True开关可能无法提升性能:
没有安装faster_tokenizer。若在没有安装faster_tokenizer库的情况下打开use_faster开关,PaddleNLP会给出以下warning:"Can’tfindthefaster_tokenizerpackage,pleaseensureinstallfaster_tokenizercorrectly."。加载的Tokenizer类型暂不支持Faster版本。目前支持4种Tokenizer的Faster版本,分别是BERT、ERNIE、TinyBERT以及ERNIE-MTokenizer。若加载不支持Faster版本的Tokenizer情况下打开use_faster开关,PaddleNLP会给出以下warning:“ThetokenizerXXXdoesn’thavethefasterversion.Pleasecheckthemappaddlenlp.transformers.auto.tokenizer.FASTER_TOKENIZER_MAPPING_NAMEStoseewhichfastertokenizersarecurrentlysupported.”待切词文本长度过短。这种情况下切词开销可能不是整个文本预处理的性能瓶颈,导致在使用FasterTokenizer后仍无法提升整体性能。
1方案一
2方案二
from pprint import pprint
import json
from paddlenlp.taskflow import Taskflow
import pandas as pd
#运行时间
import time
def openreadtxt(file_name):
data = []
file = open(file_name,"r",encoding="UTF-8") #打开文件
file_data = file.readlines() #读取所有行
for row in file_data:
data.append(row) #将每行数据插入data中
return data
# 时间1
old_time = time.time()
data_input=openreadtxt("/home/aistudio/数据集/unlabeled_data-Copy1.txt")
few_ie = Taskflow("information_extraction", model="uie-data-distill-gp", task_path="/home/aistudio/data_distill/checkpoint2/model_best",use_faster=True,batch_size=32) # Schema 在闭域信息抽取中是固定的
# few_ie = Taskflow("information_extraction", model="uie-data-distill-gp", task_path="/home/aistudio/data_distill/checkpoint2/model_best",batch_size=32) # Schema 在闭域信息抽取中是固定的
# schema = {"疾病": ["手术治疗", "实验室检查", "影像学检查"]}
# few_ie = Taskflow("information_extraction", schema=schema, batch_size=32,use_faster=True,task_path="/home/aistudio/checkpoint/model_best")
# few_ie = Taskflow("information_extraction", schema=schema, batch_size=32,task_path="/home/aistudio/checkpoint/model_best")
# 时间1
current_time = time.time()
print("数据模型载入运行时间为" + str(current_time - old_time) + "s")
#时间2
old_time1 = time.time()
results=few_ie(data_input)
current_time1 = time.time()
print("模型计算运行时间为" + str(current_time1 - old_time1) + "s")
#时间2
#时间三
old_time3 = time.time()
test = pd.DataFrame(data=results)
test.to_csv("/home/aistudio/output/reslut.txt", sep=" ", index=False,header=False) #本地
# with open("/home/aistudio/output/reslut.txt", "w+",encoding="UTF-8") as f: #a : 写入文件,若文件不存在则会先创建再写入,但不会覆盖原文件,而是追加在文件末尾
# for result in results:
# line = json.dumps(result, ensure_ascii=False) #对中文默认使用的ascii编码.想输出真正的中文需要指定ensure_ascii=False
# f.write(line + "
")
current_time3 = time.time()
print("数据导出运行时间为" + str(current_time3 - old_time3) + "s")
# for idx, text in enumerate(data):
# print("Data: {} Lable: {}".format(text[0], results[idx]))
print("数据结果已导出")
3UIEFasterTokenizer加速,提升推理性能
数据样本增大为原来的三倍:unlabeled_data-Copytxt
数据模型载入运行时间为6006419658660889s
模型计算运行时间为2095947885513306s
数据导出运行时间为0.07103896141052246s
数据模型载入运行时间为6196515560150146s
模型计算运行时间为1717986011505127s
数据导出运行时间为0.07898902893066406s
UIE蒸馏mini
数据模型载入运行时间为0.8441095352172852s
模型计算运行时间为2979790925979614s
数据导出运行时间为0.02339339256286621s
数据模型载入运行时间为0.7269768714904785s
模型计算运行时间为20.155770540237427s
数据导出运行时间为0.012202978134155273s
总结
测试硬件情况:
uie-base | 68.61049008s | 0.69277 | 0.72327 | 0.70769 |
uie-mini | 28.932519437s | 0.74138 | 0.54088 | 0.62545 |
uie-micro | 26.36701917 | 0.74757 | 0.48428 | 0.58779 |
uie-nano | 24.8937761 | 0.74286 | 0.49057 | 0.59091 |
蒸馏mini | 6.839258904s | 0.7732 | 0.75 | 0.76142 |
蒸馏micro | 6.776990s | 0.78261 | 0.72 | 0.75 |
蒸馏nano | 6.6231770s | 0.7957 | 0.74 | 0.76684 |
模型计算运行时间:
UIE base | 203.95947s | 1 |
UIE base + FasterTokenizer | 177.1798s | 1.15 |
UIE蒸馏mini | 21.97979s | 9.28 |
UIE蒸馏mini + FasterTokenizer | 20.1557s | 10.12 |
可以看出UIE蒸馏在小网络下,性能差不多可以按需选择。可能会在更大任务性能会更好点
这里uie-base等只简单运行了10个epoch,可以多训练会提升性能
一般学生模型会选择参数量比较小的,UIE蒸馏版是schema并行推理的,速度会比UIE快很多,特别是schema比较多以及关系抽取等需要多阶段推理的情况
封闭域UIE的话schema是固定的,可以在label_maps.json查看,目前支持实体抽取、关系抽取、观点抽取和事件抽取,句子级情感分类目前蒸馏还不支持
想要更快的推理换下学生模型的backbone就行
展望:
后续对FasterTokenizer进行补充;以及研究一下UIE模型的量化、剪枝、NAS
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点