saveΒΆ
- onnx_ir.save(model, path, format=None, external_data=None, size_threshold_bytes=256, max_shard_size_bytes=None, callback=None)ΒΆ
Save an ONNX model to a file.
The model remains unchanged after the call. If any existing external tensor references the provided
external_datapath, it will be invalidated after the external data is overwritten. To obtain a valid model, useload()to load the newly saved model, or provide a different external data path that is not currently referenced by any tensors in the model.Tip
A simple progress bar can be implemented by passing a callback function as the following:
import onnx_ir as ir import tqdm with tqdm.tqdm() as pbar: total_set = False def callback(tensor: ir.TensorProtocol, metadata: ir.external_data.CallbackInfo) -> None: nonlocal total_set if not total_set: pbar.total = metadata.total total_set = True pbar.update() pbar.set_description(f"Saving {tensor.name} ({tensor.dtype}, {tensor.shape}) at offset {metadata.offset}") ir.save( ..., callback=callback, )
- Parameters:
model (Model) β The model to save.
path (str | PathLike) β The path to save the model to. E.g. βmodel.onnxβ.
format (str | None) β The format of the file (e.g.
protobuf,textproto,json, etc.). If None, the format is inferred from the file extension.external_data (str | PathLike | None) β The relative path to save external data to. When specified, all initializers in the model will be converted to external data and saved to the specified directory. If None, all tensors will be saved unmodified. That is, if a tensor in the model is already external, it will be saved with the same external information; if the tensor is not external, it will be serialized in the ONNX Proto message.
size_threshold_bytes (int) β Save to external data if the tensor size in bytes is larger than this threshold. Effective only when
external_datais set.max_shard_size_bytes (int | None) β Maximum cumulative size in bytes for a single external data shard file. When
None(the default) all external tensors are written to the single file given byexternal_data. When set, tensors are distributed across numbered shard files (e.g.model-00001-of-00003.data). Because the ONNX format storeslocation,offset, andlengthper tensor, no separate index file is created β the saved ONNX proto itself encodes which shard each tensor lives in. If a single tensor is larger than this value, it is written in its own oversized shard file. Effective only whenexternal_datais set.callback (Callable[[TensorProtocol, CallbackInfo], None] | None) β A callback function that is called for each tensor that is saved to external data for debugging or logging purposes.
- Raises:
ValueError β If the external data path is an absolute path.
ValueError β If
max_shard_size_bytesis not greater than 0.ValueError β If
max_shard_size_bytesis set withoutexternal_data.FileExistsError β When
max_shard_size_bytesis set and any destination shard file already exists on disk. The sharded write path never overwrites existing files; delete the conflicting files or choose a different external data path to re-save. The single-file path (max_shard_size_bytes is None) instead overwritesexternal_dataunconditionally and never raises here.
- Return type:
None