基於TensorRT的BERT實時自然語言理解(上)


基於TensorRT的BERT實時自然語言理解(上)

大規模語言模型(LSLMs)如BERT、GPT-2和XL-Net為許多自然語言理解(NLU)任務帶來了最先進的精准飛躍。自2018年10月發布以來,BERT1(來自Transformer的雙向編碼器表示)仍然是最流行的語言模型之一,並且在編寫時仍能提供最先進的精准。

BERT為NLU任務的准確性提供了一個飛躍,使得基於語言的高質量服務在許多行業的公司都能達到。要在生產中使用模型,除了精准之外,還需要考慮延遲等因素,這些因素會影響最終用戶對服務的滿意度。由於BERT是一個12/24層的多層多頭注意網絡,在推理過程中需要大量的計算。到目前為止,這給公司在實時應用程序中部署BERT帶來了挑戰。

NVIDIA發布了針對BERT的新的TensorRT優化,允許您在t4gpu上執行2.2ms*的推理。這比僅使用CPU的平台快17倍,而且在對話式人工智能應用程序所需的10ms延遲預算之內。這些優化使得在生產中使用BERT變得切實可行,例如,作為會話AI服務的一部分。              TensorRT是一個用於高性能深度學習推理的平台,它包括一個優化器和運行時,可以最大限度地減少延遲並最大化生產中的吞吐量。使用TensorRT,您可以優化在所有主要框架中訓練的模型,以高精度校准較低精度,最后在生產中部署。

在這個TensorRT示例repo中,所有用於使用BERT實現此性能的優化和代碼都將作為開放源代碼發布。我們已經優化了Transformer層,它是BERT編碼器的基本構建塊,因此您可以將這些優化應用於任何基於BERT的NLP任務。BERT被應用於會話式人工智能之外的一組擴展的語音和NLP應用程序,所有這些都可以利用這些優化。             

問答(QA)或閱讀理解是測試模型理解上下文能力的一種非常流行的方法。SQuAD leaderboard3排行榜3跟蹤此任務的最佳執行者,以及他們提供的數據集和測試集。在過去的幾年里,隨着學術界和公司的全球貢獻,質量保證能力有了快速的發展。在本文中,將演示如何使用Python創建一個簡單的問答應用程序,它由我們今天發布的TensorRT優化BERT代碼提供支持。這個例子提供了一個API來輸入段落和問題,並返回由BERT模型生成的響應。             

從使用TensorRT for BERT執行訓練和推理的步驟開始。

BERT Training and Inference Pipeline

NLP研究人員和開發人員面臨的一個主要問題是缺乏針對其特定NLP任務的高質量標記訓練數據。為了克服從零開始學習任務模型的問題,NLP的最新突破是利用大量未標記的文本,將NLP任務分解為兩個部分:1)學習表示單詞的含義和它們之間的關系,即使用輔助任務和大量文本語料庫建立語言模型;2)通過使用一個相對較小的任務特定網絡,以有監督的方式訓練語言模型,使語言模型專門化為實際任務。

這兩個階段通常稱為預訓練和微調。這種范式允許將預先訓練的語言模型用於廣泛的任務,而不需要對模型體系結構進行任何特定於任務的更改。在我們的例子中,BERT提供了一個高質量的語言模型,該模型針對問答進行了微調,但也適用於其他任務,如句子分類和情感分析。

要對BERT進行預訓練,您可以從在線提供的預訓練檢查點(圖1(左))開始,也可以在您自己的自定義語料庫上預訓練BERT(圖1(右))。還可以從檢查點初始化預訓練,然后繼續自定義數據。雖然使用定制或領域特定數據進行預訓練可能會產生有趣的結果(例如BioBert5),但它是計算密集型的,需要大量並行計算基礎設施才能在合理的時間內完成。GPU支持的多節點訓練是此類場景的理想解決方案。

在“用GPU訓練伯特”博客中了解NVIDIA開發人員如何在不到一個小時內訓練Bert。

在微調步驟中,使用特定於任務的訓練數據(對於問答,這是(段落,問題,答案)三倍)訓練基於預先訓練的BERT語言模型的任務特定網絡。請注意,與預訓練相比,微調通常在計算上的要求要少得多。             

使用QA神經網絡進行推理:

  1. Create a TensorRT engine by passing the fine-tuned weights and network definition to the TensorRT builder.
  2. Start the TensorRT runtime with this engine.
  3. 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:

  1. Create a Docker image with the prerequisites
  2. Compile TensorRT optimized plugins
  3. Build the TensorRT engine from the fine-tuned weights
  4. 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 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'

模型提供的答案是准確的,基於所提供的文章文本。該示例使用FP16精度執行TensorRT推理。這有助於實現NVIDIA GPU中張量核心的最高性能。在我們的測試中,我們測量了TensorRT的精確度,與框架內推理的FP16精度相當。             

讓我們檢查腳本的可用選項。create_docker_容器.sh腳本使用BERT示例中提供的Dockerfile構建Docker映像,它基於NGC中的TensorRT容器。它安裝所有必要的包並啟動創建的映像bert_tensorrt,作為一個正常運行的容器。將腳本執行為:

sh create_docker_container.sh

在創建環境之后,為BERT下載經過微調的權重。請注意,創建TensorRT引擎不需要預先訓練的權重(只需要微調權重)。除了微調權重之外,還可以使用相關的配置文件,該文件指定諸如注意文件頭、層數和vocab.txt文件,其中包含從訓練過程中學習到的詞匯。它們與從NGC下載的微調模型一起打包;使用構建來下載它們_示例.sh腳本。作為這個腳本的一部分,您可以為想要下載的BERT模型指定一組經過微調的權重。命令行參數控制精確的BERT模型,該模型將在以后用於模型構建和推理,可以如下所示使用:

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推斷構建TensorRT插件。接下來,它下載並安裝NGC CLI,從NVIDIA的NGC模型庫下載一個經過微調的模型。生成的命令行build_examples.sh指定要使用TensorRT優化的模型。默認情況下,它下載經過微調的BERT-base,精度為FP16,序列長度為384。             

除了微調模型外,我們還使用配置文件枚舉模型參數和詞匯表文件,用於將BERT模型輸出轉換為文本答案。在為所選模型下載模型和配置信息之后,將為TensorRT構建BERT插件。這些插件的共享對象文件放在BERT推理示例的build目錄中。             

接下來,我們可以構建TensorRT引擎並將其用於問答示例(即推理)。腳本bert_builder.py基於下載的BERT微調模型構建TensorRT推理引擎。它使用在上一步中構建的定制TensorRT插件,以及下載的經過微調的模型和配置文件。確保提供給此腳本的序列長度與下載的模型的序列長度匹配。按如下方式使用腳本:

Usage:python bert_builder.py -m <checkpoint> -o <bert.engine> -b <batch size> -s <sequence length> -c <config file_directory>

  • -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

你現在應該有一個TensorRT引擎(即bert.engine)在推理腳本中使用(bert_inference.py)對於QA。我們將在后面的章節中描述構建TensorRT引擎的過程。現在您可以向bert提供一個段落和一個bert_inference.py並查看模型是否能夠正確回答您的查詢。與推理腳本交互的方法很少:段落和問題可以作為命令行參數提供(使用passage and –question標志),也可以從給定文件傳入(使用–passage_file and –question_file標志)。如果在執行過程中沒有給出這兩個標志,則在執行開始后,將提示用戶輸入段落和問題。bert_inference.py腳本如下:

Usage: python bert_inference.py --bert_engine <bert.engine> [--passage | --passage_file] [--question | --question_file] --vocab_file <vocabulary 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM