Class OrtSession.SessionOptions
- All Implemented Interfaces:
AutoCloseable
- Enclosing class:
- OrtSession
Used to set the number of threads, optimisation level, computation backend and other options.
Modifying this after the session has been constructed will have no effect.
The SessionOptions object must not be closed until all sessions which use it are closed, as otherwise it could release resources that are in use.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe execution mode to use.static enumThe optimisation level to use. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddACL(boolean enableFastMath) Adds the ARM Compute Library as an execution backend.voidaddArmNN(boolean useArena) Adds the ARM Neural Net library as an execution backend.voidaddConfigEntry(String configKey, String configValue) Adds a single session configuration entry as a pair of strings.voidAdds Apple's CoreML as an execution backend.voidaddCoreML(EnumSet<CoreMLFlags> flags) Adds Apple's CoreML as an execution backend.voidAdds CoreML as an execution backend.voidaddCPU(boolean useArena) Adds the CPU as an execution backend, using the arena allocator if desired.voidaddCUDA()Add CUDA as an execution backend, using device 0.voidaddCUDA(int deviceNum) Add CUDA as an execution backend, using the specified CUDA device id.voidaddCUDA(OrtCUDAProviderOptions cudaOpts) Adds CUDA as an execution backend, using the specified CUDA options.voidaddDirectML(int deviceId) Adds DirectML as an execution backend.voidaddDnnl(boolean useArena) Adds Intel's Deep Neural Network Library as an execution backend.voidaddExternalInitializers(Map<String, OnnxTensorLike> initializers) Adds in the supplied externally loaded initializers.voidaddInitializer(String name, OnnxTensorLike initializer) Adds an initializer to override one from the ONNX model.voidaddNnapi()Adds Android's NNAPI as an execution backend.voidaddNnapi(EnumSet<NNAPIFlags> flags) Adds Android's NNAPI as an execution backend.voidaddOpenVINO(String deviceId) Adds OpenVINO as an execution backend.voidAdds QNN as an execution backend.voidaddROCM()Add ROCM as an execution backend, using device 0.voidaddROCM(int deviceNum) Add ROCM as an execution backend, using the specified ROCM device id.voidaddTensorrt(int deviceNum) Adds Nvidia's TensorRT as an execution backend.voidaddTensorrt(OrtTensorRTProviderOptions tensorRTOpts) Adds Nvidia's TensorRT as an execution backend.voidAdds TVM as an execution backend.voidAdds WebGPU as an execution backend.voidaddXnnpack(Map<String, String> providerOptions) Adds XNNPACK as an execution backend.voidclose()Closes the session options, releasing any memory acquired.voidDisables the per session thread pools.voidDisables profiling in sessions using this SessionOptions.voidenableProfiling(String filePath) Enables profiling in sessions using this SessionOptions.Returns an unmodifiable view of the map contains all session configuration entries.voidRegisters a library of custom ops for use withOrtSessions using this SessionOptions.voidregisterCustomOpsUsingFunction(String registrationFuncName) Registers custom ops for use withOrtSessions using this SessionOptions by calling the specified native function name.voidsetCPUArenaAllocator(boolean useArena) Sets the CPU to use an arena memory allocator.voidsetDeterministicCompute(boolean value) Set whether to use deterministic compute.voidSets the execution mode of this options object, overriding the old setting.voidsetInterOpNumThreads(int numThreads) Sets the size of the CPU thread pool used for executing multiple request concurrently, if executing on a CPU.voidsetIntraOpNumThreads(int numThreads) Sets the size of the CPU thread pool used for executing a single graph, if executing on a CPU.voidsetLoggerId(String loggerId) Sets the logger id to use.voidsetMemoryPatternOptimization(boolean memoryPatternOptimization) Turns on memory pattern optimizations, where memory is preallocated if all shapes are known.voidSets the optimization level of this options object, overriding the old setting.voidsetOptimizedModelFilePath(String outputPath) Sets the output path for the optimized model.voidsetSessionLogLevel(OrtLoggingLevel logLevel) Sets the Session's logging level.voidsetSessionLogVerbosityLevel(int logLevel) Sets the Session's logging verbosity level.voidsetSymbolicDimensionValue(String dimensionName, long dimensionValue) Sets the value of a symbolic dimension.
-
Constructor Details
-
SessionOptions
public SessionOptions()Create an empty session options.
-
-
Method Details
-
close
public void close()Closes the session options, releasing any memory acquired.- Specified by:
closein interfaceAutoCloseable
-
setExecutionMode
Sets the execution mode of this options object, overriding the old setting.- Parameters:
mode- The execution mode to use.- Throws:
OrtException- If there was an error in native code.
-
setOptimizationLevel
Sets the optimization level of this options object, overriding the old setting.- Parameters:
level- The optimization level to use.- Throws:
OrtException- If there was an error in native code.
-
setInterOpNumThreads
Sets the size of the CPU thread pool used for executing multiple request concurrently, if executing on a CPU.- Parameters:
numThreads- The number of threads to use.- Throws:
OrtException- If there was an error in native code.
-
setIntraOpNumThreads
Sets the size of the CPU thread pool used for executing a single graph, if executing on a CPU.- Parameters:
numThreads- The number of threads to use.- Throws:
OrtException- If there was an error in native code.
-
setOptimizedModelFilePath
Sets the output path for the optimized model.- Parameters:
outputPath- The output path to write the model to.- Throws:
OrtException- If there was an error in native code.
-
setLoggerId
Sets the logger id to use.- Parameters:
loggerId- The logger id string.- Throws:
OrtException- If there was an error in native code.
-
enableProfiling
Enables profiling in sessions using this SessionOptions.- Parameters:
filePath- The file to write profile information to.- Throws:
OrtException- If there was an error in native code.
-
disableProfiling
Disables profiling in sessions using this SessionOptions.- Throws:
OrtException- If there was an error in native code.
-
setMemoryPatternOptimization
Turns on memory pattern optimizations, where memory is preallocated if all shapes are known.- Parameters:
memoryPatternOptimization- If true enable memory pattern optimizations.- Throws:
OrtException- If there was an error in native code.
-
setCPUArenaAllocator
Sets the CPU to use an arena memory allocator.- Parameters:
useArena- If true use an arena memory allocator for the CPU execution provider.- Throws:
OrtException- If there was an error in native code.
-
setSessionLogLevel
Sets the Session's logging level.- Parameters:
logLevel- The log level to use.- Throws:
OrtException- If there was an error in native code.
-
setSessionLogVerbosityLevel
Sets the Session's logging verbosity level.- Parameters:
logLevel- The logging verbosity to use.- Throws:
OrtException- If there was an error in native code.
-
registerCustomOpLibrary
Registers a library of custom ops for use withOrtSessions using this SessionOptions.- Parameters:
path- The path to the library on disk.- Throws:
OrtException- If there was an error loading the library.
-
registerCustomOpsUsingFunction
Registers custom ops for use withOrtSessions using this SessionOptions by calling the specified native function name. The custom ops library must either be linked against, or have previously been loaded by the user.The registration function must have the signature:
OrtStatus* (*fn)(OrtSessionOptions* options, const OrtApiBase* api);
See https://onnxruntime.ai/docs/reference/operators/add-custom-op.html for more information on custom ops. See https://github.com/microsoft/onnxruntime/blob/342a5bf2b756d1a1fc6fdc582cfeac15182632fe/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc#L115 for an example of a custom op library registration function.
- Parameters:
registrationFuncName- The name of the registration function to call.- Throws:
OrtException- If there was an error finding or calling the registration function.
-
setSymbolicDimensionValue
public void setSymbolicDimensionValue(String dimensionName, long dimensionValue) throws OrtException Sets the value of a symbolic dimension. Fixed dimension computations may have more optimizations applied to them.- Parameters:
dimensionName- The name of the symbolic dimension.dimensionValue- The value to set that dimension to.- Throws:
OrtException- If there was an error in native code.
-
setDeterministicCompute
Set whether to use deterministic compute.Default is false. If set to true, this will enable deterministic compute for GPU kernels where possible. Note that this most likely will have a performance cost.
- Parameters:
value- Should the compute be deterministic?- Throws:
OrtException- If there was an error in native code.
-
disablePerSessionThreads
Disables the per session thread pools. Must be used in conjunction with an environment containing global thread pools.- Throws:
OrtException- If there was an error in native code.
-
addConfigEntry
Adds a single session configuration entry as a pair of strings.- Parameters:
configKey- The config key string.configValue- The config value string.- Throws:
OrtException- If there was an error in native code.
-
getConfigEntries
Returns an unmodifiable view of the map contains all session configuration entries.- Returns:
- All session configuration entries
-
addExternalInitializers
Adds in the supplied externally loaded initializers.Note the initializers are copied into the session once it has been created, and the native references are removed from this
SessionOptions. Once the session has been created those initializers can be closed. This is a different lifetime to initializers added viaaddInitializer(String, OnnxTensorLike). The initializers must be created fromBufferobjects.- Parameters:
initializers- The map of names to initializers.- Throws:
OrtException- If the initializers could not be loaded.
-
addInitializer
Adds an initializer to override one from the ONNX model.Note the initializer lifetime must outlive the session and session options. This is a different lifetime to initializers added via
addExternalInitializers(Map). The initializers must be created fromBufferobjects.- Parameters:
name- The initializer name.initializer- The initializer value.- Throws:
OrtException- If the initializer could not be loaded into the session options.
-
addCUDA
Add CUDA as an execution backend, using device 0.- Throws:
OrtException- If there was an error in native code.
-
addCUDA
Add CUDA as an execution backend, using the specified CUDA device id.- Parameters:
deviceNum- The CUDA device id.- Throws:
OrtException- If there was an error in native code.
-
addCUDA
Adds CUDA as an execution backend, using the specified CUDA options.- Parameters:
cudaOpts- The CUDA execution provider options.- Throws:
OrtException- If there was an error in the native code.
-
addROCM
Add ROCM as an execution backend, using device 0.- Throws:
OrtException- If there was an error in native code.
-
addROCM
Add ROCM as an execution backend, using the specified ROCM device id.- Parameters:
deviceNum- The ROCM device id.- Throws:
OrtException- If there was an error in native code.
-
addCPU
Adds the CPU as an execution backend, using the arena allocator if desired.By default this backend is used, but if other backends are requested, it should be requested last.
- Parameters:
useArena- If true use the arena memory allocator.- Throws:
OrtException- If there was an error in native code.
-
addDnnl
Adds Intel's Deep Neural Network Library as an execution backend.- Parameters:
useArena- If true use the arena memory allocator.- Throws:
OrtException- If there was an error in native code.
-
addOpenVINO
Adds OpenVINO as an execution backend.- Parameters:
deviceId- The id of the OpenVINO execution device.- Throws:
OrtException- If there was an error in native code.
-
addTensorrt
Adds Nvidia's TensorRT as an execution backend.- Parameters:
deviceNum- The id of the CUDA device.- Throws:
OrtException- If there was an error in native code.
-
addTensorrt
Adds Nvidia's TensorRT as an execution backend.- Parameters:
tensorRTOpts- The configuration parameters for TensorRT.- Throws:
OrtException- If there was an error in native code.
-
addNnapi
Adds Android's NNAPI as an execution backend. Uses the default empty flag.- Throws:
OrtException- If there was an error in native code.
-
addNnapi
Adds Android's NNAPI as an execution backend.- Parameters:
flags- The flags which control the NNAPI configuration.- Throws:
OrtException- If there was an error in native code.
-
addTvm
Adds TVM as an execution backend.- Parameters:
settings- See the documentation for valid settings strings.- Throws:
OrtException- If there was an error in native code.
-
addDirectML
Adds DirectML as an execution backend.- Parameters:
deviceId- The id of the DirectML device.- Throws:
OrtException- If there was an error in native code.
-
addACL
Adds the ARM Compute Library as an execution backend.- Parameters:
enableFastMath- Enable fast math mode in ACL.- Throws:
OrtException- If there was an error in native code.
-
addArmNN
Adds the ARM Neural Net library as an execution backend.- Parameters:
useArena- If true use the arena memory allocator.- Throws:
OrtException- If there was an error in native code.
-
addCoreML
Adds Apple's CoreML as an execution backend. Uses the default empty flag.- Throws:
OrtException- If there was an error in native code.
-
addCoreML
Adds Apple's CoreML as an execution backend.- Parameters:
flags- The flags which control the CoreML configuration.- Throws:
OrtException- If there was an error in native code.
-
addXnnpack
Adds XNNPACK as an execution backend.- Parameters:
providerOptions- Configuration options for the XNNPACK backend. Refer to the XNNPACK execution provider's documentation.- Throws:
OrtException- If there was an error in native code.
-
addQnn
Adds QNN as an execution backend.- Parameters:
providerOptions- Configuration options for the QNN backend. Refer to the QNN execution provider's documentation.- Throws:
OrtException- If there was an error in native code.
-
addCoreML
Adds CoreML as an execution backend.- Parameters:
providerOptions- Configuration options for the CoreML backend. Refer to the CoreML execution provider's documentation.- Throws:
OrtException- If there was an error in native code.
-
addWebGPU
Adds WebGPU as an execution backend.- Parameters:
providerOptions- Configuration options for the WebGPU backend. Refer to the WebGPU execution provider's documentation.- Throws:
OrtException- If there was an error in native code.
-