onnx.external_data_helper

convert_model_from_external_data

onnx.external_data_helper.convert_model_from_external_data(model: ModelProto) None[source]

Call to set all tensors which use external data as embedded data. save_model saves all the tensors data as embedded data after calling this function.

Parameters:

model (ModelProto) – Model to be converted.

convert_model_to_external_data

onnx.external_data_helper.convert_model_to_external_data(model: ModelProto, all_tensors_to_one_file: bool = True, location: str | None = None, size_threshold: int = 1024, convert_attribute: bool = False) None[source]

Call to set all tensors with raw data as external data. This call should precede ‘save_model’. ‘save_model’ saves all the tensors data as external data after calling this function.

Parameters:
  • model (ModelProto) – Model to be converted.

  • all_tensors_to_one_file (bool) – If true, save all tensors to one external file specified by location. If false, save each tensor to a file named with the tensor name.

  • location – specify the external file relative to the model that all tensors to save to. Path is relative to the model path. If not specified, will use the model name.

  • size_threshold – Threshold for size of data. Only when tensor’s data is >= the size_threshold it will be converted to external data. To convert every tensor with raw data to external data set size_threshold=0.

  • convert_attribute (bool) – If true, convert all tensors to external data If false, convert only non-attribute tensors to external data

Raises:

ExternalDataInfo

class onnx.external_data_helper.ExternalDataInfo(tensor: TensorProto)[source]

load_external_data_for_model

onnx.external_data_helper.load_external_data_for_model(model: ModelProto, base_dir: str) None[source]

Loads external tensors into model

Parameters:
  • model – ModelProto to load external data to

  • base_dir – directory that contains external data

load_external_data_for_tensor

onnx.external_data_helper.load_external_data_for_tensor(tensor: TensorProto, base_dir: str) None[source]

Loads data from an external file for tensor. Ideally TensorProto should not hold any raw data but if it does it will be ignored.

Parameters:
  • tensor – a TensorProto object.

  • base_dir – directory that contains the external data.

remove_external_data_field

onnx.external_data_helper.remove_external_data_field(tensor: TensorProto, field_key: str) None[source]

Removes a field from a Tensor’s external_data key-value store.

Modifies tensor object in place.

Parameters:
  • tensor (TensorProto) – Tensor object from which value will be removed

  • field_key (string) – The key of the field to be removed

save_external_data

onnx.external_data_helper.save_external_data(tensor: TensorProto, base_path: str) None[source]

Writes tensor data to an external file according to information in the external_data field.

Parameters:
  • tensor (TensorProto) – Tensor object to be serialized

  • base_path – System path of a folder where tensor data is to be stored

set_external_data

onnx.external_data_helper.set_external_data(tensor: TensorProto, location: str, offset: int | None = None, length: int | None = None, checksum: str | None = None, basepath: str | None = None) None[source]

uses_external_data

onnx.external_data_helper.uses_external_data(tensor: TensorProto) bool[source]

Returns true if the tensor stores data in an external location.

write_external_data_tensors

onnx.external_data_helper.write_external_data_tensors(model: ModelProto, filepath: str) ModelProto[source]

Serializes data for all the tensors which have data location set to TensorProto.External.

Note: This function also strips basepath information from all tensors’ external_data fields.

Parameters:
  • model (ModelProto) – Model object which is the source of tensors to serialize.

  • filepath – System path to the directory which should be treated as base path for external data.

Returns:

The modified model object.

Return type:

ModelProto