headroom/tests/test_transforms/test_ort_dylib.py
Parideboy c46cd8f950
fix(core): load ONNX Runtime dynamically so headroom._core imports on non-AVX2 x86-64 (#1715)
## Description

`import headroom._core` dies with SIGILL (`Illegal instruction`) on
x86-64 CPUs without AVX2 (Pentium N4200, Celeron N4500, AMD FX 8350 —
all reported on the issue). The repo sets no `RUSTFLAGS`/`target-cpu`
anywhere, so first-party Rust code is baseline x86-64; the AVX2 code
comes from Microsoft's prebuilt ONNX Runtime, statically linked into the
extension by fastembed's `ort-download-binaries-rustls-tls` feature on
non-Windows targets. Because it is statically linked, its code is mapped
and initialized when the extension module loads — **before** the runtime
AVX2 guard from #1162 can run, which is why that fix helped Magika init
but not the import-time crash.

Fix, mirroring what Windows already does for its own reasons (DirectML
link libs): build with `ort-load-dynamic` on every platform, so ONNX
Runtime is only `dlopen`'d at first use, where the #1162 AVX2 guard
falls back to the non-ONNX detection tiers on unsupported CPUs. Since
both target blocks became identical, they are collapsed into one
platform-independent `fastembed` dependency.

To keep Magika/fastembed working out of the box on Linux/macOS, the
existing `ORT_DYLIB_PATH` auto-pin (`headroom/_ort.py`, previously
Windows-only) now resolves the pip `onnxruntime` package's shared
library on all platforms (`onnxruntime.dll` / `libonnxruntime.so*` /
`libonnxruntime*.dylib`). The pip `onnxruntime` CPU wheels use runtime
CPU dispatch, so they also work on pre-AVX2 machines — non-AVX2 users
get working ML detection instead of a crash. Without the `onnxruntime`
package, ML detection degrades gracefully to the non-ONNX tiers exactly
as it already does on Windows.

Fixes #1278

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `crates/headroom-core/Cargo.toml`: replaced the per-target `fastembed`
blocks (`ort-download-binaries-rustls-tls` on non-Windows,
`ort-load-dynamic` on Windows) with a single platform-independent
dependency on `ort-load-dynamic`, with a comment documenting both the
DirectML and the AVX2/#1278 rationale.
- `Cargo.lock`: regenerated — `ort-sys` drops its static-download
dependencies (`hmac-sha256`, `lzma-rust2`, `ureq`); no version bumps.
- `headroom/_ort.py`: `ORT_DYLIB_PATH` auto-pin extended from
Windows-only to all platforms via a small `_find_dylib` helper that
resolves the platform's shared-library name inside the pip `onnxruntime`
package.
- `tests/test_transforms/test_ort_dylib.py`: replaced the obsolete
`test_noop_on_non_windows` with Linux (versioned `.so`) and macOS
(`.dylib`) pin tests; module docstring updated.
- `docs/content/docs/configuration.mdx`: `ORT_DYLIB_PATH` row updated
from Windows-only wording to the cross-platform behavior.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ cargo fmt --all -- --check && cargo clippy --workspace -- -D warnings && cargo test -p headroom-core --lib
clean
test result: 844 passed; 0 failed; 1 ignored

$ python -m pytest tests/test_transforms/test_ort_dylib.py -q
8 passed

$ ruff check headroom/_ort.py tests/test_transforms/test_ort_dylib.py
All checks passed!
```

## Real Behavior Proof

- Environment: Windows 11 (AVX2-capable — the SIGILL itself is not
reproducible on this machine), Python 3.13, Rust 1.95.0, local checkout
branched from `upstream/main` (9fbd47ba).
- Exact command / steps: `cargo check -p headroom-core` after the
feature switch; inspected the `Cargo.lock` diff; rebuilt and ran `python
-c "import headroom; from headroom._core import detect_content_type;
print(detect_content_type('hello world'))"`; ran the ort-pin test suite
with monkeypatched `linux`/`darwin` platforms.
- Observed result: build succeeds with `ort-load-dynamic`; the lockfile
shows `ort-sys` no longer pulls the binary-download machinery
(`hmac-sha256`, `lzma-rust2`, `ureq` removed), confirming the
statically-linked prebuilt ORT is gone; import + content detection works
with `ORT_DYLIB_PATH` auto-pinned to the pip onnxruntime library; all 8
pin tests pass including the new Linux/macOS branches.
- Not tested: actual pre-AVX2 x86-64 hardware (none available — the fix
removes AVX2 code from the import path by construction, and the issue
reporters on #1278 can verify); Linux/macOS wheel runtime behavior
beyond CI's ubuntu/macOS wheel-build jobs; embedding quality/performance
under a pip-provided ORT version differing from the previously vendored
one.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
2026-07-14 13:25:41 -04:00

129 lines
4.2 KiB
Python

"""Tests for headroom._ort -- the ORT_DYLIB_PATH auto-pin.
The resolver points the Rust core's ort-load-dynamic runtime at the pip
onnxruntime package's shared library on every platform: on Windows it
guards against the DLL search picking up the Windows ML System32
onnxruntime.dll, and on Linux/macOS it avoids static ORT import-time
CPU feature faults on older x86_64 CPUs (#1278). The platform is
monkeypatched so every branch runs on any CI OS.
"""
from __future__ import annotations
import sys
from types import SimpleNamespace
import pytest
import headroom._ort as _ort
@pytest.fixture(autouse=True)
def _fresh_resolver(monkeypatch):
"""Reset the module-level cache and scrub the env before every test."""
monkeypatch.setattr(_ort, "_pinned", _ort._UNSET)
monkeypatch.delenv("ORT_DYLIB_PATH", raising=False)
def _force_windows(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
def _fake_spec_for(monkeypatch, package_dir):
"""Make find_spec('onnxruntime') resolve to a fake package directory."""
spec = SimpleNamespace(origin=str(package_dir / "__init__.py"))
monkeypatch.setattr(
_ort.importlib.util,
"find_spec",
lambda name: spec if name == "onnxruntime" else None,
)
def test_pins_versioned_so_on_linux(monkeypatch, tmp_path):
monkeypatch.setattr(sys, "platform", "linux")
pkg = tmp_path / "onnxruntime"
capi = pkg / "capi"
capi.mkdir(parents=True)
so = capi / "libonnxruntime.so.1.22.0"
so.write_bytes(b"not really a shared object")
_fake_spec_for(monkeypatch, pkg)
assert _ort.ensure_ort_dylib_pinned() == str(so)
assert _ort.os.environ["ORT_DYLIB_PATH"] == str(so)
def test_pins_dylib_on_macos(monkeypatch, tmp_path):
monkeypatch.setattr(sys, "platform", "darwin")
pkg = tmp_path / "onnxruntime"
capi = pkg / "capi"
capi.mkdir(parents=True)
dylib = capi / "libonnxruntime.1.23.2.dylib"
dylib.write_bytes(b"not really a dylib")
_fake_spec_for(monkeypatch, pkg)
assert _ort.ensure_ort_dylib_pinned() == str(dylib)
assert _ort.os.environ["ORT_DYLIB_PATH"] == str(dylib)
def test_respects_existing_env(monkeypatch):
_force_windows(monkeypatch)
monkeypatch.setenv("ORT_DYLIB_PATH", r"C:\custom\onnxruntime.dll")
assert _ort.ensure_ort_dylib_pinned() == r"C:\custom\onnxruntime.dll"
assert _ort.os.environ["ORT_DYLIB_PATH"] == r"C:\custom\onnxruntime.dll"
def test_pins_to_package_capi_dll(monkeypatch, tmp_path):
_force_windows(monkeypatch)
pkg = tmp_path / "onnxruntime"
capi = pkg / "capi"
capi.mkdir(parents=True)
dll = capi / "onnxruntime.dll"
dll.write_bytes(b"not really a dll")
_fake_spec_for(monkeypatch, pkg)
assert _ort.ensure_ort_dylib_pinned() == str(dll)
assert _ort.os.environ["ORT_DYLIB_PATH"] == str(dll)
def test_idempotent_after_first_resolution(monkeypatch, tmp_path):
_force_windows(monkeypatch)
pkg = tmp_path / "onnxruntime"
(pkg / "capi").mkdir(parents=True)
(pkg / "capi" / "onnxruntime.dll").write_bytes(b"x")
_fake_spec_for(monkeypatch, pkg)
first = _ort.ensure_ort_dylib_pinned()
# Resolution must not re-run: break find_spec and call again.
monkeypatch.setattr(
_ort.importlib.util,
"find_spec",
lambda name: pytest.fail("resolution ran twice"),
)
assert _ort.ensure_ort_dylib_pinned() == first
def test_no_pin_when_package_missing(monkeypatch):
_force_windows(monkeypatch)
monkeypatch.setattr(_ort.importlib.util, "find_spec", lambda name: None)
assert _ort.ensure_ort_dylib_pinned() is None
assert "ORT_DYLIB_PATH" not in _ort.os.environ
def test_no_pin_when_native_library_absent(monkeypatch, tmp_path):
_force_windows(monkeypatch)
pkg = tmp_path / "onnxruntime"
pkg.mkdir() # package exists, but no capi/onnxruntime.dll inside
_fake_spec_for(monkeypatch, pkg)
assert _ort.ensure_ort_dylib_pinned() is None
assert "ORT_DYLIB_PATH" not in _ort.os.environ
def test_never_raises(monkeypatch):
_force_windows(monkeypatch)
def boom(name):
raise RuntimeError("synthetic find_spec failure")
monkeypatch.setattr(_ort.importlib.util, "find_spec", boom)
assert _ort.ensure_ort_dylib_pinned() is None
assert "ORT_DYLIB_PATH" not in _ort.os.environ