Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Onnxruntime OrtUtil

From Leeroopedia
Revision as of 15:47, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Onnxruntime_OrtUtil.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources Description
Source File java/src/main/java/ai/onnxruntime/OrtUtil.java
Repository Microsoft/onnxruntime

Domains

  • Machine Learning Runtime
  • Array and Buffer Utilities
  • Shape Manipulation

Overview

OrtUtil is a public final utility class providing static methods for array and buffer manipulation in the ONNX Runtime Java API. It handles shape transformations, multidimensional array creation and reshaping, element counting, array-to-buffer and buffer-to-array conversions, boxed primitive conversions, buffer preparation for JNI calls, and string flattening. These utilities are used throughout the API for tensor construction and value extraction.

Description

Key method groups:

  • Shape transformation: transformShape(long[]) converts long shapes to int shapes (validating bounds); transformShape(int[]) converts int shapes to long shapes.
  • Array creation: newFloatArray, newDoubleArray, newIntArray, newLongArray, newShortArray, newByteArray, newBooleanArray, newStringArray create multidimensional arrays from shape specifications (up to 8 dimensions).
  • Reshape: Overloaded reshape() methods convert flat primitive or String arrays into multidimensional arrays based on shape.
  • Element count: elementCount(long[]) computes the total number of elements from a shape.
  • Shape validation: validateShape(long[]) checks that shape entries are positive and fit in a Java int.
  • Buffer preparation: prepareBuffer(Buffer, OnnxJavaType) returns a BufferTuple containing a direct buffer, byte position, byte size, element count, and whether a copy was made. Non-direct buffers are copied to direct buffers.
  • Array to buffer: convertArrayToBuffer(TensorInfo, Object) copies multidimensional Java arrays into direct NIO buffers.
  • Buffer to array: fillArrayFromBuffer(TensorInfo, Buffer, int, Object) copies buffer contents into multidimensional Java arrays.
  • Boxed primitive conversion: convertBoxedPrimitiveToBuffer and convertBoxedPrimitiveToArray handle scalar values.
  • String utilities: flattenString(Object) flattens multidimensional String arrays.
  • Map utilities: convertToMap and unpackMap convert between zipped String arrays and Maps.

Code Reference

Source Location

// File: java/src/main/java/ai/onnxruntime/OrtUtil.java
// Package: ai.onnxruntime

Signature

public final class OrtUtil {
    public static int[] transformShape(long[] shape);
    public static long[] transformShape(int[] shape);
    public static Object newBooleanArray(long[] shape);
    public static Object newByteArray(long[] shape);
    public static Object newShortArray(long[] shape);
    public static Object newIntArray(long[] shape);
    public static Object newLongArray(long[] shape);
    public static Object newFloatArray(long[] shape);
    public static Object newDoubleArray(long[] shape);
    public static Object newStringArray(long[] shape);
    public static Object reshape(float[] input, long[] shape);
    public static Object reshape(double[] input, long[] shape);
    public static Object reshape(int[] input, long[] shape);
    public static Object reshape(long[] input, long[] shape);
    public static Object reshape(short[] input, long[] shape);
    public static Object reshape(byte[] input, long[] shape);
    public static Object reshape(boolean[] input, long[] shape);
    public static Object reshape(String[] input, long[] shape);
    public static long elementCount(long[] shape);
    public static boolean validateShape(long[] shape);
    public static String[] flattenString(Object o);
    static BufferTuple prepareBuffer(Buffer data, OnnxJavaType type);
    static Buffer convertArrayToBuffer(TensorInfo info, Object array);
    static void fillArrayFromBuffer(TensorInfo info, Buffer buffer, int curDim, Object array);
}

Import

import ai.onnxruntime.OrtUtil;

I/O Contract

Inputs

Name Type Description
shape long[] or int[] The tensor shape to transform or use for array creation
input primitive[] or String[] Flat array to reshape
data Buffer NIO buffer to prepare for JNI transfer
type OnnxJavaType The Java type of the tensor elements

Outputs

Name Type Description
transformShape int[] or long[] Converted shape array
newXxxArray Object Multidimensional array of the specified type and shape
reshape Object Multidimensional array reshaped from flat input
elementCount long Total number of elements in the shape
prepareBuffer BufferTuple Direct buffer with position, size, and copy flag

Usage Examples

import ai.onnxruntime.OrtUtil;

// Convert a long shape to int shape
long[] longShape = {3, 224, 224};
int[] intShape = OrtUtil.transformShape(longShape);
// intShape = [3, 224, 224]

// Create a multidimensional float array
Object floatArray = OrtUtil.newFloatArray(new long[]{2, 3});
// floatArray is a float[2][3]

// Reshape a flat array into a 2D array
float[] flat = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
float[][] reshaped = (float[][]) OrtUtil.reshape(flat, new long[]{2, 3});
// reshaped = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]

// Count elements
long count = OrtUtil.elementCount(new long[]{3, 224, 224});
// count = 150528

// Flatten a multidimensional String array
String[][] strings = {{"a", "b"}, {"c", "d"}};
String[] flattened = OrtUtil.flattenString(strings);
// flattened = ["a", "b", "c", "d"]

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment