aitune.torch.backend.onnx_runtime_backend
aitune.torch.backend.onnx_runtime_backend
ONNX Runtime backend.
Module Contents
Classes
Data
API
Bases: enum.Enum
Supported ONNX Runtime execution providers.
Only NVIDIA GPU-backed providers are supported:
CUDA—CUDAExecutionProvider: standard GPU execution.TENSORRT—TensorrtExecutionProviderwithCUDAExecutionProvideras fallback: enables TensorRT engine compilation for maximum GPU throughput.
Bases: Backend
Backend that exports models to ONNX and runs inference with ONNX Runtime.
Exports the model to a .onnx artifact at build time (trace or dynamo),
then loads an onnxruntime.InferenceSession for inference. Dynamic batch
and spatial dimensions are inferred automatically from graph_spec.
Workflow::
backend = ONNXRuntimeBackend()
build / tune as usual through ait.tune()
ait.save(model, “model.ait”)
later
ait.load(model, “model.ait”)
Load the ONNX Runtime session from disk.
Bind prepared inputs to an IOBinding handle.
GPU tensors are bound zero-copy via DLPack; CPU tensors and other array-like values are bound as numpy arrays.
Tell ORT to allocate all outputs on the CUDA device.
ORT owns the output buffers; shapes are resolved at inference time.
Tensors are retrieved after inference via _collect_outputs.
Export the model to ONNX then load the session.
Collect ORT CUDA outputs into torch tensors via D2D memcpy (no CPU round-trip).
Deactivate backend.
Deploy backend.
Return execution providers based on config (no automatic CPU fallback).
CUDA→[("CUDAExecutionProvider", {"device_id": ...})]TENSORRT→["TensorrtExecutionProvider", ("CUDAExecutionProvider", {"device_id": ...})]
Get the output object from the module and sample.
Note: to avoid case where a module returns a reference to the input argument, we make a deep copy of the output object.
Parameters:
PyTorch module
Sample input to use for model inference.
Returns: Any
The output object from the module.
Run inference through the ONNX Runtime session via IOBinding.
Map args/kwargs to session input names using graph_spec locators.
Tensors are returned as-is (preserving their device); conversion to the
format expected by ONNX Runtime happens in _infer via IOBinding.
Reconstruct original output structure from session output tensors.
Run representative samples to initialize the execution provider.
Some execution providers defer work until inference. In particular, the TensorRT execution provider compiles the ONNX graph when it first runs. Exercising a small number of recorded samples makes those failures part of the backend build instead of deferring them until profiling or application inference.
Parameters:
Recorded input samples for the backend build.
Returns the description of the backend.
Creates a backend from a state_dict.
Returns the key of the backend.
Returns the state_dict of the backend.
Bases: BackendConfig
Configuration for ONNXRuntimeBackend.
Parameters:
If True (default), export via torch.onnx.export(dynamo=True)
which calls torch.export.export internally and produces a more accurate
graph (no Python-level tracing limitations). If False, use the classic
trace-based exporter — faster and broader model coverage.
ONNX Runtime execution provider to use. When None
the backend defaults to :attr:ONNXExecutionProvider.CUDA. Only
:attr:ONNXExecutionProvider.CUDA and
:attr:ONNXExecutionProvider.TENSORRT are supported.
ONNX opset version passed to torch.onnx.export.
None uses the torch default.
Post init.
Initialise config from a plain dict (e.g. parsed from YAML).
execution_provider may be passed as a string and will be
converted to an ONNXExecutionProvider enum value automatically.