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:
- 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 meta: MetadataStore¶
The metadata store for intermediate analysis.
Write to the
metadata_propsif 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 raw: Callable[[], TensorProtocol]¶
The thunk that materializes the backing tensor.