mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(kompress): check the CPU before the loader in onnx_runtime_ready
Review on #4644: onnx_runtime_ready() called dynamic_ort_loader_ready() directly, so kompress_parity's skip check could initialize the native runtime on an x86 CPU without AVX2 before Kompress::from_files reached #4559's CPU-then-loader gate, the SIGILL that gate exists to prevent. onnx_runtime_ready() now goes through ort_session_gate with the same CPU verdict and loader as ort_session_preconditions, so the loader never runs on an unsupported CPU and the reason comes back as "cpu_unsupported: ...". A unit test drives the injectable onnx_runtime_ready_with and checks the loader is not called when the CPU check fails. build_session is back to #4559's single ort_session_preconditions() call (the extra loader call was redundant after the merge). kompress_ort_unavailable accepts the CPU reason too, since on such a CPU the gate answers before any library load.
This commit is contained in:
@@ -641,22 +641,31 @@ fn build_session(path: &Path) -> Result<Session, Box<dyn std::error::Error + Sen
|
||||
// Every Kompress constructor (from_files / from_pretrained / from_cache)
|
||||
// funnels through here, so guarding here covers all of them.
|
||||
ort_session_preconditions().map_err(|(reason, e)| format!("{reason}: {e}"))?;
|
||||
// Resolve and commit the ONNX Runtime dylib before touching `ort`: with
|
||||
// `ort-load-dynamic`, a failed load inside `ort` deadlocks instead of
|
||||
// erroring (see `dynamic_ort_loader_ready`).
|
||||
onnx_runtime_ready()?;
|
||||
let session = Session::builder()?.commit_from_file(path)?;
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
/// Whether an ONNX Runtime library can be loaded for Kompress sessions:
|
||||
/// `ORT_DYLIB_PATH`, or the pip `onnxruntime` found through `VIRTUAL_ENV`,
|
||||
/// `CONDA_PREFIX` or `./.venv`. Resolved and committed once per process;
|
||||
/// `Err` carries the reason. For harnesses that should skip, not fail, on a
|
||||
/// machine without ONNX Runtime; every `Kompress` constructor already checks
|
||||
/// it before building a session.
|
||||
/// Whether this process can build Kompress sessions: the same CPU-then-loader
|
||||
/// preconditions every `Kompress` constructor checks, in the same order. The
|
||||
/// CPU must run the precompiled ONNX Runtime (AVX2 on x86), and only then is
|
||||
/// the library resolved (`ORT_DYLIB_PATH`, or the pip `onnxruntime` under
|
||||
/// `VIRTUAL_ENV`, `CONDA_PREFIX` or `./.venv`) and committed, once per
|
||||
/// process. `Err` is `"<reason>: <detail>"`. For harnesses that should skip,
|
||||
/// not fail, on a machine that can't run Kompress.
|
||||
pub fn onnx_runtime_ready() -> Result<(), String> {
|
||||
crate::transforms::magika_detector::dynamic_ort_loader_ready()
|
||||
onnx_runtime_ready_with(
|
||||
crate::onnx_cpu::onnx_runtime_supported_by_cpu(),
|
||||
crate::transforms::magika_detector::dynamic_ort_loader_ready,
|
||||
)
|
||||
}
|
||||
|
||||
/// [`onnx_runtime_ready`] with the CPU verdict and loader passed in, so tests
|
||||
/// can check the loader never runs on an unsupported CPU.
|
||||
fn onnx_runtime_ready_with(
|
||||
cpu_ok: bool,
|
||||
loader: impl FnOnce() -> Result<(), String>,
|
||||
) -> Result<(), String> {
|
||||
ort_session_gate(cpu_ok, loader).map_err(|(reason, e)| format!("{reason}: {e}"))
|
||||
}
|
||||
|
||||
/// The tokenizer and the ONNX artifact `onnx` (e.g.
|
||||
@@ -904,6 +913,27 @@ mod tests {
|
||||
assert!(called.get());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn onnx_runtime_ready_checks_the_cpu_before_the_loader() {
|
||||
// The public readiness check (used by harnesses to skip) must keep
|
||||
// the constructors' order: on an unsupported CPU it reports the CPU
|
||||
// reason without initializing the native runtime.
|
||||
let called = std::cell::Cell::new(false);
|
||||
let out = onnx_runtime_ready_with(false, || {
|
||||
called.set(true);
|
||||
Ok(())
|
||||
});
|
||||
let message = out.unwrap_err();
|
||||
assert!(message.starts_with("cpu_unsupported: "), "{message}");
|
||||
assert!(!called.get(), "loader ran although the CPU check failed");
|
||||
|
||||
assert_eq!(
|
||||
onnx_runtime_ready_with(true, || Err("no dylib".into())),
|
||||
Err("ort_loader_unavailable: no dylib".to_string())
|
||||
);
|
||||
assert_eq!(onnx_runtime_ready_with(true, || Ok(())), Ok(()));
|
||||
}
|
||||
|
||||
/// `from_pretrained` must refuse before `Api::new()` when the operator
|
||||
/// air-gapped the box.
|
||||
///
|
||||
|
||||
@@ -55,10 +55,12 @@ fn missing_onnx_runtime_is_an_error_not_a_hang() {
|
||||
|
||||
match rx.recv_timeout(Duration::from_secs(60)) {
|
||||
Ok(Err(err @ KompressError::Onnx { .. })) => {
|
||||
// On an x86 CPU without AVX2 the CPU gate answers first, before
|
||||
// any library is loaded; either reason proves it returned.
|
||||
let message = err.to_string();
|
||||
assert!(
|
||||
message.contains("ORT_DYLIB_PATH"),
|
||||
"error should name ORT_DYLIB_PATH: {message}"
|
||||
message.contains("ORT_DYLIB_PATH") || message.contains("cpu_unsupported"),
|
||||
"error should name ORT_DYLIB_PATH (or the CPU gate): {message}"
|
||||
);
|
||||
}
|
||||
Ok(Err(other)) => panic!("expected KompressError::Onnx, got: {other}"),
|
||||
|
||||
@@ -11,10 +11,13 @@
|
||||
//! locally after `python scripts/record_kompress_trace.py` to get the
|
||||
//! real assertion.
|
||||
//!
|
||||
//! Runtime-gated too: with the model cached but no ONNX Runtime library to
|
||||
//! run it (`ORT_DYLIB_PATH` unset and no pip `onnxruntime` under
|
||||
//! `VIRTUAL_ENV`, `CONDA_PREFIX` or `./.venv`), it also SKIPS, the way
|
||||
//! `magika_idle_threads` skips without an ONNX Runtime.
|
||||
//! Runtime-gated too: with the model cached but no way to run it (a CPU
|
||||
//! without AVX2, or no ONNX Runtime library: `ORT_DYLIB_PATH` unset and no
|
||||
//! pip `onnxruntime` under `VIRTUAL_ENV`, `CONDA_PREFIX` or `./.venv`), it
|
||||
//! also SKIPS, the way `magika_idle_threads` skips without an ONNX Runtime.
|
||||
//! The check is `kompress::onnx_runtime_ready`, the constructors' own
|
||||
//! CPU-then-loader preconditions, so it never loads the runtime on a CPU
|
||||
//! that can't run it.
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -29,7 +32,7 @@ fn cached_artifacts() -> Option<(PathBuf, PathBuf)> {
|
||||
}
|
||||
|
||||
/// [`cached_artifacts`], or `None` (with the reason on stderr) when the
|
||||
/// model isn't cached or no ONNX Runtime can be loaded to run it.
|
||||
/// model isn't cached or this machine can't run ONNX Runtime.
|
||||
fn loadable_artifacts() -> Option<(PathBuf, PathBuf)> {
|
||||
let Some(paths) = cached_artifacts() else {
|
||||
eprintln!(
|
||||
@@ -39,7 +42,7 @@ fn loadable_artifacts() -> Option<(PathBuf, PathBuf)> {
|
||||
return None;
|
||||
};
|
||||
if let Err(reason) = kompress::onnx_runtime_ready() {
|
||||
eprintln!("SKIP: no ONNX Runtime to run kompress: {reason}");
|
||||
eprintln!("SKIP: can't run ONNX Runtime for kompress here: {reason}");
|
||||
return None;
|
||||
}
|
||||
Some(paths)
|
||||
|
||||
Reference in New Issue
Block a user