Skip to main content

qualia_vision/weights/
load_onnx_bytes.rs

1//! Cold-path ONNX file validation (no runtime inference).
2//!
3//! PermissiveReady zoo weights: check magic + size so adapters fail closed
4//! before claiming ProductionWeights. Actual ORT/tract session is feature work.
5
6use std::fs;
7use std::path::Path;
8
9/// ONNX protobuf files typically start with field tags; many begin with 0x08
10/// after optional size, but OpenCV Zoo files often start with `\x08` varint
11/// or the ASCII is not reliable. We require: readable file, min size, and
12/// either protobuf-ish leading bytes or presence of "onnx" string in header.
13
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct OnnxFileMeta {
16    pub bytes: usize,
17    pub looks_like_onnx: bool,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum OnnxLoadError {
22    Io,
23    TooSmall,
24    NotOnnxLike,
25}
26
27const MIN_ONNX_BYTES: usize = 256;
28
29/// Load entire ONNX file into memory (cold path; weights stay outside NQuin).
30pub fn load_onnx_file(path: &Path) -> Result<(Vec<u8>, OnnxFileMeta), OnnxLoadError> {
31    let data = fs::read(path).map_err(|_| OnnxLoadError::Io)?;
32    let meta = validate_onnx_bytes(&data)?;
33    Ok((data, meta))
34}
35
36/// Validate bytes without reading disk.
37pub fn validate_onnx_bytes(data: &[u8]) -> Result<OnnxFileMeta, OnnxLoadError> {
38    if data.len() < MIN_ONNX_BYTES {
39        return Err(OnnxLoadError::TooSmall);
40    }
41    let looks = looks_like_onnx(data);
42    if !looks {
43        return Err(OnnxLoadError::NotOnnxLike);
44    }
45    Ok(OnnxFileMeta {
46        bytes: data.len(),
47        looks_like_onnx: true,
48    })
49}
50
51fn looks_like_onnx(data: &[u8]) -> bool {
52    // Scan first 512 bytes for "onnx" or "ONNX" or GraphProto-ish patterns.
53    let n = data.len().min(512);
54    let head = &data[..n];
55    if find_subslice(head, b"onnx") || find_subslice(head, b"ONNX") {
56        return true;
57    }
58    // Protobuf ModelProto often starts with 0x08 (ir_version field).
59    if head[0] == 0x08 || head[0] == 0x0a {
60        return true;
61    }
62    false
63}
64
65fn find_subslice(hay: &[u8], needle: &[u8]) -> bool {
66    hay.windows(needle.len()).any(|w| w == needle)
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use crate::weights::{resolve_vision_asset, VisionAssetId};
73    use std::path::PathBuf;
74
75    fn vendor_root() -> PathBuf {
76        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../vendor/vision")
77    }
78
79    #[test]
80    fn reject_tiny() {
81        assert_eq!(
82            validate_onnx_bytes(&[0u8; 10]),
83            Err(OnnxLoadError::TooSmall)
84        );
85    }
86
87    #[test]
88    fn yunet_on_disk_validates_if_present() {
89        let root = vendor_root();
90        match resolve_vision_asset(VisionAssetId::Yunet, &[root.as_path()]) {
91            Ok(a) => {
92                let (_b, meta) = load_onnx_file(&a.path).expect("yunet load");
93                assert!(meta.looks_like_onnx);
94                assert!(meta.bytes > 10_000);
95            }
96            Err(_) => {
97                // WeightAbsent on CI without download — skip
98            }
99        }
100    }
101
102    #[test]
103    fn sface_on_disk_validates_if_present() {
104        let root = vendor_root();
105        match resolve_vision_asset(VisionAssetId::Sface, &[root.as_path()]) {
106            Ok(a) => {
107                let (_b, meta) = load_onnx_file(&a.path).expect("sface load");
108                assert!(meta.bytes > 1_000_000);
109            }
110            Err(_) => {}
111        }
112    }
113}