Implementation:Onnx Onnx Save Model
| Knowledge Sources | |
|---|---|
| Domains | Serialization, Model_Persistence |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for serializing ONNX models to disk provided by the ONNX top-level package.
Description
The save_model function serializes a ModelProto to a file, supporting multiple formats and optional external data handling. The serialization format is auto-detected from the file extension or can be specified explicitly. When save_as_external_data is True, the function first converts large tensors to external data references (via convert_model_to_external_data) and then writes the tensor data to separate files (via write_external_data_tensors) before saving the model protobuf.
Usage
Import this function whenever you need to persist an ONNX model to disk. It is the standard way to save models after creation, validation, conversion, or composition. Use the external data parameters when working with models whose tensor data exceeds 2GB.
Code Reference
Source Location
- Repository: onnx
- File: onnx/__init__.py
- Lines: 295-345
Signature
def save_model(
proto: ModelProto | bytes,
f: IO[bytes] | str | os.PathLike,
format: str | None = None,
*,
save_as_external_data: bool = False,
all_tensors_to_one_file: bool = True,
location: str | None = None,
size_threshold: int = 1024,
convert_attribute: bool = False,
) -> None:
"""Saves the ModelProto to the specified path.
Args:
proto: In-memory ModelProto or serialized bytes.
f: File-like object or file path string.
format: Serialization format (auto-detected from extension if None).
save_as_external_data: If True, save large tensors to external files.
all_tensors_to_one_file: Save all tensors to one external file.
location: External file name (relative to model path).
size_threshold: Minimum tensor size in bytes to externalize (default: 1024).
convert_attribute: Also convert attribute tensors to external data.
"""
Import
import onnx
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| proto | ModelProto or bytes | Yes | Model to save |
| f | IO[bytes] or str or PathLike | Yes | Output file path or file-like object |
| format | str or None | No | Serialization format (auto-detected from extension) |
| save_as_external_data | bool | No | Save large tensors externally (default: False) |
| all_tensors_to_one_file | bool | No | Single external file for all tensors (default: True) |
| location | str or None | No | External data file name (relative path) |
| size_threshold | int | No | Minimum tensor size to externalize in bytes (default: 1024) |
| convert_attribute | bool | No | Also externalize attribute tensors (default: False) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | None | Model file written to disk (and optional external data files) |
Usage Examples
Basic Save
import onnx
# Save as binary protobuf (default)
onnx.save_model(model, "model.onnx")
# Save as JSON
onnx.save_model(model, "model.json", format="json")
Save with External Data
import onnx
# Save large model with external data
onnx.save_model(
model,
"large_model.onnx",
save_as_external_data=True,
all_tensors_to_one_file=True,
location="weights.bin",
size_threshold=1024,
)