ONNX Runtime 执行提供者
ONNX Runtime 通过其可扩展的**执行提供者** (EP) 框架与不同的硬件加速库协同工作,以在硬件平台上最佳地执行 ONNX 模型。此接口使 AP 应用程序开发人员能够灵活地在云端和边缘的不同环境中部署其 ONNX 模型,并通过利用平台的计算能力来优化执行。
ONNX Runtime 使用 GetCapability()
接口与执行提供者协同工作,将特定节点或子图分配给 EP 库在受支持的硬件上执行。预装在执行环境中的 EP 库在硬件上处理并执行 ONNX 子图。这种架构抽象了硬件特定库的细节,这些库对于优化跨 CPU、GPU、FPGA 或专用 NPU 等硬件平台的深度神经网络执行至关重要。
ONNX Runtime 目前支持许多不同的执行提供者。一些 EP 已投入生产用于实时服务,而另一些则以预览版发布,以使开发人员能够使用不同的选项开发和定制其应用程序。
支持的执行提供者摘要
CPU | GPU | IoT/边缘/移动设备 | 其他 |
---|---|---|---|
默认 CPU | NVIDIA CUDA | Intel OpenVINO | 瑞芯微 NPU (预览) |
Intel DNNL | NVIDIA TensorRT | Arm 计算库 (预览) | Xilinx Vitis-AI (预览) |
TVM (预览) | DirectML | Android 神经网络 API | 华为 CANN (预览) |
Intel OpenVINO | AMD MIGraphX | Arm NN (预览) | AZURE (预览) |
XNNPACK | Intel OpenVINO | CoreML (预览) | |
AMD ROCm | TVM (预览) | ||
TVM (预览) | Qualcomm QNN | ||
XNNPACK |
添加执行提供者
专业硬件加速解决方案的开发人员可以与 ONNX Runtime 集成,以在其堆栈上执行 ONNX 模型。要创建与 ONNX Runtime 接口的 EP,您必须首先为 EP 确定一个唯一的名称。有关详细说明,请参阅:添加新的执行提供者。
使用 EP 构建 ONNX Runtime 包
ONNX Runtime 包可以与任何 EP 组合以及默认的 CPU 执行提供者一起构建。**请注意**,如果将多个 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(...)