ONNX Runtime 执行提供程序

ONNX Runtime 通过其可扩展的执行提供程序 (EP) 框架与不同的硬件加速库协同工作,以在硬件平台上优化执行 ONNX 模型。该接口为应用程序开发者提供了灵活性,使其能够在云端和边缘的不同环境中部署 ONNX 模型,并利用平台的计算能力来优化执行。

Executing ONNX models across different HW environments

ONNX Runtime 使用 GetCapability() 接口与执行提供程序进行交互,以分配特定的节点或子图,供受支持硬件中的 EP 库执行。预安装在执行环境中的 EP 库会在硬件上处理和执行 ONNX 子图。此架构抽象出了特定硬件库的细节,这些细节对于优化深度神经网络在 CPU、GPU、FPGA 或专用 NPU 等硬件平台上的执行至关重要。

ONNX Runtime GetCapability()

目前,ONNX Runtime 支持许多不同的执行提供程序。其中一些 EP 已投入生产环境用于线上服务,而另一些则以预览版发布,以使开发者能够使用不同的选项来开发和定制其应用程序。

受支持的执行提供程序汇总

CPU GPU 物联网/边缘/移动端 其他
默认 CPU NVIDIA CUDA Intel OpenVINO Rockchip NPU (预览版)
Intel DNNL NVIDIA TensorRT Arm Compute Library (预览版) Xilinx Vitis-AI (预览版)
TVM (预览版) DirectML Android Neural Networks API Huawei CANN (预览版)
Intel OpenVINO AMD MIGraphX Arm NN (预览版) AZURE (预览版)
XNNPACK Intel OpenVINO CoreML (预览版)  
AMD ROCm(已弃用) Qualcomm QNN XNNPACK  
  WebGPU    

添加执行提供程序

专门从事硬件加速解决方案的开发者可以与 ONNX Runtime 集成,在其技术栈上执行 ONNX 模型。要创建与 ONNX Runtime 交互的 EP,您必须首先为该 EP 确定一个唯一的名称。有关详细说明,请参阅:添加新的执行提供程序

构建包含 EP 的 ONNX Runtime 软件包

可以构建包含任意 EP 组合以及默认 CPU 执行提供程序的 ONNX Runtime 软件包。注意,如果将多个 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(...)


目录