LazyTensor¶
- class onnx_ir.LazyTensor(func, dtype, shape, *, cache=False, name=None, doc_string=None, metadata_props=None)¶
一个延迟评估函数以获取实际张量的张量。
此类接受一个返回 ir.TensorProtocol 的函数、一个 dtype 和一个 shape 参数。当调用 tobytes() 或 numpy() 时,该函数将延迟评估以获取实际张量。
示例
>>> 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¶
返回实际张量的函数。
- dtype¶
张量的数据类型。
- shape¶
张量的形状。
- cache¶
是否缓存函数结果。如果为 False,每次访问张量内容时都会调用该函数。如果为 True,则只调用一次函数并将结果缓存到内存中。默认值为 False。
- name¶
张量的名称。
- doc_string¶
文档字符串。
- metadata_props¶
元数据属性。
- cache¶
- property meta: MetadataStore¶
用于中间分析的元数据存储。
如果您希望将元数据序列化为 ONNX proto,请写入
metadata_props。
- property raw: Callable[[], TensorProtocol]¶