
微信AI具有以下特征的开源涡轮转换器。
Turbotransformer已应用于Tencent的多个在线BERT服务方案。例如,它为微信加速服务带来了1.88倍的加速度,2.11倍加速到公共云情绪分析服务,并加速了13.6倍加速度,加速了QQ建议系统。此外,它已经应用于构建诸如修道,搜索和建议之类的服务。
下表是涡轮变压器和相关工作的比较。
| 相关作品 | 表现 | 需要预处理 | 可变长度 | 用法 |
|---|---|---|---|---|
| Pytorch JIT(CPU) | 快速地 | 是的 | 不 | 难的 |
| 张力(GPU) | 快速地 | 是的 | 不 | 难的 |
| TF式变压器(GPU) | 快速地 | 是的 | 不 | 难的 |
| onnx-luntime(CPU/GPU) | 快速/快速 | 不 | 是的 | 中等的 |
| TensorFlow-1.x(CPU/GPU) | 缓慢/中等 | 是的 | 不 | 简单的 |
| Pytorch(CPU/GPU) | 中/中等 | 不 | 是的 | 简单的 |
| 涡轮转化器(CPU/GPU) | 最快/最快 | 不 | 是的 | 简单的 |
我们目前支持以下变压器模型。
import torch
import transformers
import turbo_transformers
if __name__ == "__main__" :
turbo_transformers . set_num_threads ( 4 )
torch . set_num_threads ( 4 )
model_id = "bert-base-uncased"
model = transformers . BertModel . from_pretrained ( model_id )
model . eval ()
cfg = model . config
input_ids = torch . tensor (
([ 12166 , 10699 , 16752 , 4454 ], [ 5342 , 16471 , 817 , 16022 ]),
dtype = torch . long )
position_ids = torch . tensor (([ 1 , 0 , 0 , 0 ], [ 1 , 1 , 1 , 0 ]), dtype = torch . long )
segment_ids = torch . tensor (([ 1 , 1 , 1 , 0 ], [ 1 , 0 , 0 , 0 ]), dtype = torch . long )
torch . set_grad_enabled ( False )
torch_res = model (
input_ids , position_ids = position_ids , token_type_ids = segment_ids
) # sequence_output, pooled_output, (hidden_states), (attentions)
torch_seqence_output = torch_res [ 0 ][:, 0 , :]
tt_model = turbo_transformers . BertModel . from_torch ( model )
res = tt_model (
input_ids , position_ids = position_ids ,
token_type_ids = segment_ids ) # pooled_output, sequence_output
tt_seqence_output = res [ 0 ]请注意,构建脚本仅适用于特定的OS和软件(Pytorch,OpenNMT,Transformers等)版本。请根据您的需要调整它们。
git clone https://github.com/Tencent/TurboTransformers --recursive
sh tools/build_docker_cpu.sh
# optional: If you want to compare the performance of onnxrt-mkldnn during benchmark, you need to set BUILD_TYPE=dev to compile onnxruntime into the docker image, as follows
env BUILD_TYPE=dev sh tools/build_docker_cpu.sh
docker run -it --rm --name=turbort -v $PWD:/workspace your_image_name /bin/bash
方法1:我想unitest
cd /workspace
sh tools/build_and_run_unittests.sh $PWD -DWITH_GPU=OFF
# you can switch between Openblas and MKL by modifying this line in CMakeList.txt
# set(BLAS_PROVIDER "mkl" CACHE STRING "Set the blas provider library, in [openblas, mkl, blis]")
方法2:我不想unitest
cd /workspace
mkdir -p build && cd build
cmake .. -DWITH_GPU=OFF
make -j 4
pip install `find . -name *whl`
cd benchmark
bash run_benchmark.sh
sh tool/build_conda_package.sh
# The conda package will be in /workspace/dist/*.tar.bz2
# When using turbo_transformers in other environments outside this container: conda install your_root_path/dist/*.tar.bz2
我们还准备了包含CPU版本的Turbotransformers以及其他相关作品的Docker映像,即onnxrt v1.2.0和dockerhub上的pytorch-jit
docker pull thufeifeibear/turbo_transformers_cpu:latest
git clone https://github.com/Tencent/TurboTransformers --recursive
# You can modify the environment variables in the script to specify the cuda version and operating system version
sh tools/build_docker_gpu.sh $PWD
nvidia-docker run --gpus all --net=host --rm -it -v $PWD:/workspace -v /etc/passwd:/etc/passwd --name=your_container_name REPOSITORY:TAG
# for example: nvidia-docker run --gpus all --net=host --rm -it -v $PWD:/workspace -v /etc/passwd:/etc/passwd --name=turbo_gpu_env thufeifeibear:0.1.1-cuda9.0-ubuntu16.04-gpu-dev
cd /workspace
sh tools/build_and_run_unittests.sh $PWD -DWITH_GPU=ON
cd benchmark
bash gpu_run_benchmark.sh
我们还准备了包含GPU版本的Turbotransformers的Docker映像。
docker pull thufeifeibear/turbo_transformers_gpu:latest
张量芯可以在GPU上加速计算。默认情况下,在turbotransformer中被禁用。如果要打开它,在编译代码之前,请使用_module_benchmakr在cmakelists.txt中设置选项。
option(WITH_TENSOR_CORE "Use Tensor core to accelerate" ON)
Turbotransformer提供C ++ / Python API接口。我们希望尽力适应各种在线环境,以减少用户开发的困难。
使用涡轮增压的第一步是加载预训练的模型。我们提供了一种在拥抱面/变压器中加载Pytorch和Tensorflow预训练模型的方法。特定的转换方法是使用./tool中的相应脚本将预训练的模型转换为NPZ格式文件,而Turbo使用C ++或Python接口来加载NPZ格式模型。特别是,我们认为大多数预训练的模型都采用Pytorch格式,并与Python一起使用。我们提供了直接在Python中呼叫的快捷方式,以供Pytorch保存的模型。

