Class OnnxSparseTensor

java.lang.Object
ai.onnxruntime.OnnxTensorLike
ai.onnxruntime.OnnxSparseTensor
All Implemented Interfaces:
OnnxValue, 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.

  • Method Details

    • createSparseTensor

      public static <T extends 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 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 its native memory.
    • getSparseTensorType

      public OnnxSparseTensor.SparseTensorType getSparseTensorType()
      Returns the type of this OnnxSparseTensor.
      Returns:
      The sparsity type.
    • getIndicesBuffer

      public 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 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 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.