Package ai.onnxruntime
Class TensorInfo
java.lang.Object
ai.onnxruntime.TensorInfo
- All Implemented Interfaces:
ValueInfo
Describes an
OnnxTensor, including it's size, shape and element type.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe native element types supported by the ONNX runtime. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum number of dimensions supported by the Java interface methods.The native type of this tensor.final OnnxJavaTypeThe Java type of this tensor. -
Method Summary
Modifier and TypeMethodDescriptionstatic TensorInfoconstructFromBuffer(Buffer buffer, long[] shape, OnnxJavaType type) Constructs a TensorInfo from the supplied byte buffer.static TensorInfoConstructs a TensorInfo from the supplied multidimensional Java array, used to allocate the appropriate amount of native memory.static <T extends Buffer>
TensorInfoConstructs a TensorInfo from the suppliedOnnxSparseTensor.SparseTensor.String[]Get a copy of the tensor's named dimensions.longReturns the number of elements in this tensor.long[]getShape()Get a copy of the tensor's shape.booleanisScalar()Returns true if the shape represents a scalar value (i.e.Constructs an array the right shape and type to hold this tensor.toString()
-
Field Details
-
MAX_DIMENSIONS
public static final int MAX_DIMENSIONSMaximum number of dimensions supported by the Java interface methods.- See Also:
-
type
The Java type of this tensor. -
onnxType
The native type of this tensor.
-
-
Method Details
-
getShape
public long[] getShape()Get a copy of the tensor's shape.- Returns:
- A copy of the tensor's shape.
-
getDimensionNames
Get a copy of the tensor's named dimensions.- Returns:
- A copof the tensor's named dimensions.
-
toString
-
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
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
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(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 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.
-