Implementation:Microsoft Onnxruntime OrtSession
| Knowledge Sources | Description |
|---|---|
| Source File | java/src/main/java/ai/onnxruntime/OrtSession.java |
| Repository | Microsoft/onnxruntime |
Domains
- Machine Learning Runtime
- Model Inference
- Session Configuration
Overview
OrtSession wraps an ONNX model and provides inference capabilities. It allows inspection of input/output nodes, scoring input feed dictionaries, accessing model metadata, and profiling. It contains the critical inner classes SessionOptions (for execution provider and optimization configuration), RunOptions (for per-run control), and Result (for managing inference outputs). Sessions are produced by OrtEnvironment.
Description
The OrtSession class implements AutoCloseable and provides:
- Session construction: Three constructors accept model path, byte array, or direct ByteBuffer, each with an
OrtAllocatorandSessionOptions. - Input/output inspection:
getNumInputs(),getNumOutputs(),getInputNames(),getOutputNames(),getInputInfo(),getOutputInfo()provide model metadata. - Inference: Multiple
run()overloads accept input maps (name to OnnxTensorLike), optional requested output names, pinned outputs, and RunOptions. - Model metadata:
getMetadata()returnsOnnxModelMetadata. - Profiling:
getProfilingStartTimeInNs()andendProfiling()control profiling.
SessionOptions inner class provides:
- Optimization levels: NO_OPT, BASIC_OPT, EXTENDED_OPT, LAYOUT_OPT, ALL_OPT
- Execution modes: SEQUENTIAL, PARALLEL
- Threading:
setIntraOpNumThreads,setInterOpNumThreads - Execution providers:
addCUDA,addROCM,addCPU,addDnnl,addOpenVINO,addTensorrt,addNnapi,addCoreML,addDirectML,addACL,addArmNN,addXnnpack,addQnn,addWebGPU,addExecutionProvider - Custom ops:
registerCustomOpLibrary,registerCustomOpsUsingFunction - Initializers:
addExternalInitializers,addInitializer - Configuration:
addConfigEntry,setDeterministicCompute,setSymbolicDimensionValue
RunOptions inner class provides logging control, termination, run tagging, config entries, and LoRA adapter activation.
Result inner class wraps inference outputs as an iterable, closeable container with index and name-based access.
Code Reference
Source Location
// File: java/src/main/java/ai/onnxruntime/OrtSession.java
// Package: ai.onnxruntime
Signature
public class OrtSession implements AutoCloseable {
public long getNumInputs();
public long getNumOutputs();
public Set<String> getInputNames();
public Set<String> getOutputNames();
public Map<String, NodeInfo> getInputInfo() throws OrtException;
public Map<String, NodeInfo> getOutputInfo() throws OrtException;
public Result run(Map<String, ? extends OnnxTensorLike> inputs) throws OrtException;
public Result run(Map<String, ? extends OnnxTensorLike> inputs, Set<String> requestedOutputs) throws OrtException;
public Result run(Map<String, ? extends OnnxTensorLike> inputs,
Set<String> requestedOutputs, Map<String, ? extends OnnxValue> pinnedOutputs,
RunOptions runOptions) throws OrtException;
public OnnxModelMetadata getMetadata() throws OrtException;
public long getProfilingStartTimeInNs() throws OrtException;
public String endProfiling() throws OrtException;
public void close() throws OrtException;
public static class SessionOptions implements AutoCloseable { ... }
public static class RunOptions implements AutoCloseable { ... }
public static class Result implements AutoCloseable, Iterable<Map.Entry<String, OnnxValue>> { ... }
}
Import
import ai.onnxruntime.OrtSession;
import ai.onnxruntime.OrtSession.SessionOptions;
import ai.onnxruntime.OrtSession.RunOptions;
import ai.onnxruntime.OrtSession.Result;
I/O Contract
Inputs
| Name | Type | Description |
|---|---|---|
| inputs | Map<String, ? extends OnnxTensorLike> | Named input tensors for inference |
| requestedOutputs | Set<String> | Optional subset of output names to compute |
| pinnedOutputs | Map<String, ? extends OnnxValue> | Pre-allocated output tensors |
| runOptions | RunOptions | Per-run logging and termination controls |
Outputs
| Name | Type | Description |
|---|---|---|
| Result | OrtSession.Result | Container of named OnnxValues (closeable, iterable) |
| Result.get(int) | OnnxValue | Output value by index |
| Result.get(String) | Optional<OnnxValue> | Output value by name |
Usage Examples
import ai.onnxruntime.*;
import java.nio.FloatBuffer;
import java.util.*;
OrtEnvironment env = OrtEnvironment.getEnvironment();
try (OrtSession.SessionOptions opts = new OrtSession.SessionOptions()) {
opts.setOptimizationLevel(OrtSession.SessionOptions.OptLevel.ALL_OPT);
opts.setIntraOpNumThreads(4);
// Add CUDA if available
if (OrtEnvironment.getAvailableProviders().contains(OrtProvider.CUDA)) {
opts.addCUDA(0);
}
try (OrtSession session = env.createSession("/path/to/model.onnx", opts)) {
// Prepare input
float[][] inputData = new float[1][784];
// ... fill inputData ...
OnnxTensor inputTensor = OnnxTensor.createTensor(env, inputData);
Map<String, OnnxTensorLike> inputs = Collections.singletonMap("input", inputTensor);
// Run inference
try (OrtSession.Result results = session.run(inputs)) {
float[][] output = (float[][]) results.get(0).getValue();
System.out.println("Prediction: " + Arrays.toString(output[0]));
}
inputTensor.close();
}
}
Related Pages
- OrtEnvironment.java - Factory for creating sessions
- OnnxTensor.java - Input/output tensor type
- OnnxModelMetadata.java - Model metadata
- NodeInfo.java - Input/output node information
- ai_onnxruntime_OrtSession.c - JNI native implementation
- ai_onnxruntime_OrtSession_SessionOptions.c - SessionOptions JNI implementation