GEEP模型推理加速简介
BERT是一种典型的预训练语言模型,由于其在下游自然语言处理(NLP)任务中的显著改进而受到广泛关注。复杂的结构和大量的参数给BERT带来了竞争性能,但也导致模型推理速度较慢。为了提高BERT推理的速度,FastBERT在知识蒸馏和早期退出技术的基础上实现了自适应推理,推理精度降低到可以接受的程度。然而,许多因素限制了FastBERT分类器的性能,如教师分类器知识不够,批大小的缩水和子分类器的冗余计算。为了克服这些局限性,我们提出了一种基于GPU-Efficient Exit Prediction (GEEP)的BERT推理方法。在12个公开的中英文自然语言处理数据集上的实验结果证明了该方法的有效性。
GEEP算法概述&训练流程
GEEP算法
GEEP利用Shared Exit Loss将FastBERT的训练过程从两步简化为一步,通过向教师分类器提供不同的Transformer输出,使教师分类器知识更丰富,从而指导出更好的学生分类器以实现Early Exit。
加速效果如下,通过调节geep_threshold参数(0~1之间,0不加速,1最大加速),大约可以获得1~12倍之间的加速,加速比越大,准确率下降也越大,但一般不超过15%。用户可根据自己的数据调试geep_threshold参数,取得加速与效果之间的平衡。
GEEP算法的微调
GEEP模型在fine-tune阶段使用与BERT相同,目前仅支持文本分类任务:
export CUDA_VISIBLE_DEVICES=$1if [ ! -f ./train.tsv ]; thenwget https://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/release/tutorials/classification/train.tsvfiif [ ! -f ./dev.tsv ]; thenwget https://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/release/tutorials/classification/dev.tsvfimode=$2if [ "$mode" = "train" ]; theneasynlp \--mode $mode \--worker_gpu=1 \--tables=train.tsv,dev.tsv \--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1 \--first_sequence=sent1 \--second_sequence=sent2 \--label_name=label \--label_enumerate_values=0,1 \--checkpoint_dir=./classification_model \--learning_rate=3e-5 \--epoch_num=10 \--random_seed=42 \--save_checkpoint_steps=50 \--sequence_length=128 \--micro_batch_size=32 \--app_name=geep_classify \--user_defined_parameters='geep_exit_num=8pretrain_model_name_or_path=geep-base-uncased'elif [ "$mode" = "evaluate" ]; theneasynlp \--mode=$mode \--worker_gpu=1 \--tables=dev.tsv \--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1 \--first_sequence=sent1 \--second_sequence=sent2 \--label_name=label \--label_enumerate_values=0,1 \--checkpoint_dir=./classification_model \--sequence_length=128 \--micro_batch_size=32 \--app_name=geep_classify \--user_defined_parameters='geep_threshold=0.3'elif [ "$mode" = "predict" ]; theneasynlp \--mode=$mode \--worker_gpu=1 \--tables=dev.tsv \--outputs=dev.pred.tsv \--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1 \--output_schema=predictions,probabilities,logits,output \--append_cols=label \--first_sequence=sent1 \--second_sequence=sent2 \--checkpoint_path=./classification_model \--micro_batch_size=32 \--sequence_length=128 \--app_name=geep_classify \--user_defined_parameters='geep_threshold=0.3'fi