请参阅./example/python中支持的模型的示例。 turbonlp/translate-demo显示了在翻译任务中应用涡轮变压器的演示。由于BERT加速度的用户始终需要为任务进行自定义的后处理过程,因此我们提供了一个如何编写序列分类应用程序的示例。
有关一个示例,请参阅./example/cpp。我们的示例提供了GPU和两个CPU多线程调用方法。一种是使用多个线程进行一个BERT推断。另一个是进行多个BERT推断,每种推理都使用一个线程。用户可以通过add_subdirectory将涡轮转化器链接到您的代码。
通常,将一批不同长度的请求送入BERT模型以进行推理,需要零填充才能使所有请求具有相同的长度。例如,服务长度列表(100、10、50)的请求列表,您需要一个预处理阶段才能将其作为长度(100、100、100)添加。这样,浪费了最后两个序列计算的90%和50%。如有效变压器所示,不需要填充输入张量。作为替代方案,您只需要在多头注意力中添加批处理操作,这使得对整个BERT计算的少量提高。因此,大多数GEMM操作都是在没有零盖的情况下处理的。 Turbo提供了一个模型,即BertModelSmartBatch包括智能批处理技术。该示例以.//example/python/bert_smart_pad.py表示。
如何知道代码的热点?
如何添加新图层?
目前(2020年6月),在不久的将来,我们将增加对低精度模型(CPU INT8,GPU FP16)的支持。期待您的贡献!
BSD 3条规定许可证
如果您在研究出版物中使用Turbotrandformer,请引用本文。
@inproceedings{fang2021turbotransformers,
title={TurboTransformers: an efficient GPU serving system for transformer models},
author={Fang, Jiarui and Yu, Yang and Zhao, Chengduo and Zhou, Jie},
booktitle={Proceedings of the 26th ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming},
pages={389--402},
year={2021}
}
可以在分支ppopp21_artifact_centos上找到纸张的工件。
尽管我们建议您在GitHub问题上发布问题,但您也可以加入我们的Turbo用户组。