onnx_ir.external_data

The onnx_ir.external_data module provides utilities for handling external data in ONNX models. It enables the conversion of tensors to and from external data files, allowing for efficient storage and manipulation of large tensor data. This is particularly useful for models with large initializers that exceed memory constraints.

Functions

onnx_ir.external_data.load_to_model(model: Model) Model[source]

Convert all external model initializers to memory tensors in-place.

All initializers in the main graph and subgraphs are handled.

Parameters:

model – Model to process.

onnx_ir.external_data.unload_from_model(model: Model, base_dir: str | PathLike, relative_path: str | PathLike, *, size_threshold_bytes: int = 0) Model[source]

Convert all initializers equal or above size_threshold_bytes to external tensors in-place and save data to a single data file.

It should only replace the initializers in the model with external tensors and not make any other modifications to the model.

If any existing external tensor references the provided external_data path, it will be invalidated after the external data is overwritten. To obtain a valid model, use load() to load the newly saved model, or provide a different external data path that is not currently referenced by any tensors in the model.

All initializers in the main graph and subgraphs are handled.

Parameters:
  • model – Model to process.

  • base_dir – Path the directory where the ONNX model file is.

  • relative_path – Path to which external data is to be stored, relative to the ONNX file. E.g. “model.data”

  • size_threshold_bytes – Save to external data if the tensor size in bytes is larger than this threshold.

Returns:

An ir.Model with all initializer data equal or above size_threshold_bytes converted to external tensors.

onnx_ir.external_data.convert_tensors_to_external(tensors: Sequence[TensorProtocol], base_dir: str | PathLike, relative_path: str | PathLike) list[ExternalTensor][source]

Convert a sequence of any TensorProtocol tensors to external tensors.

Existing external tensors are loaded to memory if they are referring to the same file path as the destination path.

Parameters:
  • tensors – Tensors to be converted to external tensors. They can be external tensors themselves.

  • base_dir – Path of base directory.

  • relative_path – Path to which external data is to be stored, relative to the ONNX file.

Returns:

A list of external tensors derived from a list of input tensors. The order should match the input tensor order.

onnx_ir.external_data.convert_tensors_from_external(tensors: Sequence[TensorProtocol]) list[TensorProtocol][source]

Convert a sequence of external tensors to in-memory tensors.

Parameters:

tensors – External tensors to be converted to in-memory tensors.

Returns:

A list of in-memory tensors derived from a list of external tensors.

onnx_ir.external_data.set_base_dir(graph: Graph | GraphView, base_dir: str | PathLike) None[source]

Set the base directory for external data in a graph.

Parameters:
  • graph – The graph to traverse tensors on.

  • base_dir – The base directory. This is the directory where the ONNX file is.