Tensor¶

class onnx_ir.Tensor(value: TArrayCompatible, dtype: DataType | None = None, *, shape: Shape | None = None, name: str | None = None, doc_string: str | None = None, metadata_props: dict[str, str] | None = None)¶

An immutable concrete tensor.

This class is a wrapper around the raw tensor data. The raw tensor data can be a numpy array compatible object (e.g. np.ndarray, torch.Tensor) or a DLPack compatible object. The tensor is immutable and the data is not copied at initialization.

To create a tensor from a numpy array:

>>> import numpy as np
>>> array = np.array([1, 2, 3])
>>> tensor = Tensor(array)
>>> # The tensor itself can be treated as a numpy array because it implements the __array__ method
>>> np.allclose(tensor, array)
True

To get a numpy array from the tensor, call numpy(). To convert the tensor to a byte string for serialization, call tobytes().

It is recommended to check the size of the tensor first before accessing the underlying data, because accessing the data may be expensive and incur IO overhead.

Subclass this class to efficiently handle different types of tensors from different frameworks.

name¶

The name of the tensor.

shape¶

The shape of the tensor.

dtype¶

The data type of the elements of the tensor. It is an ir.DataType enum.

doc_string¶

Documentation string.

raw¶

The raw data behind this tensor. It can be anything.

size¶

The number of elements in the tensor.

nbytes¶

The number of bytes in the tensor.

metadata_props¶

Metadata that will be serialized to the ONNX file.

meta¶

Metadata store for graph transform passes.

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¶

The data type of the tensor. Immutable.

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.

When the data type is not supported by numpy, the dtypes from the ml_dtype package are used. The values can be reinterpreted as bit representations using the .view() method.

property raw: TArrayCompatible¶

Backing data of the tensor. Immutable.

property shape: Shape¶

The shape of the tensor. Immutable.

property size: int¶

The number of elements in the tensor.

tobytes() bytes[source]¶

Returns the value as bytes encoded in little endian.

Override this method for more efficient serialization when the raw value is not a numpy array.