基于TensorRT的BERT实时自然语言理解(上)
基于TensorRT的BERT實時自然語言理解(上)
大規(guī)模語言模型(LSLMs)如BERT、GPT-2和XL-Net為許多自然語言理解(NLU)任務(wù)帶來了最先進(jìn)的精準(zhǔn)飛躍。自2018年10月發(fā)布以來,BERT1(來自Transformer的雙向編碼器表示)仍然是最流行的語言模型之一,并且在編寫時仍能提供最先進(jìn)的精準(zhǔn)。
BERT為NLU任務(wù)的準(zhǔn)確性提供了一個飛躍,使得基于語言的高質(zhì)量服務(wù)在許多行業(yè)的公司都能達(dá)到。要在生產(chǎn)中使用模型,除了精準(zhǔn)之外,還需要考慮延遲等因素,這些因素會影響最終用戶對服務(wù)的滿意度。由于BERT是一個12/24層的多層多頭注意網(wǎng)絡(luò),在推理過程中需要大量的計算。到目前為止,這給公司在實時應(yīng)用程序中部署B(yǎng)ERT帶來了挑戰(zhàn)。
NVIDIA發(fā)布了針對BERT的新的TensorRT優(yōu)化,允許您在t4gpu上執(zhí)行2.2ms*的推理。這比僅使用CPU的平臺快17倍,而且在對話式人工智能應(yīng)用程序所需的10ms延遲預(yù)算之內(nèi)。這些優(yōu)化使得在生產(chǎn)中使用BERT變得切實可行,例如,作為會話AI服務(wù)的一部分。 TensorRT是一個用于高性能深度學(xué)習(xí)推理的平臺,它包括一個優(yōu)化器和運行時,可以最大限度地減少延遲并最大化生產(chǎn)中的吞吐量。使用TensorRT,您可以優(yōu)化在所有主要框架中訓(xùn)練的模型,以高精度校準(zhǔn)較低精度,最后在生產(chǎn)中部署。
在這個TensorRT示例repo中,所有用于使用BERT實現(xiàn)此性能的優(yōu)化和代碼都將作為開放源代碼發(fā)布。我們已經(jīng)優(yōu)化了Transformer層,它是BERT編碼器的基本構(gòu)建塊,因此您可以將這些優(yōu)化應(yīng)用于任何基于BERT的NLP任務(wù)。BERT被應(yīng)用于會話式人工智能之外的一組擴(kuò)展的語音和NLP應(yīng)用程序,所有這些都可以利用這些優(yōu)化。
問答(QA)或閱讀理解是測試模型理解上下文能力的一種非常流行的方法。SQuAD leaderboard3排行榜3跟蹤此任務(wù)的最佳執(zhí)行者,以及他們提供的數(shù)據(jù)集和測試集。在過去的幾年里,隨著學(xué)術(shù)界和公司的全球貢獻(xiàn),質(zhì)量保證能力有了快速的發(fā)展。在本文中,將演示如何使用Python創(chuàng)建一個簡單的問答應(yīng)用程序,它由我們今天發(fā)布的TensorRT優(yōu)化BERT代碼提供支持。這個例子提供了一個API來輸入段落和問題,并返回由BERT模型生成的響應(yīng)。
從使用TensorRT for BERT執(zhí)行訓(xùn)練和推理的步驟開始。
BERT Training and Inference Pipeline
NLP研究人員和開發(fā)人員面臨的一個主要問題是缺乏針對其特定NLP任務(wù)的高質(zhì)量標(biāo)記訓(xùn)練數(shù)據(jù)。為了克服從零開始學(xué)習(xí)任務(wù)模型的問題,NLP的最新突破是利用大量未標(biāo)記的文本,將NLP任務(wù)分解為兩個部分:1)學(xué)習(xí)表示單詞的含義和它們之間的關(guān)系,即使用輔助任務(wù)和大量文本語料庫建立語言模型;2)通過使用一個相對較小的任務(wù)特定網(wǎng)絡(luò),以有監(jiān)督的方式訓(xùn)練語言模型,使語言模型專門化為實際任務(wù)。
這兩個階段通常稱為預(yù)訓(xùn)練和微調(diào)。這種范式允許將預(yù)先訓(xùn)練的語言模型用于廣泛的任務(wù),而不需要對模型體系結(jié)構(gòu)進(jìn)行任何特定于任務(wù)的更改。在我們的例子中,BERT提供了一個高質(zhì)量的語言模型,該模型針對問答進(jìn)行了微調(diào),但也適用于其他任務(wù),如句子分類和情感分析。
要對BERT進(jìn)行預(yù)訓(xùn)練,您可以從在線提供的預(yù)訓(xùn)練檢查點(圖1(左))開始,也可以在您自己的自定義語料庫上預(yù)訓(xùn)練BERT(圖1(右))。還可以從檢查點初始化預(yù)訓(xùn)練,然后繼續(xù)自定義數(shù)據(jù)。雖然使用定制或領(lǐng)域特定數(shù)據(jù)進(jìn)行預(yù)訓(xùn)練可能會產(chǎn)生有趣的結(jié)果(例如BioBert5),但它是計算密集型的,需要大量并行計算基礎(chǔ)設(shè)施才能在合理的時間內(nèi)完成。GPU支持的多節(jié)點訓(xùn)練是此類場景的理想解決方案。
在“用GPU訓(xùn)練伯特”博客中了解NVIDIA開發(fā)人員如何在不到一個小時內(nèi)訓(xùn)練Bert。
在微調(diào)步驟中,使用特定于任務(wù)的訓(xùn)練數(shù)據(jù)(對于問答,這是(段落,問題,答案)三倍)訓(xùn)練基于預(yù)先訓(xùn)練的BERT語言模型的任務(wù)特定網(wǎng)絡(luò)。請注意,與預(yù)訓(xùn)練相比,微調(diào)通常在計算上的要求要少得多。
使用QA神經(jīng)網(wǎng)絡(luò)進(jìn)行推理:
-
Create a TensorRT engine by passing the
fine-tuned weights and network definition to the TensorRT builder. -
Start the TensorRT runtime with this
engine. -
Feed a passage and a question to the
TensorRT runtime and receive as output the answer predicted by the network.
This entire workflow is outlined in Figure 2
Figure 1: Generating BERT TensorRT engine
from pretrained checkpoints
Figure 2: Workflow to perform inference with
TensorRT runtime engine for BERT QA task
Let’s Run the
Sample!
Set up your environment to perform BERT
inference with the steps below:
-
Create a Docker image with the
prerequisites -
Compile TensorRT optimized plugins
-
Build the TensorRT engine from the
fine-tuned weights -
Perform inference given a passage and a
query
We use scripts to perform these steps, which
you can find in the TensorRT BERT sample repo. While we describe several options you can pass to each script, you could also execute the code below at the command prompt to
get started quickly:
Clone the TensorRT repository, check
out the specific release, and navigate to the BERT demo directory
git clone --recursive https://github.com/NVIDIA/TensorRT && cd TensorRT/ && git checkout release/5.1 && cd demo/BERT
Create and launch the Docker image
sh python/create_docker_container.sh
Build the plugins and download the
fine-tuned models
cd TensorRT/demo/BERT && sh python/build_examples.sh
Build the TensorRT runtime engine
python python/bert_builder.py -m /workspace/models/fine-tuned/bert_tf_v2_base_fp16_384_v2/model.ckpt-8144 -o bert_base_384.engine -b 1 -s 384 -c /workspace/models/fine-tuned/bert_tf_v2_base_fp16_384_v2
Now, give it a passage and see how much
information it can decipher by asking it a few questions.
python python/bert_inference.py -e bert_base_384.engine -p “TensorRT is a high performance deep learning inference platform that delivers low latency and high throughput
for apps such as recommenders, speech and image/video on NVIDIA GPUs. It includes parsers to import models, and plugins to support novel ops and layers before applying optimizations for inference. Today NVIDIA is open sourcing
parsers and plugins in TensorRT so that the deep learning community can customize and extend these components to take advantage of powerful TensorRT optimizations for your apps.” -q “What is TensorRT?” -v /workspace/models/fine-tuned/bert_tf_v2_base_fp16_384_v2/vocab.txt -b 1
Passage: TensorRT is a high performance deep learning inference platform that delivers low latency and high throughput for apps such as recommenders, speech and image/video on NVIDIA GPUs. It includes parsers
to import models, and plugins to support novel ops and layers before applying optimizations for inference. Today NVIDIA is open sourcing parsers and plugins in TensorRT so that the deep learning community can customize and extend these
components to take advantage of powerful TensorRT optimizations for your apps.
Question: What is TensorRT?
Answer: ‘a(chǎn) high performance deep learning inference platform’
—- Given the same passage with a different
question —-
Question: What is included in TensorRT?
Answer: ‘parsers to import models, and plugins to support novel ops and layers before applying optimizations for inference’
模型提供的答案是準(zhǔn)確的,基于所提供的文章文本。該示例使用FP16精度執(zhí)行TensorRT推理。這有助于實現(xiàn)NVIDIA GPU中張量核心的最高性能。在我們的測試中,我們測量了TensorRT的精確度,與框架內(nèi)推理的FP16精度相當(dāng)。
讓我們檢查腳本的可用選項。create_docker_容器.sh腳本使用BERT示例中提供的Dockerfile構(gòu)建Docker映像,它基于NGC中的TensorRT容器。它安裝所有必要的包并啟動創(chuàng)建的映像bert_tensorrt,作為一個正常運行的容器。將腳本執(zhí)行為:
sh create_docker_container.sh
在創(chuàng)建環(huán)境之后,為BERT下載經(jīng)過微調(diào)的權(quán)重。請注意,創(chuàng)建TensorRT引擎不需要預(yù)先訓(xùn)練的權(quán)重(只需要微調(diào)權(quán)重)。除了微調(diào)權(quán)重之外,還可以使用相關(guān)的配置文件,該文件指定諸如注意文件頭、層數(shù)和vocab.txt文件,其中包含從訓(xùn)練過程中學(xué)習(xí)到的詞匯。它們與從NGC下載的微調(diào)模型一起打包;使用構(gòu)建來下載它們_示例.sh腳本。作為這個腳本的一部分,您可以為想要下載的BERT模型指定一組經(jīng)過微調(diào)的權(quán)重。命令行參數(shù)控制精確的BERT模型,該模型將在以后用于模型構(gòu)建和推理,可以如下所示使用:
Usage: sh build_examples.sh
[base | large] [ft-fp16 | ft-fp32] [128 | 384]
·
base | large – determine whether to
download a BERT-base or BERT-large model to optimize
·
ft-fp16 | ft-fp32 – determine whether to
download a BERT model fine-tuned with precision FP16 or FP32
·
128 | 384 – determine whether to
download a BERT model for sequence length 128 or 384
Examples
Running with default parameters
sh build_examples.sh
Running with custom parameters
(BERT-large, FP132 fine-tuned weights, 128 sequence length)
sh build_examples.sh large ft-fp32 128
此腳本將首先使用示例存儲庫中的代碼,并為BERT推斷構(gòu)建TensorRT插件。接下來,它下載并安裝NGC CLI,從NVIDIA的NGC模型庫下載一個經(jīng)過微調(diào)的模型。生成的命令行build_examples.sh指定要使用TensorRT優(yōu)化的模型。默認(rèn)情況下,它下載經(jīng)過微調(diào)的BERT-base,精度為FP16,序列長度為384。
除了微調(diào)模型外,我們還使用配置文件枚舉模型參數(shù)和詞匯表文件,用于將BERT模型輸出轉(zhuǎn)換為文本答案。在為所選模型下載模型和配置信息之后,將為TensorRT構(gòu)建BERT插件。這些插件的共享對象文件放在BERT推理示例的build目錄中。
接下來,我們可以構(gòu)建TensorRT引擎并將其用于問答示例(即推理)。腳本bert_builder.py基于下載的BERT微調(diào)模型構(gòu)建TensorRT推理引擎。它使用在上一步中構(gòu)建的定制TensorRT插件,以及下載的經(jīng)過微調(diào)的模型和配置文件。確保提供給此腳本的序列長度與下載的模型的序列長度匹配。按如下方式使用腳本:
Usage:python
bert_builder.py -m -o <bert.engine> -b -s -c
· -m, – checkpoint file for the
fine-tuned model
· -o, – path for the output TensorRT
engine file (i.e. bert.engine)
· -b, – batch size for inference
(default=1)
· -s, – sequence length matching the
downloaded BERT fine-tuned model
· -c, – directory containing
configuration file for BERT parameters (attention heads, hidden layers, etc.)
Example:
python python/bert_builder.py -m /workspace/models/fine-tuned/bert_tf_v2_base_fp16_384_v2/model.ckpt-8144 -o bert_base_384.engine -b 1 -s 384 -c /workspace/models/fine-tuned/bert_tf_v2_base_fp16_384_v2
你現(xiàn)在應(yīng)該有一個TensorRT引擎(即bert.engine)在推理腳本中使用(bert_inference.py)對于QA。我們將在后面的章節(jié)中描述構(gòu)建TensorRT引擎的過程。現(xiàn)在您可以向bert提供一個段落和一個bert_inference.py并查看模型是否能夠正確回答您的查詢。與推理腳本交互的方法很少:段落和問題可以作為命令行參數(shù)提供(使用passage and –question標(biāo)志),也可以從給定文件傳入(使用–passage_file and –question_file標(biāo)志)。如果在執(zhí)行過程中沒有給出這兩個標(biāo)志,則在執(zhí)行開始后,將提示用戶輸入段落和問題。bert_inference.py腳本如下:
Usage: python bert_inference.py --bert_engine <bert.engine> [–passage |–passage_file] [–question | --question_file] --vocab_file --batch_size <batch_size>
· -e, –bert_engine – path to the TensorRT
engine created in the previous step
· -p, –passage – text for
paragraph/passage for BERT QA
· -pf, –passage_file – file containing
text for paragraph/passage
· -q, –question – text for query/question
for BERT QA
· -qf, –question_file – file containing
text for query/question
· -v, –vocab_file – file containing entire
dictionary of words
· -b, –batch_size – batch size for
inference
總結(jié)
以上是生活随笔為你收集整理的基于TensorRT的BERT实时自然语言理解(上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NVIDIA TensorRT高性能深度
- 下一篇: 基于TensorRT的BERT实时自然语