Class OrtValue

java.lang.Object
smile.onnx.OrtValue
All Implemented Interfaces:
AutoCloseable

public class OrtValue extends Object implements AutoCloseable
A wrapper around an OrtValue — the fundamental data container in ONNX Runtime. An OrtValue may hold a dense tensor, a sparse tensor, a sequence, or a map, depending on the ONNX model's input/output types.

Instances of this class must be closed after use. Factory methods support creation from Java primitive arrays; data is copied into a native memory segment owned by this object.

float[] data = { 1f, 2f, 3f, 4f };
long[] shape = { 1, 4 };
try (OrtValue input = OrtValue.fromFloatArray(data, shape)) {
    OrtValue[] outputs = session.run(inputs, outputNames);
    float[] result = outputs[0].toFloatArray();
}
  • Method Details

    • fromFloatArray

      public static OrtValue fromFloatArray(float[] data, long[] shape)
      Creates an OrtValue tensor backed by a copy of the given float[] array.
      Parameters:
      data - the float values.
      shape - the tensor dimensions; the product must equal data.length.
      Returns:
      a new OrtValue owning the tensor data.
    • fromDoubleArray

      public static OrtValue fromDoubleArray(double[] data, long[] shape)
      Creates an OrtValue tensor backed by a copy of the given double[] array.
      Parameters:
      data - the double values.
      shape - the tensor dimensions.
      Returns:
      a new OrtValue owning the tensor data.
    • fromIntArray

      public static OrtValue fromIntArray(int[] data, long[] shape)
      Creates an OrtValue tensor backed by a copy of the given int[] array.
      Parameters:
      data - the int32 values.
      shape - the tensor dimensions.
      Returns:
      a new OrtValue owning the tensor data.
    • fromLongArray

      public static OrtValue fromLongArray(long[] data, long[] shape)
      Creates an OrtValue tensor backed by a copy of the given long[] array.
      Parameters:
      data - the int64 values.
      shape - the tensor dimensions.
      Returns:
      a new OrtValue owning the tensor data.
    • fromByteArray

      public static OrtValue fromByteArray(byte[] data, long[] shape)
      Creates an OrtValue tensor backed by a copy of the given byte[] array (INT8 element type).
      Parameters:
      data - the int8 values.
      shape - the tensor dimensions.
      Returns:
      a new OrtValue owning the tensor data.
    • fromBooleanArray

      public static OrtValue fromBooleanArray(boolean[] data, long[] shape)
      Creates an OrtValue tensor backed by a copy of the given boolean[] array (BOOL element type, stored as bytes).
      Parameters:
      data - the boolean values.
      shape - the tensor dimensions.
      Returns:
      a new OrtValue owning the tensor data.
    • fromTensor

      public static OrtValue fromTensor(JTensor tensor)
      Creates an OrtValue tensor from a JTensor.

      The tensor data is copied into a contiguous off-heap native memory buffer in the platform's native byte order, as required by ORT. The ONNX shape is derived directly from tensor.shape(), and the ONNX element type is mapped from tensor.scalarType().

      Supported scalar types:

      Parameters:
      tensor - the source tensor; must not be null.
      Returns:
      a new OrtValue owning a copy of the tensor data.
      Throws:
      IllegalArgumentException - if the scalar type has no ONNX mapping.
    • fromMatrix

      public static OrtValue fromMatrix(DenseMatrix matrix)
      Creates an OrtValue tensor from a DenseMatrix.

      The matrix is serialized in row-major order (the standard ONNX layout) with shape [nrow, ncol], regardless of the internal column-major / padded storage used by DenseMatrix. Padding columns introduced by the optimal leading dimension are not included in the output tensor.

      Supported scalar types:

      Parameters:
      matrix - the source matrix; must not be null.
      Returns:
      a new OrtValue owning a copy of the matrix data in row-major order with shape [nrow, ncol].
      Throws:
      IllegalArgumentException - if the scalar type is not Float32 or Float64.
    • onnxType

      public OnnxType onnxType()
      Returns the ONNX value type of this OrtValue.
      Returns:
      the OnnxType.
    • isTensor

      public boolean isTensor()
      Returns true if this OrtValue is a (dense) tensor.
      Returns:
      true for tensors.
    • tensorInfo

      public TensorInfo tensorInfo()
      Returns the type and shape info for this tensor.
      Returns:
      the TensorInfo.
      Throws:
      OnnxException - if this value is not a tensor.
    • toFloatArray

      public float[] toFloatArray()
      Copies the tensor data into a new float[] array. The tensor must have element type ElementType.FLOAT.
      Returns:
      the float data.
    • toDoubleArray

      public double[] toDoubleArray()
      Copies the tensor data into a new double[] array. The tensor must have element type ElementType.DOUBLE.
      Returns:
      the double data.
    • toIntArray

      public int[] toIntArray()
      Copies the tensor data into a new int[] array. The tensor must have element type ElementType.INT32.
      Returns:
      the int data.
    • toLongArray

      public long[] toLongArray()
      Copies the tensor data into a new long[] array. The tensor must have element type ElementType.INT64.
      Returns:
      the long data.
    • toByteArray

      public byte[] toByteArray()
      Copies the tensor data into a new byte[] array. The tensor must have element type ElementType.INT8 or ElementType.UINT8.
      Returns:
      the byte data.
    • toStringArray

      public String[] toStringArray()
      Copies the string tensor data into a String[] array. The tensor must have element type ElementType.STRING.
      Returns:
      the string data.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • toString

      public String toString()
      Overrides:
      toString in class Object