Skip to main content

Session Management

The OrtSession object represents a loaded ONNX model and provides inference capabilities.

Creating Sessions

CreateSession

Create a session from a model file. Parameters:
  • env: Environment object
  • model_path: Path to the ONNX model file (wchar_t* on Windows, char* elsewhere)
  • options: Session options (configuration)
  • out: Newly created session (must be freed with ReleaseSession)
Returns: NULL on success, error status otherwise Example:

CreateSessionFromArray

Create a session from an in-memory model. Parameters:
  • env: Environment object
  • model_data: Pointer to model data in memory
  • model_data_length: Size of model data in bytes
  • options: Session options
  • out: Newly created session
Example:

CreateSessionWithPrepackedWeightsContainer

Create a session that shares pre-packed weights between multiple sessions for memory efficiency. Parameters:
  • env: Environment object
  • model_path: Path to model file
  • options: Session options
  • prepacked_weights_container: Container for sharing pre-packed weights
  • out: Newly created session
Example:

ReleaseSession

Free a session object. Parameters:
  • session: Session to free (can be NULL)
Warning: Do not call from DllMain on Windows as the session owns a thread pool.

Querying Session Information

SessionGetInputCount

Get the number of inputs required by the model. Parameters:
  • session: Session to query
  • out: Number of inputs
Example:

SessionGetOutputCount

Get the number of outputs produced by the model. Parameters:
  • session: Session to query
  • out: Number of outputs

SessionGetInputName

Get the name of an input. Parameters:
  • session: Session to query
  • index: Input index (0 to num_inputs - 1)
  • allocator: Allocator to use for the returned string
  • value: UTF-8 null-terminated input name (must be freed using allocator)
Example:

SessionGetOutputName

Get the name of an output. Parameters:
  • session: Session to query
  • index: Output index (0 to num_outputs - 1)
  • allocator: Allocator to use for the returned string
  • value: UTF-8 null-terminated output name (must be freed using allocator)

SessionGetInputTypeInfo

Get type information for an input. Parameters:
  • session: Session to query
  • index: Input index
  • type_info: Type information (must be freed with ReleaseTypeInfo)
Example:

SessionGetOutputTypeInfo

Get type information for an output.

SessionGetOverridableInitializerCount

Get the count of overridable initializers in the model. Parameters:
  • session: Session to query
  • out: Number of overridable initializers

Running Inference

Run

Run the model with the given inputs. Parameters:
  • session: Session to run
  • run_options: Run options (can be NULL for defaults)
  • input_names: Array of null-terminated UTF-8 input names
  • inputs: Array of input OrtValue objects
  • input_len: Number of inputs
  • output_names: Array of null-terminated UTF-8 output names
  • output_names_len: Number of outputs
  • outputs: Array to receive output OrtValue objects (can be pre-allocated or NULL)
Returns: NULL on success, error status otherwise Example:

RunAsync (Callback-based)

For asynchronous inference, use the callback-based API:

Run Options

CreateRunOptions

Create run options for per-run configuration. Parameters:
  • out: Newly created run options (must be freed with ReleaseRunOptions)

RunOptionsSetRunLogVerbosityLevel

Set per-run log verbosity level.

RunOptionsSetRunLogSeverityLevel

Set per-run log severity level.

RunOptionsSetRunTag

Set a tag for the run (used in logging).

RunOptionsSetTerminate

Set a flag to terminate a currently executing session from another thread.

RunOptionsUnsetTerminate

Clear the terminate flag so the run options can be reused.

AddRunConfigEntry

Add a run configuration entry. See onnxruntime_run_options_config_keys.h for valid keys.

ReleaseRunOptions

Free run options.

IO Binding (Advanced)

IO Binding allows binding pre-allocated memory for inputs and outputs to avoid copies.

CreateIoBinding

Create an IO binding for a session. Parameters:
  • session: Session to create binding for
  • out: Newly created IO binding (must be freed with ReleaseIoBinding)

BindInput

Bind an input value. Parameters:
  • binding_ptr: IO binding
  • name: Input name
  • val_ptr: Input tensor value

BindOutput

Bind an output value.

BindOutputToDevice

Bind an output to a device for dynamic shapes. Parameters:
  • binding_ptr: IO binding
  • name: Output name
  • mem_info_ptr: Memory location for output allocation

RunWithBinding

Run inference using IO binding. Example:

ClearBoundInputs

Clear all bound inputs.

ClearBoundOutputs

Clear all bound outputs.

ReleaseIoBinding

Free an IO binding.

Model Metadata

SessionGetModelMetadata

Get model metadata. Parameters:
  • session: Session to query
  • out: Model metadata (must be freed with ReleaseModelMetadata)

ModelMetadataGetProducerName

Get the producer name from model metadata.

ModelMetadataGetGraphName

Get the graph name.

ModelMetadataGetVersion

Get the model version number.

ModelMetadataLookupCustomMetadataMap

Lookup a value in the custom metadata map. Parameters:
  • model_metadata: Metadata object
  • allocator: Allocator for returned string
  • key: Metadata key to lookup
  • value: Retrieved value (NULL if key not found, must be freed with allocator)

ReleaseModelMetadata

Free model metadata.

Profiling

SessionEndProfiling

End profiling and get the profile data filename. Parameters:
  • session: Session being profiled
  • allocator: Allocator for returned string
  • out: Filename where profile data was written (must be freed with allocator)
Note: Profiling must be enabled via EnableProfiling in session options.

SessionGetProfilingStartTimeNs

Get the profiling start time in nanoseconds.

See Also