Tensors
IOBinding
- class onnxruntime.IOBinding(session)[source]
This class provides API to bind input/output to a specified device, e.g. GPU.
- bind_cpu_input(name, arr_on_cpu)[source]
bind an input to array on CPU :param name: input name :param arr_on_cpu: input values as a python array on CPU
- bind_input(name, device_type, device_id, element_type, shape, buffer_ptr)[source]
- Parameters:
name – input name
device_type – e.g. cpu, cuda
device_id – device id, e.g. 0
element_type – input element type
shape – input shape
buffer_ptr – memory pointer to input data
- bind_ortvalue_input(name, ortvalue)[source]
- Parameters:
name – input name
ortvalue – OrtValue instance to bind
- bind_ortvalue_output(name, ortvalue)[source]
- Parameters:
name – output name
ortvalue – OrtValue instance to bind
- bind_output(name, device_type='cpu', device_id=0, element_type=None, shape=None, buffer_ptr=None)[source]
- Parameters:
name – output name
device_type – e.g. cpu, cuda, cpu by default
device_id – device id, e.g. 0
element_type – output element type
shape – output shape
buffer_ptr – memory pointer to output data
OrtDevice
OrtValue
- 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.
- device_name()[source]
Returns the name of the device where the OrtValue’s data buffer resides e.g. cpu, cuda
- element_type()[source]
Returns the proto type of the data in the OrtValue if the OrtValue is a tensor.
- 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_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
- update_inplace(np_arr)[source]
Update the OrtValue in place with a new Numpy array. The numpy contents are copied over to the device memory backing the OrtValue. It can be used to update the input valuess for an InferenceSession with CUDA graph enabled or other scenarios where the OrtValue needs to be updated while the memory address can not be changed.
SparseTensor
- class onnxruntime.SparseTensor(sparse_tensor)[source]
A data structure that project the C++ SparseTensor object The class provides API to work with the object. Depending on the format, the class will hold more than one buffer depending on the format
Internal constructor
- as_blocksparse_view()[source]
The method will return coo representation of the sparse tensor which will enable querying BlockSparse indices. If the instance did not contain BlockSparse format, it would throw. You can query coo indices as:
block_sparse_indices = sparse_tensor.as_blocksparse_view().indices()
which will return a numpy array that is backed by the native memory
- as_coo_view()[source]
The method will return coo representation of the sparse tensor which will enable querying COO indices. If the instance did not contain COO format, it would throw. You can query coo indices as:
coo_indices = sparse_tensor.as_coo_view().indices()
which will return a numpy array that is backed by the native memory.
- as_csrc_view()[source]
The method will return CSR(C) representation of the sparse tensor which will enable querying CRS(C) indices. If the instance dit not contain CSR(C) format, it would throw. You can query indices as:
inner_ndices = sparse_tensor.as_csrc_view().inner() outer_ndices = sparse_tensor.as_csrc_view().outer()
returning numpy arrays backed by the native memory.
- device_name()[source]
Returns the name of the device where the SparseTensor data buffers reside e.g. cpu, cuda
- static sparse_coo_from_numpy(dense_shape, values, coo_indices, ort_device)[source]
Factory method to construct a SparseTensor in COO format from given arguments
- Parameters:
dense_shape – 1-D numpy array(int64) or a python list that contains a dense_shape of the sparse tensor must be on cpu memory
values – a homogeneous, contiguous 1-D numpy array that contains non-zero elements of the tensor of a type.
coo_indices – contiguous numpy array(int64) that contains COO indices for the tensor. coo_indices may have a 1-D shape when it contains a linear index of non-zero values and its length must be equal to that of the values. It can also be of 2-D shape, in which has it contains pairs of coordinates for each of the nnz values and its length must be exactly twice of the values length.
ort_device –
describes the backing memory owned by the supplied nummpy arrays. Only CPU memory is
suppored for non-numeric data types.
For primitive types, the method will map values and coo_indices arrays into native memory and will use them as backing storage. It will increment the reference count for numpy arrays and it will decrement it on GC. The buffers may reside in any storage either CPU or GPU. For strings and objects, it will create a copy of the arrays in CPU memory as ORT does not support those on other devices and their memory can not be mapped.
- static sparse_csr_from_numpy(dense_shape, values, inner_indices, outer_indices, ort_device)[source]
Factory method to construct a SparseTensor in CSR format from given arguments
- Parameters:
dense_shape – 1-D numpy array(int64) or a python list that contains a dense_shape of the sparse tensor (rows, cols) must be on cpu memory
values – a contiguous, homogeneous 1-D numpy array that contains non-zero elements of the tensor of a type.
inner_indices – contiguous 1-D numpy array(int64) that contains CSR inner indices for the tensor. Its length must be equal to that of the values.
outer_indices – contiguous 1-D numpy array(int64) that contains CSR outer indices for the tensor. Its length must be equal to the number of rows + 1.
ort_device –
describes the backing memory owned by the supplied nummpy arrays. Only CPU memory is
suppored for non-numeric data types.
For primitive types, the method will map values and indices arrays into native memory and will use them as backing storage. It will increment the reference count and it will decrement then count when it is GCed. The buffers may reside in any storage either CPU or GPU. For strings and objects, it will create a copy of the arrays in CPU memory as ORT does not support those on other devices and their memory can not be mapped.
- to_cuda(ort_device)[source]
Returns a copy of this instance on the specified cuda device
- Parameters:
ort_device – with name ‘cuda’ and valid gpu device id
The method will throw if:
this instance contains strings
this instance is already on GPU. Cross GPU copy is not supported
CUDA is not present in this build
if the specified device is not valid