ExternalTensorΒΆ

class onnx_ir.ExternalTensor(location: PathLike | str, offset: int | None, length: int | None, dtype: DataType, *, shape: Shape, name: str, doc_string: str | None = None, metadata_props: dict[str, str] | None = None, base_dir: PathLike | str = '')ΒΆ

An immutable concrete tensor with its data store on disk.

This class uses memory mapping to avoid loading the tensor into memory, when the data type is supported by numpy. Otherwise, the tensor is loaded into memory lazily when accessed.

Calling shape does not incur IO. Checking shape before loading the tensor is recommended if IO overhead and memory usage is a concern.

To obtain an array, call numpy(). To obtain the bytes, call tobytes().

The location must be a relative path conforming to the ONNX specification. Given the correct base_dir, the path is computed to be the full path to the data file. Users should expect that the path always leads to the correct file. At initialization, paths are not checked. It is the user’s responsibility to ensure the paths are valid and accessible.

locationΒΆ

The location of the data file. It is the path relative to the base directory.

base_dirΒΆ

The base directory for the external data. It is used to resolve relative paths. At serialization, only the location is serialized into the β€œlocation” field of the TensorProto.

pathΒΆ

The path to the data file. This is computed by joining base_dir and location.

offsetΒΆ

The offset in bytes from the start of the file.

lengthΒΆ

The length of the data in bytes.

dtypeΒΆ

The data type of the tensor.

shapeΒΆ

The shape of the tensor.

nameΒΆ

The name of the tensor. It must be specified.

doc_stringΒΆ

The documentation string.

metadata_propsΒΆ

The metadata properties.

rawΒΆ
property base_dir: str | PathLikeΒΆ
display(*, page: bool = False) NoneΒΆ

Pretty print the object.

Parameters:

page – Whether to page the output.

property doc_string: str | NoneΒΆ

The documentation string.

property dtype: DataTypeΒΆ
invalidate() None[source]ΒΆ

Invalidate the tensor.

The external tensor is invalidated when the data is known to be corrupted or deleted.

property length: int | NoneΒΆ
property location: str | PathLikeΒΆ
property meta: MetadataStoreΒΆ

The metadata store for intermediate analysis.

Write to the metadata_props if you would like the metadata to be serialized to the ONNX proto.

property metadata_props: dict[str, str]ΒΆ
property name: str | NoneΒΆ

The name of the tensor.

property nbytes: intΒΆ

The number of bytes in the tensor.

numpy() ndarray[source]ΒΆ

Return the tensor as a numpy array.

The data will be memory mapped into memory and will not taken up physical memory space.

property offset: int | NoneΒΆ
property path: strΒΆ
release() None[source]ΒΆ

Delete all references to the memory buffer and close the memory-mapped file.

property shape: ShapeΒΆ
property size: intΒΆ

The number of elements in the tensor.

tobytes() bytes[source]ΒΆ

Return the bytes of the tensor.

This will load the tensor into memory.

valid() bool[source]ΒΆ

Check if the tensor is valid.

The external tensor is valid if it has not been invalidated.