use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use ort::ep::ExecutionProviderDispatch;
use ort::session::Session;
use ort::session::builder::GraphOptimizationLevel;
use ort::value::{DynValue, Tensor};
use serde::{Deserialize, Serialize};
use crate::error::{Result, VisionError};
use crate::manifest::{ModelManifest, RuntimeSpec, VerifiedFile, VerifiedModel, VisionTask};
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum OnnxExecutionProvider {
#[default]
Cpu,
CoreMl,
Cuda,
}
impl OnnxExecutionProvider {
fn accelerator(self) -> Option<ExecutionProviderDispatch> {
match self {
Self::Cpu => None,
#[cfg(feature = "onnx-coreml")]
Self::CoreMl => Some(ort::ep::CoreML::default().build()),
#[cfg(feature = "onnx-cuda")]
Self::Cuda => Some(ort::ep::CUDA::default().build()),
#[cfg(not(feature = "onnx-coreml"))]
Self::CoreMl => self.not_compiled(),
#[cfg(not(feature = "onnx-cuda"))]
Self::Cuda => self.not_compiled(),
}
}
#[cfg(any(not(feature = "onnx-coreml"), not(feature = "onnx-cuda")))]
fn not_compiled(self) -> Option<ExecutionProviderDispatch> {
tracing::warn!(
onnx.execution_provider = %self,
"execution provider not compiled into this build, running on the cpu"
);
None
}
}
impl std::fmt::Display for OnnxExecutionProvider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Cpu => "CPU",
Self::CoreMl => "CoreML",
Self::Cuda => "CUDA",
})
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct OnnxOptions {
#[serde(default)]
pub execution_provider: OnnxExecutionProvider,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub intra_threads: Option<usize>,
}
impl OnnxOptions {
pub fn with_execution_provider(mut self, provider: OnnxExecutionProvider) -> Self {
self.execution_provider = provider;
self
}
pub fn with_intra_threads(mut self, threads: usize) -> Self {
self.intra_threads = Some(threads);
self
}
}
pub(crate) enum Input {
F32(Vec<i64>, Vec<f32>),
I64(Vec<i64>, Vec<i64>),
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Output {
pub shape: Vec<i64>,
pub data: Vec<f32>,
}
impl Output {
pub fn dims(&self) -> Vec<usize> {
self.shape.iter().map(|&d| usize::try_from(d).unwrap_or(0)).collect()
}
}
#[derive(Clone)]
pub(crate) struct OrtModel {
backend: Arc<str>,
session: Arc<Mutex<Session>>,
inputs: Arc<[String]>,
outputs: Arc<[(String, Vec<i64>)]>,
}
impl std::fmt::Debug for OrtModel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OrtModel")
.field("backend", &self.backend)
.field("inputs", &self.inputs)
.field("outputs", &self.outputs)
.finish_non_exhaustive()
}
}
impl OrtModel {
pub fn load(backend: &str, file: &VerifiedFile, options: &OnnxOptions) -> Result<Self> {
let failed = |message: String| VisionError::Backend { backend: backend.into(), message };
let mut builder = adk_core::onnx::ort_environment()
.and_then(|environment| Session::builder(&environment))
.and_then(|b| {
b.with_optimization_level(GraphOptimizationLevel::Level3).map_err(Into::into)
})
.map_err(|e| failed(format!("the session builder failed to start: {e}")))?;
if let Some(threads) = options.intra_threads {
builder = builder
.with_intra_threads(threads)
.map_err(|e| failed(format!("could not set {threads} intra-op threads: {e}")))?;
}
builder = match options.execution_provider.accelerator() {
Some(provider) => builder.with_execution_providers([provider]).map_err(|e| {
failed(format!(
"could not register the {} execution provider: {e}",
options.execution_provider
))
})?,
None => builder,
};
let session = builder
.commit_from_file(file.path())
.map_err(|e| failed(format!("could not load {}: {e}", file.path().display())))?;
let inputs = session.inputs().iter().map(|i| i.name().to_string()).collect();
let outputs = session
.outputs()
.iter()
.map(|o| {
let shape = match o.dtype() {
ort::value::ValueType::Tensor { shape, .. } => shape.to_vec(),
_ => Vec::new(),
};
(o.name().to_string(), shape)
})
.collect();
tracing::info!(
vision.backend = backend,
model.file = %file.path().display(),
onnx.execution_provider = %options.execution_provider,
"onnx session loaded"
);
Ok(Self {
backend: backend.into(),
session: Arc::new(Mutex::new(session)),
inputs,
outputs,
})
}
pub fn outputs(&self) -> &[(String, Vec<i64>)] {
&self.outputs
}
pub fn input_named(&self, names: &[&str], fallback: usize) -> Result<String> {
self.inputs
.iter()
.find(|i| names.contains(&i.as_str()))
.or_else(|| self.inputs.get(fallback))
.cloned()
.ok_or_else(|| self.error(format!("the graph has no input named any of {names:?}")))
}
pub async fn run(
&self,
inputs: Vec<(String, Input)>,
wanted: &[&str],
) -> Result<HashMap<String, Output>> {
let model = self.clone();
let wanted: Vec<String> = wanted.iter().map(|w| (*w).to_string()).collect();
tokio::task::spawn_blocking(move || model.run_blocking(inputs, &wanted))
.await
.map_err(|e| self.error(format!("the inference task failed: {e}")))?
}
fn run_blocking(
&self,
inputs: Vec<(String, Input)>,
wanted: &[String],
) -> Result<HashMap<String, Output>> {
let mut values: Vec<(String, DynValue)> = Vec::with_capacity(inputs.len());
for (name, input) in inputs {
let value = match input {
Input::F32(shape, data) => Tensor::from_array((shape, data)).map(Tensor::into_dyn),
Input::I64(shape, data) => Tensor::from_array((shape, data)).map(Tensor::into_dyn),
}
.map_err(|e| self.error(format!("input `{name}` is not a valid tensor: {e}")))?;
values.push((name, value));
}
let mut session = self.session.lock().unwrap_or_else(|e| e.into_inner());
let outputs =
session.run(values).map_err(|e| self.error(format!("inference failed: {e}")))?;
let mut copied = HashMap::with_capacity(wanted.len());
for name in wanted {
let value = outputs
.get(name.as_str())
.ok_or_else(|| self.error(format!("the graph produced no output `{name}`")))?;
let (shape, data) = value
.try_extract_tensor::<f32>()
.map_err(|e| self.error(format!("output `{name}` is not an f32 tensor: {e}")))?;
copied.insert(name.clone(), Output { shape: shape.to_vec(), data: data.to_vec() });
}
Ok(copied)
}
pub fn error(&self, message: String) -> VisionError {
VisionError::Backend { backend: self.backend.to_string(), message }
}
}
pub(crate) fn check_manifest(manifest: &ModelManifest, task: VisionTask) -> Result<()> {
if manifest.task != task {
return Err(manifest_error(
manifest,
format!(
"the backend runs {task:?} models; the manifest is a {:?} model",
manifest.task
),
));
}
if !matches!(manifest.runtime, RuntimeSpec::Ort { .. }) {
return Err(manifest_error(
manifest,
format!("the backend runs on `ort`; the manifest names {:?}", manifest.runtime),
));
}
Ok(())
}
pub(crate) fn onnx_file<'m>(
model: &'m VerifiedModel,
role: &str,
unless: &str,
) -> Result<&'m VerifiedFile> {
let manifest = model.manifest();
let path = onnx_path(manifest, role, unless)?;
model.file(path).ok_or_else(|| manifest_error(manifest, format!("`{path}` was not verified")))
}
fn onnx_path<'a>(manifest: &'a ModelManifest, role: &str, unless: &str) -> Result<&'a str> {
let onnx: Vec<&str> =
manifest.files.iter().map(|f| f.path.as_str()).filter(|p| p.ends_with(".onnx")).collect();
let named: Vec<&str> = onnx
.iter()
.copied()
.filter(|p| {
let name = file_name(p);
name.contains(role) && (unless.is_empty() || !name.contains(unless))
})
.collect();
match named.as_slice() {
[path] => Ok(path),
_ => {
let wanted = if role.is_empty() {
"exactly one `.onnx` file".to_string()
} else {
format!("exactly one `.onnx` file whose name contains `{role}`")
};
Err(manifest_error(
manifest,
format!("the backend needs {wanted}; the manifest pins {onnx:?}"),
))
}
}
}
pub(crate) fn named_file<'m>(model: &'m VerifiedModel, name: &str) -> Result<&'m VerifiedFile> {
let manifest = model.manifest();
let path =
manifest.files.iter().map(|f| f.path.as_str()).find(|p| file_name(p) == name).ok_or_else(
|| manifest_error(manifest, format!("the backend needs a `{name}` file")),
)?;
model.file(path).ok_or_else(|| manifest_error(manifest, format!("`{path}` was not verified")))
}
fn file_name(path: &str) -> &str {
path.rsplit('/').next().unwrap_or(path)
}
pub(crate) fn manifest_error(manifest: &ModelManifest, reason: String) -> VisionError {
VisionError::Manifest { id: manifest.id.clone(), reason }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::manifest::{InMemoryAcceptanceStore, LicenceGate, LicencePolicy, ManifestSet};
#[test]
fn onnxruntime_picks_no_device_by_itself() {
const {
assert!(ort::sys::ORT_API_VERSION < 22, "a crate in this build turns on `ort` api-22+");
}
}
#[test]
fn an_accelerator_registers_only_when_its_feature_is_compiled() {
assert!(OnnxExecutionProvider::Cpu.accelerator().is_none());
assert_eq!(
OnnxExecutionProvider::CoreMl.accelerator().is_some(),
cfg!(feature = "onnx-coreml")
);
assert_eq!(
OnnxExecutionProvider::Cuda.accelerator().is_some(),
cfg!(feature = "onnx-cuda")
);
}
#[test]
fn roles_pick_the_encoder_and_the_decoder_by_name() {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir.path().join("onnx")).unwrap();
let paths = [
"onnx/vision_encoder_q4.onnx",
"onnx/vision_encoder_q4.onnx_data",
"onnx/prompt_encoder_mask_decoder_q4.onnx",
];
let mut files = Vec::new();
for path in paths {
std::fs::write(dir.path().join(path), path.as_bytes()).unwrap();
files.push(serde_json::json!({
"path": path,
"sha256": crate::digest::Digest::of(path.as_bytes()),
"size": path.len(),
}));
}
let manifest = ModelManifest::from_json(
&serde_json::json!({
"id": "toy-sam", "task": "segment",
"source": {"kind": "local", "dir": dir.path()},
"files": files,
"licence": {"id": "Apache-2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0", "class": "permissive"},
"runtime": {"kind": "ort", "opset": 18}
})
.to_string(),
)
.unwrap();
let (policy, store) = (LicencePolicy::new(), InMemoryAcceptanceStore::new());
let model =
VerifiedModel::check_dir(&manifest, dir.path(), &LicenceGate::new(&policy, &store))
.unwrap();
let path = |role, unless| onnx_file(&model, role, unless).map(|f| f.path().to_path_buf());
assert_eq!(path("encoder", "decoder").unwrap(), dir.path().join(paths[0]));
assert_eq!(path("decoder", "").unwrap(), dir.path().join(paths[2]));
assert!(matches!(path("encoder", ""), Err(VisionError::Manifest { .. })));
assert!(matches!(path("", ""), Err(VisionError::Manifest { .. })));
assert!(check_manifest(&manifest, VisionTask::Detect).is_err());
assert!(check_manifest(&manifest, VisionTask::Segment).is_ok());
}
#[test]
fn every_built_in_onnx_backend_model_has_what_its_backend_reads() {
let builtin = ManifestSet::builtin().unwrap();
let families = ["rf-detr-", "efficientsam-", "sam2.1-", "siglip2-", "yunet-"];
let ort: Vec<_> =
builtin.iter().filter(|m| families.iter().any(|f| m.id.starts_with(f))).collect();
assert_eq!(ort.len(), 12, "the built-in RF-DETR, SAM-family, SigLIP 2 and YuNet records");
for m in ort {
assert!(m.io.input.is_some(), "`{}` names no input", m.id);
match m.task {
VisionTask::Detect if m.id.starts_with("yunet-") => {
onnx_path(m, "", "").unwrap();
assert_eq!(m.io.labels, ["face"], "`{}`", m.id);
}
VisionTask::Detect => {
onnx_path(m, "", "").unwrap();
assert_eq!(m.io.labels.len(), 91, "`{}`", m.id);
assert_eq!(m.io.labels.iter().filter(|l| !l.is_empty()).count(), 80);
assert_eq!(
(m.io.labels[1].as_str(), m.io.labels[90].as_str()),
("person", "toothbrush")
);
}
VisionTask::Segment => {
onnx_path(m, "encoder", "decoder").unwrap();
onnx_path(m, "decoder", "").unwrap();
}
VisionTask::Embed => {
onnx_path(m, "vision", "").unwrap();
onnx_path(m, "text", "").unwrap();
assert!(m.files.iter().any(|f| f.path == "tokenizer.json"), "`{}`", m.id);
}
other => panic!("`{}` is a built-in {other:?} model with no ort backend", m.id),
}
}
}
}