ONNX Runtime 执行提供者
ONNX Runtime 通过其可扩展的**执行提供者 (EP)** 框架与不同的硬件加速库协同工作,以在硬件平台上最佳地执行 ONNX 模型。该接口为应用开发者提供了灵活性,使其能够在云端和边缘的不同环境中部署 ONNX 模型,并通过利用平台的计算能力来优化执行。
ONNX Runtime 使用 GetCapability()
接口与执行提供者协同工作,将特定节点或子图分配给 EP 库在支持的硬件上执行。预装在执行环境中的 EP 库处理并在硬件上执行 ONNX 子图。这种架构抽象了硬件特定库的细节,这些库对于优化跨 CPU、GPU、FPGA 或专用 NPU 等硬件平台的深度神经网络执行至关重要。
ONNX Runtime 目前支持许多不同的执行提供者。一些 EP 已投入生产用于实际服务,而另一些则以预览版发布,以使开发者能够利用不同的选项进行开发和定制其应用。
支持的执行提供者摘要
CPU | GPU | IoT/边缘/移动端 | 其他 |
---|---|---|---|
默认 CPU | NVIDIA CUDA | Intel OpenVINO | Rockchip 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 包可以与默认 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(...)