LazyTensor¶

class onnx_ir.LazyTensor(func, dtype, shape, *, cache=False, name=None, doc_string=None, metadata_props=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]]
Parameters:
  • func (Callable[[], _protocols.TensorProtocol])

  • dtype (_enums.DataType)

  • shape (Shape)

  • cache (bool)

  • name (str | None)

  • doc_string (str | None)

  • metadata_props (dict[str, str] | None)

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.

display(*, page=False)¶

Pretty print the object.

Parameters:

page (bool) – Whether to page the output.

Return type:

None

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]¶

The metadata properties of the tensor.

The metadata properties are used to store additional information about the tensor. Unlike meta, this property is serialized to the ONNX proto.

property name: str | None¶

The name of the tensor.

property nbytes: int¶

The number of bytes in the tensor.

numpy()[source]¶

Return the tensor as a numpy array.

Return type:

ndarray

property raw: Callable[[], TensorProtocol]¶

The thunk that materializes the backing tensor.

property shape: Shape¶

The shape of the tensor. Immutable.

property size: int¶

The number of elements in the tensor.

tobytes()[source]¶

Return the bytes of the tensor.

Return type:

bytes

tofile(file)[source]¶

Write tensor bytes to a binary file-like object.

Return type:

None