ONNX Runtime 執行提供程式
ONNX Runtime 透過其可擴充套件的執行提供程式 (Execution Providers, EP) 框架與各種硬體加速庫協作,以在硬體平臺上最佳化執行 ONNX 模型。該介面為應用程式開發人員提供了靈活性,使其能夠將 ONNX 模型部署到雲端和邊緣側的不同環境中,並利用平臺的計算能力來最佳化執行效果。

ONNX Runtime 使用 GetCapability() 介面與執行提供程式協作,分配特定的節點或子圖,以便在受支援的硬體上由 EP 庫執行。預安裝在執行環境中的 EP 庫負責在硬體上處理和執行 ONNX 子圖。這種架構抽象化了硬體特定庫的細節,而這些庫對於在 CPU、GPU、FPGA 或專用 NPU 等硬體平臺上最佳化深度神經網路的執行至關重要。

目前,ONNX Runtime 支援許多不同的執行提供程式。其中一些 EP 已投入生產環境用於即時服務,而另一些則以預覽版形式釋出,以便開發人員能夠使用不同的選項來開發和定製其應用程式。
支援的執行提供程式摘要
| CPU | GPU | 物聯網/邊緣/移動端 | 其他 |
|---|---|---|---|
| 預設 CPU | NVIDIA CUDA | Intel OpenVINO | 瑞芯微 NPU (預覽版) |
| Intel DNNL | NVIDIA TensorRT | Arm 計算庫 (預覽版) | 賽靈思 Vitis-AI (預覽版) |
| TVM (預覽版) | DirectML | Android 神經網路 API | 華為 CANN (預覽版) |
| Intel OpenVINO | AMD MIGraphX | Arm NN (預覽版) | AZURE (預覽版) |
| XNNPACK | Intel OpenVINO | CoreML (預覽版) | |
| AMD ROCm(已棄用) | 高通 QNN | XNNPACK |
新增執行提供程式
專用硬體加速解決方案的開發人員可以整合到 ONNX Runtime,以便在他們的技術棧上執行 ONNX 模型。要建立與 ONNX Runtime 互動的 EP,您必須首先為 EP 確定一個唯一名稱。有關詳細說明,請參閱:新增新的執行提供程式。
構建包含 EP 的 ONNX Runtime 包
ONNX Runtime 包可以與預設 CPU 執行提供程式以及任何 EP 組合進行構建。注意:如果將多個 EP 合併到同一個 ONNX Runtime 包中,則所有依賴庫必須都存在於執行環境中。使用不同 EP 構建 ONNX Runtime 包的步驟記錄在此處。
執行提供程式 API
所有 EP 使用相同的 ONNX Runtime API。這為應用程式在不同的硬體加速平臺上執行提供了統一的介面。用於設定 EP 選項的 API 可用於 Python、C/C++/C#、Java 和 node.js。
注意:我們正在更新 API 支援,以實現所有語言繫結的一致性,並會在此處更新具體細節。
`get_providers`: Return list of registered execution providers.
`get_provider_options`: Return the registered execution providers' configurations.
`set_providers`: Register the given list of execution providers. The underlying session is re-created.
The list of providers is ordered by Priority. For example ['CUDAExecutionProvider', 'CPUExecutionProvider']
means execute a node using CUDAExecutionProvider if capable, otherwise execute using CPUExecutionProvider.
使用執行提供程式
import onnxruntime as rt
#define the priority order for the execution providers
# prefer CUDA Execution Provider over CPU Execution Provider
EP_list = ['CUDAExecutionProvider', 'CPUExecutionProvider']
# initialize the model.onnx
sess = rt.InferenceSession("model.onnx", providers=EP_list)
# get the outputs metadata as a list of :class:`onnxruntime.NodeArg`
output_name = sess.get_outputs()[0].name
# get the inputs metadata as a list of :class:`onnxruntime.NodeArg`
input_name = sess.get_inputs()[0].name
# inference run using image_data as the input to the model
detections = sess.run([output_name], {input_name: image_data})[0]
print("Output shape:", detections.shape)
# Process the image to mark the inference points
image = post.image_postprocess(original_image, input_size, detections)
image = Image.fromarray(image)
image.save("kite-with-objects.jpg")
# Update EP priority to only CPUExecutionProvider
sess.set_providers(['CPUExecutionProvider'])
cpu_detection = sess.run(...)