# ONNX Runtime

**ONNX Runtime** 是微软（Microsoft）开发的开源跨平台推理引擎，用于运行 ONNX 格式的机器学习模型，于 2018 年 12 月 4 日首次开源<sup>[1](https://news.microsoft.com/wp-content/uploads/prod/sites/543/2018/12/Connect-2018-Book-of-News.pdf)</sup>。它可以在云服务器、边缘与移动设备以及网页浏览器中执行模型，提供 Python、C++、C#、C、Java、JavaScript 等多种语言的 API<sup>[2](https://onnxruntime.ai/inference)</sup>。其核心设计思想是执行提供程序（execution provider）：这一抽象层让硬件专用的加速库接管模型图的一部分，同时由默认的 CPU 提供程序保证每个算子始终能够运行<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 首次发布 | 微软于 2018 年 12 月 4 日开源，作为面向 ONNX 格式模型的高性能推理引擎<sup>[1](https://news.microsoft.com/wp-content/uploads/prod/sites/543/2018/12/Connect-2018-Book-of-News.pdf)</sup> |
| 1.0 版本 | 2019 年 10 月 30 日发布；第一年内即在微软 60 多个模型中投入生产，包括 Office、Bing、认知服务（Cognitive Services）、Windows 和 Skype<sup>[4](https://opensource.microsoft.com/blog/2019/10/30/announcing-onnx-runtime-1-0/)</sup> |
| 核心抽象 | 执行提供程序声明其可执行的节点或融合子图、内存分配器等；能力通过 `GetCapability()` API 查询<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup> |
| 图优化 | 三个级别（基础、扩展、布局），按顺序应用，可在推理前在线进行，也可离线进行并将优化后的图保存到磁盘<sup>[5](https://onnxruntime.ai/docs/performance/graph-optimizations)</sup> |
| 硬件覆盖 | 面向英伟达 CUDA 与 TensorRT、英特尔 OpenVINO 与 oneDNN、DirectML、AMD ROCm 与 MIGraphX、高通（Qualcomm）QNN、Android NNAPI、苹果（Apple）CoreML、WebGPU、XNNPACK 的执行提供程序，以及由社区维护的提供程序，如 Arm NN、Apache TVM、瑞芯微（Rockchip）RKNPU 和华为（Huawei）CANN<sup>[6](https://onnxruntime.ai/docs/execution-providers/)</sup> |
| 发布节奏 | 每月发布主版本，其间视需要发布补丁版本<sup>[7](https://onnxruntime.ai/roadmap)</sup> |
| 最新版本 | v1.31.0，稳定了模型包与 EPContext 数据 API，并新增 Arm KleidiAI SVE2.1 FP16 GEMM 支持<sup>[8](https://github.com/microsoft/onnxruntime/releases)</sup> |

## 执行机制

会话启动时，ONNX Runtime 将 ONNX 模型图转换为内存中的图表示，应用一组与提供程序无关的优化，根据可用的执行提供程序将图划分为子图，并把每个子图分配给一个提供程序<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup>。只有在运行时通过 `GetCapability()` 确认某提供程序能够执行某个子图之后，该子图才会被分配给它<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup>。

**分区与回退。** 提供程序按特定顺序考虑，每个提供程序被分配其能处理的最大子图。默认的 CPU 执行提供程序最后考虑，确保完整性，因此专用硬件无法运行的算子仍然能够执行<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup>。从概念上讲，每个分区都归约为一个单一的融合算子，通过调用提供程序的 `Compile()` 方法创建，提供程序还会公开用于其输入张量的内存分配器<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup>。应用程序直接控制这一顺序：例如提供程序列表 `['CUDAExecutionProvider', 'CPUExecutionProvider']` 意味着节点在有能力时于 CUDA 上运行，否则在 CPU 上运行<sup>[6](https://onnxruntime.ai/docs/execution-providers/)</sup>。

**设计保证。** 所有内核都是无状态的，因此多个线程可以在同一个推理会话上调用 `Run()`；ONNX Runtime 保证每个算子都受默认执行提供程序支持<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup>。提供程序可以使用自己的内部张量表示，但必须在其子图边界处与标准表示相互转换<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup>。

## 图优化

图优化是模型到模型的变换，从小的简化和节点消除，到节点融合和布局优化，均属此类<sup>[5](https://onnxruntime.ai/docs/performance/graph-optimizations)</sup>。它们分为两大类：需要分析整张图的全局变换，以及可表达为简单代数重写规则的局部变换<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup>。

优化分为三个级别，按顺序应用：基础优化、扩展优化和布局优化<sup>[5](https://onnxruntime.ai/docs/performance/graph-optimizations)</sup>。它们可以在推理前在线运行，也可以离线运行——离线时运行时将优化后的模型序列化到磁盘。离线预优化必须使用与目标机器相同的选项和硬件；为 GPU 执行提供程序预优化的模型无法在仅有 CPU 的机器上运行<sup>[5](https://onnxruntime.ai/docs/performance/graph-optimizations)</sup>。

## 执行提供程序与硬件

执行提供程序框架是 ONNX Runtime 触达各类硬件的途径。每个提供程序封装一个硬件加速库并声明其能执行什么；添加新的提供程序不需要修改 ONNX Runtime 核心<sup>[3](https://onnxruntime.ai/docs/reference/high-level-design.html)</sup><sup> • </sup><sup>[9](https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/framework/execution_provider.h)</sup>。文档中列出的目录涵盖 CPU 提供程序（默认 CPU、英特尔 oneDNN、[OpenVINO](https://www.edgepedia.org/zh/openvino)、XNNPACK）、GPU 提供程序（英伟达 CUDA、TensorRT、DirectML、AMD MIGraphX、ROCm、OpenVINO、WebGPU），以及移动、物联网和其他提供程序（英特尔 OpenVINO、Arm Compute Library、Android NNAPI、Arm NN、CoreML、XNNPACK、瑞芯微 NPU、赛灵思（Xilinx）Vitis-AI、华为 CANN、Azure）<sup>[6](https://onnxruntime.ai/docs/execution-providers/)</sup>。

近期的版本将部分提供程序重构为独立版本化的插件库：CUDA Plugin EP 和 WebGPU Plugin EP 各自发布版本说明，且必须与核心版本中的贡献算子模式匹配，因为模式不匹配可能导致执行错误或崩溃<sup>[8](https://github.com/microsoft/onnxruntime/releases)</sup>。Arm Compute Library EP 已弃用，并计划在未来版本中移除<sup>[8](https://github.com/microsoft/onnxruntime/releases)</sup>。

## 平台与语言

ONNX Runtime 面向完整的部署范围。服务器和桌面端使用由 Python、C++、C#、C 和 Java API 覆盖；移动开发支持 Swift、Objective-C、Java、Kotlin、JavaScript、C 和 C++，面向 Android、iOS、[React Native](https://www.edgepedia.org/zh/react-native) 和 MAUI/Xamarin 应用<sup>[2](https://onnxruntime.ai/inference)</sup>。ONNX Runtime Web 将推理带入浏览器，支持 Windows、Android、macOS 和 iOS 上的 Chrome、Edge、[Safari](https://www.edgepedia.org/zh/safari-web-browser) 和 Firefox，以及 Node.js，并附有文档化示例，包括图像分类、目标检测和在浏览器中运行 OpenAI Whisper<sup>[10](https://onnxruntime.ai/docs/get-started/with-javascript/web.html)</sup>。

## 相对替代方案的性能

独立对比将 ONNX Runtime 与厂商专用引擎进行比较，呈现出一种一致的格局：它以部分峰值性能换取硬件广度。一项 2026 年针对 LLaMA-3 8B 推理的 GPU 基准测试发现，ONNX Runtime 的 CUDA 执行提供程序在标准 Transformer 架构上达到 TensorRT 吞吐量的 85% 至 95%，在 H200 上首令牌时间为 48 毫秒，而 TensorRT 为 42 毫秒；在生产批大小下，H200 上的吞吐量为每秒 2,940 个令牌，而 TensorRT 为每秒 3,480 个<sup>[11](https://clusterbid.com/blog/onnx-runtime-vs-tensorrt-vs-openvino-inference-benchmark)</sup>。一项 2026 年关于边缘设备工业机器视觉的研究，在 CPU 和 GPU 平台上比较了原生 PyTorch、ONNX Runtime、OpenVINO 和 TensorRT，发现 OpenVINO 在 CPU 上实现了最低推理时间，TensorRT 在 GPU 上最低<sup>[12](https://arxiv.org/abs/2607.11356)</sup>。

## 发布历史

微软创建 ONNX Runtime 是为了满足其内部推理需求，并于 2018 年 12 月将其开源；六个月后的 0.4 版本增加了英伟达 TensorRT 执行提供程序的正式可用性，以及英特尔 nGraph 提供程序的公开预览<sup>[13](https://opensource.microsoft.com/blog/2019/05/22/onnx-runtime-machine-learning-inferencing-0-4-release/)</sup>。2019 年 10 月发布 1.0 版本，来自英特尔、英伟达、京东（JD.com）、恩智浦（NXP）等的贡献带来了对英特尔 OpenVINO、DNNL、nGraph、英伟达 TensorRT、Android NN API 和 Arm Compute Library 的加速支持<sup>[4](https://opensource.microsoft.com/blog/2019/10/30/announcing-onnx-runtime-1-0/)</sup>。此后继续每月发布；v1.30.0 扩展了生成式 AI 推理，加入用于连续批处理的可变长度因果卷积、分页 XQA 中的投机解码和 INT4 分页 KV 缓存，并新增了 Go 绑定<sup>[8](https://github.com/microsoft/onnxruntime/releases)</sup>。

## References

1. [Book of News, Connect 2018, Microsoft](https://news.microsoft.com/wp-content/uploads/prod/sites/543/2018/12/Connect-2018-Book-of-News.pdf)
2. [ONNX Runtime Inference, onnxruntime.ai](https://onnxruntime.ai/inference)
3. [ONNX Runtime Architecture, onnxruntime.ai](https://onnxruntime.ai/docs/reference/high-level-design.html)
4. [Announcing ONNX Runtime 1.0, Microsoft Open Source Blog](https://opensource.microsoft.com/blog/2019/10/30/announcing-onnx-runtime-1-0/)
5. [Graph Optimizations in ONNX Runtime, onnxruntime.ai](https://onnxruntime.ai/docs/performance/graph-optimizations)
6. [ONNX Runtime Execution Providers, onnxruntime.ai](https://onnxruntime.ai/docs/execution-providers/)
7. [ONNX Runtime Release Roadmap, onnxruntime.ai](https://onnxruntime.ai/roadmap)
8. [Releases, microsoft/onnxruntime, GitHub](https://github.com/microsoft/onnxruntime/releases)
9. [execution_provider.h, microsoft/onnxruntime, GitHub](https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/framework/execution_provider.h)
10. [ONNX Runtime Web, onnxruntime.ai](https://onnxruntime.ai/docs/get-started/with-javascript/web.html)
11. [ONNX Runtime vs TensorRT vs OpenVINO: GPU Inference Engine Benchmark, ClusterBid](https://clusterbid.com/blog/onnx-runtime-vs-tensorrt-vs-openvino-inference-benchmark)
12. [Benchmarking Edge Inference Strategies for Deep Learning Models in Industrial Machine Vision, arXiv](https://arxiv.org/abs/2607.11356)
13. [ONNX Runtime: a one-stop shop for machine learning inferencing, Microsoft Open Source Blog](https://opensource.microsoft.com/blog/2019/05/22/onnx-runtime-machine-learning-inferencing-0-4-release/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Deep learning software and hardware › Edge and mobile deep learning

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/onnx-runtime

English original: https://www.edgepedia.org/onnx-runtime
