tensorΒΆ

onnx_ir.tensor(value: npt.ArrayLike | onnx.TensorProto | ir.DLPackCompatible | ir.ArrayCompatible, dtype: _enums.DataType | None = None, name: str | None = None, doc_string: str | None = None) _protocols.TensorProtocolΒΆ

Create a tensor value from an ArrayLike object or a TensorProto.

The dtype must match the value. Reinterpretation of the value is not supported, unless if the value is a plain Python object, in which case it is converted to a numpy array with the given dtype.

value can be a numpy array, a plain Python object, or a TensorProto.

Example:

>>> import onnx_ir as ir
>>> import numpy as np
>>> import ml_dtypes
>>> import onnx
>>> ir.tensor(np.array([1, 2, 3], dtype=np.int16))
Tensor<INT16,[3]>(array([1, 2, 3], dtype=int16), name=None)
>>> ir.tensor([1, 2, 3], dtype=ir.DataType.BFLOAT16)
Tensor<BFLOAT16,[3]>(array([1, 2, 3], dtype=bfloat16), name=None)
>>> tp_tensor = ir.tensor(onnx.helper.make_tensor("tensor", onnx.TensorProto.FLOAT, dims=[], vals=[0.5]))
>>> tp_tensor.numpy()
array(0.5, dtype=float32)
>>> import torch
>>> ir.tensor(torch.tensor([1.0, 2.0]), name="torch_tensor")
TorchTensor<FLOAT,[2]>(tensor([1., 2.]), name='torch_tensor')
Parameters:
  • value – The numpy array to create the tensor from.

  • dtype – The data type of the tensor.

  • name – The name of the tensor.

  • doc_string – The documentation string of the tensor.

Returns:

A tensor value.

Raises:

ValueError – If the dtype does not match the value when value is not a plain Python object like list[int].