SessionOptions
TheSessionOptions class allows you to configure various aspects of an InferenceSession, including graph optimization, thread pool sizes, execution providers, and profiling.
Constructor
Properties
Controls the level of graph optimizations applied.
ORT_DISABLE_ALL- No optimizationsORT_ENABLE_BASIC- Basic optimizations (default)ORT_ENABLE_EXTENDED- Extended optimizationsORT_ENABLE_ALL- All optimizations including layout transformations
Number of threads used to parallelize execution within nodes. Default is 0 (use default number).
Number of threads used to parallelize execution of nodes. Default is 0 (use default number).
Controls whether operators are executed sequentially or in parallel.
ORT_SEQUENTIAL- Execute operators sequentiallyORT_PARALLEL- Execute operators in parallel when possible
Controls the order in which graph nodes are executed.
DEFAULT- Use default topological orderPRIORITY_BASED- Use priority-based scheduling
Enable profiling to collect performance data. Default is False.
Path to save the optimized model. If set, the optimized graph will be saved to this location.
Logging verbosity level (0=Verbose, 1=Info, 2=Warning, 3=Error, 4=Fatal). Default is 2.
VLOG level for verbose logging. Default is 0.
Enable memory pattern optimization. Default is True.
Enable memory reuse optimization. Default is True.
Enable CPU memory arena allocator. Default is True.
Methods
add_session_config_entry()
Add custom session configuration entry.Configuration key.
Configuration value.
session.load_model_format- Set to “ONNX” or “ORT”session.use_env_allocators- Use environment allocatorssession.record_ep_graph_assignment_info- Record EP graph assignment (“1” to enable)session.disable_prepacking- Disable weight prepacking
register_custom_ops_library()
Register a shared library containing custom operators.Path to the shared library (.so, .dll, or .dylib).
add_external_initializers()
Add external initializers to the session.Names of the initializers.
OrtValue objects containing initializer data.
add_free_dimension_override_by_name()
Override a free dimension with a specific value.Name of the dimension to override.
Value to use for the dimension.
Example Usage
Basic Configuration
Enable Profiling
Save Optimized Model
Custom Configuration
Register Custom Operators
Performance Tuning
Related APIs
- InferenceSession - Create sessions with options
- RunOptions - Per-run configuration
- Execution Providers - Hardware acceleration