Class OrtValue
- All Implemented Interfaces:
AutoCloseable
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 Summary
Modifier and TypeMethodDescriptionvoidclose()static OrtValuefromBooleanArray(boolean[] data, long[] shape) Creates an OrtValue tensor backed by a copy of the givenboolean[]array (BOOL element type, stored as bytes).static OrtValuefromByteArray(byte[] data, long[] shape) Creates an OrtValue tensor backed by a copy of the givenbyte[]array (INT8 element type).static OrtValuefromDoubleArray(double[] data, long[] shape) Creates an OrtValue tensor backed by a copy of the givendouble[]array.static OrtValuefromFloatArray(float[] data, long[] shape) Creates an OrtValue tensor backed by a copy of the givenfloat[]array.static OrtValuefromIntArray(int[] data, long[] shape) Creates an OrtValue tensor backed by a copy of the givenint[]array.static OrtValuefromLongArray(long[] data, long[] shape) Creates an OrtValue tensor backed by a copy of the givenlong[]array.static OrtValuefromMatrix(DenseMatrix matrix) Creates an OrtValue tensor from aDenseMatrix.static OrtValuefromTensor(JTensor tensor) Creates an OrtValue tensor from aJTensor.booleanisTensor()Returnstrueif this OrtValue is a (dense) tensor.onnxType()Returns the ONNX value type of this OrtValue.Returns the type and shape info for this tensor.byte[]Copies the tensor data into a newbyte[]array.double[]Copies the tensor data into a newdouble[]array.float[]Copies the tensor data into a newfloat[]array.int[]Copies the tensor data into a newint[]array.long[]Copies the tensor data into a newlong[]array.toString()String[]Copies the string tensor data into aString[]array.
-
Method Details
-
fromFloatArray
Creates an OrtValue tensor backed by a copy of the givenfloat[]array.- Parameters:
data- the float values.shape- the tensor dimensions; the product must equaldata.length.- Returns:
- a new OrtValue owning the tensor data.
-
fromDoubleArray
Creates an OrtValue tensor backed by a copy of the givendouble[]array.- Parameters:
data- the double values.shape- the tensor dimensions.- Returns:
- a new OrtValue owning the tensor data.
-
fromIntArray
Creates an OrtValue tensor backed by a copy of the givenint[]array.- Parameters:
data- the int32 values.shape- the tensor dimensions.- Returns:
- a new OrtValue owning the tensor data.
-
fromLongArray
Creates an OrtValue tensor backed by a copy of the givenlong[]array.- Parameters:
data- the int64 values.shape- the tensor dimensions.- Returns:
- a new OrtValue owning the tensor data.
-
fromByteArray
Creates an OrtValue tensor backed by a copy of the givenbyte[]array (INT8 element type).- Parameters:
data- the int8 values.shape- the tensor dimensions.- Returns:
- a new OrtValue owning the tensor data.
-
fromBooleanArray
Creates an OrtValue tensor backed by a copy of the givenboolean[]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
Creates an OrtValue tensor from aJTensor.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 fromtensor.scalarType().Supported scalar types:
Float32→ElementType.FLOATFloat64→ElementType.DOUBLEInt8/QInt8/QUInt8→ElementType.INT8/ElementType.UINT8Int16→ElementType.INT16Int32→ElementType.INT32Int64→ElementType.INT64Float16→ElementType.FLOAT16BFloat16→ElementType.BFLOAT16
- 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
Creates an OrtValue tensor from aDenseMatrix.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 byDenseMatrix. Padding columns introduced by the optimal leading dimension are not included in the output tensor.Supported scalar types:
Float32→ElementType.FLOATFloat64→ElementType.DOUBLE
- 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 notFloat32orFloat64.
-
onnxType
-
isTensor
public boolean isTensor()Returnstrueif this OrtValue is a (dense) tensor.- Returns:
- true for tensors.
-
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 newfloat[]array. The tensor must have element typeElementType.FLOAT.- Returns:
- the float data.
-
toDoubleArray
public double[] toDoubleArray()Copies the tensor data into a newdouble[]array. The tensor must have element typeElementType.DOUBLE.- Returns:
- the double data.
-
toIntArray
public int[] toIntArray()Copies the tensor data into a newint[]array. The tensor must have element typeElementType.INT32.- Returns:
- the int data.
-
toLongArray
public long[] toLongArray()Copies the tensor data into a newlong[]array. The tensor must have element typeElementType.INT64.- Returns:
- the long data.
-
toByteArray
public byte[] toByteArray()Copies the tensor data into a newbyte[]array. The tensor must have element typeElementType.INT8orElementType.UINT8.- Returns:
- the byte data.
-
toStringArray
Copies the string tensor data into aString[]array. The tensor must have element typeElementType.STRING.- Returns:
- the string data.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
toString
-