use ort::ep::ExecutionProviderDispatch;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OnnxExecutionProvider {
Cuda,
DirectMl,
CoreMl,
Cpu,
}
impl OnnxExecutionProvider {
pub fn auto_detect() -> Self {
if cfg!(target_os = "macos") {
OnnxExecutionProvider::CoreMl
} else {
OnnxExecutionProvider::Cpu
}
}
pub(crate) fn dispatch(self) -> Option<ExecutionProviderDispatch> {
let provider = match self {
#[cfg(feature = "onnx-cuda")]
Self::Cuda => Some(ort::ep::CUDA::default().build()),
#[cfg(feature = "onnx-coreml")]
Self::CoreMl => Some(ort::ep::CoreML::default().build()),
#[cfg(not(feature = "onnx-cuda"))]
Self::Cuda => None,
#[cfg(not(feature = "onnx-coreml"))]
Self::CoreMl => None,
Self::DirectMl => None,
Self::Cpu => return None,
};
if provider.is_none() {
tracing::warn!(
onnx.execution_provider = %self,
"execution provider not available in this build, falling back to cpu"
);
}
provider
}
}
impl std::fmt::Display for OnnxExecutionProvider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Cuda => write!(f, "CUDA"),
Self::DirectMl => write!(f, "DirectML"),
Self::CoreMl => write!(f, "CoreML"),
Self::Cpu => write!(f, "CPU"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_cpu_registers_no_provider() {
assert!(OnnxExecutionProvider::Cpu.dispatch().is_none());
}
#[test]
fn a_provider_registers_only_when_its_feature_is_compiled() {
assert_eq!(OnnxExecutionProvider::Cuda.dispatch().is_some(), cfg!(feature = "onnx-cuda"));
assert_eq!(
OnnxExecutionProvider::CoreMl.dispatch().is_some(),
cfg!(feature = "onnx-coreml")
);
assert!(OnnxExecutionProvider::DirectMl.dispatch().is_none());
}
}