SparseTensor

Python Wrapper for C Classes

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

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.

data_type()[source]

Returns a string data type of the data in the OrtValue

dense_shape()[source]

Returns a numpy array(int64) containing a dense shape of a sparse tensor

device_name()[source]

Returns the name of the device where the SparseTensor data buffers reside e.g. cpu, cuda

format()[source]

Returns a OrtSparseFormat enumeration

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

values()[source]

The method returns a numpy array that is backed by the native memory if the data type is numeric. Otherwise, the returned numpy array that contains copies of the strings.

C classes

C++ class SparseTensor

class onnxruntime.capi._pybind_state.SparseTensor
static blocksparse_from_numpy(arg0: List[int], arg1: numpy.ndarray, arg2: numpy.ndarray[numpy.int32], arg3: onnxruntime.capi.onnxruntime_pybind11_state.OrtDevice) onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor
data_type(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor) str
dense_shape(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor) list
device_name(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor) str
property format
get_blocksparse_data(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor) onnxruntime.capi.onnxruntime_pybind11_state.SparseBlockSparseView
get_coo_data(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor) onnxruntime.capi.onnxruntime_pybind11_state.SparseCooView
get_csrc_data(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor) onnxruntime.capi.onnxruntime_pybind11_state.SparseCsrView
static sparse_coo_from_numpy(arg0: List[int], arg1: numpy.ndarray, arg2: numpy.ndarray[numpy.int64], arg3: onnxruntime.capi.onnxruntime_pybind11_state.OrtDevice) onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor
static sparse_csr_from_numpy(arg0: List[int], arg1: numpy.ndarray, arg2: numpy.ndarray[numpy.int64], arg3: numpy.ndarray[numpy.int64], arg4: onnxruntime.capi.onnxruntime_pybind11_state.OrtDevice) onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor
to_cuda(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor, arg0: onnxruntime.capi.onnxruntime_pybind11_state.OrtDevice) None
values(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor) numpy.ndarray

OrtSparseFormat

class onnxruntime.capi._pybind_state.OrtSparseFormat

Members:

ORT_SPARSE_UNDEFINED

ORT_SPARSE_COO

ORT_SPARSE_CSRC

ORT_SPARSE_BLOCK_SPARSE

property name

SparseCooView

class onnxruntime.capi._pybind_state.SparseCooView
indices(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseCooView) numpy.ndarray

SparseBlockSparseView

class onnxruntime.capi._pybind_state.SparseBlockSparseView
indices(self: onnxruntime.capi.onnxruntime_pybind11_state.SparseBlockSparseView) numpy.ndarray