tensorRT:入門

TensorRT簡介

tensorRT的核心是c++運行庫,這個運行庫能大大提高網絡在gpu上的推理(inference)速度。tensorflow、caffe、pytorch等訓練框架更關注網絡設計的靈活性,tensorRT能彌補其運行速度的缺陷。tensorRT專門關注對訓練好的網絡如何優化,以更快的生成結果。

一些訓練框架,比如tensorflow,已經集成了tensorRT。另外,tensorRT可以作爲運行庫供用戶調用。

TensorRT包含三部分:

  • 模型解析器 parser
  • c++ API
  • Python API

在這裏插入圖片描述
TensorRT通過組合多個層和優化內核的選擇來優化網絡,改善延遲(latency),吞吐量(throughput),能效(power efficiency)和內存消耗(memory consumption)。另外在程序中可以指定以半精度(fp16)或整數(int8)方式運行。

TensorRT API包括最常見的深度學習layers的實現。 有關layers的更多信息,請參見TensorRT Layers。 您還可以使用C ++ plugin API或Python plugin API爲TensorRT不支持的不常用或更具創新性的layers提供實現。

基於tensorRT的深度學習模型系統開發和部署

一般來說,開發和部署一個深度學習模型需要三步:

  • 訓練
  • 開發解決方案
  • 部署解決方案

階段一:訓練

在訓練階段,首先明確要解決的問題,確定輸入輸出和損失函數,標註數據集。然後設計網絡結構訓練模型。訓練時監視學習過程,根據網絡表現修改損失函數、添加或修改數據集,驗證模型性能並保存訓練過的模型。訓練和驗證通常使用tensorflow、pytorch等框架,使用gpu進行。

階段二:開發解決方案

1.這一階段將利用訓練好的模型,創建解決方案並驗證。

考慮神經網絡是在更大的系統中的哪一部分,並設計和實施適當的方案。包含模型的系統千差萬別,可能是自動駕駛系統、視頻安全系統、設備的語音接口、生產線的自動化質量保證系統、提供產品推薦的在線零售系統等等。
確定優先事項。在設計時需要考慮很多因素:
a. 網絡模型的數量及結構(並行還是級聯),或是根據終端用戶的數據得到最終模型
b. 運行網絡的設備
c. 數據如何到達模型,相機、傳感器還是網絡
d. 進行哪些預處理? 什麼格式輸入,圖像是否需要裁剪、旋轉;文本是哪一字符集
e. 對吞吐量和時延的需求
f. 是否可以多個請求並行處理
g. 是否需要多個網絡的實例來一起批處理
h. 如何處理網絡的輸出
i. 需要哪些後續的步驟

2.定義解決方案的體系結構、確定了優先級之後,可以使用tensorRT從保存的網絡模型中構建推理引擎(build inference engine)。根據訓練框架和網絡結構,可以有多種方式執行此操作。通常使用onnx解析器、uff解析器等。

3.解析網絡之後,還需要考慮一些優化設置:

  • batchsize大小
  • 工作空間(workspace)大小
  • 混合精度(mixed precision)

在tensorRT構建步驟中選擇並指定了這些選項。

4.使用tensorRT創建inference engine後,需要驗證它是否可以重現訓練過程中測得的模型結果。如果選擇了fp32或fp16,則它應與結果非常接近。如果選擇了int8,則在訓練過程中獲得的準確度(accuracy)與推理準確度之間可能有一些差距。

5.序列化輸出inference engine,也叫plan file。

階段三:部署解決方案

tensorRT庫將鏈接到部署應用程序中,部署應用程序將在需要推斷結果時調用tensorRT庫。要初始化engine,應用程序將首先將plan file中的模型反序列化爲engine。

tensorRT通常異步(asynchronously)使用,因此,當輸入數據到達時,程序將調用入隊函數(enqueue function)寫入輸入緩衝區(input buffer)。同時,tensorRT會將推斷結果放入輸出緩衝區,像下面這樣:

net.doInference(inputData.data(), outputData.get());

tensorRT如何工作

爲了優化inference engine,tensorRT會採用對網絡執行組合層的優化、針對平臺特定優化等一系列優化,並生成inference engine。這一過程成爲build phase,在這一過程中會花費大量的時間,尤其是在嵌入式平臺。因此,typical 應用只會build engine once,結果保存爲plan file供以後使用。

注意:生成的plan file不能跨平臺或跨tensorRT版本移植。plan特定於其所構建的指定gpu模型。

build phase 對layer graph 執行以下優化:
Elimination of layers whose outputs are not used 消除輸出沒有被使用的層
Elimination of operations which are equivalent to no-op 消除對於輸入輸出無變化的運算
Fusion of convolution, bias and ReLU operations 融合卷積,偏置和relu層到一個層
Aggregation of operations with sufficiently similar parameters and the same source tensor (for example, the 1x1 convolutions in GoogleNet v5’s inception module) 聚合相似運算
Merging of concatenation layers by directing layer outputs to the correct eventual destination.
融合cancatenation層

builder還可以修改權重的精度。 當生成8位整數精度的網絡時,它使用稱爲calibration的過程來確定中間激活層的動態範圍,從而確定用於量化的適當縮放因子。

