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 TensorInfo

From Leeroopedia


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

Domains

  • Machine Learning Runtime
  • Tensor Metadata
  • Type System

Overview

TensorInfo describes an OnnxTensor, including its shape, element type (both Java and native ONNX types), number of elements, and dimension names. It implements the ValueInfo interface and provides factory methods to construct tensor info from Java arrays, NIO buffers, and sparse tensors. It also contains the OnnxTensorType enum mapping all native ONNX tensor element types.

Description

The TensorInfo class provides:

  • Shape and type: getShape(), type (OnnxJavaType), onnxType (OnnxTensorType), getNumElements().
  • Dimension names: getDimensionNames() returns named dimensions (for symbolic dimensions).
  • Scalar detection: isScalar() returns true for zero-dimensional tensors.
  • Carrier creation: makeCarrier() creates a multidimensional Java array of the correct type and shape to hold tensor data. FP16/BF16 tensors produce float arrays.
  • Factory methods: constructFromJavaArray(Object) infers shape and type via reflection; constructFromBuffer(Buffer, long[], OnnxJavaType) validates buffer against shape; constructFromSparseTensor(SparseTensor) builds info from sparse tensor metadata.
  • MAX_DIMENSIONS: Constant set to 8, the maximum number of dimensions for Java-side arrays.

The OnnxTensorType enum defines all 21 native tensor element types (UNDEFINED through FLOAT8E5M2FNUZ) with mapFromInt and mapFromJavaType conversion methods.

Code Reference

Source Location

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

Signature

public class TensorInfo implements ValueInfo {
    public static final int MAX_DIMENSIONS = 8;
    public final OnnxJavaType type;
    public final OnnxTensorType onnxType;

    public long[] getShape();
    public String[] getDimensionNames();
    public boolean isScalar();
    public long getNumElements();
    public Object makeCarrier() throws OrtException;

    public static TensorInfo constructFromJavaArray(Object obj) throws OrtException;
    public static TensorInfo constructFromBuffer(Buffer buffer, long[] shape, OnnxJavaType type) throws OrtException;
    public static <T extends Buffer> TensorInfo constructFromSparseTensor(
        OnnxSparseTensor.SparseTensor<T> tensor) throws OrtException;

    public enum OnnxTensorType {
        ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED, ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8,
        ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8, ..., ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT8E5M2FNUZ;
        public static OnnxTensorType mapFromInt(int value);
        public static OnnxTensorType mapFromJavaType(OnnxJavaType type);
    }
}

Import

import ai.onnxruntime.TensorInfo;
import ai.onnxruntime.TensorInfo.OnnxTensorType;

I/O Contract

Inputs

Name Type Description
obj Object Java array or scalar to inspect for shape and type
buffer Buffer NIO buffer with tensor data
shape long[] The desired tensor shape
type OnnxJavaType The Java element type

Outputs

Name Type Description
TensorInfo TensorInfo Metadata object describing the tensor
getShape() long[] Copy of the tensor shape
getNumElements() long Total element count (may be negative for symbolic dims)
makeCarrier() Object Multidimensional Java array matching the tensor info

Usage Examples

import ai.onnxruntime.*;

// Construct TensorInfo from a Java array
float[][] data = new float[3][4];
TensorInfo info = TensorInfo.constructFromJavaArray(data);
System.out.println("Shape: " + java.util.Arrays.toString(info.getShape())); // [3, 4]
System.out.println("Type: " + info.type);    // FLOAT
System.out.println("Elements: " + info.getNumElements()); // 12
System.out.println("Scalar: " + info.isScalar()); // false

// Construct TensorInfo from a buffer
java.nio.FloatBuffer buf = java.nio.FloatBuffer.allocate(12);
TensorInfo bufInfo = TensorInfo.constructFromBuffer(buf, new long[]{3, 4}, OnnxJavaType.FLOAT);
System.out.println("Buffer info shape: " + java.util.Arrays.toString(bufInfo.getShape()));

// Create a carrier array for holding tensor output
Object carrier = info.makeCarrier(); // returns a float[3][4]

Related Pages

Page Connections

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