pub const ORT_DYLIB_PATH_VAR: &str = "ORT_DYLIB_PATH";
#[cfg(feature = "ort-load-dynamic")]
pub fn ensure_loaded() -> anyhow::Result<()> {
static OUTCOME: std::sync::OnceLock<Result<(), String>> = std::sync::OnceLock::new();
OUTCOME
.get_or_init(load_dylib)
.clone()
.map_err(anyhow::Error::msg)
}
#[cfg(feature = "ort-load-dynamic")]
fn load_dylib() -> Result<(), String> {
let path = dylib_path(std::env::var_os(ORT_DYLIB_PATH_VAR));
let builder = ort::init_from(&path)
.map_err(|e| load_error_message(&e.to_string(), ort::MINOR_VERSION))?;
drop(builder);
Ok(())
}
#[cfg(not(feature = "ort-load-dynamic"))]
pub fn ensure_loaded() -> anyhow::Result<()> {
Ok(())
}
#[cfg(feature = "ort-load-dynamic")]
fn dylib_path(var: Option<std::ffi::OsString>) -> std::path::PathBuf {
match var {
Some(path) if !path.is_empty() => path.into(),
_ => default_dylib_name().into(),
}
}
#[cfg(feature = "ort-load-dynamic")]
const fn default_dylib_name() -> &'static str {
if cfg!(target_os = "windows") {
"onnxruntime.dll"
} else if cfg!(target_os = "macos") {
"libonnxruntime.dylib"
} else {
"libonnxruntime.so"
}
}
#[cfg(feature = "ort-load-dynamic")]
fn load_error_message(cause: &str, min_minor: u32) -> String {
format!(
"Could not load ONNX Runtime: {cause}\n\
This build of knot uses the `ort-load-dynamic` feature: it loads ONNX Runtime \
at run time instead of bundling it. Install ONNX Runtime 1.{min_minor} or newer \
(https://github.com/microsoft/onnxruntime/releases) and point {ORT_DYLIB_PATH_VAR} \
at its library, e.g. {ORT_DYLIB_PATH_VAR}=/opt/onnxruntime/lib/{}",
default_dylib_name()
)
}
#[cfg(all(test, feature = "ort-load-dynamic"))]
mod tests {
use super::*;
#[test]
fn dylib_path_prefers_the_env_var() {
let path = dylib_path(Some("/opt/ort/lib/libonnxruntime.so.1.28.0".into()));
assert_eq!(
path,
std::path::PathBuf::from("/opt/ort/lib/libonnxruntime.so.1.28.0")
);
}
#[test]
fn dylib_path_falls_back_to_the_default_name() {
assert_eq!(
dylib_path(None),
std::path::PathBuf::from(default_dylib_name())
);
assert_eq!(
dylib_path(Some("".into())),
std::path::PathBuf::from(default_dylib_name())
);
}
#[test]
fn load_error_message_names_the_env_var_and_version() {
let msg = load_error_message("failed to load from `/x`: not found", 27);
assert!(msg.contains("failed to load from `/x`"), "{msg}");
assert!(msg.contains("ORT_DYLIB_PATH="), "{msg}");
assert!(msg.contains("1.27 or newer"), "{msg}");
assert!(msg.contains("ort-load-dynamic"), "{msg}");
}
#[test]
fn ensure_loaded_reports_a_missing_library() {
let missing = std::env::temp_dir().join("knot-no-such-dir/libonnxruntime.so");
let err = temp_env::with_var(ORT_DYLIB_PATH_VAR, Some(&missing), ensure_loaded)
.expect_err("a missing library must not load");
let msg = err.to_string();
assert!(msg.contains(ORT_DYLIB_PATH_VAR), "{msg}");
assert!(msg.contains("knot-no-such-dir"), "{msg}");
let again = ensure_loaded().expect_err("a failed load stays failed");
assert_eq!(again.to_string(), msg);
}
#[test]
fn embedder_init_fails_before_downloading_the_model() {
use crate::pipeline::embed::{DEFAULT_EMBED_MODEL, EmbedModelChoice, Embedder};
let cache = tempfile::tempdir().expect("temp dir");
let choice: EmbedModelChoice = DEFAULT_EMBED_MODEL.parse().expect("default model");
let missing = std::env::temp_dir().join("knot-no-such-dir/libonnxruntime.so");
let result = temp_env::with_var(ORT_DYLIB_PATH_VAR, Some(&missing), || {
Embedder::init_with_model(cache.path().join("models"), choice)
});
let Err(err) = result else {
panic!("a missing library must fail Embedder init");
};
assert!(err.to_string().contains(ORT_DYLIB_PATH_VAR), "{err}");
assert!(!cache.path().join("models").exists());
}
}