# OnnxSparseTensor (onnxruntime 1.15.0 API)

Package ai.onnxruntime

## Class OnnxSparseTensor

- java.lang.Object
- - ai.onnxruntime.OnnxTensorLike
  - - ai.onnxruntime.OnnxSparseTensor

All Implemented Interfaces:
  `OnnxValue`, `java.lang.AutoCloseable`

    public final class OnnxSparseTensor
    extends OnnxTensorLike

A Java object wrapping an OnnxSparseTensor.

Sparse tensors support a variety of formats, and the `getValue()` method returns a different static inner class representing each type.

### Nested Class Summary

Nested Classes
`static class` `OnnxSparseTensor.BlockSparseTensor` The Java side representation of a block sparse tensor.
`static class` `OnnxSparseTensor.COOTensor` The Java side representation of a COO sparse tensor.
`static class` `OnnxSparseTensor.CSRCTensor` The Java side representation of a CSRC sparse tensor.
`static class` `OnnxSparseTensor.SparseTensor<T extends java.nio.Buffer>` Abstract base class for Java sparse tensors
`static class` `OnnxSparseTensor.SparseTensorType` The type of the sparse tensor.

### Nested classes/interfaces inherited from interface ai.onnxruntime.OnnxValue

`OnnxValue.OnnxValueType`

### Field Summary

### Fields inherited from class ai.onnxruntime.OnnxTensorLike

`allocatorHandle, info, nativeHandle`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`void` `close()` Closes the OnnxValue, freeing it's native memory.
`static <T extends java.nio.Buffer> OnnxSparseTensor` `createSparseTensor​(OrtEnvironment env, OnnxSparseTensor.SparseTensor<T> tensor)` Creates a Sparse Tensor in ORT from the Java side representation.
`java.nio.Buffer` `getIndicesBuffer()` Gets a copy of the indices.
`long[]` `getIndicesShape()` Gets the shape of the (outer) indices.
`java.nio.LongBuffer` `getInnerIndicesBuffer()` Gets a copy of the inner indices in a CSRC sparse tensor.
`long[]` `getInnerIndicesShape()` Gets the shape of the inner indices in a CSRC sparse tensor.
`OnnxSparseTensor.SparseTensorType` `getSparseTensorType()` Returns the type of this OnnxSparseTensor.
`OnnxValue.OnnxValueType` `getType()` Gets the type of this OnnxValue.
`OnnxSparseTensor.SparseTensor<? extends java.nio.Buffer>` `getValue()` Returns the value as a Java object copying it out of the native heap.
`java.nio.Buffer` `getValuesBuffer()` Gets a copy of the data buffer.
`long[]` `getValuesShape()` Gets the shape of the values.

### Methods inherited from class ai.onnxruntime.OnnxTensorLike

`getInfo`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### createSparseTensor

    public static <T extends java.nio.Buffer> OnnxSparseTensor createSparseTensor​(OrtEnvironment env,
                                                                                  OnnxSparseTensor.SparseTensor<T> tensor)
                                                                           throws OrtException

Creates a Sparse Tensor in ORT from the Java side representation.

Type Parameters:
  `T` - The buffer type.
Parameters:
  `env` - The OrtEnvironment.
  `tensor` - The Java side representation.
Returns:
  The sparse tensor in ORT.
Throws:
  `OrtException` - If the tensor could not be created or was invalid.

#### getType

    public OnnxValue.OnnxValueType getType()

Description copied from interface: `OnnxValue`
Gets the type of this OnnxValue.

Returns:
  The value type.

#### getValue

    public OnnxSparseTensor.SparseTensor<? extends java.nio.Buffer> getValue()
                                                                      throws OrtException

Description copied from interface: `OnnxValue`
Returns the value as a Java object copying it out of the native heap. This operation can be quite slow for high dimensional tensors, where you should prefer `OnnxTensor.getByteBuffer()` etc.

Overridden by the subclasses with a more specific type if available.

Returns:
  The value.
Throws:
  `OrtException` - If an error occurred reading the value.

#### close

    public void close()

Description copied from interface: `OnnxValue`
Closes the OnnxValue, freeing it's native memory.

#### getSparseTensorType

    public OnnxSparseTensor.SparseTensorType getSparseTensorType()

Returns the type of this OnnxSparseTensor.

Returns:
  The sparsity type.

#### getIndicesBuffer

    public java.nio.Buffer getIndicesBuffer()

Gets a copy of the indices.

These are the outer indices if it's a CSRC sparse tensor.

It's a `LongBuffer` if COO or CSRC, and `IntBuffer` if Block Sparse.

Returns:
  The indices.

#### getInnerIndicesBuffer

    public java.nio.LongBuffer getInnerIndicesBuffer()

Gets a copy of the inner indices in a CSRC sparse tensor.

Throws `IllegalStateException` if called on a different sparse tensor type.

Returns:
  The inner indices.

#### getValuesBuffer

    public java.nio.Buffer getValuesBuffer()

Gets a copy of the data buffer.

As with `OnnxTensor` fp16 values are upcast into fp32 and returned as a `FloatBuffer`.

Returns:
  The data buffer.

#### getIndicesShape

    public long[] getIndicesShape()

Gets the shape of the (outer) indices.

Returns:
  The indices shape.

#### getInnerIndicesShape

    public long[] getInnerIndicesShape()

Gets the shape of the inner indices in a CSRC sparse tensor.

Returns:
  The indices shape.

#### getValuesShape

    public long[] getValuesShape()

Gets the shape of the values.

Returns:
  The values shape.