How to load and save ONNX files#
This guide covers the Python interface. For the other language, see How to load and save ONNX files.
This page covers the onnx-light Python load/save API for one-file
.onnx models, two-file models with external tensor data, split external
data across multiple files, alignment options, and the thread-count options for
larger models.
Common load/save patterns#
Standalone tensor files (.pb)#
ONNX backend datasets store individual inputs and expected outputs as
TensorProto messages, commonly named input_0.pb and output_0.pb.
Use load_tensor and save_tensor for these files, not the model helpers
load and save. The .pb extension does not identify the message type.
import numpy as np
import onnx_light.onnx as onnxl
import onnx_light.onnx.numpy_helper as onh
tensor = onh.from_array(np.array([1, 2, 3], dtype=np.float32), name="X")
onnxl.save_tensor(tensor, "input_0.pb")
restored = onnxl.load_tensor("input_0.pb")
values = onh.to_array(restored)
Both helpers accept string paths, pathlib.Path objects, and binary file-like
objects, which they leave open. .pb defaults to binary protobuf;
.textproto selects text format, or use format="protobuf" or
format="textproto" explicitly. These helpers read and write standalone
tensor messages; they do not resolve or create external tensor-data files.
Load one file#
import onnx_light.onnx as onnxl
model = onnxl.load("model.onnx")
Load two files#
import onnx_light.onnx as onnxl
model = onnxl.load(
"model.onnx",
location="model.onnx.data",
load_external_data=True,
)
Save one file#
import onnx_light.onnx as onnxl
onnxl.save(model, "out.onnx")
Save two files#
import onnx_light.onnx as onnxl
onnxl.save(model, "out.onnx", location="out.onnx.data")
Load/save split external files#
import onnx_light.onnx as onnxl
onnxl.save(
model,
"out.onnx",
location="out.onnx.data",
max_external_file_size=2 * 1024 ** 3,
)
model = onnxl.load("out.onnx", load_external_data=True)
Parallel load#
import onnx_light.onnx as onnxl
model = onnxl.load(
"model.onnx",
num_threads=4,
)
Parallel save#
import onnx_light.onnx as onnxl
onnxl.save(
model,
"out.onnx",
location="out.onnx.data",
num_threads=4,
)
Load/save with aligned external data#
import onnx_light.onnx as onnxl
save_options = onnxl.SerializeOptions()
save_options.alignment = 4096
model.SerializeToFile("out.onnx", save_options, "out.onnx.data")
loaded = onnxl.ModelProto()
load_options = onnxl.ParseOptions()
load_options.alignment = 4096
loaded.ParseFromFile(
"out.onnx",
load_options,
external_data_file="out.onnx.data",
)
Save and load the text format (textproto)#
The Python helpers also support the protobuf text format (.textproto),
a human-readable, pure-Python (de)serialization that does not require the
protobuf runtime. The format is selected automatically from the
.textproto extension, or explicitly with format="textproto".
import onnx_light.onnx as onnxl
# Inferred from the .textproto extension.
onnxl.save(model, "model.textproto")
loaded = onnxl.load("model.textproto")
# Explicit format, any extension.
onnxl.save(model, "model.onnx", format="textproto")
loaded = onnxl.load("model.onnx", format="textproto")
The textproto format keeps the full model structure inline; saving with external tensor data is not supported for this format.
Notes#
Python switches from one-file to two-file save when
location=...is provided. C++ usesonnx_light::utils::FileWriteStreamfor one file andonnx_light::utils::TwoFilesWriteStreamfor two files.Python loads external tensor data with
load_external_data=True. When the weights file lives next to the.onnxfile and the stored location is still valid, the explicitlocation=...override can be omitted.Split external-data saves use
max_external_file_sizeto cap each weights file. The first file keeps the requested base name (for exampleout.onnx.data), then additional files append numbered suffixes such asout.onnx.data.1andout.onnx.data.2. During load, both Python and C++ follow the per-tensorexternal_data.locationentries stored in the model.Alignment lives on
onnx_light.onnx.SerializeOptions,onnx_light.onnx.ParseOptions,onnx_light::SerializeOptions, andonnx_light::ParseOptionsrather than the high-levelsave()/load()helper keywords. Use a power of two such as4096when you want page-aligned external-data offsets.The same parallel options apply to one-file and two-file I/O. In C++, set
num_threadsononnx_light::ParseOptionsoronnx_light::SerializeOptionsbefore calling the helper functions.
See also#
Load and save ONNX models with external data - Python walkthrough for external data round-trips.
Standalone C++ example: load an ONNX file with onnx_light - standalone C++ load example.