Package ai.onnxruntime
Class TensorInfo
- java.lang.Object
-
- ai.onnxruntime.TensorInfo
-
- All Implemented Interfaces:
ValueInfo
public class TensorInfo extends java.lang.Object implements ValueInfo
Describes anOnnxTensor
, including it's size, shape and element type.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TensorInfo.OnnxTensorType
The native element types supported by the ONNX runtime.
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_DIMENSIONS
Maximum number of dimensions supported by the Java interface methods.TensorInfo.OnnxTensorType
onnxType
The native type of this tensor.OnnxJavaType
type
The Java type of this tensor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static TensorInfo
constructFromBuffer(java.nio.Buffer buffer, long[] shape, OnnxJavaType type)
Constructs a TensorInfo from the supplied byte buffer.static TensorInfo
constructFromJavaArray(java.lang.Object obj)
Constructs a TensorInfo from the supplied multidimensional Java array, used to allocate the appropriate amount of native memory.static <T extends java.nio.Buffer>
TensorInfoconstructFromSparseTensor(OnnxSparseTensor.SparseTensor<T> tensor)
Constructs a TensorInfo from the suppliedOnnxSparseTensor.SparseTensor
.java.lang.String[]
getDimensionNames()
Get a copy of the tensor's named dimensions.long
getNumElements()
Returns the number of elements in this tensor.long[]
getShape()
Get a copy of the tensor's shape.boolean
isScalar()
Returns true if the shape represents a scalar value (i.e.java.lang.Object
makeCarrier()
Constructs an array the right shape and type to hold this tensor.java.lang.String
toString()
-
-
-
Field Detail
-
MAX_DIMENSIONS
public static final int MAX_DIMENSIONS
Maximum number of dimensions supported by the Java interface methods.- See Also:
- Constant Field Values
-
type
public final OnnxJavaType type
The Java type of this tensor.
-
onnxType
public final TensorInfo.OnnxTensorType onnxType
The native type of this tensor.
-
-
Method Detail
-
getShape
public long[] getShape()
Get a copy of the tensor's shape.- Returns:
- A copy of the tensor's shape.
-
getDimensionNames
public java.lang.String[] getDimensionNames()
Get a copy of the tensor's named dimensions.- Returns:
- A copof the tensor's named dimensions.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
isScalar
public boolean isScalar()
Returns true if the shape represents a scalar value (i.e. it has zero dimensions).- Returns:
- True if the shape is a scalar.
-
getNumElements
public long getNumElements()
Returns the number of elements in this tensor.If the returned value is negative, then this tensor info refers to an input or output placeholder which has symbolic dimensions, and the element count cannot be computed without specifying the symbolic dimensions.
- Returns:
- The number of elements.
-
makeCarrier
public java.lang.Object makeCarrier() throws OrtException
Constructs an array the right shape and type to hold this tensor.Note for String tensors, this carrier is a single dimensional array with enough space for all elements as that's the expected format of the native code. It can be reshaped to the correct shape using
OrtUtil.reshape(String[],long[])
.For fp16 and bf16 tensors the output carrier type is float, and so this method produces multidimensional float arrays.
- Returns:
- A multidimensional array of the appropriate primitive type (or String).
- Throws:
OrtException
- If the shape isn't representable in Java (i.e. if one of its indices is greater than an int).
-
constructFromJavaArray
public static TensorInfo constructFromJavaArray(java.lang.Object obj) throws OrtException
Constructs a TensorInfo from the supplied multidimensional Java array, used to allocate the appropriate amount of native memory.- Parameters:
obj
- The object to inspect.- Returns:
- A TensorInfo which can be used to make the right size Tensor.
- Throws:
OrtException
- If the supplied Object isn't an array, or is an invalid type.
-
constructFromBuffer
public static TensorInfo constructFromBuffer(java.nio.Buffer buffer, long[] shape, OnnxJavaType type) throws OrtException
Constructs a TensorInfo from the supplied byte buffer.- Parameters:
buffer
- The buffer to inspect.shape
- The shape of the tensor.type
- The Java type.- Returns:
- A TensorInfo for a tensor.
- Throws:
OrtException
- If the supplied buffer doesn't match the shape.
-
constructFromSparseTensor
public static <T extends java.nio.Buffer> TensorInfo constructFromSparseTensor(OnnxSparseTensor.SparseTensor<T> tensor) throws OrtException
Constructs a TensorInfo from the suppliedOnnxSparseTensor.SparseTensor
.- Type Parameters:
T
- The buffer type.- Parameters:
tensor
- The sparse tensor.- Returns:
- A TensorInfo for a sparse tensor.
- Throws:
OrtException
- If the supplied tensor has too many elements for it's shape.
-
-