Package smile.onnx


package smile.onnx
Java API for the ONNX Runtime inference engine.

This package provides an idiomatic Java interface for loading and running ONNX models via the ONNX Runtime native library. It is built on top of the low-level Panama Foreign Function & Memory (FFM) bindings in smile.onnx.foreign — no JNI is used.

Key classes

InferenceSession
Loads an ONNX model from a file or byte array and exposes a run() method for executing inference.
OrtValue
Wraps an OrtValue — the fundamental tensor / sequence / map container. Factory methods create tensors from Java primitive arrays; extraction methods copy data back.
SessionOptions
Fluent builder for session-level configuration: thread counts, graph optimisation level, execution providers, profiling, etc.
RunOptions
Per-inference-run options: log tag, severity level, cancellation.
Environment
Wraps an OrtEnv and owns shared thread pools. Use when multiple sessions should share resources.
ModelMetadata
Model producer name, graph name, version, and custom key-value metadata embedded in the model.
NodeInfo
Name, ONNX type, and (for tensors) shape/element-type of a model input or output.

Quick start

// Load a model and run inference
try (var session = InferenceSession.create("yolo.onnx")) {

    float[] pixels = ...; // pre-processed image data
    long[] shape   = { 1, 3, 640, 640 };

    try (OrtValue input = OrtValue.fromFloatArray(pixels, shape)) {
        OrtValue[] outputs = session.run(Map.of("images", input));
        float[] boxes = outputs[0].toFloatArray();
        for (OrtValue v : outputs) v.close();
    }
}

Dependencies

The native onnxruntime shared library must be present on the java.library.path (or the OS library search path). Pre-built packages are available from the ORT releases page.

Java 22+ with the --enable-native-access=ALL-UNNAMED JVM flag (or a named module declaration) is required for the Panama FFM layer.

  • Class
    Description
    The data type of individual tensor elements, corresponding to ONNXTensorElementDataType in the ONNX Runtime C API.
    A wrapper around the ONNX Runtime OrtEnv object.
    Execution mode for an ONNX Runtime session, corresponding to ExecutionMode in the ONNX Runtime C API.
    Graph optimization level for an ONNX Runtime session, corresponding to GraphOptimizationLevel in the ONNX Runtime C API.
    Represents an ONNX Runtime inference session for a single model.
    Logging severity level for the ONNX Runtime environment, corresponding to OrtLoggingLevel in the ONNX Runtime C API.
    Metadata associated with an ONNX model.
    Describes a model input or output node, including its name, ONNX value type, and (for tensor nodes) its tensor type/shape information.
    Exception thrown when an ONNX Runtime operation fails.
    The type of an ONNX value, corresponding to ONNXType in the ONNX Runtime C API.
    A wrapper around an OrtValue — the fundamental data container in ONNX Runtime.
    Per-run configuration options passed to InferenceSession.run(Map, String[], RunOptions).
    Configuration options for an InferenceSession.
    Describes the type and shape of a tensor.