ort#

OrtTensor#

class onnx_array_api.ort.ort_tensors.EagerOrtTensor(tensor: OrtValue | OrtTensor)[source]#

Defines a value for a specific backend.

class onnx_array_api.ort.ort_tensors.JitOrtTensor(tensor: OrtValue | OrtTensor)[source]#

Defines a value for a specific backend.

class onnx_array_api.ort.ort_tensors.OrtTensor(tensor: OrtValue | OrtTensor)[source]#

Default backend based on onnxruntime.InferenceSession. Data is not copied.

Parameters:
  • input_names – input names

  • onx – onnx model

class Evaluator(tensor_class: type, input_names: List[str], onx: ModelProto)[source]#

Wraps class onnxruntime.InferenceSession to have a signature closer to python function.

run(*inputs: List[OrtTensor]) List[OrtTensor][source]#

Executes the function.

Parameters:

inputs – function inputs

Returns:

outputs

classmethod create_function(input_names: List[str], onx: ModelProto) Callable[source]#

Creates a python function calling the onnx backend used by this class.

Parameters:

onx – onnx model

Returns:

python function

property dims#

Returns the dimensions of the tensor. First dimension is the batch dimension if the tensor has more than one dimension.

property dtype: Any#

Returns the element type of this tensor.

static from_array(value: ndarray, device: OrtDevice | None = None) OrtTensor[source]#

Creates an instance of OrtTensor from a numpy array. Relies on ortvalue_from_numpy. A copy of the data in the Numpy object is held by the C_OrtValue only if the device is not cpu. Any expression such as from_array(x.copy()), or from_array(x.astype(np.float32)), … creates an intermediate variable scheduled to be deleted by the garbage collector as soon as the function returns. In that case, the buffer holding the values is deleted and the instance OrtTenor is no longer equal to the original value: assert_allclose(value, tensor.numpy()) is false. value must remain alive as long as the OrtTensor is.

Parameters:
  • value – value

  • device – CPU, GPU, value such as OrtTensor.CPU, OrtTensor.CUDA0

Returns:

instance of OrtTensor

property key: Any#

Unique key for a tensor of the same type.

numpy() ndarray[source]#

Converts the OrtValue into numpy array.

property shape: Tuple[int, ...]#

Returns the shape of the tensor.

property tensor_type: TensorType#

Returns the tensor type of this tensor.

property tensor_type_dims: TensorType#

Returns the tensor type of this tensor. This property is used to define a key used to cache a jitted function. Same keys keys means same ONNX graph. Different keys usually means same ONNX graph but different input shapes.

property value: OrtValue#

Returns the value of this tensor as a numpy array.