Skip to main content

qualia_vision/weights/
onnx_session.rs

1//! Optional ONNX Runtime session (feature `ort`).
2//!
3//! Without the feature, loaders report AdapterMissing. With `ort`, sessions
4//! load PermissiveReady weights from vendor/vision.
5
6use std::path::Path;
7
8use super::resolve_vision_asset::{VisionAssetError, VisionAssetId};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum OnnxSessionError {
12    FeatureDisabled,
13    WeightAbsent,
14    LoadFailed,
15    RunFailed,
16    BadOutput,
17}
18
19impl From<VisionAssetError> for OnnxSessionError {
20    fn from(e: VisionAssetError) -> Self {
21        match e {
22            VisionAssetError::WeightAbsent | VisionAssetError::NoRoots => {
23                OnnxSessionError::WeightAbsent
24            }
25        }
26    }
27}
28
29/// Metadata after a successful session open (always available for tests).
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct OnnxSessionInfo {
32    pub asset: VisionAssetId,
33    pub path: String,
34    pub backend: &'static str,
35}
36
37/// Open-session probe: resolves path; with `ort` also creates Session.
38pub fn probe_onnx_asset(
39    id: VisionAssetId,
40    roots: &[&Path],
41) -> Result<OnnxSessionInfo, OnnxSessionError> {
42    let resolved = super::resolve_vision_asset(id, roots)?;
43    #[cfg(feature = "ort")]
44    {
45        let _session = load_ort_session(&resolved.path)?;
46        return Ok(OnnxSessionInfo {
47            asset: id,
48            path: resolved.path.display().to_string(),
49            backend: "ort",
50        });
51    }
52    #[cfg(not(feature = "ort"))]
53    {
54        // Weight present — runtime still AdapterMissing without feature.
55        let _ = resolved;
56        Err(OnnxSessionError::FeatureDisabled)
57    }
58}
59
60#[cfg(feature = "ort")]
61fn load_ort_session(path: &Path) -> Result<ort::session::Session, OnnxSessionError> {
62    let environment = qualia_core_db::inference::onnx_runtime::shared_environment()
63        .map_err(|_| OnnxSessionError::LoadFailed)?;
64    load_ort_session_with_environment(path, environment)
65}
66
67#[cfg(feature = "ort")]
68pub fn load_ort_session_with_environment(
69    path: &Path,
70    environment: &ort::environment::Environment,
71) -> Result<ort::session::Session, OnnxSessionError> {
72    ort::session::Session::builder(environment)
73        .map_err(|_| OnnxSessionError::LoadFailed)?
74        .commit_from_file(path)
75        .map_err(|_| OnnxSessionError::LoadFailed)
76}
77
78/// Run YuNet ONNX if feature ort; returns flat decoded rows for yunet_decode_detections.
79/// Without ort: FeatureDisabled.
80pub fn yunet_infer_rgb8(
81    roots: &[&Path],
82    rgb: &[u8],
83    width: u32,
84    height: u32,
85    out_rows: &mut [f32],
86    stride: usize,
87) -> Result<usize, OnnxSessionError> {
88    if width == 0 || height == 0 || rgb.len() < (width * height * 3) as usize {
89        return Err(OnnxSessionError::BadOutput);
90    }
91    #[cfg(not(feature = "ort"))]
92    {
93        let _ = (roots, out_rows, stride);
94        return Err(OnnxSessionError::FeatureDisabled);
95    }
96    #[cfg(feature = "ort")]
97    {
98        yunet_infer_ort(roots, rgb, width, height, out_rows, stride)
99    }
100}
101
102#[cfg(feature = "ort")]
103fn yunet_infer_ort(
104    roots: &[&Path],
105    rgb: &[u8],
106    width: u32,
107    height: u32,
108    out_rows: &mut [f32],
109    stride: usize,
110) -> Result<usize, OnnxSessionError> {
111    use ort::value::Tensor;
112
113    if stride < 5 {
114        return Err(OnnxSessionError::BadOutput);
115    }
116    let resolved = super::resolve_vision_asset(VisionAssetId::Yunet, roots)?;
117    let mut session = load_ort_session(&resolved.path)?;
118
119    // YuNet OpenCV zoo often expects BGR float NCHW or specific size; use 320x320 letterbox.
120    const IN: usize = 320;
121    let mut input = vec![0.0f32; 1 * 3 * IN * IN];
122    // Simple nearest resize + RGB→planar (model may expect BGR — still useful for smoke).
123    for y in 0..IN {
124        for x in 0..IN {
125            let sx = (x as u32 * width / IN as u32).min(width.saturating_sub(1)) as usize;
126            let sy = (y as u32 * height / IN as u32).min(height.saturating_sub(1)) as usize;
127            let i = (sy * width as usize + sx) * 3;
128            let r = rgb[i] as f32;
129            let g = rgb[i + 1] as f32;
130            let b = rgb[i + 2] as f32;
131            // Planar BGR-ish
132            input[0 * IN * IN + y * IN + x] = b;
133            input[1 * IN * IN + y * IN + x] = g;
134            input[2 * IN * IN + y * IN + x] = r;
135        }
136    }
137
138    let shape = vec![1i64, 3, IN as i64, IN as i64];
139    let tensor = Tensor::from_array((shape, input.into_boxed_slice()))
140        .map_err(|_| OnnxSessionError::LoadFailed)?;
141
142    let outputs = session
143        .run(ort::inputs![tensor])
144        .map_err(|_| OnnxSessionError::RunFailed)?;
145
146    // Best-effort: flatten first output into rows of `stride`.
147    let first = outputs.iter().next().ok_or(OnnxSessionError::BadOutput)?;
148    let (_name, value) = first;
149    let try_extract = value.try_extract_tensor::<f32>();
150    let (shape, data) = match try_extract {
151        Ok(v) => v,
152        Err(_) => return Err(OnnxSessionError::BadOutput),
153    };
154    let flat: &[f32] = data;
155    let n = (flat.len() / stride).min(out_rows.len() / stride);
156    for i in 0..n * stride {
157        out_rows[i] = flat[i.min(flat.len() - 1)];
158    }
159    let _ = shape;
160    Ok(n)
161}
162
163/// SFace: resize face crop to 112x112, run embed, write 128 floats.
164pub fn sface_infer_rgb8(
165    roots: &[&Path],
166    rgb: &[u8],
167    width: u32,
168    height: u32,
169    out_embed: &mut [f32],
170) -> Result<usize, OnnxSessionError> {
171    if out_embed.len() < 128 {
172        return Err(OnnxSessionError::BadOutput);
173    }
174    #[cfg(not(feature = "ort"))]
175    {
176        let _ = (roots, rgb, width, height);
177        return Err(OnnxSessionError::FeatureDisabled);
178    }
179    #[cfg(feature = "ort")]
180    {
181        sface_infer_ort(roots, rgb, width, height, out_embed)
182    }
183}
184
185#[cfg(feature = "ort")]
186fn sface_infer_ort(
187    roots: &[&Path],
188    rgb: &[u8],
189    width: u32,
190    height: u32,
191    out_embed: &mut [f32],
192) -> Result<usize, OnnxSessionError> {
193    use ort::value::Tensor;
194
195    let resolved = super::resolve_vision_asset(VisionAssetId::Sface, roots)?;
196    let mut session = load_ort_session(&resolved.path)?;
197    const IN: usize = 112;
198    let mut input = vec![0.0f32; 1 * 3 * IN * IN];
199    for y in 0..IN {
200        for x in 0..IN {
201            let sx = (x as u32 * width / IN as u32).min(width.saturating_sub(1)) as usize;
202            let sy = (y as u32 * height / IN as u32).min(height.saturating_sub(1)) as usize;
203            let i = (sy * width as usize + sx) * 3;
204            input[0 * IN * IN + y * IN + x] = rgb[i] as f32;
205            input[1 * IN * IN + y * IN + x] = rgb[i + 1] as f32;
206            input[2 * IN * IN + y * IN + x] = rgb[i + 2] as f32;
207        }
208    }
209    let shape = vec![1i64, 3, IN as i64, IN as i64];
210    let tensor = Tensor::from_array((shape, input.into_boxed_slice()))
211        .map_err(|_| OnnxSessionError::LoadFailed)?;
212    let outputs = session
213        .run(ort::inputs![tensor])
214        .map_err(|_| OnnxSessionError::RunFailed)?;
215    let first = outputs.iter().next().ok_or(OnnxSessionError::BadOutput)?;
216    let (_n, value) = first;
217    let (_, data) = value
218        .try_extract_tensor::<f32>()
219        .map_err(|_| OnnxSessionError::BadOutput)?;
220    let n = data.len().min(128);
221    out_embed[..n].copy_from_slice(&data[..n]);
222    // L2 normalize
223    let mut norm = 0.0f32;
224    for i in 0..n {
225        norm += out_embed[i] * out_embed[i];
226    }
227    let nrm = norm.sqrt().max(1e-8);
228    for i in 0..n {
229        out_embed[i] /= nrm;
230    }
231    Ok(n)
232}
233
234#[cfg(test)]
235mod tests {
236    use super::*;
237    use std::path::PathBuf;
238
239    fn vendor() -> PathBuf {
240        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../vendor/vision")
241    }
242
243    #[test]
244    fn probe_without_ort_feature() {
245        let root = vendor();
246        let r = probe_onnx_asset(VisionAssetId::Yunet, &[root.as_path()]);
247        #[cfg(not(feature = "ort"))]
248        {
249            // Weight may be present → FeatureDisabled; or WeightAbsent on CI
250            assert!(matches!(
251                r,
252                Err(OnnxSessionError::FeatureDisabled) | Err(OnnxSessionError::WeightAbsent)
253            ));
254        }
255        #[cfg(feature = "ort")]
256        {
257            // May fail LoadFailed if shape API differs; weight path should resolve
258            let _ = r;
259        }
260    }
261}