从源码构建需先安装 protobuf,且要 git clone --recursive 拉取子模块;命令行不指定 -p 时默认只跑 fuse_and_elimination_passes,而非全部 pass;pip 安装失败时需先 pip3 install -U pip 升级 pip
ONNX 模型的 C++ 优化库,提供可复用的预置优化 pass,供各后端共享图优化工作
对 ONNX 模型做图级优化的 Python 工具,通过算子融合、常量折叠等变换精简计算图,让推理更快、模型更小。
核心能力
- 提供 C++ 库对 ONNX 模型执行任意优化
- 内置一系列预置优化 pass 可直接调用
- 支持用 -p 指定 pass 列表,默认跑 fuse 与 elimination pass
- 支持 --fixed_point 反复迭代直到不再变化
- 提供命令行入口,无需写 Python 脚本
容易踩的坑
- v0.4.20.4.2最新正式版2026-01-07 · 2026-01-07 12:58 UTC
What's Changed
- Update build-and-test.yml by @andife in https://github.com/onnx/optimizer/pull/235
- Update VERSION_NUMBER by @andife in https://github.com/onnx/optimizer/pull/236
- Fix torch export tests by @justinchuby in https://github.com/onnx/optimizer/pull/237
- Remove redundant builds by @andife in https://github.com/onnx/optimizer/pull/238
- Update VERSION_NUMBER, use rc by @andife in https://github.com/onnx/optimizer/pull/241
- Check ABI3 after upload by @justinchuby in https://github.com/onnx/optimizer/pull/242
- Bump actions/checkout from 5.0.0 to 6.0.1 by @dependabot[bot] in https://github.com/onnx/optimizer/pull/247
- Bump fsfe/reuse-action from 5.0.0 to 6.0.0 by @dependabot[bot] in https://github.com/onnx/optimizer/pull/248
- Bump pypa/cibuildwheel from 3.2.1 to 3.3.0 by @dependabot[bot] in https://github.com/onnx/optimizer/pull/246
- Bug fix pyproject.toml by @andife in https://github.com/onnx/optimizer/pull/245
- Update VERSION_NUMBER to 0.4.2rc2 by @andife in https://github.com/onnx/optimizer/pull/249
- Update VERSION_NUMBER by @andife in https://github.com/onnx/optimizer/pull/250
- [opt:fuse_pad_into_conv] remove NOTSET auto_pad by @take-cheeze in https://github.com/onnx/optimizer/pull/251
Full Changelog: https://github.com/onnx/optimizer/compare/v0.4.1...v0.4.2
- v0.4.2rc2pre-release2026-01-01 · 2026-01-01 16:33 UTC
这个版本没有写更新说明。
- v0.4.2rc1pre-release2025-12-31 · 2025-12-31 07:36 UTC
这个版本没有写更新说明。
- v0.4.1_12025-12-30 · 2025-12-30 17:03 UTC
查看更新说明
What's Changed
- Update build-and-test.yml by @andife in https://github.com/onnx/optimizer/pull/235
Full Changelog: https://github.com/onnx/optimizer/compare/v0.4.1...v0.4.1_1
- v0.4.12025-12-30 · 2025-12-30 06:59 UTC
查看更新说明
What's Changed
- Add tag trigger for build and test workflow by @andife in https://github.com/onnx/optimizer/pull/231
- Update VERSION_NUMBER by @andife in https://github.com/onnx/optimizer/pull/233
- Remove unnecessary future imports from setup.py by @justinchuby in https://github.com/onnx/optimizer/pull/232
Full Changelog: https://github.com/onnx/optimizer/compare/v0.4.0...v0.4.1
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
ONNX Optimizer
Introduction
ONNX provides a C++ library for performing arbitrary optimizations on ONNX models, as well as a growing list of prepackaged optimization passes.
The primary motivation is to share work between the many ONNX backend implementations. Not all possible optimizations can be directly implemented on ONNX graphs - some will need additional backend-specific information - but many can, and our aim is to provide all such passes along with ONNX so that they can be re-used with a single function call.
You may be interested in invoking the provided passes, or in implementing new ones (or both).
Installation
You can install onnxoptimizer from PyPI:
pip3 install onnxoptimizer
Note that you may need to upgrade your pip first if you have trouble:
pip3 install -U pip
If you want to build from source:
git clone --recursive https://github.com/onnx/optimizer onnxoptimizer
cd onnxoptimizer
pip3 install -e .
Note that you need to install protobuf before building from source.
Command-line API
Now you can use command-line api in terminal instead of python script.
python -m onnxoptimizer input_model.onnx output_model.onnx
Arguments list is following:
# python3 -m onnxoptimizer -h
usage: python -m onnxoptimizer input_model.onnx output_model.onnx
onnxoptimizer command-line api
optional arguments:
-h, --help show this help message and exit
--print_all_passes print all available passes
--print_fuse_elimination_passes
print all fuse and elimination passes
-p [PASSES ...], --passes [PASSES ...]
list of optimization passes name, if no set, fuse_and_elimination_passes will be used
--fixed_point fixed point
Roadmap
- More built-in pass
- Separate graph rewriting and constant folding (or a pure graph rewriting mode, see issue #9 for the details)
Relevant tools
- onnx-simplifier: A handy and popular tool based on onnxoptimizer