Skip to content

feat(xla): bind operator numeric contracts into production artifacts - #3242

Merged
inureyes merged 6 commits into
mainfrom
feature/issue-3211-bind-operator-contracts
Nov 22, 2026
Merged

inureyes merged 6 commits into
mainfrom
feature/issue-3211-bind-operator-contracts

Conversation

@inureyes

@inureyes inureyes commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Every production auxiliary artifact now writes the v3 manifest with an operator numeric contract instead of the unqualified v1 one. A cached VMFB whose stored contract differs is rejected by verify_auxiliary_manifest (the check IreeAuxiliaryModule::load runs) and recompiled by ensure_qualified_auxiliary_artifact. The kernel IREE picks per dispatch stays recorded as unobserved, so this binds identity and does not claim production qualification.

  • One builder (operator_numeric_contract/production.rs) maps a family's declared operators (families.rs), the Precision its emitter used, the checkpoint dtype classes its loader observed, and the backend identity onto an OperatorNumericContractSet. All 11 former new_legacy_unqualified call sites use it. LLaVA/Qwen3-VL/Molmo/Molmo2/Youtu-VL emitters and Gemma3n audio now take their fixed precision from a named constant the runtime also binds; Qwen2-VL and Phi4MM pass the resolved value they emit with.
  • compiler_version / runtime_archive_identity move to iree_identity.rs (pub(crate), shared with numeric_probe.rs and feat(xla): run the operator micro-oracles against MLX on CUDA #3212). build.rs bakes the SHA-256 of the linked libiree_runtime_unified.a for the dist, CUDA, ROCm and macOS layouts, because the CUDA source build has no dist archive to read at run time.
  • The false dead_code allows on new_with_operator_numeric_contract and the operator_numeric_contract module are gone; variants no production entry constructs keep item-level allows with reasons. new_legacy_unqualified stays for aux_smoke and the downgrade tests.

Text-only token/decode emitters and goldens are untouched; no tolerance changed.

Changes during review

  • Gemma3n's host affine dequantization rounds to BF16 after the multiply and again after the add, unlike the F32 dequant Qwen2-VL/Qwen3-VL use, but both produced the same contract, so changing the rounding would not have invalidated a cached artifact. A new AffineEvaluationOrder::SeparateMultiplyThenAddBf16Rounded (with AffineExecution::HostBf16Sequential) is now selected by one constant that drives both the dequantizer and the contract, with a test that the two rounding modes give different identities.
  • This is the first change to write v3 manifests in production, and the existing downgrade refusal would have made any older binary sharing the same temp caches (an unrebased branch on a shared runner) fail its vision/audio loads. Artifacts with a newer schema now live in a schema-named subdirectory (mlxcel-xla-aux-artifact-v3-operator-numeric/) chosen in one shared helper, qualify_auxiliary_artifact, so v1 and v3 entries coexist. Verified on GB10 by alternating main and branch binaries on qwen3-vl-2b-instruct-4bit: each reused its own entry, no refusal, identical greedy output.
  • iree-compile --version is memoized per compiler path, size and mtime, so building one artifact spawns the compiler at most once; generation identity strings are unchanged.
  • Final narrow suite: cargo test -p mlxcel-xla --lib --profile test-fast 435 passed; --features iree,micro-oracle over the runtime, manifest, contract and probe modules 105 passed. cargo check --features cuda,xla-iree --all-targets and cargo check --no-default-features --features xla-diagnostics --all-targets pass.
  • The pre-existing world-writable temp cache location for all XLA artifacts is tracked separately in fix(security): make the XLA artifact caches private to the user #3248.

Test plan

  • cargo test -p mlxcel-xla --lib --profile test-fast: 433 passed (baseline 422 plus 11 new: per-family pins at default CUDA and CPU precision, one stale-cache rejection plus recompile per field class (operator dtype, accumulator, association, precision, compiler, runtime, device), v1 to v3 one-time replacement, observer and backend identity)
  • cargo test -p mlxcel-xla --lib --features iree,micro-oracle --profile test-fast over the runtime, manifest and probe modules: 103 passed
  • cargo clippy -p mlxcel-xla --lib --tests -D warnings with no features, iree, and iree,micro-oracle,diagnostics; cargo fmt --check
  • Real checkpoints on GB10 (MLXCEL_BACKEND=xla, gpu-lock, -n 24 --temp 0), baseline binary from main vs this branch, each branch load run twice. Pass 1 replaced the v1 manifest with v3, pass 2 reused it (VMFB and manifest mtimes unchanged), and greedy output was byte-identical across all three runs for: llava-interleave-qwen-0.5b-bf16 (vision_runtime), qwen2-vl-2b-instruct-4bit (qwen2_vl_runtime, host-dequantized Q4 vision), qwen3-vl-2b-instruct-4bit, molmo-7b-d-0924-4bit (in-graph Q4 dequant, MLXCEL_XLA_CONTEXT_CAPACITY=512), molmo2-4b, youtu-vl-4b-instruct, all on cuda; gemma-3n-e4b-it-4bit audio on local-task

Not covered: no Phi4MM checkpoint on this host (unit pins only). Pre-existing failures on main block some planned runs: Gemma3n's text decode graph fails iree-compile on CUDA (failed to distribute, e2b and e4b), hence CPU for its audio runtime; qwen2.5-vl-3b OOMs in text prefill at a 16410-token prompt; llava-1.5-7b-4bit rejects its config for IREE vision; gemma-3-4b hits the request-sized context capacity. Youtu-VL emits repeated D before and after.

Closes #3211

Every production auxiliary artifact (LLaVA/SigLIP/Gemma3 vision, Qwen2-VL and Qwen2.5-VL main and diagnostics, Qwen3-VL, Molmo, Molmo2, Youtu-VL main and diagnostics, Gemma3n audio encode and merge, Phi4MM audio) now writes the v3 manifest instead of the v1 legacy one, so a cached VMFB whose stored numeric identity differs is rejected by verify_auxiliary_manifest and recompiled before IreeAuxiliaryModule::load runs it.

operator_numeric_contract gains one shared builder (production.rs) that turns a family's declared operator list (families.rs), the Precision value its emitter used, the checkpoint dtype classes its loader observed, and the IREE backend identity into an OperatorNumericContractSet. Fixed-precision vision emitters now take their precision from a named constant the runtime also binds, and Gemma3n audio does the same, so the contract cannot drift from the graph. The kernel IREE picks per dispatch stays recorded as unobserved; this binds identity and claims no oracle qualification.

compiler_version and runtime_archive_identity move from numeric_probe.rs to iree_identity.rs, shared by the probes and production. build.rs bakes the SHA-256 of the linked libiree_runtime_unified.a for the dist, CUDA, ROCm, and macOS layouts, since the CUDA source build has no dist archive to read at run time.

Tests pin each family's operator set at its default CUDA and CPU precision, reject a stale cache for one mutation per field class (operator dtype, accumulator, association, precision, compiler, runtime, device), and cover the one-time v1 to v3 replacement. Text-only token/decode bundles, their goldens, and all tolerances are unchanged.

Closes #3211
@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:high High priority area:architecture Architecture and code structure changes area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 22, 2026
Gemma3n audio dequantizes affine weights on the host with BF16 rounding after the multiply and after the add, while Qwen2-VL and Qwen3-VL dequantize in F32. Both resolved to the same affine contract (host-affine-dequantized, separate-multiply-then-add), so switching Gemma3n's dequantizer would leave the manifest digest unchanged and a stale cached artifact would be reused.

Add AffineEvaluationOrder::SeparateMultiplyThenAddBf16Rounded and AffineExecution::HostBf16Sequential, which resolves to it. Gemma3n now selects its dequantizer from one GEMMA3N_AUDIO_AFFINE constant and binds the same value into both audio contracts, so the rounding and the digest cannot drift apart. Existing identities for the other families and the micro-oracle probes are unchanged.

A new test asserts the BF16-rounded and F32 host dequantization produce different contract identities. Contract and Gemma3n lib tests pass with and without the iree feature; clippy with iree is clean.

Refs #3211
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Bind a v3 operator numeric contract (built from the emitter's own precision, observed checkpoint dtypes, and IREE compiler/runtime/device identity) into every production auxiliary artifact, without claiming qualification or changing tolerances.

Findings Addressed

  • Gemma3n host dequantization rounds to BF16 after the multiply and after the add, but its contract matched the F32 host dequant of Qwen2-VL/Qwen3-VL, so changing the rounding would not change the digest (HIGH). Fixed in 5c0bf52: new separate-multiply-then-add-bf16-rounded evaluation order, selected by one GEMMA3N_AUDIO_AFFINE constant that also picks the dequantizer, plus a test that the two host dequant modes produce different identities.

Remaining Items

  • bake_runtime_archive_digest emits rerun-if-changed even when the archive is missing, so that build script reruns on every build until it appears (LOW). Kept: it lets the digest self-heal once the archive exists, and every linking layout already asserts the archive is present.
  • The family pins omit the round= and dequant= fields; they are derived from pinned fields and covered by the field-class rejection tests (LOW).
  • Real-checkpoint loads under gpu-lock were not part of this review.

Verification

  • All stated requirements implemented (every listed new_legacy_unqualified site replaced; the remaining calls are in tests and aux_smoke)
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable (probe identity helpers moved to iree_identity.rs)
  • No unintended structural changes (fixed-F32 emitters only add .with_precision(F32), which equals the Builder::new() default, so their MLIR is unchanged)
  • Tests pass (contract and Gemma3n lib tests with and without iree; clippy --features iree clean)

@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review

No CRITICAL or HIGH findings in this diff, so nothing was auto-fixed. Checked: the v3 manifest read/verify/replace path, the build.rs archive hashing, the per-tensor dtype observer, and the Gemma3n load reorder.

  • A v3 manifest written for a different operator contract is never accepted: verify_auxiliary_manifest compares operator_numeric_contract_sha256 and the artifact digest, and reject_manifest_downgrade only allows same-schema or upgrade replacement before the recompile. The read and replace code itself is unchanged by this PR.
  • build.rs reads and hashes a roughly 3 MB libiree_runtime_unified.a once per build-script run. It adds rerun-if-changed on the same archive that the root build.rs already requires for linking, so it does not add any reruns.
  • CheckpointSourceObserver holds three counters and one Option. Each loader calls it per tensor on tensor.dtype() alone, so it makes no copies and cannot grow without bound.
  • Gemma3n: each contract is now built right after its own load_weights. Encode and merge weights still load in the same order with the same lifetimes, so peak memory is unchanged and nothing loads twice.

Remaining items (not fixed here)

  • MEDIUM (operational): this PR is the first to write v3 manifests in production. A binary built before it that reads the same /tmp/mlxcel-xla-* cache now gets the hard error refusing to replace auxiliary artifact manifest ... from schema v3 with v1 instead of recompiling. On shared self-hosted runners and dev boxes, unrebased branches will fail vision/audio loads until the cache is cleared. The refusal is the intended downgrade policy, so this needs a decision rather than a patch: document it, or key the cache directory by schema.
  • LOW (perf): each production contract now runs iree-compile --version a second time, once in production_backend and again in the existing generation identity. That is about 45 ms per artifact at load time and could be shared.
  • Pre-existing, out of scope: every XLA VMFB cache (iree.rs and all auxiliary runtimes) lives in a fixed-name std::env::temp_dir() directory with no ownership or permission check. Every manifest field can be computed from public inputs, so a different local user on a multi-user host could pre-create the directory and plant a matching VMFB plus manifest. The PTX cache got this hardening in fix(cuda): harden the JIT PTX disk cache's trust and keying #2743. A separate issue is recommended.

Verified: cargo clippy -p mlxcel-xla --lib --tests --features iree -D warnings is clean; the aux_manifest (iree, micro-oracle) tests pass (14) and the operator_numeric_contract tests pass (17).

This PR is the first to write v3 auxiliary manifests in production. An older binary sharing the fixed temp_dir() caches (mlxcel-xla-vision-vmfb and the other auxiliary cache dirs) would hit reject_manifest_downgrade on the v3 manifest and fail its vision and audio loads instead of recompiling.

aux_manifest gains schema_scoped_vmfb_path and qualify_auxiliary_artifact. A v1 contract keeps its caller path; every numeric schema lives in a sibling directory named after its manifest schema. All production call sites (and the micro-oracle probes) qualify through qualify_auxiliary_artifact and load the path it returns, so old and new binaries keep separate entries and the downgrade refusal still guards each schema's own directory.

iree_identity::compiler_version_output memoizes the trimmed iree-compile --version stdout per compiler path, size and mtime. The six generation-identity helpers and compiler_version share it, so one artifact build spawns the compiler at most once; the generation identity strings are unchanged.

Validated with the mlxcel-xla lib tests (435 without features, 105 with iree,micro-oracle over the manifest, contract, runtime and probe modules), clippy -D warnings with iree and with iree,micro-oracle,diagnostics, and fmt. A new test publishes v1 at the legacy path, qualifies v3 into the schema directory, and alternates both without a refusal or a third compile. On GB10, qwen3-vl-2b-instruct-4bit alternated between the main binary and this branch (main, branch, main, branch): main kept its v1 entry, the branch compiled once into the v3 directory, neither recompiled afterwards, and all four greedy outputs matched the earlier run.

Refs #3211
Rewrap the paragraph describing the operator-numeric v3 manifest and its schema-named cache subdirectory to the file's 80-column style, which had a few overlong lines and a missing comma. No content change. The repository has no Korean counterpart of this document, and the mlxcel-xla README does not describe the artifact cache layout or manifest schema, so neither needed edits.

Validation: cargo fmt --all -- --check and cargo clippy -p mlxcel-xla --lib --tests --features iree -- -D warnings pass.

Refs #3211
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 22, 2026
The env-var-docs gate flagged MLXCEL_XLA_IREE_RUNTIME_SHA256, which the mlxcel-xla build script sets through cargo:rustc-env and iree_identity.rs reads with option_env!. Operators never set it, so it joins MLXCEL_XLA_IREE_DIST in the build-internal allowlist instead of the user-facing docs.

Refs #3211
@inureyes
inureyes marked this pull request as ready for review November 22, 2026 19:45
@inureyes
inureyes merged commit 4cd537c into main Nov 22, 2026
32 of 33 checks passed
@inureyes
inureyes deleted the feature/issue-3211-bind-operator-contracts branch November 22, 2026 19:45
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:architecture Architecture and code structure changes area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(xla): bind operator numeric contracts into production artifacts

1 participant