Class Environment

java.lang.Object
smile.onnx.Environment
All Implemented Interfaces:
AutoCloseable

public class Environment extends Object implements AutoCloseable
A wrapper around the ONNX Runtime OrtEnv object. The environment manages thread pools and logging configuration that are shared across all sessions created from the same environment.

For most use cases the convenience factory methods on InferenceSession (which create their own private environment) are sufficient. Use this class when you want to share one environment — and therefore its thread pools — across several sessions.

try (var env = new Environment(LoggingLevel.WARNING, "my-app")) {
    try (var opts = new SessionOptions()) {
        var session = env.createSession("model.onnx", opts);
        // ...
    }
}
  • Constructor Details

    • Environment

      public Environment()
      Creates an Environment with LoggingLevel.WARNING and the log id "smile-onnx".
    • Environment

      public Environment(LoggingLevel loggingLevel, String logId)
      Creates an Environment with the given logging level and log identifier.
      Parameters:
      loggingLevel - minimum severity for log messages.
      logId - short string identifying this environment in logs.
  • Method Details

    • buildInfo

      public static String buildInfo()
      Returns the ONNX Runtime build information string (e.g. version and commit hash).
      Returns:
      build info string.
    • availableProviders

      public static List<String> availableProviders()
      Returns the list of available execution provider names on this machine/installation (e.g. ["CPUExecutionProvider", "CUDAExecutionProvider"]).
      Returns:
      available provider names.
    • setLoggingLevel

      public void setLoggingLevel(LoggingLevel level)
      Updates the minimum logging level of this environment.
      Parameters:
      level - new minimum logging level.
    • createSession

      public InferenceSession createSession(String modelPath)
      Creates an InferenceSession that shares this environment using default session options.
      Parameters:
      modelPath - path to the .onnx model file.
      Returns:
      the loaded session.
    • createSession

      public InferenceSession createSession(byte[] modelBytes)
      Creates an InferenceSession from an in-memory model that shares this environment, using default session options.
      Parameters:
      modelBytes - the serialized ONNX model bytes.
      Returns:
      the loaded session.
    • createSession

      public InferenceSession createSession(String modelPath, SessionOptions sessionOptions)
      Creates an InferenceSession that shares this environment.
      Parameters:
      modelPath - path to the .onnx model file.
      sessionOptions - session configuration.
      Returns:
      the loaded session.
    • createSession

      public InferenceSession createSession(byte[] modelBytes, SessionOptions sessionOptions)
      Creates an InferenceSession from an in-memory model that shares this environment.
      Parameters:
      modelBytes - the serialized ONNX model bytes.
      sessionOptions - session configuration.
      Returns:
      the loaded session.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable