Interface IOnnxModel<T>
- Namespace
- AiDotNet.Interfaces
- Assembly
- AiDotNet.dll
Defines the contract for ONNX model wrappers that provide cross-platform model inference.
public interface IOnnxModel<T> : IDisposable
Type Parameters
TThe numeric type used for calculations.
- Inherited Members
Remarks
This interface provides a unified way to work with ONNX models in AiDotNet. It supports loading models from files, byte arrays, or URLs, and provides methods for running inference with AiDotNet Tensor types.
For Beginners: ONNX (Open Neural Network Exchange) is a universal format for neural network models. This interface allows you to:
- Load models trained in PyTorch, TensorFlow, or other frameworks
- Run inference using CPU, GPU (CUDA), TensorRT, or DirectML
- Convert between AiDotNet tensors and ONNX tensors automatically
Properties
ExecutionProvider
Gets the execution provider currently being used (CPU, CUDA, TensorRT, DirectML).
string ExecutionProvider { get; }
Property Value
IsLoaded
Gets whether the model has been successfully loaded and is ready for inference.
bool IsLoaded { get; }
Property Value
Metadata
Gets the metadata about the loaded ONNX model.
IOnnxModelMetadata Metadata { get; }
Property Value
Methods
Run(Tensor<T>)
Runs inference with a single input tensor.
Tensor<T> Run(Tensor<T> input)
Parameters
inputTensor<T>The input tensor.
Returns
- Tensor<T>
The output tensor from the model.
Run(IReadOnlyDictionary<string, Tensor<T>>)
Runs inference with named inputs.
IReadOnlyDictionary<string, Tensor<T>> Run(IReadOnlyDictionary<string, Tensor<T>> inputs)
Parameters
inputsIReadOnlyDictionary<string, Tensor<T>>Dictionary mapping input names to tensors.
Returns
- IReadOnlyDictionary<string, Tensor<T>>
Dictionary mapping output names to tensors.
RunAsync(Tensor<T>, CancellationToken)
Runs inference asynchronously with a single input tensor.
Task<Tensor<T>> RunAsync(Tensor<T> input, CancellationToken cancellationToken = default)
Parameters
inputTensor<T>The input tensor.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<Tensor<T>>
The output tensor from the model.
RunAsync(IReadOnlyDictionary<string, Tensor<T>>, CancellationToken)
Runs inference asynchronously with named inputs.
Task<IReadOnlyDictionary<string, Tensor<T>>> RunAsync(IReadOnlyDictionary<string, Tensor<T>> inputs, CancellationToken cancellationToken = default)
Parameters
inputsIReadOnlyDictionary<string, Tensor<T>>Dictionary mapping input names to tensors.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<IReadOnlyDictionary<string, Tensor<T>>>
Dictionary mapping output names to tensors.
WarmUp()
Warms up the model by running a single inference with dummy data. This helps ensure consistent inference times by initializing lazy resources.
void WarmUp()
WarmUpAsync(CancellationToken)
Warms up the model asynchronously.
Task WarmUpAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token.