此外,build phase還會在虛擬數據(dummy data )上運行各層,以從其內核目錄中選擇最快的文件,並在適當的情況下執行權重預格式化(pre-formatting)和內存優化。
查看更多Working With Mixed Precision

tensorRT提供了什麼能力

TensorRT使開發人員能夠導入,校準,生成和部署優化的網絡。 網絡可以直接從Caffe導入,也可以通過UFF或ONNX格式從其他框架導入。 也可以通過實例化各個layer並直接設置參數和權重以編程方式創建它們。

用戶還可以使用Plugin interface通過TensorRT運行自定義layer。 GraphSurgeon utility提供了將TensorFlow節點映射到TensorRT中的自定義layer的功能,從而可以使用TensorRT對許多TensorFlow網絡進行推理。

TensorRT在所有支持的平臺上提供C ++實現,並在x86,aarch64和ppc64le上提供Python實現。
TensorRT provides a C++ implementation on all supported platforms, and a Python implementation on x86, aarch64, and ppc64le.

TensorRT核心庫中的關鍵接口是:

  • Network Definition 網絡定義
    The Network Definition interface provides methods for the application to specify the definition of a network. Input and output tensors can be specified, layers can be added, and there is an interface for configuring each supported layer type. As well as layer types, such as convolutional and recurrent layers, and a Plugin layer type allows the application to implement functionality not natively supported by TensorRT. For more information about the Network Definition, see Network Definition API.
    網絡定義接口爲應用程序提供了指定網絡定義的方法。 可以指定輸入和輸出張量,可以添加層,並且有一個用於配置每種支持的層類型的接口。 和layer types一樣,像卷積層和循環層等層,以及Plugin層類型都允許應用程序實現TensorRT本身不支持的功能。
    有關網絡定義的更多信息,請參見網絡定義API
  • Builder 構建器
    The Builder interface allows creation of an optimized engine from a network definition. It allows the application to specify the maximum batch and workspace size, the minimum acceptable level of precision, timing iteration counts for autotuning, and an interface for quantizing networks to run in 8-bit precision. For more information about the Builder, see Builder API.
    Builder界面允許根據網絡定義創建優化的引擎。 它允許應用程序指定最大批處理和工作空間大小,最小可接受的精度級別,用於自動調整的定時迭代計數以及用於量化網絡以8位精度運行的接口。 有關Builder的更多信息,請參見Builder API
  • Engine 引擎
    The Engine interface allows the application to execute inference. It supports synchronous and asynchronous execution, profiling, and enumeration and querying of the bindings for the engine inputs and outputs. A single engine can have multiple execution contexts, allowing a single set of trained parameters to be used for the simultaneous execution of multiple batches. For more information about the Engine, see Execution API.
    Engine接口允許應用程序執行推理。 它支持同步和異步執行,性能分析(profiling)以及枚舉和查詢引擎輸入和輸出的綁定。 單個引擎可以具有多個執行上下文,從而允許將一組訓練好的參數用於同時執行多個批次。 有關引擎的更多信息,請參見Execution API

TensorRT provides parsers for importing trained networks to create network definitions:
TensorRT提供瞭解析器,用於導入經過訓練的網絡以創建網絡定義:

  • Caffe Parser
    This parser can be used to parse a Caffe network created in BVLC Caffe or NVCaffe 0.16. It also provides the ability to register a plugin factory for custom layers. For more details on the C++ Caffe Parser, see NvCaffeParser or the Python Caffe Parser.
    該解析器可用於解析在BVLC Caffe或NVCaffe 0.16中創建的Caffe網絡。 它還提供了爲自定義layer 註冊到plugin factory的功能。 有關C ++ Caffe解析器的更多詳細信息,請參見NvCaffeParser或Python Caffe解析器。
  • UFF Parser
    This parser can be used to parse a network in UFF format. It also provides the ability to register a plugin factory and pass field attributes for custom layers. For more details on the C++ UFF Parser, see NvUffParser or the Python UFF Parser.
    該解析器可用於解析UFF格式的網絡。 它還提供了註冊插件工廠併爲自定義層傳遞字段屬性的功能。 有關C ++ UFF解析器的更多詳細信息,請參見NvUffParser或Python UFF解析器。
  • ONNX Parser
    This parser can be used to parse an ONNX model. For more details on the C++ ONNX Parser, see NvONNXParser or the Python ONNX Parser.
    該解析器可用於解析ONNX模型。 有關C ++ ONNX解析器的更多詳細信息,請參見NvONNXParserPython ONNX解析器
    Restriction: Since the ONNX format is quickly developing, you may encounter a version mismatch between the model version and the parser version. The ONNX Parser shipped with TensorRT 5.1.x supports ONNX IR (Intermediate Representation) version 0.0.3, opset version 9.
    Note: Additionally, some TensorRT Caffe and ONNX parsers and plugins can be found on GitHub.
    限制:由於ONNX格式正在快速開發中,因此您可能會遇到模型版本與解析器版本之間的版本不匹配的情況。 TensorRT 5.1.x隨附的ONNX解析器支持ONNX IR(中間表示)版本0.0.3,操作集版本9。
    注意:此外,可以在GitHub上找到一些TensorRT Caffe和ONNX解析器和插件。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章