Session Management
TheOrtSession object represents a loaded ONNX model and provides inference capabilities.
Creating Sessions
CreateSession
env: Environment objectmodel_path: Path to the ONNX model file (wchar_t*on Windows,char*elsewhere)options: Session options (configuration)out: Newly created session (must be freed withReleaseSession)
NULL on success, error status otherwise
Example:
CreateSessionFromArray
env: Environment objectmodel_data: Pointer to model data in memorymodel_data_length: Size of model data in bytesoptions: Session optionsout: Newly created session
CreateSessionWithPrepackedWeightsContainer
env: Environment objectmodel_path: Path to model fileoptions: Session optionsprepacked_weights_container: Container for sharing pre-packed weightsout: Newly created session
ReleaseSession
session: Session to free (can be NULL)
DllMain on Windows as the session owns a thread pool.
Querying Session Information
SessionGetInputCount
session: Session to queryout: Number of inputs
SessionGetOutputCount
session: Session to queryout: Number of outputs
SessionGetInputName
session: Session to queryindex: Input index (0 tonum_inputs - 1)allocator: Allocator to use for the returned stringvalue: UTF-8 null-terminated input name (must be freed usingallocator)
SessionGetOutputName
session: Session to queryindex: Output index (0 tonum_outputs - 1)allocator: Allocator to use for the returned stringvalue: UTF-8 null-terminated output name (must be freed usingallocator)
SessionGetInputTypeInfo
session: Session to queryindex: Input indextype_info: Type information (must be freed withReleaseTypeInfo)
SessionGetOutputTypeInfo
SessionGetOverridableInitializerCount
session: Session to queryout: Number of overridable initializers
Running Inference
Run
session: Session to runrun_options: Run options (can be NULL for defaults)input_names: Array of null-terminated UTF-8 input namesinputs: Array of inputOrtValueobjectsinput_len: Number of inputsoutput_names: Array of null-terminated UTF-8 output namesoutput_names_len: Number of outputsoutputs: Array to receive outputOrtValueobjects (can be pre-allocated or NULL)
NULL on success, error status otherwise
Example:
RunAsync (Callback-based)
For asynchronous inference, use the callback-based API:Run Options
CreateRunOptions
out: Newly created run options (must be freed withReleaseRunOptions)
RunOptionsSetRunLogVerbosityLevel
RunOptionsSetRunLogSeverityLevel
RunOptionsSetRunTag
RunOptionsSetTerminate
RunOptionsUnsetTerminate
AddRunConfigEntry
onnxruntime_run_options_config_keys.h for valid keys.
ReleaseRunOptions
IO Binding (Advanced)
IO Binding allows binding pre-allocated memory for inputs and outputs to avoid copies.CreateIoBinding
session: Session to create binding forout: Newly created IO binding (must be freed withReleaseIoBinding)
BindInput
binding_ptr: IO bindingname: Input nameval_ptr: Input tensor value
BindOutput
BindOutputToDevice
binding_ptr: IO bindingname: Output namemem_info_ptr: Memory location for output allocation
RunWithBinding
ClearBoundInputs
ClearBoundOutputs
ReleaseIoBinding
Model Metadata
SessionGetModelMetadata
session: Session to queryout: Model metadata (must be freed withReleaseModelMetadata)
ModelMetadataGetProducerName
ModelMetadataGetGraphName
ModelMetadataGetVersion
ModelMetadataLookupCustomMetadataMap
model_metadata: Metadata objectallocator: Allocator for returned stringkey: Metadata key to lookupvalue: Retrieved value (NULL if key not found, must be freed withallocator)
ReleaseModelMetadata
Profiling
SessionEndProfiling
session: Session being profiledallocator: Allocator for returned stringout: Filename where profile data was written (must be freed withallocator)
EnableProfiling in session options.