SessionOptions
TheSessionOptions class allows you to configure various aspects of an InferenceSession, including graph optimization, thread pool sizes, execution providers, and profiling.
Constructor
Properties
GraphOptimizationLevel
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
int
Number of threads used to parallelize execution within nodes. Default is 0 (use default number).
int
Number of threads used to parallelize execution of nodes. Default is 0 (use default number).
ExecutionMode
Controls whether operators are executed sequentially or in parallel.
ORT_SEQUENTIAL- Execute operators sequentiallyORT_PARALLEL- Execute operators in parallel when possible
ExecutionOrder
Controls the order in which graph nodes are executed.
DEFAULT- Use default topological orderPRIORITY_BASED- Use priority-based scheduling
bool
Enable profiling to collect performance data. Default is False.
str
Path to save the optimized model. If set, the optimized graph will be saved to this location.
int
Logging verbosity level (0=Verbose, 1=Info, 2=Warning, 3=Error, 4=Fatal). Default is 2.
int
VLOG level for verbose logging. Default is 0.
bool
Enable memory pattern optimization. Default is True.
bool
Enable memory reuse optimization. Default is True.
bool
Enable CPU memory arena allocator. Default is True.
Methods
add_session_config_entry()
Add custom session configuration entry.str
required
Configuration key.
str
required
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.str
required
Path to the shared library (.so, .dll, or .dylib).
add_external_initializers()
Add external initializers to the session.list[str]
required
Names of the initializers.
list[OrtValue]
required
OrtValue objects containing initializer data.
add_free_dimension_override_by_name()
Override a free dimension with a specific value.str
required
Name of the dimension to override.
int
required
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