mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
## 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>
181 lines
6 KiB
YAML
181 lines
6 KiB
YAML
name: rust
|
|
|
|
on:
|
|
push:
|
|
branches: [ main, rust-rewrite ]
|
|
paths:
|
|
- 'crates/**'
|
|
- 'Cargo.toml'
|
|
- 'Cargo.lock'
|
|
- 'rust-toolchain.toml'
|
|
- 'tests/parity/**'
|
|
- 'Makefile'
|
|
- '.github/workflows/rust.yml'
|
|
pull_request:
|
|
paths:
|
|
- 'crates/**'
|
|
- 'Cargo.toml'
|
|
- 'Cargo.lock'
|
|
- 'rust-toolchain.toml'
|
|
- 'tests/parity/**'
|
|
- 'Makefile'
|
|
- '.github/workflows/rust.yml'
|
|
schedule:
|
|
# Nightly parity run at 07:17 UTC (weekdays only). Phase 0 allows failure.
|
|
- cron: '17 7 * * 1-5'
|
|
|
|
concurrency:
|
|
group: rust-${{ github.ref }}
|
|
cancel-in-progress: true
|
|
|
|
# Default permissions: read-only. Individual jobs override only what they need.
|
|
# Mitigates CodeQL/CWE-275 (missing-workflow-permissions): the GITHUB_TOKEN
|
|
# defaults to whatever the repo policy is, which can be read-write. Pinning
|
|
# this here means even if the repo default changes, this workflow stays safe.
|
|
permissions:
|
|
contents: read
|
|
|
|
jobs:
|
|
test:
|
|
name: test (ubuntu)
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 30
|
|
steps:
|
|
- uses: actions/checkout@v7
|
|
- name: Install stable toolchain
|
|
# Pin action code to @stable (latest fixes), toolchain version
|
|
# via input. The @1.95.0 ref shipped action code that errors on
|
|
# ubuntu-latest with `detected conflict: 'bin/cargo-clippy'`
|
|
# because the pre-installed runner Rust collides with the
|
|
# clippy-preview component install.
|
|
uses: dtolnay/rust-toolchain@stable
|
|
with:
|
|
toolchain: 1.95.0
|
|
components: rustfmt, clippy
|
|
- name: Cache cargo registry + build
|
|
uses: Swatinem/rust-cache@v2
|
|
- uses: actions/setup-python@v6
|
|
with:
|
|
python-version: '3.11'
|
|
- name: Provide ONNX Runtime dylib
|
|
# headroom-core is built with `ort-load-dynamic` (see its
|
|
# Cargo.toml): the ONNX Runtime shared library is dlopen'd at
|
|
# runtime instead of statically linked, so the magika/detection
|
|
# tests need a real libonnxruntime.so and ORT_DYLIB_PATH pointing
|
|
# at it — same contract `headroom/_ort.py` fulfills for Python
|
|
# users via the pip `onnxruntime` package.
|
|
run: |
|
|
pip install 'onnxruntime>=1.16.0'
|
|
echo "ORT_DYLIB_PATH=$(python -c "import onnxruntime, pathlib; p = pathlib.Path(onnxruntime.__file__).parent / 'capi'; print(next(iter(sorted(p.glob('libonnxruntime.so*')))))")" >> "$GITHUB_ENV"
|
|
- name: cargo fmt --check
|
|
run: cargo fmt --all -- --check
|
|
- name: cargo clippy
|
|
run: cargo clippy --workspace -- -D warnings
|
|
- name: cargo test
|
|
run: cargo test --workspace
|
|
|
|
simulator-e2e:
|
|
name: simulator e2e (${{ matrix.os }})
|
|
runs-on: ${{ matrix.os }}
|
|
timeout-minutes: 30
|
|
strategy:
|
|
fail-fast: false
|
|
matrix:
|
|
os: [ubuntu-latest, macos-latest, windows-latest]
|
|
steps:
|
|
- uses: actions/checkout@v7
|
|
- name: Install stable toolchain
|
|
uses: dtolnay/rust-toolchain@stable
|
|
with:
|
|
toolchain: 1.95.0
|
|
- name: Cache cargo registry + build
|
|
uses: Swatinem/rust-cache@v2
|
|
- name: cargo test simulator-backed proxy e2e
|
|
run: cargo test -p headroom-proxy --test e2e_simulators
|
|
|
|
wheels:
|
|
name: wheels (${{ matrix.target }})
|
|
runs-on: ${{ matrix.os }}
|
|
timeout-minutes: 45
|
|
strategy:
|
|
fail-fast: false
|
|
matrix:
|
|
include:
|
|
- os: ubuntu-latest
|
|
target: x86_64-unknown-linux-gnu
|
|
maturin-target: x86_64
|
|
- os: macos-14
|
|
target: aarch64-apple-darwin
|
|
maturin-target: aarch64-apple-darwin
|
|
- os: macos-15-intel
|
|
target: x86_64-apple-darwin
|
|
maturin-target: x86_64-apple-darwin
|
|
# Intel macOS uses `ort-load-dynamic` (no prebuilt ORT from ort-sys);
|
|
# Apple Silicon bundles ORT via `ort-download-binaries-rustls-tls`.
|
|
steps:
|
|
- uses: actions/checkout@v7
|
|
- uses: actions/setup-python@v6
|
|
with:
|
|
python-version: '3.11'
|
|
- name: "Build wheel (single-wheel architecture builds headroom-ai)"
|
|
uses: PyO3/maturin-action@v1
|
|
# Maturin reads `[tool.maturin]` from the root `pyproject.toml`
|
|
# which points at `crates/headroom-py/Cargo.toml` for the cdylib.
|
|
# Output is `headroom_ai-<ver>-<py>-<py>-<platform>.whl` containing
|
|
# both Python source and the compiled `headroom/_core.so`.
|
|
with:
|
|
command: build
|
|
args: --release --out dist
|
|
target: ${{ matrix.maturin-target }}
|
|
- name: Upload wheel artifact
|
|
uses: actions/upload-artifact@v7
|
|
with:
|
|
name: wheels-${{ matrix.target }}
|
|
path: dist/*.whl
|
|
|
|
audit:
|
|
name: audit
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 20
|
|
steps:
|
|
- uses: actions/checkout@v7
|
|
- uses: dtolnay/rust-toolchain@stable
|
|
with:
|
|
toolchain: 1.95.0
|
|
- uses: Swatinem/rust-cache@v2
|
|
- name: Install cargo-audit + cargo-deny
|
|
uses: taiki-e/install-action@v2
|
|
with:
|
|
tool: cargo-audit,cargo-deny
|
|
- name: cargo audit (soft-fail)
|
|
continue-on-error: true
|
|
run: cargo audit
|
|
- name: cargo deny check licenses
|
|
continue-on-error: true
|
|
run: cargo deny check licenses
|
|
|
|
parity-nightly:
|
|
name: parity (nightly, allowed to fail during Phase 0)
|
|
if: github.event_name == 'schedule'
|
|
runs-on: ubuntu-latest
|
|
continue-on-error: true
|
|
steps:
|
|
- uses: actions/checkout@v7
|
|
- uses: dtolnay/rust-toolchain@stable
|
|
with:
|
|
toolchain: 1.95.0
|
|
- uses: actions/setup-python@v6
|
|
with:
|
|
python-version: '3.11'
|
|
- uses: Swatinem/rust-cache@v2
|
|
- name: Install deps
|
|
run: |
|
|
python -m venv .venv
|
|
source .venv/bin/activate
|
|
pip install --upgrade pip
|
|
pip install maturin
|
|
pip install -e .
|
|
- name: Run parity harness
|
|
run: |
|
|
source .venv/bin/activate
|
|
make test-parity
|