基于自动驾驶车辆的NVIDIA-TensorRT推理实时优化
基于自動駕駛車輛的NVIDIA-TensorRT推理實時優化
Optimizing NVIDIA TensorRT Conversion for Real-time Inference on Autonomous Vehicles
自動駕駛系統使用各種神經網絡模型,這些模型需要在gpu上進行非常精確和高效的計算。Zoox是一家全新開發robotaxis的初創公司,利用NVIDIA DRIVE的高性能、節能計算。最近,Zoox在舊金山發布了一個小時的完全自主的游戲,詳細展示了他們的人工智能堆棧。
與TensorFlow相比,NVIDIA TensorRT提供了顯著的加速(fp32為2-6倍,Zoox網絡為int8中為9-19倍),支持使用CUDA流的異步和并發推理能力Zooxvision/lidar/radar/prediction 算法嚴重依賴于深度神經網絡,這些神經網絡都運行在我們的車輛上的NVIDIA GPU上,并且大多與TensorRT一起部署。
TensorRT是一個用于高性能深度學習推理的SDK,它為深度學習推理應用程序提供低延遲和高吞吐量。
可以使用各種轉換管道將模型轉換為TensorRT引擎。例如,使用Caffe訓練的模型可以使用Caffe解析器輕松地轉換為TensorRT運行時。
然而,TensorFlow模型需要使用ONNX(開放式神經網絡交換)轉換為TensorRT引擎。TensorFlow也可用于其他培訓工具。
在為所有這些深層神經網絡部署和維護TensorRT引擎的過程中,我們發現了以下痛點:
ONNX和TensorRT只支持一組有限的TensorFlow操作。
內核大小和步幅的某些組合可能會對TensorRT產生副作用。
遷移到低精度推理或TensorRT升級可能會導致性能下降。
在Zoox,我們開發了一組工具來促進TensorRT引擎的部署、驗證和維護,如圖2所示。在下面的部分中,將詳細介紹這些模塊。
TensorRT conversion checker
TensorRT轉換檢查器的目標是幫助您在訓練網絡之前識別可能的轉換失敗。checker是輕量級的,設計上是最小的(在本文后面的代碼示例中重點介紹)。在訓練前,它在所構建的網絡上觸發一個TensorRT轉換過程。我們只有在轉換成功后才開始訓練。
下面的代碼示例顯示了TensorRT轉換檢查器。要使用該插件,用戶只需導入包,在網絡構建期間注冊輸入/輸出節點,然后在培訓開始前觸發轉換檢查。
import trt_checker
class Lenet5():
def network(self, X):input = tf.identity(X,
name = “input”)
# Registers the input in
the conversion checker.
trt_checker.register_input(input)# Network definition....# Output node.output =
tf.identity(logits, name=“output”)
# Registers the output
node in the conversion checker.
trt_checker.register_output(output)return output
def main():
...# Checks if the model can be converted to trt.conversion_result =
trt_checker.check_conversion()
# Only train when trt
conversion is successful.
if conversion_result:accuracy =
lenet_network.train()
Output deviation inspection
此插件的目標是在運行整個特定于模型的評估之前,報告轉換后的TensorRT引擎的潛在精度回歸。這個插件在轉換后的TensorRT引擎和原始的TensorFlow圖上運行推理,輸入完全相同(由用戶隨機生成或指定)。然后報告輸出偏差的分布,給開發人員一個潛在精度回歸的預警。該模塊是逐層檢測模塊的構建模塊。
Figure 3. Output deviation inspection
Layer-by-layer inspection
The following code example shows the
layer-by-layer inspection:
def
layer_by_layer_analysis(graph, input_layer):
median_error = []
for layer in graph.layers():
errors = convert(graph, input=input_layer, output=layer)
median_error.append(median(errors))
plot(median_error)
如果觀察到精度回歸,我們希望找出TensorRT引擎中的哪一層或操作對回歸有顯著貢獻。這促使我們開發了逐層檢測模塊。當被調用時,模塊為每個中間操作運行一個轉換作業,并報告由這個特定操作生成的中間值/最大值錯誤(如圖4所示)。本模塊在研究不同版本的TensorRT中觀察到的不同行為時非常有用。
Latency flame graph
Figure 4. Example regression observed in semantic
segmentation when upgrading from TensorRT 5.1.5 to TensorRT 7.0.
圖4顯示了這種回歸的一個例子,在這個例子中,我們觀察到語義分段輸出有輕微的回歸。我們對TensorRT 5.1引擎和TensorRT 7.0引擎進行了逐層檢查,然后繪制了每層的中值誤差。
圖6顯示了為每個層生成的中值誤差。我們可以看到,在這個特定網絡的上采樣層中有一個潛在的缺陷。基于這些信息,我們能夠在一個較小的網絡上復制這個回歸,并將這個錯誤報告給NVIDIA。這個錯誤現在已在TensorRT 7.1中修復。
Figure 5. Layer-by-layer inspection results on the two
TensorRT engines used in Figure 4. The orange line shows the median error of the TensorRT 7.0 inference outputs compared to the TensorFlow inference outputs. The blue line shows the results generated by the TensorRT 5.0 engine. There is a significant difference in the error distributions on the upsampling layers.
Latency flame graph
為了可視化推理中的瓶頸并找出可能的優化操作,我們將TensorRT profiler生成的分層計時信息繪制成火焰圖。計時細節根據每個層的名稱范圍分組,如圖6所示。這使我們能夠看到網絡的哪個部分比預期的時間要長。
Figure 6. Latency flame graph on Inception Net. The 1767 samples shown in this graph indicates that a forward pass on this network takes 1.767 ms.
Automated conversion pipeline
在Zoox,我們維護一個自動轉換管道,跟蹤每個模型使用的轉換選項。當觸發時,自動轉換管道將所有記錄的模型轉換為TensorRT引擎,并將它們上載到云中進行部署。它還為新轉換的TensorRT引擎運行驗證作業,以驗證準確性。這個管道只需一個命令就可以幫助我們將所有現有模型升級到TensorRT的更新版本。
Incompatible graph test suite
Zoox維護一個TensorFlow到TensorRT的轉換測試套件。它測試從TensorFlow圖形到TensorRT引擎的轉換失敗案例,以及報告的NVIDIA錯誤識別。
每個測試構建一個TensorFlow圖,將其轉換為TensorRT,并將輸出偏差與TensorFlow圖進行比較。使用這個測試套件,我們不僅可以向Zoox工程師演示哪些圖形結構或操作可能無法使用TensorRT,而且還可以檢測到在升級到較新版本的TensorRT時修復了哪些回歸。Summary
本文介紹了Zoox-TensorRT轉換管道中的幾個特性。TensorRT轉換檢查器參與神經網絡訓練的早期階段,以確保在您浪費時間和資源進行全面訓練之前發現不兼容的操作。您可以在每個層調用推理精度驗證,以識別不利于降低精度計算的操作。詳細的分析揭示了不必要的計算,這些計算在TensorRT內部沒有得到優化,但是可以通過在圖構造期間進行簡單的代碼更改來優化這些計算。
自動轉換管道幫助您驗證每個TensorRT升級或模型重新轉換。利用這條管道,我們成功地為在Zoox自主駕駛平臺上執行各種流線型感知任務的神經網絡提供了TensorRT轉換支持。
總結
以上是生活随笔為你收集整理的基于自动驾驶车辆的NVIDIA-TensorRT推理实时优化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用NVIDIA NGC的TensorR
- 下一篇: Yolov4性能分析(上)