Package ai.onnxruntime
Class OrtUtil
- java.lang.Object
-
- ai.onnxruntime.OrtUtil
-
public final class OrtUtil extends java.lang.Object
Util code for interacting with Java arrays.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
elementCount(long[] shape)
Counts the number of elements stored in a Tensor of this shape.static java.lang.String[]
flattenString(java.lang.Object o)
Flatten a multidimensional String array into a single dimensional String array, reading it in a multidimensional row-major order.static java.lang.Object
newBooleanArray(long[] shape)
Creates a new primitive boolean array of up to 8 dimensions, using the supplied shape.static java.lang.Object
newByteArray(long[] shape)
Creates a new primitive byte array of up to 8 dimensions, using the supplied shape.static java.lang.Object
newDoubleArray(long[] shape)
Creates a new primitive double array of up to 8 dimensions, using the supplied shape.static java.lang.Object
newFloatArray(long[] shape)
Creates a new primitive float array of up to 8 dimensions, using the supplied shape.static java.lang.Object
newIntArray(long[] shape)
Creates a new primitive int array of up to 8 dimensions, using the supplied shape.static java.lang.Object
newLongArray(long[] shape)
Creates a new primitive long array of up to 8 dimensions, using the supplied shape.static java.lang.Object
newShortArray(long[] shape)
Creates a new primitive short array of up to 8 dimensions, using the supplied shape.static java.lang.Object
newStringArray(long[] shape)
Creates a new String array of up to 8 dimensions, using the supplied shape.static java.lang.Object
reshape(boolean[] input, long[] shape)
Reshapes a boolean array into the desired n-dimensional array assuming the boolean array is stored in n-dimensional row-major order.static java.lang.Object
reshape(byte[] input, long[] shape)
Reshapes a byte array into the desired n-dimensional array assuming the byte array is stored in n-dimensional row-major order.static java.lang.Object
reshape(double[] input, long[] shape)
Reshapes a double array into the desired n-dimensional array assuming the double array is stored in n-dimensional row-major order.static java.lang.Object
reshape(float[] input, long[] shape)
Reshapes a float array into the desired n-dimensional array assuming the float array is stored in n-dimensional row-major order.static java.lang.Object
reshape(int[] input, long[] shape)
Reshapes an int array into the desired n-dimensional array, assuming the int array is stored in n-dimensional row-major order.static java.lang.Object
reshape(long[] input, long[] shape)
Reshapes a long array into the desired n-dimensional array, assuming the long array is stored in n-dimensional row-major order.static java.lang.Object
reshape(short[] input, long[] shape)
Reshapes a short array into the desired n-dimensional array assuming the short array is stored in n-dimensional row-major order.static java.lang.Object
reshape(java.lang.String[] input, long[] shape)
Reshapes a String array into the desired n-dimensional array assuming the String array is stored in n-dimensional row-major order.static long[]
transformShape(int[] shape)
Converts an int shape into a long shape.static int[]
transformShape(long[] shape)
Converts an long shape into a int shape.static boolean
validateShape(long[] shape)
Checks that the shape is a valid shape for a Java array (i.e.
-
-
-
Method Detail
-
transformShape
public static int[] transformShape(long[] shape)
Converts an long shape into a int shape.Validates that the shape has more than 1 elements, less than 9 elements, each element is less than
Integer.MAX_VALUE
and that each entry is non-negative.- Parameters:
shape
- The long shape.- Returns:
- The int shape.
-
transformShape
public static long[] transformShape(int[] shape)
Converts an int shape into a long shape.Validates that the shape has more than 1 element, less than 9 elements and that each entry is non-negative.
- Parameters:
shape
- The int shape.- Returns:
- The long shape.
-
newBooleanArray
public static java.lang.Object newBooleanArray(long[] shape)
Creates a new primitive boolean array of up to 8 dimensions, using the supplied shape.- Parameters:
shape
- The shape of array to create.- Returns:
- A boolean array.
-
newByteArray
public static java.lang.Object newByteArray(long[] shape)
Creates a new primitive byte array of up to 8 dimensions, using the supplied shape.- Parameters:
shape
- The shape of array to create.- Returns:
- A byte array.
-
newShortArray
public static java.lang.Object newShortArray(long[] shape)
Creates a new primitive short array of up to 8 dimensions, using the supplied shape.- Parameters:
shape
- The shape of array to create.- Returns:
- A short array.
-
newIntArray
public static java.lang.Object newIntArray(long[] shape)
Creates a new primitive int array of up to 8 dimensions, using the supplied shape.- Parameters:
shape
- The shape of array to create.- Returns:
- A int array.
-
newLongArray
public static java.lang.Object newLongArray(long[] shape)
Creates a new primitive long array of up to 8 dimensions, using the supplied shape.- Parameters:
shape
- The shape of array to create.- Returns:
- A long array.
-
newFloatArray
public static java.lang.Object newFloatArray(long[] shape)
Creates a new primitive float array of up to 8 dimensions, using the supplied shape.- Parameters:
shape
- The shape of array to create.- Returns:
- A float array.
-
newDoubleArray
public static java.lang.Object newDoubleArray(long[] shape)
Creates a new primitive double array of up to 8 dimensions, using the supplied shape.- Parameters:
shape
- The shape of array to create.- Returns:
- A double array.
-
newStringArray
public static java.lang.Object newStringArray(long[] shape)
Creates a new String array of up to 8 dimensions, using the supplied shape.- Parameters:
shape
- The shape of array to create.- Returns:
- A double array.
-
reshape
public static java.lang.Object reshape(boolean[] input, long[] shape)
Reshapes a boolean array into the desired n-dimensional array assuming the boolean array is stored in n-dimensional row-major order. ThrowsIllegalArgumentException
if the number of elements doesn't match between the shape and the input or the shape is invalid.- Parameters:
input
- The boolean array.shape
- The desired shape.- Returns:
- An n-dimensional boolean array.
-
reshape
public static java.lang.Object reshape(byte[] input, long[] shape)
Reshapes a byte array into the desired n-dimensional array assuming the byte array is stored in n-dimensional row-major order. ThrowsIllegalArgumentException
if the number of elements doesn't match between the shape and the input or the shape is invalid.- Parameters:
input
- The byte array.shape
- The desired shape.- Returns:
- An n-dimensional byte array.
-
reshape
public static java.lang.Object reshape(short[] input, long[] shape)
Reshapes a short array into the desired n-dimensional array assuming the short array is stored in n-dimensional row-major order. ThrowsIllegalArgumentException
if the number of elements doesn't match between the shape and the input or the shape is invalid.- Parameters:
input
- The short array.shape
- The desired shape.- Returns:
- An n-dimensional short array.
-
reshape
public static java.lang.Object reshape(int[] input, long[] shape)
Reshapes an int array into the desired n-dimensional array, assuming the int array is stored in n-dimensional row-major order. ThrowsIllegalArgumentException
if the number of elements doesn't match between the shape and the input or the shape is invalid.- Parameters:
input
- The int array.shape
- The desired shape.- Returns:
- An n-dimensional int array.
-
reshape
public static java.lang.Object reshape(long[] input, long[] shape)
Reshapes a long array into the desired n-dimensional array, assuming the long array is stored in n-dimensional row-major order. ThrowsIllegalArgumentException
if the number of elements doesn't match between the shape and the input or the shape is invalid.- Parameters:
input
- The long array.shape
- The desired shape.- Returns:
- An n-dimensional long array.
-
reshape
public static java.lang.Object reshape(float[] input, long[] shape)
Reshapes a float array into the desired n-dimensional array assuming the float array is stored in n-dimensional row-major order. ThrowsIllegalArgumentException
if the number of elements doesn't match between the shape and the input or the shape is invalid.- Parameters:
input
- The float array.shape
- The desired shape.- Returns:
- An n-dimensional float array.
-
reshape
public static java.lang.Object reshape(double[] input, long[] shape)
Reshapes a double array into the desired n-dimensional array assuming the double array is stored in n-dimensional row-major order. ThrowsIllegalArgumentException
if the number of elements doesn't match between the shape and the input or the shape is invalid.- Parameters:
input
- The double array.shape
- The desired shape.- Returns:
- An n-dimensional double array.
-
reshape
public static java.lang.Object reshape(java.lang.String[] input, long[] shape)
Reshapes a String array into the desired n-dimensional array assuming the String array is stored in n-dimensional row-major order. ThrowsIllegalArgumentException
if the number of elements doesn't match between the shape and the input or the shape is invalid.- Parameters:
input
- The double array.shape
- The desired shape.- Returns:
- An n-dimensional String array.
-
elementCount
public static long elementCount(long[] shape)
Counts the number of elements stored in a Tensor of this shape.Multiplies all the elements together if they are non-negative, throws an
IllegalArgumentException
otherwise.- Parameters:
shape
- The shape to use.- Returns:
- The number of elements.
-
validateShape
public static boolean validateShape(long[] shape)
Checks that the shape is a valid shape for a Java array (i.e. that the values are all positive and representable by an int).- Parameters:
shape
- The shape to check.- Returns:
- True if the shape is valid.
-
flattenString
public static java.lang.String[] flattenString(java.lang.Object o)
Flatten a multidimensional String array into a single dimensional String array, reading it in a multidimensional row-major order.- Parameters:
o
- A multidimensional String array.- Returns:
- A single dimensional String array.
-
-