Skip to content

feat(xla): run the operator micro-oracles against MLX on CUDA - #3269

Merged
inureyes merged 5 commits into
mainfrom
feature/issue-3212-cuda-mlx-micro-oracles
Nov 23, 2026
Merged

inureyes merged 5 commits into
mainfrom
feature/issue-3212-cuda-mlx-micro-oracles

Conversation

@inureyes

@inureyes inureyes commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

The micro-oracle executor now runs the emitted IREE operation on cuda and hip as well as local CPU, and mlxcel_xla::run_production_probe takes an external reference with its backend identity, so mlxcel-xla never calls MLX. The new xla_mlx_micro_oracle example (--features cuda,xla-micro-oracle) supplies MLX CUDA references through mlxcel-core and produces production-mlx reports. Every family reference-check example pins MLX_ENABLE_TF32=0 before MLX starts, refuses any other value, and records it.

  • Probe artifacts get their operator contract from the production builder (production_operator_contract, emitter revision mlxcel-xla-stablehlo-emitter-v1); the SSCP convolution reuses Gemma3n's own subsample.convolution declaration and the subsampler's operand-layout and BF16-result helpers, factored out of sscp_conv without changing its emission order.
  • Comparisons: the canonical suite's modes for the f32 operators (ExactBits for dense matmul, residual add, cumsum); Q4 uses the Qwen same-input --atol/--rtol 0.08; the convolution uses ExactBits, because its BF16-exact fixture keeps every window sum exact in F32 under any accumulation order (the Gemma3n stage gate's max_abs 5e-3 plus RMS 5e-4 has no single element-wise mode). No new tolerance.
  • The example refuses a binary whose Q4 kernel sources changed since build or lack the fix(cuda): drain cp.async before qmm_sm80 epilogue smem store (#910) #923 qmm_sm80_r2 revision, and writes Q4 reports in a separate q4_post_923_reports section.

Evidence (GB10 spark-102, all GPU work under gpu-lock)

MLX fc344c98, overlays sha256 543a0239..., Q4 kernel sha256 abbae771..., MLX_ENABLE_TF32=0, CUDA archs 121, NVIDIA GB10 sm_121. IREE compiler 3.12.0rc20260721 @ dc9601f8, runtime libiree-runtime-sha256-41529a0b..., --iree-hal-target-device=cuda.

Operator Mode Result max_abs
dense matmul ExactBits pass 0
RMSNorm 2e-6/2e-5 pass 0
LayerNorm 2e-6/2e-5 pass 2.98e-8
softmax 2e-6/2e-5 pass 2.98e-8
SiLU projection 3e-6/3e-5 pass 4.77e-7
GELU projection 3e-6/3e-5 pass 4.77e-7
residual add ExactBits pass 0
cumsum [2,130] ExactBits pass 0
SSCP conv2d [1,32,128,1] x [128,3,3,1], stride 2 ExactBits pass 0
Q4 qmv, 1x256 -> 128 (MLX path qmv) 0.08/0.08 pass 1.95e-3
Q4 qmm, 16x256 -> 128 (MLX path qmm_sm80) 0.08/0.08 pass (bit-identical) 0

Every report is production-mlx with production_qualified=false, because IREE exposes no dispatch kernel identity. Kernel paths come from MLXCEL_TRACE_ARCH=1 runs with --only q4-qmv and --only q4-qmm. With MLX_ENABLE_TF32=1 the example exits before MLX starts.

Greedy token comparison, tests/fixtures/test_image.png, prompt "Describe this image.", -n 64 --temp 0, XLA with MLXCEL_BACKEND=xla MLXCEL_XLA_DEVICE=cuda MLXCEL_XLA_CONTEXT_CAPACITY=16512:

xla_qwen3_vl_deepstack_check gains --token-ids (teacher-forced replay of a real prompt), top-5 logits and top-2 margins for IREE, eager, and eager on IREE vision, report-only per-block same-input vision controls for blocks 2-23, and a same-input language control. Qwen3VLModel::get_input_embeddings_from_features shares the existing merge, DeepStack and MRoPE steps with the production path through one private helper.

Two more pre-existing findings: the CLI sizes an XLA Qwen-VL image request from the worst-case floor at DEFAULT_MAX_PIXELS (16410 positions for a 224x224 image that expands to 64 tokens), and the request-sized 32768 shape runs out of CUDA memory in prefill_embeddings. An explicit 16512 capacity works.

Test plan

  • cargo test -p mlxcel-xla --lib --profile test-fast --features iree,micro-oracle filters numeric_probe, production_probe_ops, gemma3n_audio, numeric_ops, operator_numeric_contract (all pass)
  • cargo clippy -p mlxcel-xla --lib --tests --features iree,micro-oracle -- -D warnings, and --lib --features iree
  • cargo clippy --features cuda,xla-diagnostics,xla-reference-diagnostics,xla-micro-oracle --examples -- -D warnings
  • xla_numeric_probe local-task: all 9 canonical reports pass unchanged
  • xla_mlx_micro_oracle on cuda: 11 of 11 pass
  • python3 scripts/ci/check_env_var_docs.py, cargo fmt --check
  • cargo check --features cuda,xla-iree --all-targets and --no-default-features --features xla-diagnostics --all-targets
  • spike/openxla/test_youtu_vl_reference_oracle.py (15 pass; the oracle now accepts mlx_enable_tf32=0 on the mlxcel capture only)

Closes #3212

The micro-oracle executor only accepted local IREE CPU targets and compared against hand-written Rust references, so every report was a canonical decomposition and NumericOracleClaim::ProductionMlx was never produced.

The executor now resolves an IREE toolchain for local-*, cuda, and hip, and runs a probe artifact against any reference closure. The new public entry run_production_probe takes an external reference with its NumericBackendIdentity and builds the artifact's operator contract with the production contract builder, so the probe carries the identity scheme production v3 manifests use. Probes cover dense matmul, Q4 qmv (1 row) and qmm (16 rows, MLX qmm_sm80) against emitted dequant_affine plus the F16 projection, RMSNorm, LayerNorm, softmax, SiLU and GELU projections, residual add, cumsum, and the Gemma3n first SSCP convolution, which shares its operand layout and BF16 result helpers with the production subsampler. Comparisons reuse the canonical suite's modes; Q4 uses the Qwen same-input threshold and the convolution the Gemma3n audio stage bound.

The xla_mlx_micro_oracle example (cuda, xla-micro-oracle) supplies MLX CUDA references and records MLX commit, overlay and Q4 kernel digests (refusing pre-#923 or stale kernels), MLX_ENABLE_TF32, compiled CUDA architectures, and device. Every family reference-check example now pins MLX_ENABLE_TF32=0 before MLX starts and refuses any other value.

Refs #3212
@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
The Qwen3-VL DeepStack oracle could only score a synthetic four-token prompt and reported nothing about how close the top-1 decision was, so a greedy divergence against eager MLX could not be told apart from an XLA defect.

- `--token-ids` replays an actual unexpanded prompt plus teacher-forced generated tokens, so the final-position logits can be read at any greedy index.
- The report records the top-5 logits and the top-2 margin for IREE, for eager MLX, and for eager MLX fed the IREE vision features.
- Every vision block now gets a same-input projection control. Blocks 0 and 1 stay gated as before; blocks 2 and later are reported under `same_input_report` and never gated, so no threshold changes.
- A same-input language control runs the eager language model on the IREE main-merger and DeepStack features through the new `Qwen3VLModel::get_input_embeddings_from_features`, which shares the merge, DeepStack-state and MRoPE-state code with `get_input_embeddings`.
- Each comparison records the reference peak magnitude and the element that most exceeds the tolerance.

On `tests/fixtures/test_image.png` at generated index 8 (GB10, CUDA) the IREE top-2 are 6437 at 22.8980 and 4285 at 22.8781 (margin 0.0198). Eager MLX gives 4285 at 22.875 and 6437 at 22.75, a margin of 0.125, which is one bf16 step at that magnitude. When eager MLX is fed the IREE vision features the two tokens tie exactly at 23.0. Every same-input control passes, and all three post-injection language layers pass with zero failures when both sides see the same vision input. The divergence is a near-tie inside the reported drift (final-logit max_abs 0.31 chained, 0.26 same-input), not an XLA defect.
The production-MLX SSCP convolution probe gated only the Gemma3n audio stage max_abs bound of 5e-3 and reported the stage RMS bound of 5e-4 without enforcing it, so the probe admitted outputs the family gate would reject.

The fixture makes the comparison exact by construction: the BF16-exact dyadic operands give products that are multiples of 2^-14, and every 3x3 window sum stays far inside the 24-bit F32 significand, so any F32 accumulation order or FMA contraction yields the same sum and both backends round one value to BF16. The probe now uses ExactBits, and a unit test checks the window-sum bound on the actual fixture so a future fixture change cannot silently void the argument.

Validated with the mlxcel-xla production and numeric_probe unit tests, clippy on mlxcel-xla, and xla_mlx_micro_oracle on GB10 cuda under gpu-lock: 11 of 11 pass, the SSCP report in exact-bits mode with max_abs 0.

Refs #3212
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Run the operator micro-oracles against production MLX on CUDA (ProductionMlx reports with full backend identity), and pin MLX_ENABLE_TF32=0 in every family reference-check example. The Qwen3-VL token-exact criterion moved to a follow-up issue by decision.

Findings Addressed

  • The SSCP convolution probe gated only the Gemma3n stage max_abs 5e-3 and reported the RMS 5e-4 clause without enforcing it, which is looser than the family gate. It now uses ExactBits, justified by the fixture (every window sum is exact in F32 under any order), with a unit test on the bound. Re-run on GB10 cuda: 11 of 11 pass, SSCP in exact-bits mode at max_abs 0 (2457c0d). (HIGH)

Remaining Items

  • identity.rs hashes the overlay tree from the checkout at run time, not the tree the binary was built from; only the Q4 sources are checked against the compiled bytes. A build-time digest from mlxcel-core/build.rs would close it. (MEDIUM)
  • Gemma3, Youtu-VL, Molmo, Gemma3n audio, Phi4 audio and the SSCP oracle record MLX_ENABLE_TF32 on stderr only; Youtu-VL writes a JSON manifest without it. Meets "print the value", but the JSON reports are not uniform. (LOW)
  • The dense f32 probes reuse the tiny canonical shapes ([2,3] matmul), so the MLX CUDA kernel they exercise may differ from prefill-sized production GEMMs. (LOW)

Verification

  • All stated requirements implemented (Qwen3-VL token-exact moved to a follow-up issue by decision)
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass

@inureyes

Copy link
Copy Markdown
Member Author

Security and Performance Review

No CRITICAL, HIGH, or MEDIUM findings, so no fix commits were pushed. The two items the implementation review already left open (overlay digest computed from the checkout, TF32 missing from some JSON reports) are not repeated here.

Checked

  • Probe device gate: validate_probe_device accepts only local*, cuda, and hip, and the VMFB cache key hashes the target flags and compiler path, so a CPU artifact cannot be reused for a GPU probe or the reverse.
  • External reference entry: run_production_probe labels the report ProductionMlx from caller-supplied identity, but production_qualified still requires an observed candidate algorithm, which IREE GPU targets never report, and run_bounded_numeric_oracle rejects a reference whose output shape differs from the candidate's. Nothing in the production path consumes these reports.
  • MLX_ENABLE_TF32 pin: runs as the first statement of main before argument parsing or any thread, accepts only the exact value 0, and refuses everything else (including " 0" and empty).
  • --output and file reads: operator-supplied local paths in a diagnostic example; no privilege boundary.
  • unsafe MLX calls in references.rs: the raw-pointer operands (biases, weight, bias) are bound locals that outlive the call.
  • Production hot path: the qwen3_vl.rs refactor only moves the merge into merge_vision_features and passes borrows; no extra copy, cast, or eval on get_input_embeddings. The gemma3n_audio_ops.rs helper split emits the same MLIR op sequence (the diagnostic BF16-result convolution is still emitted before the F32-accumulate one), so production graph text and cache keys are unchanged.

LOW

  • The numeric-probe VMFB cache still lives at temp_dir()/mlxcel-xla-numeric-probes, and this PR now also loads cuda/hip probe artifacts from it. A planted GPU VMFB with a matching manifest could fake a passing oracle. This is the fix(security): make the XLA artifact caches private to the user #3248 class and PR fix(security): make the XLA artifact caches private to the user #3289 already routes this cache through cache_dir::xla_cache_dir(XlaCache::NumericProbes); because this PR moved that line from execute_probe into execute_artifact, whichever PR merges second has to reapply the replacement there by hand.
  • identity.rs::collect_files uses path.is_dir(), which follows symlinks, so a symlink cycle inside the overlay tree would recurse until stack overflow. The tree is repository-controlled, so this only matters for a corrupted checkout.

…fest

The micro-oracle identity walker used Path::is_dir, which follows symlinks, so a symlink loop under the overlay tree could recurse without bound. It now classifies entries with DirEntry::file_type, which does not follow links, and hashes a symlink as its link target under a "symlink:" prefix so the digest stays deterministic. A unit test covers a loop.

The Youtu-VL reference example now records MLX_ENABLE_TF32 in its JSON manifest through the shared mlx_reference_policy helper, like the Qwen, vision and llava reports. The Youtu-VL oracle accepts that optional top-level key on the mlxcel capture only, requires the value 0, and still rejects it on the HF reference; a test covers all three cases (the Python test could not be run here because numpy is not installed; both files compile).

Validation:
- cargo fmt --check
- cargo clippy -p mlxcel-xla --lib --tests --features iree,micro-oracle -- -D warnings
- cargo clippy --features cuda,xla-diagnostics,xla-reference-diagnostics,xla-micro-oracle --examples -- -D warnings
- cargo test -p mlxcel-xla --lib --profile test-fast --features iree,micro-oracle numeric_probe (16 passed)

Refs #3212
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 23, 2026
@inureyes
inureyes marked this pull request as ready for review November 23, 2026 02:18
…da-mlx-micro-oracles

# Conflicts:
#	CHANGELOG.md
#	src/lib/mlxcel-xla/src/numeric_probe.rs
@inureyes
inureyes merged commit ee969c6 into main Nov 23, 2026
64 of 65 checks passed
@inureyes
inureyes deleted the feature/issue-3212-cuda-mlx-micro-oracles branch November 23, 2026 06:54
@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): run the operator micro-oracles against MLX on CUDA

1 participant