### Get Provider Options Source: https://onnxruntime.ai/docs/api/python/api_summary.html Retrieve the configurations for registered execution providers. ```python get_provider_options() ``` -------------------------------- ### Training Loop Source: https://onnxruntime.ai/docs/api/python/on_device_training/training_api.html Example of a typical training loop using the onnxruntime training API. ```APIDOC ## Training Loop Example ### Description Demonstrates a basic training loop, including setting the module to train mode, performing a training step, updating the optimizer, and resetting gradients. ### Code ```python # Assuming module and optimizer are already initialized # Training loop for ...: module.train() training_loss = module(...) optimizer.step() module.lazy_reset_grad() # Evaluation after training module.eval() eval_loss = module(...) ``` ``` -------------------------------- ### Build and Version Information Source: https://onnxruntime.ai/docs/api/python/api_summary.html Functions to get build information and version string. ```APIDOC ## onnxruntime.get_build_info ### Description Returns build information for the ONNX Runtime library. ### Method `onnxruntime.get_build_info() -> str` ``` ```APIDOC ## onnxruntime.get_version_string ### Description Returns the version string of the ONNX Runtime library. ### Method `onnxruntime.get_version_string() -> str` ``` ```APIDOC ## onnxruntime.has_collective_ops ### Description Checks if the ONNX Runtime build includes support for collective operations. ### Method `onnxruntime.has_collective_ops() -> bool` ``` -------------------------------- ### Get Profiling Start Time Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Retrieves the start time of the profiling session in nanoseconds. This can be used for performance analysis and timing comparisons. ```python return self._sess.get_profiling_start_time_ns ``` -------------------------------- ### Get Profiling Start Time Source: https://onnxruntime.ai/docs/api/python/api_summary.html Return the nanoseconds of the profiling start time. This is comparable to time.monotonic_ns() after Python 3.3. Note that on some platforms, like Windows and MacOS, the precision may be around 100ns. ```python get_profiling_start_time_ns() ``` -------------------------------- ### Get Session Options Source: https://onnxruntime.ai/docs/api/python/api_summary.html Return the session options. Refer to onnxruntime.SessionOptions for more details. ```python get_session_options() ``` -------------------------------- ### Device and Logging Source: https://onnxruntime.ai/docs/api/python/api_summary.html APIs for getting the current device and configuring logger severity and verbosity. ```APIDOC ## Backend ### Device #### `get_device()` #### Description Retrieves the current device being used by ONNX Runtime. ### Logging #### `set_default_logger_severity()` #### Description Sets the default severity level for ONNX Runtime logs. #### `set_default_logger_verbosity()` #### Description Sets the default verbosity level for ONNX Runtime logs. ``` -------------------------------- ### get_profiling_start_time_ns Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Retrieves the start time of the profiling session in nanoseconds. ```APIDOC ## get_profiling_start_time_ns ### Description Returns the nanoseconds of profiling's start time. This can be compared to `time.monotonic_ns()` (available in Python 3.3+). Note that precision may vary by platform (e.g., ~100ns on Windows and macOS). ### Method `get_profiling_start_time_ns()` ### Response - **start_time_ns** (int): The start time of profiling in nanoseconds. ``` -------------------------------- ### Example ForwardBlock Implementation Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/onnxblock/onnxblock.html Subclass ForwardBlock to automatically build a forward model by stacking blocks. The build method should return the name of the graph output. ```python class MyForwardBlock(ForwardBlock): def __init__(self): super().__init__() self.loss = onnxblock.loss.CrossEntropyLoss() def build(self, loss_input_name: str): # Add a cross entropy loss on top of the output so far (loss_input_name) return self.loss(loss_input_name) ``` -------------------------------- ### Example TrainingBlock Implementation Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/onnxblock/onnxblock.html Subclass TrainingBlock to automatically build a gradient model. The build method should return the name of the output from where backpropagation must begin, typically the loss function's output. ```python class MyTrainingBlock(TrainingBlock): def __init__(self): super().__init__() self.loss = onnxblock.loss.CrossEntropyLoss() def build(self, loss_input_name: str): # Add a cross entropy loss on top of the output so far (loss_input_name) return self.loss(loss_input_name) ``` -------------------------------- ### Profiling Information Source: https://onnxruntime.ai/docs/api/python/api_summary.html Methods for retrieving profiling start time. ```APIDOC ## Profiling Methods ### `get_profiling_start_time_ns()` Returns the nanoseconds of the profiling's start time. This is comparable to `time.monotonic_ns()` after Python 3.3. On some platforms, this timer may not be as precise as nanoseconds; for instance, on Windows and macOS, the precision will be approximately 100ns. ``` -------------------------------- ### Get Registered Providers Source: https://onnxruntime.ai/docs/api/python/api_summary.html Return a list of registered execution providers. ```python get_providers() ``` -------------------------------- ### Manage Properties in Checkpoint State Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/checkpoint_state.html Demonstrates adding, accessing, checking for existence, iterating, and getting the count of properties within the CheckpointState. ```python properties["learning_rate"] = 0.01 print(properties["learning_rate"]) print("learning_rate" in properties) for name, value in properties: print(f"{name}: {value}") print(len(properties)) ``` -------------------------------- ### Import ONNX Runtime Training Artifacts Modules Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/artifacts.html Imports necessary modules from onnxruntime.training and related tools for model conversion and optimization. Ensure these libraries are installed. ```python # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import contextlib import logging import os import pathlib from enum import Enum from typing import List, Optional, Union import onnx from onnxruntime.tools.convert_onnx_models_to_ort import OptimizationStyle, convert_onnx_models_to_ort from onnxruntime.training import onnxblock ``` -------------------------------- ### Get Overridable Initializers Source: https://onnxruntime.ai/docs/api/python/api_summary.html Return the inputs, including initializers, as a list of onnxruntime.NodeArg objects. ```python get_overridable_initializers() ``` -------------------------------- ### Get Outputs from IOBinding Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Retrieves the output OrtValues from the preceding Run() call. Note that the data buffers may not be on CPU memory. ```python outputs = self._iobinding.get_outputs() if not isinstance(outputs, C.OrtValueVector): raise TypeError("get_outputs() must return an instance of type 'OrtValueVector'.") return [OrtValue(ortvalue) for ortvalue in outputs] ``` -------------------------------- ### IOBinding: Bind CPU Input and Output Source: https://onnxruntime.ai/docs/api/python/api_summary.html Demonstrates using IOBinding to bind a CPU input and an output for a model executed on a device. ```APIDOC ## IOBinding: Bind CPU Input and Output ### Description Uses IOBinding to bind a CPU input and an output for a model executed on a device. ### Method ```python # X is numpy array on cpu session = onnxruntime.InferenceSession( 'model.onnx', providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) ) io_binding = session.io_binding() # OnnxRuntime will copy the data over to the CUDA device if 'input' is consumed by nodes on the CUDA device io_binding.bind_cpu_input('input', X) io_binding.bind_output('output') session.run_with_iobinding(io_binding) Y = io_binding.copy_outputs_to_cpu()[0] ``` ``` -------------------------------- ### Load Checkpoint and Initialize Module for On-Device Training Source: https://onnxruntime.ai/docs/api/python/on_device_training/training_api.html Load the checkpoint state and initialize the training module with specified models and device. Ensure all required training artifacts are available. ```python from onnxruntime.training.api import CheckpointState, Module, Optimizer # Load the checkpoint state state = CheckpointState.load_checkpoint(path_to_the_checkpoint_artifact) # Create the module module = Module(path_to_the_training_model, state, path_to_the_eval_model, device="cpu") optimizer = Optimizer(path_to_the_optimizer_model, module) ``` -------------------------------- ### Properties Class Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/checkpoint_state.html The Properties class manages user-defined properties within the CheckpointState. It allows getting, setting, checking existence, iterating, and getting the count of properties. ```APIDOC ## Properties ### Description Manages user-defined properties within the CheckpointState. ### Methods #### `__getitem__(self, name: str) -> int | float | str` Gets the property value for the given name. **Args**: - `name` (str): The name of the property. **Returns**: - `int | float | str`: The value of the property. **Raises**: - `KeyError`: If the property with the given name is not found. #### `__setitem__(self, name: str, value: int | float | str) -> None` Sets the property value for the given name. Searches for the name in the properties of the checkpoint state. The value is added or updated in the properties. **Args**: - `name` (str): The name of the property. - `value` (int | float | str): The value of the property. Properties only support int, float and str values. #### `__contains__(self, name: str) -> bool` Checks if the property exists in the state. **Args**: - `name` (str): The name of the property. **Returns**: - `bool`: True if the name is a property, False otherwise. #### `__iter__(self)` Returns an iterator over the properties. #### `__repr__(self) -> str` Returns a string representation of the properties. #### `__len__(self) -> int` Returns the number of properties. ``` -------------------------------- ### Run with IOBinding Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Executes the model using a pre-configured IOBinding object. This method is efficient for scenarios where inputs and outputs are already prepared on specific devices. ```python self._sess.run_with_iobinding(iobinding._iobinding, run_options) ``` -------------------------------- ### onnxruntime.backend.prepare Source: https://onnxruntime.ai/docs/api/python/api_summary.html Loads an ONNX model and creates an InferenceSession ready to be used as a backend. ```APIDOC ## onnxruntime.backend.prepare ### Description Load the model and creates a `onnxruntime.InferenceSession` ready to be used as a backend. ### Parameters * **model** – ModelProto (returned by onnx.load), string for a filename or bytes for a serialized model * **device** – requested device for the computation, None means the default one which depends on the compilation settings * **kwargs** – see `onnxruntime.SessionOptions` ### Returns `onnxruntime.InferenceSession` ``` -------------------------------- ### Basic IO Binding with Explicit Output Buffer Source: https://onnxruntime.ai/docs/api/python/api_summary.html Demonstrates how to bind input and output data for inference when both input and output are on a device. The output buffer is explicitly provided. ```APIDOC ## Basic IO Binding with Explicit Output Buffer ### Description This example shows how to perform inference using ONNX Runtime's IO binding. The input data is converted to an OrtValue on the CUDA device, and an output OrtValue is pre-allocated on the CUDA device. The session then runs with these bindings. ### Method `session.run_with_iobinding(io_binding)` ### Endpoint N/A (Python API) ### Parameters #### Input Binding - `name` (string): Name of the input. - `device_type` (string): Type of the device (e.g., 'cuda'). - `device_id` (int): ID of the device. - `element_type` (type): Data type of the elements. - `shape` (tuple): Shape of the input tensor. - `buffer_ptr` (pointer): Pointer to the input data buffer. #### Output Binding - `name` (string): Name of the output. - `device_type` (string): Type of the device (e.g., 'cuda'). - `device_id` (int): ID of the device. - `element_type` (type): Data type of the elements. - `shape` (tuple): Shape of the output tensor. - `buffer_ptr` (pointer): Pointer to the output data buffer. ### Request Example ```python # X is numpy array on cpu X_ortvalue = onnxruntime.OrtValue.ortvalue_from_numpy(X, 'cuda', 0) Y_ortvalue = onnxruntime.OrtValue.ortvalue_from_shape_and_type([3, 2], np.float32, 'cuda', 0) # Change the shape to the actual shape of the output being bound session = onnxruntime.InferenceSession( 'model.onnx', providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) ) io_binding = session.io_binding() io_binding.bind_input( name='input', device_type=X_ortvalue.device_name(), device_id=0, element_type=np.float32, shape=X_ortvalue.shape(), buffer_ptr=X_ortvalue.data_ptr() ) io_binding.bind_output( name='output', device_type=Y_ortvalue.device_name(), device_id=0, element_type=np.float32, shape=Y_ortvalue.shape(), buffer_ptr=Y_ortvalue.data_ptr() ) session.run_with_iobinding(io_binding) ``` ### Response #### Success Response (200) Outputs are copied to CPU after inference. #### Response Example ```python Y = io_binding.copy_outputs_to_cpu()[0] ``` ``` -------------------------------- ### Initialize Optimizer Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/optimizer.html Initializes the Optimizer with the path to the optimizer model and the module to be trained. Ensure the optimizer URI and module are correctly provided. ```python from __future__ import annotations import os from typing import TYPE_CHECKING from onnxruntime.capi import _pybind_state as C if TYPE_CHECKING: from onnxruntime.training.api.module import Module class Optimizer: """Class that provides methods to update the model parameters based on the computed gradients. Args: optimizer_uri: The path to the optimizer model. model: The module to be trained. """ def __init__(self, optimizer_uri: str | os.PathLike, module: Module): self._optimizer = C.Optimizer( os.fspath(optimizer_uri), module._state._state, module._device, module._session_options ) ``` -------------------------------- ### Get Contiguous Parameters Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/module.html Retrieves contiguous parameters from the model. ```APIDOC ## Module.get_contiguous_parameters ### Description Retrieves contiguous parameters from the model. ### Parameters - **trainable_only** (bool) - Optional. If True, only returns trainable parameters. Default is False. ### Returns - **OrtValue** - An OrtValue containing the contiguous parameters. ``` -------------------------------- ### Bind Output with Device Options Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Binds an output to a specified device, with optional parameters for device type, ID, element type, shape, and buffer pointer. Defaults to CPU. ```python def bind_output( self, name, device_type="cpu", device_id=0, element_type=None, shape=None, buffer_ptr=None, ): """ ``` -------------------------------- ### Module Initialization Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/module.html Initializes the Module class with training and evaluation models, checkpoint state, and device configurations. ```APIDOC ## Module.__init__ ### Description Initializes the Module class with training and evaluation models, checkpoint state, and device configurations. ### Parameters - **train_model_uri** (os.PathLike) - The path to the training model. - **state** (CheckpointState) - The checkpoint state object. - **eval_model_uri** (os.PathLike | None) - Optional. The path to the evaluation model. - **device** (str) - The device to run the model on. Default is "cpu". - **session_options** (SessionOptions | None) - Optional. The session options to use for the model. ``` -------------------------------- ### Load and Run a Model Source: https://onnxruntime.ai/docs/api/python/api_summary.html Demonstrates how to load an ONNX model using InferenceSession and run inference with specified output names and inputs. ```APIDOC ## Load and Run a Model ### Description Loads an ONNX model and runs inference. ### Method ```python session = onnxruntime.InferenceSession('model.onnx') outputs = session.run([output names], inputs) ``` ``` -------------------------------- ### Get Training Output Names Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/module.html Retrieves the names of the outputs for the training model. ```python return self._model.output_names(self.training) ``` -------------------------------- ### Get Training Input Names Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/module.html Retrieves the names of the inputs for the training model. ```python return self._model.input_names(self.training) ``` -------------------------------- ### Enable Profiling Source: https://onnxruntime.ai/docs/api/python/api_summary.html Configure session options to enable profiling. Pass these options when creating the InferenceSession. ```python options = onnxruntime.SessionOptions() options.enable_profiling=True session = onnxruntime.InferenceSession( 'model.onnx', sess_options=options, providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) ) ``` -------------------------------- ### Get Data Type of OrtValue Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Returns the data type of the elements stored in the OrtValue. ```python ort_value.data_type() ``` -------------------------------- ### IOBinding Class Source: https://onnxruntime.ai/docs/api/python/api_summary.html The IOBinding class provides API to bind input/output to a specified device, e.g. GPU. ```APIDOC ## class onnxruntime.IOBinding(session : Session) This class provides API to bind input/output to a specified device, e.g. GPU. ### Methods #### bind_cpu_input(name , arr_on_cpu) Bind an input to array on CPU. * **name** (str) - input name * **arr_on_cpu** (numpy.ndarray) - input values as a python array on CPU #### bind_input(name , device_type, device_id, element_type, shape, buffer_ptr) Bind an input to a specified device. * **name** (str) - input name * **device_type** (str) - e.g. cpu, cuda, cann * **device_id** (int) - device id, e.g. 0 * **element_type** (int) - input element type * **shape** (list[int]) - input shape * **buffer_ptr** (int) - memory pointer to input data #### bind_ortvalue_input(name , ortvalue) Bind an input using an OrtValue instance. * **name** (str) - input name * **ortvalue** (onnxruntime.OrtValue) - OrtValue instance to bind #### bind_ortvalue_output(name , ortvalue) Bind an output using an OrtValue instance. * **name** (str) - output name * **ortvalue** (onnxruntime.OrtValue) - OrtValue instance to bind #### bind_output(name , device_type='cpu', device_id=0, element_type=None, shape=None, buffer_ptr=None) Bind an output to a specified device. * **name** (str) - output name * **device_type** (str) - e.g. cpu, cuda, cann, cpu by default * **device_id** (int) - device id, e.g. 0 * **element_type** (int) - output element type * **shape** (list[int]) - output shape * **buffer_ptr** (int) - memory pointer to output data #### copy_outputs_to_cpu() Copy output contents to CPU. * Returns: None #### get_outputs() Returns the output OrtValues from the Run() that preceded the call. The data buffer of the obtained OrtValues may not reside on CPU memory. * Returns: list[onnxruntime.OrtValue] ``` -------------------------------- ### Get Model Metadata Source: https://onnxruntime.ai/docs/api/python/api_summary.html Retrieve the model's metadata. Refer to onnxruntime.ModelMetadata for details. ```python get_modelmeta() ``` -------------------------------- ### Inference Session Initialization with Fallback Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Initializes an inference session, attempting to use specified providers and options. If an error occurs and fallback is enabled, it retries with a predefined fallback provider list. ```python try: self._create_inference_session(providers, provider_options, disabled_optimizers) except (ValueError, RuntimeError) as e: if self._enable_fallback: try: print("*************** EP Error ***************") print(f"EP Error {e} when using {providers}") print(f"Falling back to {self._fallback_providers} and retrying.") print("****************************************") self._create_inference_session(self._fallback_providers, None) # Fallback only once. self.disable_fallback() return except Exception as fallback_error: raise fallback_error from e # Fallback is disabled. Raise the original error. raise e ``` -------------------------------- ### Configure Execution Providers Source: https://onnxruntime.ai/docs/api/python/api_summary.html Load a model specifying preferred execution providers for hardware acceleration. Kernels are chosen based on provider priority. ```python session = onnxruntime.InferenceSession( model, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'] ) ``` -------------------------------- ### Get Shape of OrtValue Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Returns the shape of the data contained within the OrtValue as a list of integers. ```python ort_value.shape() ``` -------------------------------- ### Get Model Outputs Source: https://onnxruntime.ai/docs/api/python/api_summary.html Retrieve the model's output metadata as a list of onnxruntime.NodeArg objects. ```python get_outputs() ``` -------------------------------- ### Initialize IOBinding with Session Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Initializes an IOBinding object for a given ONNX Runtime session. This object is used to manage input and output binding to specific devices. ```python def __init__(self, session: Session): self._iobinding = C.SessionIOBinding(session._sess) self._numpy_obj_references = {} ``` -------------------------------- ### Get Model Inputs Source: https://onnxruntime.ai/docs/api/python/api_summary.html Retrieve the model's input metadata as a list of onnxruntime.NodeArg objects. ```python get_inputs() ``` -------------------------------- ### InferenceSession.__init__ Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Initializes an InferenceSession object to load and run an ONNX model. ```APIDOC ## InferenceSession.__init__ ### Description This is the main class constructor used to load and prepare an ONNX model for inference. ### Parameters - **path_or_bytes** (str | bytes | os.PathLike): Path to the ONNX model file or the model bytes. - **sess_options** (:class:`onnxruntime.SessionOptions` | None): Optional session options to configure the inference runtime. - **providers** (Sequence[str | tuple[str, dict[Any, Any]]] | None): Optional sequence of execution providers in decreasing order of preference. - **provider_options** (Sequence[dict[Any, Any]] | None): Optional dictionary of options for each provider. ``` -------------------------------- ### Get Sparse Tensor Data Type Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Returns the data type of the elements within the OrtValue as a string. ```python self._tensor.data_type() ``` -------------------------------- ### Load Model with Execution Providers Source: https://onnxruntime.ai/docs/api/python/api_summary.html Shows how to load an ONNX model and specify execution providers for hardware acceleration, such as CUDA and CPU. ```APIDOC ## Load Model with Execution Providers ### Description Loads an ONNX model and specifies execution providers for hardware acceleration. ### Method ```python session = onnxruntime.InferenceSession( model, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'] ) ``` ``` -------------------------------- ### Initialize InferenceSession with SessionOptions Source: https://onnxruntime.ai/docs/api/python/api_summary.html Initializes an ONNX Runtime InferenceSession. Session options can be configured to control model behavior, provider selection, and other inference parameters. ```python so = onnxruntime.SessionOptions() ``` -------------------------------- ### Get Sparse Tensor Format Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Returns the OrtSparseFormat enumeration value representing the format of the sparse tensor. ```python self._tensor.format ``` -------------------------------- ### Get Sparse Tensor Dense Shape Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Retrieves the dense shape of the sparse tensor as a numpy array of int64. ```python self._tensor.dense_shape() ``` -------------------------------- ### Build and Version Information Source: https://onnxruntime.ai/docs/api/python/api_summary.html Functions to retrieve build-related information and version strings for ONNX Runtime. ```APIDOC ## Backend ### Build, Version #### `get_build_info()` #### Description Retrieves information about the ONNX Runtime build. #### `get_version_string()` #### Description Retrieves the version string of the ONNX Runtime. #### `has_collective_ops()` #### Description Checks if the current build has collective operations support. ``` -------------------------------- ### Get Tuning Results Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Retrieves the results of model tuning operations. This can be used to apply previously computed optimizations. ```python return self._sess.get_tuning_results() ``` -------------------------------- ### Get Learning Rate Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/optimizer.html Retrieves the current learning rate of the optimizer. This is useful for monitoring or logging the training progress. ```python def get_learning_rate(self) -> float: """Gets the current learning rate of the optimizer. Returns: The current learning rate. """ return self._optimizer.get_learning_rate() ``` -------------------------------- ### Device Type Retrieval Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Function to get the ONNX Runtime device type object based on a string name and index. ```APIDOC ## get_ort_device_type ### Description Retrieves the ONNX Runtime device type object based on a string name and index. ### Parameters #### device_type - **Type**: str - **Description**: The name of the device type (e.g., "cuda", "cpu", "dml"). #### device_index - **Type**: int - **Description**: The index of the device. ### Returns - **Type**: C.OrtDevice - **Description**: The ONNX Runtime device object. ### Raises - **Exception**: If the provided device_type is unsupported. ``` -------------------------------- ### Create IOBinding Object Source: https://onnxruntime.ai/docs/api/python/api_summary.html Return an onnxruntime.IOBinding object for managing input and output bindings. ```python io_binding() ``` -------------------------------- ### CheckpointState Source: https://onnxruntime.ai/docs/api/python/on_device_training/training_api.html Provides functionality to load the checkpoint state for training. ```APIDOC ## CheckpointState.load_checkpoint ### Description Loads the checkpoint state from a specified path. ### Method `CheckpointState.load_checkpoint(path_to_the_checkpoint_artifact)` ### Parameters #### Path Parameters - **path_to_the_checkpoint_artifact** (string) - Required - The file path to the checkpoint artifact. ``` -------------------------------- ### Run Session with OrtValue Input Source: https://onnxruntime.ai/docs/api/python/api_summary.html Shows how to use an OrtValue as input to the session.run() method. ```APIDOC ## Run Session with OrtValue Input ### Description Uses an OrtValue as input to the session.run() method. ### Method ```python # ortvalue can be provided as part of the input feed to a model session = onnxruntime.InferenceSession( 'model.onnx', providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) ) results = session.run(["Y"], {"X": ortvalue}) ``` ``` -------------------------------- ### Get Element Type of OrtValue Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Returns the protocol buffer type of the data if the OrtValue holds a tensor. This is distinct from the numpy data type. ```python ort_value.element_type() ``` -------------------------------- ### run_with_iobinding Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Executes the inference using a pre-configured IOBinding object. ```APIDOC ## run_with_iobinding ### Description Computes the predictions using an `IOBinding` object that has graph inputs/outputs already bound. ### Method `run_with_iobinding(iobinding, run_options=None)` ### Parameters - **iobinding** (:class:`onnxruntime.IOBinding`): The IOBinding object with bound inputs and outputs. - **run_options** (:class:`onnxruntime.RunOptions`): Options for the run. ### Request Example ```python # Assuming io_binding_obj is a configured IOBinding object session.run_with_iobinding(io_binding_obj) ``` ``` -------------------------------- ### Get Run Configuration Entry Source: https://onnxruntime.ai/docs/api/python/api_summary.html Retrieve a single run configuration value using its key. This allows inspecting specific run-time settings. ```python get_run_config_entry(_self : onnxruntime.capi.onnxruntime_pybind11_state.RunOptions_, _arg0 : str_) -> str# ``` -------------------------------- ### Get Data Pointer of OrtValue Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Returns the memory address of the first element in the OrtValue's data buffer. Useful for low-level memory access. ```python ort_value.data_ptr() ``` -------------------------------- ### Create IOBinding Object Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Returns an IOBinding object, which is used to explicitly manage input and output binding to devices. This is useful for fine-grained control over data placement. ```python return IOBinding(self) ``` -------------------------------- ### Enable Path to Fetches Execution Source: https://onnxruntime.ai/docs/api/python/api_summary.html Configure the session to only execute nodes that are necessary to produce the requested fetch list. This can improve performance by avoiding unnecessary computations. ```python _property _only_execute_path_to_fetches# ``` -------------------------------- ### Get Sparse Tensor Device Name Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Returns the name of the device where the sparse tensor's data buffers are located, converted to lowercase (e.g., 'cpu', 'cuda'). ```python self._tensor.device_name().lower() ``` -------------------------------- ### Binding Outputs with PyTorch Tensors Source: https://onnxruntime.ai/docs/api/python/api_summary.html Demonstrates how to allocate a PyTorch tensor for model output and bind it using `bind_output` for GPU execution. ```APIDOC ## Allocate the PyTorch tensor for the model output Y_shape = ... # You need to specify the output PyTorch tensor shape Y_tensor = torch.empty(Y_shape, dtype=torch.float32, device='cuda:0').contiguous() binding.bind_output( name='Y', device_type='cuda', device_id=0, element_type=np.float32, shape=tuple(Y_tensor.shape), buffer_ptr=Y_tensor.data_ptr(), ) session.run_with_iobinding(binding) ``` -------------------------------- ### Get SparseTensor values Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Retrieve the values of a SparseTensor as a NumPy array. If the data type is numeric, the array is backed by native memory. For strings or objects, it returns a copy. ```python sparse_tensor.values() ``` -------------------------------- ### io_binding Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Creates and returns an IOBinding object for managing input and output binding. ```APIDOC ## io_binding ### Description Returns an `onnxruntime.IOBinding` object, which can be used to bind inputs and outputs to specific devices or memory locations. ### Method `io_binding()` ### Response - **io_binding_object** (:class:`onnxruntime.IOBinding`): An instance of the IOBinding class. ``` -------------------------------- ### Get Device Name of OrtValue Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Retrieves the name of the device where the OrtValue's data buffer is located (e.g., 'cpu', 'cuda', 'cann'). The name is returned in lowercase. ```python ort_value.device_name() ``` -------------------------------- ### onnxruntime.RunOptions Source: https://onnxruntime.ai/docs/api/python/api_summary.html Configuration information for a single Run invocation. ```APIDOC ## onnxruntime.RunOptions ### Description Configuration information for a single Run invocation. ### Methods - **add_run_config_entry(arg0: str, arg1: str) -> None**: Set a single run configuration entry as a pair of strings. - **get_run_config_entry(arg0: str) -> str**: Get a single run configuration value using the given configuration key. ### Properties - **log_severity_level** (int): Log severity level for a particular Run() invocation. 0: Verbose, 1: Info, 2: Warning, 3: Error, 4: Fatal. Default is 2. - **log_verbosity_level** (int): VLOG level if DEBUG build and run_log_severity_level is 0. Applies to a particular Run() invocation. Default is 0. - **logid** (str): To identify logs generated by a particular Run() invocation. - **only_execute_path_to_fetches** (bool): Only execute the nodes needed by fetch list. - **terminate** (bool): Set to True to terminate any currently executing calls that are using this RunOptions instance. The individual calls will exit gracefully and return an error status. - **training_mode** (bool): Choose to run in training or inferencing mode. ``` -------------------------------- ### Get Parameter Size Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/module.html Retrieves the total number of primitive elements (e.g., floating-point numbers) in the model's parameters. Specify `trainable_only=False` to include all parameters, not just those that are trainable. ```python return self._model.get_parameters_size(trainable_only) ``` -------------------------------- ### IOBinding for Device Data Transfer Source: https://onnxruntime.ai/docs/api/python/api_summary.html Use IOBinding to efficiently transfer data between CPU and a device (e.g., CUDA). Bind CPU input and output, then run the session. ```python # X is numpy array on cpu session = onnxruntime.InferenceSession( 'model.onnx', providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) ) io_binding = session.io_binding() # OnnxRuntime will copy the data over to the CUDA device if 'input' is consumed by nodes on the CUDA device io_binding.bind_cpu_input('input', X) io_binding.bind_output('output') session.run_with_iobinding(io_binding) Y = io_binding.copy_outputs_to_cpu()[0] ``` -------------------------------- ### TrainingBlock Source: https://onnxruntime.ai/docs/api/python/on_device_training/training_artifacts.html Base class for blocks that require a gradient model to be automatically built. Subclasses should implement the `build` method to define the forward graph and specify the starting point for backpropagation. ```APIDOC ## class onnxruntime.training.onnxblock.TrainingBlock Bases: `Block` Base class for all blocks that require gradient model to be automatically built. Blocks that require the gradient graph to be computed based on the output of the block must subclass this class. The subclass’s implementation of the build method must return the name of the output from where backpropagation must begin (typically the name of the output from the loss function). ### Method: `build(_* args_, _** kwargs_)` Customize the forward graph for this model by stacking up blocks on top of the inputs to this function. This method should be overridden by the subclass. The output of this method should be the name of the output from where backpropagation must begin (typically the name of the output from the loss function). ### Method: `requires_grad(_argument_name : str_, _value : bool = True_)` Specify whether the argument requires gradient or not. The auto-diff will compute the gradient graph for only the arguments that require gradient. By default, none of the arguments require gradient. The user must explicitly specify which arguments require gradient. Parameters: * **argument_name** (_str_) – The name of the argument that require/does not require gradient. * **value** (_bool_) – True if the argument requires gradient, False otherwise. ### Method: `parameters()` Trainable as well as non-trainable (frozen) parameters of the model. Model parameters that are extracted while building the training model are returned by this method. Note that the parameters are not known before the training model is built. As a result, if this method is invoked before the training model is built, an exception will be raised. Returns: trainable_params (list of onnx.TensorProto): The trainable parameters of the model. frozen_params (list of onnx.TensorProto): The non-trainable parameters of the model. Raises: RuntimeError: If the build method has not been invoked (i.e. the training model has not been built yet). ``` -------------------------------- ### Load and Run ONNX Model for Predictions Source: https://onnxruntime.ai/docs/api/python/tutorial.html Loads the ONNX model using ONNX Runtime and computes predictions on the test data. This snippet shows how to get all outputs from the model. ```python import numpy import onnxruntime as rt sess = rt.InferenceSession( "logreg_iris.onnx", providers=rt.get_available_providers()) input_name = sess.get_inputs()[0].name pred_onx = sess.run(None, {input_name: X_test.astype(numpy.float32)})[0] print(pred_onx) ``` -------------------------------- ### Get ONNX Runtime Device Type Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html This function maps a string device type to an ONNX Runtime OrtDevice object. It supports CUDA, CANN, CPU, DML, and ORT devices. ```python def get_ort_device_type(device_type: str, device_index): if device_type == "cuda": return C.OrtDevice.cuda() elif device_type == "cann": return C.OrtDevice.cann() elif device_type == "cpu": return C.OrtDevice.cpu() elif device_type == "dml": return C.OrtDevice.dml() elif device_type == "ort": return C.get_ort_device(device_index).device_type() else: raise Exception("Unsupported device type: " + device_type) ``` -------------------------------- ### Load Model with Session Options Source: https://onnxruntime.ai/docs/api/python/api_summary.html Illustrates loading an ONNX model with custom session options, such as enabling profiling. ```APIDOC ## Load Model with Session Options ### Description Loads an ONNX model with custom session options, like enabling profiling. ### Method ```python options = onnxruntime.SessionOptions() options.enable_profiling=True session = onnxruntime.InferenceSession( 'model.onnx', sess_options=options, providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) ) ``` ``` -------------------------------- ### IOBinding Class Methods Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Methods for binding inputs and outputs, synchronizing, and retrieving results from the ONNX Runtime inference session. ```APIDOC ## bind_output ### Description Binds an output to a specific device or a pre-allocated buffer. ### Method `bind_output(self, name, device_type='cpu', device_id=0, element_type=None, shape=None, buffer_ptr=None)` ### Parameters - **name** (string) - Required - output name - **device_type** (string) - Optional - e.g. cpu, cuda, cann, cpu by default - **device_id** (int) - Optional - device id, e.g. 0 - **element_type** (any) - Optional - output element type (required if buffer_ptr is provided) - **shape** (tuple) - Optional - output shape (required if buffer_ptr is provided) - **buffer_ptr** (any) - Optional - memory pointer to output data ## bind_ortvalue_output ### Description Binds an output using an existing OrtValue instance. ### Method `bind_ortvalue_output(self, name, ortvalue)` ### Parameters - **name** (string) - Required - output name - **ortvalue** (OrtValue) - Required - OrtValue instance to bind ## synchronize_outputs ### Description Synchronizes output data, ensuring all data is available. ### Method `synchronize_outputs(self)` ## get_outputs ### Description Retrieves the output OrtValues from the last inference run. ### Method `get_outputs(self)` ### Returns - **OrtValueVector** - A vector of OrtValue objects representing the outputs. ## copy_outputs_to_cpu ### Description Copies the contents of all outputs to CPU memory. ### Method `copy_outputs_to_cpu(self)` ## clear_binding_inputs ### Description Clears all bound inputs from the IOBinding. ### Method `clear_binding_inputs(self)` ## clear_binding_outputs ### Description Clears all bound outputs from the IOBinding. ### Method `clear_binding_outputs(self)` ``` -------------------------------- ### Implement Custom Weighted Average Loss Source: https://onnxruntime.ai/docs/api/python/on_device_training/training_artifacts.html Define a custom loss block by inheriting from `onnxblock.Block`. This example implements a weighted average of two MSE losses, suitable when a model produces multiple outputs that require separate loss calculations. ```python import onnxruntime.training.onnxblock as onnxblock from onnxruntime.training import artifacts # Define a custom loss block that takes in two inputs # and performs a weighted average of the losses from these # two inputs. class WeightedAverageLoss(onnxblock.Block): def __init__(self): self._loss1 = onnxblock.loss.MSELoss() self._loss2 = onnxblock.loss.MSELoss() self._w1 = onnxblock.blocks.Constant(0.4) self._w2 = onnxblock.blocks.Constant(0.6) self._add = onnxblock.blocks.Add() self._mul = onnxblock.blocks.Mul() def build(self, loss_input_name1, loss_input_name2): # The build method defines how the block should be stacked on top of # loss_input_name1 and loss_input_name2 # Returns weighted average of the two losses return self._add( self._mul(self._w1(), self._loss1(loss_input_name1, target_name="target1")), self._mul(self._w2(), self._loss2(loss_input_name2, target_name="target2")) ) my_custom_loss = WeightedAverageLoss() ``` -------------------------------- ### Initialize LinearLRScheduler Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/api/lr_scheduler.html Instantiate the LinearLRScheduler with an optimizer, warmup steps, total steps, and initial learning rate. This scheduler is suitable for training scenarios requiring a gradual increase and then decrease of the learning rate. ```python from onnxruntime.capi import _pybind_state as C from onnxruntime.training.api.optimizer import Optimizer class LinearLRScheduler: """Linearly updates the learning rate in the optimizer The linear learning rate scheduler decays the learning rate by linearly updated multiplicative factor from the initial learning rate set on the training session to 0. The decay is performed after the initial warm up phase where the learning rate is linearly incremented from 0 to the initial learning rate provided. Args: optimizer: User's onnxruntime training Optimizer warmup_step_count: The number of steps in the warm up phase. total_step_count: The total number of training steps. initial_lr: The initial learning rate. """ def __init__(self, optimizer: Optimizer, warmup_step_count: int, total_step_count: int, initial_lr: float): self._scheduler = C.LinearLRScheduler(optimizer._optimizer, warmup_step_count, total_step_count, initial_lr) ``` -------------------------------- ### Setting Model Load Format in SessionOptions Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Demonstrates how to explicitly set the model loading format (ONNX or ORT) using SessionOptions. A '.ort' file extension is inferred as ORT format, while others default to ONNX. ```python so = onnxruntime.SessionOptions() so.add_session_config_entry('session.load_model_format', 'ONNX') or so.add_session_config_entry('session.load_model_format', 'ORT') ``` -------------------------------- ### Add Run Configuration Entry Source: https://onnxruntime.ai/docs/api/python/api_summary.html Set a single run configuration entry as a pair of strings for a specific Run() invocation. Use this to customize run-time behavior. ```python add_run_config_entry(_self : onnxruntime.capi.onnxruntime_pybind11_state.RunOptions_, _arg0 : str_, _arg1 : str_) -> None# ``` -------------------------------- ### Bind Output with Pre-allocated Buffer Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/capi/onnxruntime_inference_collection.html Binds an output to a specific device using a pre-allocated buffer. If no buffer is provided, ONNX Runtime allocates memory. ```python self._iobinding.bind_output( name, C.OrtDevice( get_ort_device_type(device_type, device_id), C.OrtDevice.default_memory(), device_id, ), ) ``` -------------------------------- ### IOBinding Operations Source: https://onnxruntime.ai/docs/api/python/api_summary.html Methods for managing input and output bindings for ONNX Runtime sessions. ```APIDOC ## Devices ### IOBinding #### `IOBinding.bind_cpu_input()` #### Description Binds a CPU input to the IOBinding. #### `IOBinding.bind_input()` #### Description Binds an input to the IOBinding. #### `IOBinding.bind_ortvalue_input()` #### Description Binds an OrtValue as an input to the IOBinding. #### `IOBinding.bind_ortvalue_output()` #### Description Binds an OrtValue as an output to the IOBinding. #### `IOBinding.bind_output()` #### Description Binds an output to the IOBinding. #### `IOBinding.copy_outputs_to_cpu()` #### Description Copies outputs from the device to the CPU. #### `IOBinding.get_outputs()` #### Description Retrieves the bound outputs from the IOBinding. ### SessionIOBinding #### `SessionIOBinding.bind_input()` #### Description Binds an input to the session's IOBinding. #### `SessionIOBinding.bind_ortvalue_input()` #### Description Binds an OrtValue as an input to the session's IOBinding. #### `SessionIOBinding.bind_ortvalue_output()` #### Description Binds an OrtValue as an output to the session's IOBinding. #### `SessionIOBinding.bind_output()` #### Description Binds an output to the session's IOBinding. #### `SessionIOBinding.clear_binding_inputs()` #### Description Clears all input bindings for the session. #### `SessionIOBinding.clear_binding_outputs()` #### Description Clears all output bindings for the session. #### `SessionIOBinding.copy_outputs_to_cpu()` #### Description Copies session outputs from the device to the CPU. #### `SessionIOBinding.get_outputs()` #### Description Retrieves the bound outputs for the session. #### `SessionIOBinding.synchronize_inputs()` #### Description Synchronizes input data for the session. #### `SessionIOBinding.synchronize_outputs()` #### Description Synchronizes output data for the session. ``` -------------------------------- ### Configure Session Load Format Source: https://onnxruntime.ai/docs/api/python/api_summary.html Set the session load format to ONNX or ORT. A '.ort' file extension is inferred as ORT format, while other extensions are assumed to be ONNX format. ```python so.add_session_config_entry('session.load_model_format', 'ONNX') so.add_session_config_entry('session.load_model_format', 'ORT') ``` -------------------------------- ### sess.run_async Source: https://onnxruntime.ai/docs/api/python/api_summary.html Executes the model asynchronously. Results are saved to user_data via a callback. ```APIDOC ## run_async ### Description Executes the model asynchronously. Results are saved to user_data via a callback. ### Parameters - **output_names** (list[str]): Names of the outputs to fetch. - **input_feed** (dict[str, object]): Dictionary of input names to input values. - **callback** (function): A callback function to be executed when the asynchronous run completes. ``` -------------------------------- ### Initialize Log Block Source: https://onnxruntime.ai/docs/api/python/modules/onnxruntime/training/onnxblock/blocks.html Adds a Log node to the ONNX model. ```python class Log(_UnaryOp): """Adds Log node to the onnx model.""" def __init__(self): super().__init__("Log") ``` -------------------------------- ### Bind Input and Pre-allocated Output to Device Memory Source: https://onnxruntime.ai/docs/api/python/api_summary.html Use this to bind both input and a pre-allocated output OrtValue to a device. This is useful for managing memory explicitly or when dealing with known output shapes. ```python #X is numpy array on cpu X_ortvalue = onnxruntime.OrtValue.ortvalue_from_numpy(X, 'cuda', 0) Y_ortvalue = onnxruntime.OrtValue.ortvalue_from_shape_and_type([3, 2], np.float32, 'cuda', 0) # Change the shape to the actual shape of the output being bound session = onnxruntime.InferenceSession( 'model.onnx', providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) ) io_binding = session.io_binding() io_binding.bind_input( name='input', device_type=X_ortvalue.device_name(), device_id=0, element_type=np.float32, shape=X_ortvalue.shape(), buffer_ptr=X_ortvalue.data_ptr() ) io_binding.bind_output( name='output', device_type=Y_ortvalue.device_name(), device_id=0, element_type=np.float32, shape=Y_ortvalue.shape(), buffer_ptr=Y_ortvalue.data_ptr() ) session.run_with_iobinding(io_binding) ```