Skip to content

fix(relevance): gate ONNX embedding backend behind AVX2 to avoid SIGILL (#1723)#1765

Merged
chopratejas merged 1 commit into
headroomlabs-ai:mainfrom
Parideboy:fix/1723-onnx-avx2-guard-embedding
Jul 3, 2026
Merged

fix(relevance): gate ONNX embedding backend behind AVX2 to avoid SIGILL (#1723)#1765
chopratejas merged 1 commit into
headroomlabs-ai:mainfrom
Parideboy:fix/1723-onnx-avx2-guard-embedding

Conversation

@Parideboy

@Parideboy Parideboy commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes the SIGILL / Illegal instruction crash in headroom.compress on CPUs without AVX2 (Docker / QEMU / older cloud VMs). The precompiled ONNX Runtime binary shipped by ort-sys (via fastembed's ort-download-binaries* feature) contains AVX2-family instructions on x86; running it on a non-AVX2 CPU traps with SIGILL — an uncatchable native fault that kills the whole host process. Magika detection was already guarded (#1162, landed after v0.28.0); the embedding relevance scorer shared the same ort-sys binary with no guard. This PR closes that remaining entry point and documents the requirement.

Closes #1723

Type of Change

  • 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)
  • Documentation update
  • Performance improvement
  • Code refactoring (no functional changes)

Changes Made

  • Add shared onnx_cpu::onnx_runtime_supported_by_cpu() helper (AVX2 check on x86/x86_64, true on other arches) as the single source of truth.
  • Route magika_detector through the shared helper (no behavior change).
  • Gate EmbeddingScorer::try_new* on the helper: unsupported CPU returns Err before touching ONNX, so callers fall back to BM25/stub instead of crashing.
  • Document the x86 AVX2 requirement + auto-fallback in the README.
  • Add offline tests (no network / no RUN_FASTEMBED_TESTS).

Testing

  • Unit tests pass (Rust: cargo test -p headroom-core)
  • Linting passes (cargo clippy -p headroom-core --all-targets, cargo fmt --check)
  • Type checking passes (mypy headroom) — N/A, Rust-only change
  • New tests added for new functionality
  • Manual testing performed

Test Output

$ cargo test -p headroom-core --lib relevance::embedding
cargo test: 13 passed, 834 filtered out (1 suite, 0.00s)

$ cargo test -p headroom-core --lib magika
cargo test: 16 passed, 831 filtered out (1 suite, 0.16s)

$ cargo clippy -p headroom-core --all-targets
(no warnings, no errors)

$ cargo fmt --check -p headroom-core
(clean)

$ cargo build --workspace
cargo build (225 crates compiled)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 6m 57s

Real Behavior Proof

  • Environment: headroom-core workspace, Rust stable, x86_64 (AVX2-capable dev host).
  • Exact command / steps: added onnx_guard_matches_cpu_features and try_new_errors_on_unsupported_cpu_instead_of_sigill tests; ran the suites above. On a no-AVX2 host the guard makes EmbeddingScorer::try_new() return Err(... "AVX2" ...) instead of executing the AVX2 ONNX binary; callers fall back to BM25 relevance rather than crashing.
  • Observed result: guard returns false only when the CPU lacks AVX2; embedding + magika ONNX paths both short-circuit to non-ONNX fallbacks; no SIGILL. All suites green.
  • Not tested: end-to-end pip install run on a physically AVX2-less machine (dev host has AVX2); guard behavior is unit-tested via the shared onnx_cpu helper and mirrors the already-shipped magika guard (fix: skip Magika backend on x86 CPUs without AVX2 #1162).

Review Readiness

  • I have performed a self-review
  • This PR is ready for human review

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the CHANGELOG.md if applicable — N/A (release-please generates the changelog)

Additional Notes

Rust-only change, so the Python pytest/ruff/mypy items are N/A; equivalent Rust cargo test/clippy/fmt were run and pasted above. The fix is defense-in-depth parity with the existing magika AVX2 guard (#1162), applied to the second ONNX entry point (embedding relevance).

…LL (headroomlabs-ai#1723)

The precompiled ONNX Runtime binary shipped by `ort-sys` (via fastembed's
`ort-download-binaries*` feature) contains AVX2 instructions on x86. Running
it on a CPU without AVX2 (common in Docker/QEMU/older cloud VMs) traps with
SIGILL — an uncatchable native fault that kills the whole host process.

Magika detection was already guarded (headroomlabs-ai#1162), but the embedding relevance
scorer shared the same ort-sys binary with no guard. Add a shared
`onnx_cpu::onnx_runtime_supported_by_cpu()` helper (AVX2 check on x86, true
elsewhere), route magika through it for a single source of truth, and gate
`EmbeddingScorer::try_new*` on it so callers fall back to BM25 instead of
crashing. Document the x86 AVX2 requirement in the README.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

PR governance

This PR follows the template and is marked ready for human review.

@github-actions github-actions Bot added status: needs author action Pull request body or readiness checklist still needs author updates status: ready for review Pull request body is complete and the author marked it ready for human review and removed status: needs author action Pull request body or readiness checklist still needs author updates labels Jul 3, 2026
@chopratejas chopratejas merged commit 728b330 into headroomlabs-ai:main Jul 3, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: ready for review Pull request body is complete and the author marked it ready for human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]headroom-ai crashes the Python process with SIGILL / Illegal instruction when running real compression through the Python library API.

2 participants