T5模型可用于多个NLP任务,例如摘要,QA,QG,翻译,文本生成等。连续文本生成自然慢,对于较大的T5模型,它的速度甚至更慢。 FASTT5通过在OnnxRuntime上运行T5模型推理速度更快。它还通过量化模型大小来降低模型的大小。
FASTT5库允许您将验证的T5模型转换为ONNX,对其进行量化,并将模型作为输出,该输出在单个代码中以OnnxRuntime运行。您还可以自定义整个过程。
您可以从PYPI安装FASTT5:
pip install fastt5如果您想从来源构建:
git clone https : // github . com / Ki6an / fastT5
cd fastT5
pip3 install - e .export_and_get_onnx_model()方法将给定的T5模型导出到ONNX,对其进行量化并在带有默认设置的ONXRUNTIME上运行它。此方法返回的模型支持generate() hugingface的方法。
如果您不希望量化模型,则在方法中使用
quantized=False。
from fastT5 import export_and_get_onnx_model
from transformers import AutoTokenizer
model_name = 't5-small'
model = export_and_get_onnx_model ( model_name )
tokenizer = AutoTokenizer . from_pretrained ( model_name )
t_input = "translate English to French: The universe is a dark forest."
token = tokenizer ( t_input , return_tensors = 'pt' )
tokens = model . generate ( input_ids = token [ 'input_ids' ],
attention_mask = token [ 'attention_mask' ],
num_beams = 2 )
output = tokenizer . decode ( tokens . squeeze (), skip_special_tokens = True )
print ( output )运行已经导出的模型使用
get_onnx_model()
您可以按照以下代码示例自定义整个管道:
from fastT5 import ( OnnxT5 , get_onnx_runtime_sessions ,
generate_onnx_representation , quantize )
from transformers import AutoTokenizer
model_or_model_path = 't5-small'
# Step 1. convert huggingfaces t5 model to onnx
onnx_model_paths = generate_onnx_representation ( model_or_model_path )
# Step 2. (recommended) quantize the converted model for fast inference and to reduce model size.
quant_model_paths = quantize ( onnx_model_paths )
# step 3. setup onnx runtime
model_sessions = get_onnx_runtime_sessions ( quant_model_paths )
# step 4. get the onnx model
model = OnnxT5 ( model_or_model_path , model_sessions )
...默认情况下,FastT5在当前目录中创建models文件夹,并存储所有模型。您可以为文件夹提供自定义路径以存储导出的型号。并且要运行已存储在自定义文件夹路径中的已经exported models :使用get_onnx_model(onnx_models_path="/path/to/custom/folder/")
from fastT5 import export_and_get_onnx_model , get_onnx_model
model_name = "t5-small"
custom_output_path = "/path/to/custom/folder/"
# 1. stores models to custom_output_path
model = export_and_get_onnx_model ( model_name , custom_output_path )
# 2. run already exported models that are stored in custom path
# model = get_onnx_model(model_name, custom_output_path) T5是seq2seq模型(编码器),因为它反复使用解码器进行推理,我们无法将整个模型直接导出到ONNX。我们需要单独导出编码器和解码器。
past_key_values包含可用于加快顺序解码加快速度的预计的隐藏园(自我注意区块和跨注意区块中的键和值)。
模型只能用恒定数量的输入导出。与此相反,第一步的解码器并未占据past_key_values ,其余的步骤解码器也不会。为了解决这个问题,我们可以创建两个解码器:一个用于第一步,不使用past_key_values ,另一个是使用past_key_values的其余步骤。
接下来,我们将导出所有三个模型(编码器,解码器,init_decoder)。然后对它们进行量化,将32bit定量为8bit应给出4倍的内存降低。由于有一个额外的解码器,该模型尺寸将减少3倍。
最后,我们将在ONNX运行时运行量化的模型。
推断很简单,因为该模型支持
generate()hugingface的方法。
past_key_values )。generate()方法。3X 。3-4X光束搜索相比,最多可达5X速度。 基准是根据英语对法语翻译测试的T5基准模型的结果。
以下图显示了量化的ONNX模型的延迟与波束数量从1到9不等的Pytorch模型的延迟。此处显示的潜伏期是序列长度的平均值,最高为130。
以下热图显示了X倍更快的速度,而Pytorch与ONNX模型的潜伏期比率更快。 ONX模型的表现大多数情况都优于大多数情况。但是,模型的速度降低了较长的序列长度。
量化模型是前面提到的轻巧模型,这些模型的精度几乎与原始模型相同(在下一部分中提到了量化的模型得分)。与OnNX和Pytorch模型相比,量化的ONX模型的延迟最低。
该模型的表现平均比Pytorch模型以5.7倍的速度和3-4倍的搜索量优于Pytorch模型。
注意:结果是在
AMD EPYC 7B12上生成的,这些结果可能因设备而异。 ONX型号通常在具有更多核心的高端CPU上表现良好。
结果测试了英文对法语翻译的测试,梁搜索号为3。
| bleu_4 | 流星 | rouge_l | |
|---|---|---|---|
| T5-SMALL(QUANT) | 0.240769 | 0.282342 | 0.468817 |
| T5-Mall(Pytorch) | 0.254601 | 0.295172 | 0.492749 |
| T5碱(量子) | 0.267606 | 0.306019 | 0.499188 |
| T5碱(Pytorch) | 0.268346 | 0.304969 | 0.503306 |
| T5-LARGE(量子) | 0.286726 | 0.316845 | 0.503585 |
| t5-large(pytorch) | 0.294015 | 0.315774 | 0.508677 |
HuggingFace模型中心支持私有模型。要使用fastt5使用私有的,预先训练的T5版本,您必须首先已通过$ transformers-cli login到HuggingFace生态系统中进行身份验证。然后,使用FastT5时,会有额外的导入并致电:
from fastT5 import (
OnnxT5 ,
get_onnx_runtime_sessions ,
generate_onnx_representation ,
quantize ,
set_auth_token )
from transformers import AutoTokenizer
set_auth_token ( True )
# the rest of the code is the same as using a public model如果您无法调用$ transformers-cli login或更喜欢使用API键,请在https://huggingface.co/settings/token(或https:/https://huggingface.co/organizations/org_name/settings/token)中找到该组织,则可以通过该字符串到set_auth_token 。避免通过设置环境变量HF_API_KEY=<redacted>将API键进行对代码进行硬编码,然后在代码中:
import os
from fastT5 import (
OnnxT5 ,
get_onnx_runtime_sessions ,
generate_onnx_representation ,
quantize ,
set_auth_token )
from transformers import AutoTokenizer
auth_token = os . environ . get ( "HF_API_KEY" )
set_auth_token ( auth_token )
# code proceeds as normal @ article { 2019 t5 ,
author = { Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J . Liu },
title = { Exploring the Limits of Transfer Learning with a Unified Text - to - Text Transformer },
journal = { arXiv e - prints },
year = { 2019 },
archivePrefix = { arXiv },
eprint = { 1910.10683 },
}