LazyTensor¶

class onnx_ir.LazyTensor(func: Callable[[], TensorProtocol], dtype: DataType, shape: Shape, *, cache: bool = False, name: str | None = None, doc_string: str | None = None, metadata_props: dict[str, str] | None = None)¶

A tensor that lazily evaluates a function to get the actual tensor.

This class takes a function returning an ir.TensorProtocol, a dtype, and a shape argument. The function is lazily evaluated to get the actual tensor when tobytes() or numpy() is called.

Example:

>>> import numpy as np
>>> import onnx_ir as ir
>>> weights = np.array([[1, 2, 3]])
>>> def create_tensor():  # Delay applying transformations to the weights
...     weights_t = weights.transpose()
...     return ir.tensor(weights_t)
>>> lazy_tensor = ir.LazyTensor(create_tensor, dtype=ir.DataType.INT64, shape=ir.Shape([1, 3]))
>>> print(lazy_tensor.numpy())
[[1]
 [2]
 [3]]
func¶

The function that returns the actual tensor.

dtype¶

The data type of the tensor.

shape¶

The shape of the tensor.

cache¶

Whether to cache the result of the function. If False, the function is called every time the tensor content is accessed. If True, the function is called only once and the result is cached in memory. Default is False.

name¶

The name of the tensor.

doc_string¶

The documentation string.

metadata_props¶

The metadata properties.

cache¶
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.

property raw: Callable[[], TensorProtocol]¶
property shape: Shape¶

The shape of the tensor. Immutable.

property size: int¶

The number of elements in the tensor.

tobytes() bytes[source]¶

Return the bytes of the tensor.