OrtValue

onnxruntime implements tensors with class OrtValue. It has the same properties as a numpy.array, shape and type but only represents a contiguous array. The current implementation is just a container and does not allow standard operators such as addition, substraction. onnxruntime has a C implementation wrapped into a Python class with the same.

Python Wrapper for OrtValue

Note

Method ortvalue_from_numpy does not copy data, it borrows the data pointer. The numpy array must remain alive while the instance of OrtValue is in use.

class onnxruntime.OrtValue(ortvalue, numpy_obj=None)[source]

A data structure that supports all ONNX data formats (tensors and non-tensors) that allows users to place the data backing these on a device, for example, on a CUDA supported device. This class provides APIs to construct and deal with OrtValues.

as_sparse_tensor()[source]

The function will return SparseTensor contained in this OrtValue

data_ptr()[source]

Returns the address of the first element in the OrtValue’s data buffer

data_type()[source]

Returns the data type of the data in the OrtValue

device_name()[source]

Returns the name of the device where the OrtValue’s data buffer resides e.g. cpu, cuda

has_value()[source]

Returns True if the OrtValue corresponding to an optional type contains data, else returns False

is_sparse_tensor()[source]

Returns True if the OrtValue contains a SparseTensor, else returns False

is_tensor()[source]

Returns True if the OrtValue contains a Tensor, else returns False

is_tensor_sequence()[source]

Returns True if the OrtValue contains a Tensor Sequence, else returns False

numpy()[source]

Returns a Numpy object from the OrtValue. Valid only for OrtValues holding Tensors. Throws for OrtValues holding non-Tensors. Use accessors to gain a reference to non-Tensor objects such as SparseTensor

static ort_value_from_sparse_tensor(sparse_tensor)[source]

The function will construct an OrtValue instance from a valid SparseTensor The new instance of OrtValue will assume the ownership of sparse_tensor

static ortvalue_from_numpy(numpy_obj, device_type='cpu', device_id=0)[source]

Factory method to construct an OrtValue (which holds a Tensor) from a given Numpy object A copy of the data in the Numpy object is held by the OrtValue only if the device is NOT cpu

Parameters
  • numpy_obj – The Numpy object to construct the OrtValue from

  • device_type – e.g. cpu, cuda, cpu by default

  • device_id – device id, e.g. 0

static ortvalue_from_shape_and_type(shape=None, element_type=None, device_type='cpu', device_id=0)[source]

Factory method to construct an OrtValue (which holds a Tensor) from given shape and element_type

Parameters
  • shape – List of integers indicating the shape of the OrtValue

  • element_type – The data type of the elements in the OrtValue (numpy type)

  • device_type – e.g. cpu, cuda, cpu by default

  • device_id – device id, e.g. 0

shape()[source]

Returns the shape of the data in the OrtValue

C Class OrtValue or C_OrtValue

class onnxruntime.capi._pybind_state.OrtValue
as_sparse_tensor(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) onnxruntime::python::PySparseTensor
data_ptr(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) int
data_type(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) str
device_name(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) str
static from_dlpack(arg0: object, arg1: bool) onnxruntime.capi.onnxruntime_pybind11_state.OrtValue
has_value(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) bool
is_sparse_tensor(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) bool
is_tensor(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) bool
is_tensor_sequence(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) bool
numpy(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) object
static ort_value_from_sparse_tensor(arg0: onnxruntime::python::PySparseTensor) onnxruntime.capi.onnxruntime_pybind11_state.OrtValue
static ortvalue_from_numpy(arg0: object, arg1: onnxruntime.capi.onnxruntime_pybind11_state.OrtDevice) onnxruntime.capi.onnxruntime_pybind11_state.OrtValue
static ortvalue_from_shape_and_type(arg0: List[int], arg1: object, arg2: onnxruntime.capi.onnxruntime_pybind11_state.OrtDevice) onnxruntime.capi.onnxruntime_pybind11_state.OrtValue
shape(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) list
to_dlpack(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) object

C Class OrtValueVector

class onnxruntime.capi._pybind_state.OrtValueVector
dlpack_at(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValueVector, arg0: int) object
push_back(*args, **kwargs)

Overloaded function.

  1. push_back(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValueVector, arg0: onnxruntime.capi.onnxruntime_pybind11_state.OrtValue) -> None

  2. push_back(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValueVector, arg0: object, arg1: bool) -> None

reserve(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValueVector, arg0: int) None
shrink_to_fit(self: onnxruntime.capi.onnxruntime_pybind11_state.OrtValueVector) None