//! Walk the ladder as the app does — probe, engines, then a session — and //! say what each step chose. The M5 check of docs/inference.md §6 without //! the app around it. //! //! DARKROOM_ORT_DIR=/usr/lib \ //! cargo run --release -p dr-inference-engine --features native,tract \ //! --example ladder -- CACHE_DIR models/face/scrfd_500m_640.onnx [MODEL.onnx ...] //! //! Every model named is a `Detector` for the config's purposes, which is //! enough to see the rung taken, the engines compiled and a session land //! on it. Delete `CACHE_DIR` to see the first run again; keep it to see the //! second. use std::path::PathBuf; use std::time::{Duration, Instant}; fn main() { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); let mut args = std::env::args_os().skip(1).map(PathBuf::from); let (Some(cache_dir), models) = (args.next(), args.collect::>()) else { eprintln!("usage: ladder CACHE_DIR MODEL.onnx [MODEL.onnx ...]"); std::process::exit(2); }; if models.is_empty() { eprintln!("usage: ladder CACHE_DIR MODEL.onnx [MODEL.onnx ...]"); std::process::exit(2); } let runtime_dirs: Vec = std::env::var_os("DARKROOM_ORT_DIR") .map(PathBuf::from) .into_iter() .collect(); let started = Instant::now(); dr_inference_engine::init(dr_inference_engine::Config { runtime_dirs, cache_dir: cache_dir.clone(), models: models .iter() .map(|p| (dr_inference_engine::Role::Detector, p.clone())) .collect(), embedded: Vec::new(), ceiling: None, threads: 0, decay: Duration::ZERO, }); let mut last = String::new(); loop { let s = dr_inference_engine::status(); let line = format!( "{} · {} · engines {}/{}{}", s.line(), if s.probing { "probing" } else { s.reason.as_str() }, s.engines.0, s.engines.1, if s.failed.is_empty() { String::new() } else { format!( " · tried {}", s.failed .iter() .map(|(r, why)| format!("{}: {why}", r.label())) .collect::>() .join(" · ") ) } ); if line != last { println!("{:>6.1} s {line}", started.elapsed().as_secs_f64()); last = line; } if !s.probing && s.engines.0 >= s.engines.1 { break; } std::thread::sleep(Duration::from_millis(500)); } for path in &models { let bytes = std::fs::read(path).expect("read model"); let t = Instant::now(); let model = dr_inference_engine::open( dr_inference_engine::Role::Detector, dr_inference_engine::Form::F32, &bytes, ) .expect("open model"); let acquired = model.acquire().expect("acquire session"); println!( "{} on {} in {:.2} s", path.file_name().unwrap().to_string_lossy(), acquired.rung().label(), t.elapsed().as_secs_f64() ); } }