Introduction

onnxruntime のビルドオプションに --minimal_build が存在する。
これ単独で使うことはまずなく、特定の演算子 (OP) 以外を除外する --include_ops_by_config と組み合わせることで生成されるバイナリのサイズを小さくできる。
モバイル向けのビルドでは特に効果が大きい。
制限もあり、一部の EP がビルドできない (CoreML 等)、従来の onnx ファイルは使えなくなり、 ORT フォーマットに変換が必要になる等がある。
(ORT フォーマットに変換しても、既存の読み込みコードや推論コード自体は一切変更不要)

では、このオプションを有効にすると、どの程度バイナリに違いが生じるか?

Try!!

実験環境は下記

  • OS
    • Windows 10 22H2
    • Ubuntu 22.04
    • macos 15.7
  • Onnxruntime
    • 1.24.3 (ただし、linux のみ 1.23.2)
  • モデルファイル
    • MNIST

余談だが、少し古い onnxruntime では --minimal_build を付与するとビルドに失敗するという問題 [Build] onnx_minimal.cmake removed from v1.22.1 and v1.22.1 causing minimal build errors #25796 があったので注意。
(会社で時間を無駄にした)

必要な演算子の特定

推論対象の onnx ファイルを含むディレクトリを用意し、下記のスクリプトを実行する。
(実行には onnxruntime の python パッケージを事前にインストール)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ python3 onnxruntime/tools/python/convert_onnx_models_to_ort.py testdata --output_dir output

Converting models with optimization style 'Fixed' and level 'all'
Converting optimized ONNX model C:\Work\testdata\mnist.onnx to ORT format model C:\Work\output\mnist.ort
Converted 1/1 models successfully.
Generating config file from ORT format models with optimization style 'Fixed' and level 'all'
2026-03-20 09:56:17,595 ort_format_model.utils [INFO] - Created config in C:\Work\output\required_operators.config
Converting models with optimization style 'Runtime' and level 'all'
Converting optimized ONNX model C:\Work\testdata\mnist.onnx to ORT format model C:\Work\output\mnist.with_runtime_opt.ort
Converted 1/1 models successfully.
Converting models again without runtime optimizations to generate a complete config file. These converted models are temporary and will be deleted.
Converting optimized ONNX model C:\Work\testdata\mnist.onnx to ORT format model C:\Work\testdata\tmp6nt8njfj.without_runtime_opt\mnist.ort
Converted 1/1 models successfully.
Generating config file from ORT format models with optimization style 'Runtime' and level 'all'
2026-03-20 09:56:17,625 ort_format_model.utils [INFO] - Created config in C:\Work\output\required_operators.with_runtime_opt.config

出力先のディレクトリに下記が生成される。

  • xxxx.ort
  • xxxx.with_runtime_opt.ort
  • required_operators.config
  • required_operators.with_runtime_opt.config

複数の onnx ファイルがあっても、全ての onnx ファイルに必要な演算子一覧が抽出される。

required_operators.config の中身は下記のようになっており、必要な演算子が列挙されていることがわかる。

1
2
3
4
5
6
# Generated from model/s:
# - C:\Work\output\mnist.ort
ai.onnx;5;Reshape
ai.onnx;7;Gemm
ai.onnx;8;MaxPool
com.microsoft;1;FusedConv

with_runtime_opt の有無の違いは

  • with_runtime_opt あり
    • 実行時に追加の最適化が必要になる演算子を含めた設定。 CoreML や NNAPI など向け
  • with_runtime_opt なし
    • 最適化済みの演算子の一覧。

最適化に関しては Convert ONNX models to ORT format script usage で説明があるのでそちらを参照。

バイナリの比較

ビルドのコマンドは下記 (Windows/Liux/Mac でそこまで差はない)。

full build
1
2
3
4
5
6
7
8
9
10
11
$ python tools/ci_build/build.py --config Release `
--cmake_generator "Visual Studio 17 2022" `
--enable_msvc_static_runtime `
--parallel `
--build_dir build `
--skip_tests `
--skip_onnx_tests `
--use_full_protobuf `
--cmake_extra_defines CMAKE_INSTALL_PREFIX=install `
CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded" `
onnxruntime_BUILD_UNIT_TESTS=ON
minimal build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ python tools/ci_build/build.py --config Release `
--cmake_generator "Visual Studio 17 2022" `
--enable_msvc_static_runtime `
--parallel `
--build_dir build `
--skip_tests `
--skip_onnx_tests `
--use_full_protobuf `
--minimal_build extended `
--disable_ml_ops `
--include_ops_by_config required_operators.config `
--cmake_extra_defines CMAKE_INSTALL_PREFIX=install `
CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded" `
onnxruntime_BUILD_UNIT_TESTS=ON

--disable_ml_ops は古典的な機械学習の演算子を無効化するオプション。

ここで生成されるバイナリ (CMAKE_INSTALL_PREFIX で指定した出力先の全ファイル) のサイズは下記のようになった。

os full build minimal build reduce ratio
windows 954MB 585 MB 38.6 %
linux 145 MB 82 MB 43.4 %
macos 106 MB 52 MB 50.9 %

最終的なバイナリの比較

mnist による単純なダミーデータの推論を行う C/C++ コードに対して、full build と minimal build の onnxruntime を結合した際の最終バイナリのサイズを比較してみた。

os full build minimal build reduce ratio memo
windows 16.1 MB 4.39 MB 72 % full build は onnxruntime_providers_shared.dll (103 KB) も必要
linux 37 MB 26 MB 29 %
macos 25 MB 15 MB 40 %

OS 毎に削減率に違いが出たが、モバイル向けを考えると、例え 10 MB の削減でも大きな差になる。
特にストア配信において、バイナリサイズの削減はインストール時のユーザ体験に直結するし、実行時のフットプリントの低減は決して無視できない。