SessionOptions class provides configuration options for creating and running inference sessions.
Namespace
Class Declaration
Constructor
SessionOptions()
Creates a new SessionOptions instance with default settings.Static Factory Methods
MakeSessionOptionWithCudaProvider
Creates SessionOptions configured for CUDA execution.deviceId(int): CUDA device ID (default: 0)
MakeSessionOptionWithCudaProvider (Advanced)
Creates SessionOptions with detailed CUDA provider options.MakeSessionOptionWithTensorrtProvider
Creates SessionOptions configured for TensorRT execution.Properties and Methods
Graph Optimization Level
Controls the level of graph optimizations applied.ORT_DISABLE_ALL- No optimizationsORT_ENABLE_BASIC- Basic optimizations (constant folding, redundant node elimination)ORT_ENABLE_EXTENDED- Extended optimizations (operator fusion)ORT_ENABLE_LAYOUT- Layout transformationsORT_ENABLE_ALL- All available optimizations
Execution Mode
Controls sequential vs parallel execution.ORT_SEQUENTIAL- Execute operators sequentiallyORT_PARALLEL- Execute operators in parallel when possible
Thread Configuration
IntraOpNumThreads
Sets the number of threads used to parallelize execution within nodes.InterOpNumThreads
Sets the number of threads used to parallelize execution between nodes.Memory Optimization
EnableCpuMemArena
Enables the CPU memory arena allocator.EnableMemPattern
Enables memory pattern optimization.Profiling
EnableProfiling
Enables profiling for the session.ProfileOutputPathPrefix
Sets the output path prefix for profiling data.Logging
LogId
Sets the logger ID for the session.LogSeverityLevel
Sets the minimum log severity level.Optimized Model Output
OptimizedModelFilePath
Path to save the optimized model.Execution Provider Configuration
AppendExecutionProvider_CPU
Adds CPU execution provider.AppendExecutionProvider_CUDA
Adds CUDA execution provider.AppendExecutionProvider_CUDA (Advanced)
Adds CUDA provider with detailed options.AppendExecutionProvider_DML
Adds DirectML execution provider (Windows only).AppendExecutionProvider_TensorRT
Adds TensorRT execution provider.AppendExecutionProvider_OpenVINO
Adds OpenVINO execution provider.Advanced Configuration
AddConfigEntry
Adds a configuration entry.AddSessionConfigEntry
Adds a session configuration entry.AddInitializer
Adds an initializer to the session.RegisterCustomOpLibrary
Registers a custom operator library.Complete Examples
Basic Configuration
GPU Acceleration (CUDA)
Production Configuration
Model Optimization and Export
Custom Operators
Advanced CUDA Configuration
Performance Tuning Guide
CPU Optimization
GPU Optimization
Memory-Constrained Environments
Best Practices
- Set appropriate optimization level: Use
ORT_ENABLE_ALLfor production - Configure threading: Match your workload and hardware
- Enable memory optimizations: Unless memory-constrained
- Use GPU when available: Significant performance gains
- Profile your model: Enable profiling to identify bottlenecks
- Reuse SessionOptions: Create once, use for multiple sessions
- Always dispose: SessionOptions implements IDisposable