Standalone C++ example: build, save and load an ONNX model with only lib_onnx_proto#
This page documents examples/build_save_load_onnx_proto
(view on GitHub),
a self-contained
CMake project that builds a tiny ONNX onnx::ModelProto entirely
in C++, serializes it to disk, parses it back, and checks the round-trip.
The example purposely links against onnx_light::lib_onnx_proto only:
no operator schemas, no shape inference, no checker, and no backend kernels
are needed. It is the minimum surface required to produce and consume an
.onnx file from C++.
Step 1 – Install the C++ library#
From the onnx-light repository root, build and install the static library and its public headers. The Python extension is not required:
cmake -S . -B build-install \
-DCMAKE_BUILD_TYPE=Release \
-DONNX_LIGHT_BUILD_PYTHON=OFF \
-DCMAKE_INSTALL_PREFIX=/usr/local
cmake --build build-install
cmake --install build-install
Step 2 – Build the example#
Point CMAKE_PREFIX_PATH at the install prefix chosen above:
cmake -S examples/build_save_load_onnx_proto -B build-build-save-load \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=/usr/local
cmake --build build-build-save-load
Step 3 – Run the example#
./build-build-save-load/build_save_load_onnx_proto /tmp/example_add.onnx
Example output:
Saved: /tmp/example_add.onnx
IR version : 7
Producer name : build_save_load_onnx_proto
Graph name : add_graph
Nodes : 1
Inputs : 1
Outputs : 1
Initializers : 1
Loaded: /tmp/example_add.onnx
...
Initializer B : [1, 2, 3]
Round-trip OK
The generated file is a valid ONNX model and can be opened with the
reference onnx Python package
(onnx.load(...) followed by onnx.checker.check_model(...)).
CMakeLists.txt#
The example CMake project uses find_package to locate the installed
library and links against the exported onnx_light::lib_onnx_proto target
only:
cmake_minimum_required(VERSION 3.15)
project(build_save_load_onnx_proto LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(onnx_light REQUIRED)
add_executable(build_save_load_onnx_proto main.cc)
target_link_libraries(build_save_load_onnx_proto PRIVATE onnx_light::lib_onnx_proto)
Key API types#
onnx::ModelProto,onnx::GraphProto,onnx::NodeProto,onnx::TensorProto,onnx::ValueInfoProtoPlain proto-message classes used to assemble the model in memory.
onnx::utils::FileWriteStreamBuffered binary output stream used to write the serialized model.
onnx::utils::FileStreamBuffered binary input stream used to read the model back from disk.
onnx::SerializeModelProtoToStream()Writes a
onnx::ModelPrototo a binary stream using the configuredonnx::SerializeOptions.onnx::ParseModelProtoFromStream()Parses a binary protobuf stream into a
onnx::ModelProto, optionally honouringonnx::ParseOptions(threading, no-copy, etc.).
See also#
Standalone C++ example: load an ONNX file with onnx_light – companion example that loads a pre-existing
.onnxfile and reports timing statistics.stream.h – full reference for
FileStream,FileWriteStream,StringStream, and other stream classes.onnx_helper.h –
SerializeModelProtoToStream,ParseModelProtoFromStream, and related helpers.