Repository navigation
Description
TL;DR
Evaluate a Rust-native StableHLO emitter as the production model-authoring path for the compiler-family backend, as a parallel alternative to the JAX-reference frontend validated in #449 Phase 1. Concretely: emit the SAME Llama-3.2-1B prefill and decode-step StableHLO from Rust (no Python in the authoring path), and validate it greedy-decodes token-exact against the HF transformers reference and matches the JAX-produced graph structurally. This is a frontend-only evaluation: the runtime and the mlxcel integration are unaffected because the StableHLO artifact is the swappable boundary. It resolves the open authoring-frontend decision in ADR 0004.
This runs in PARALLEL with #449 Phase 2a (int4 lowering on the proven JAX harness). The int4 lowering question is frontend-independent, so the two threads do not block each other.
Background
mlxcel is a Rust inference runtime (see ADR 0004, docs/adr/0004-compute-backend-session-seam-and-stablehlo-family.md). The compiler-family backend (OpenXLA / StableHLO, motivating targets FuriosaAI, Tenstorrent, OpenXLA) runs models as compiled StableHLO graphs. #449 Phase 1 validated that a model written once as a JAX reference exports to StableHLO via jax.export and runs token-exact (48/48 vs HF temp-0) on both PJRT and IREE with no custom_call. The findings are in spike/openxla/FINDINGS.md on the spike/openxla-export-449 branch, with the JAX model in spike/openxla/model_jax.py.
Two facts make a Rust emitter worth evaluating as the production authoring path:
- JAX is only a build-time (authoring) tool; the exported StableHLO runs Python-free on PJRT/IREE. mlxcel's "No Python in the request path" principle is already satisfied. But a JAX-reference authoring path puts compiler-family model definitions in Python, separate from the Rust tree where the MLX models live, and adds JAX to the authoring/build workflow.
- The frontend is swappable behind the StableHLO artifact boundary. A Rust emitter would keep compiler-family model definitions in-tree in Rust, remove Python from the authoring path entirely, and give direct control over the emitted graph (useful for int4 dequant insertion and custom_call to vendor kernels later). The downstream (StableHLO to runtime) and the eventual refactor: redraw ComputeBackend as an inference-session engine contract and move the MLX path behind it (byte-identical) #448-contract integration are unchanged whichever frontend wins.
Goal
Produce a Rust emitter that builds the Llama-3.2-1B prefill and decode_step StableHLO graphs (the same two graphs #449 exported from JAX), and show it is correct and portable, so we can decide JAX-reference versus Rust-emitter as the production authoring path on evidence rather than preference.
Toolchain options to evaluate (pick in P0, record the choice)
melior(safe Rust bindings to MLIR) plus the StableHLO dialect registered into the MLIR context, building the module programmatically and serializing to bytecode. Most robust (real SSA/shape handling by MLIR), but requires the StableHLO dialect to be available to the bindings (a build/integration question to confirm first).- Programmatic StableHLO text emission from Rust (generate the
.mlirtext with a parameterized emitter that loops over layers), then feed it toiree-compile(CLI or C-API). Lighter dependency (no MLIR C++ bindings), but you manage SSA value naming and shapes yourself. - Either way, run the result through the IREE runtime (C-API via Rust FFI) or PJRT; CPU target is fine (matches feat: OpenXLA reference backend - export-route spike through 4-bit quantized decode #449 Phase 1).
Reference / ground truth
spike/openxla/on branchspike/openxla-export-449:model_jax.py(the reference forward, KV layout, RoPE llama3 scaling, mask, GQA), the exporteddecode_step.stablehlo.mlir/prefill.stablehlo.mlir,check_correctness.py,run_spike.py, andFINDINGS.md(op inventory, shapes, bucketing). The Rust emitter must reproduce the SAME graph shape and numerics.- Reference model:
unsloth/Llama-3.2-1B-Instruct(ungated bf16, single safetensors). Config: 16 layers, hidden 2048, 32 q / 8 kv heads, head_dim 64, intermediate 8192, RoPE theta 5e5 with llama3 scaling, rms_norm_eps 1e-5, tied embeddings, vocab 128256. - The numeric gotchas feat: OpenXLA reference backend - export-route spike through 4-bit quantized decode #449 flagged carry over verbatim: RoPE half-split (not interleaved), llama3 inv_freq scaling, additive causal mask with -1e30, GQA head repeat, fp32 compute over bf16 weights. Getting these wrong diverges immediately, exactly as in the JAX path.
Phased plan
P0: toolchain smoke. Stand up the chosen Rust StableHLO toolchain and prove it end to end on a trivial graph: emit a single dot_general (a matmul) module from Rust, compile it (IREE or PJRT), run it, and confirm the result against a numpy reference. Deliverable: the toolchain choice recorded, plus a working trivial round-trip. This is the real gate; if the Rust StableHLO toolchain does not stand up cleanly, that is the finding.
P1: emit Llama decode_step (and prefill) from Rust. Build the two graphs with weights as graph inputs (matching the JAX signature: decode_step(params, token, pos, cache_len, kcache, vcache) -> (logits, kcache, vcache); KV as fixed-capacity [L, MAX_SEQ, n_kv, head_dim], dynamic-update-slice writes, bucketed prefill). Greedy-decode the same chat-templated prompt #449 used and verify token-exact (48/48) against the HF transformers temp-0 reference, and compare the StableHLO op inventory against the JAX-produced graph (same op kinds, no custom_call). Deliverable: the Rust emitter plus a findings doc.
Acceptance criteria
- A Rust emitter (no Python in the authoring path) produces a Llama-3.2-1B
decode_stepStableHLO that greedy-decodes token-exact against the HF reference, matching the feat: OpenXLA reference backend - export-route spike through 4-bit quantized decode #449 JAX result. - The emitted StableHLO is portable (runs on IREE and/or PJRT) with no custom_call, matching the JAX graph's op profile.
- A findings doc compares the two authoring paths: per-architecture authoring effort (lines, complexity), maintainability and debuggability, graph control (int4 insertion, custom_call), toolchain/build weight, and a recommendation for the production path. Feeds the ADR 0004 authoring-frontend decision.
- Standalone under
spike/(suggestedspike/rust-emitter/), no effect on mlxcel default crates or builds.
Constraints
- Frontend-only evaluation. Do NOT touch the refactor: redraw ComputeBackend as an inference-session engine contract and move the MLX path behind it (byte-identical) #448 session contract, the runtime integration, or mlxcel's default crates/build. The StableHLO artifact is the boundary.
- Work on
lablup/mlxcel. Branch and PR per repo conventions: English, conventional commits, no AI attribution, no em dashes. Stage only your own files by explicit path; never tree-widegit add -A, stash, clean, or reset. - CPU target is sufficient (matches feat: OpenXLA reference backend - export-route spike through 4-bit quantized decode #449 Phase 1). No GPU needed for this evaluation.
References
- ADR 0004 (
docs/adr/0004-compute-backend-session-seam-and-stablehlo-family.md), the authoring-frontend open item this resolves. - feat: OpenXLA reference backend - export-route spike through 4-bit quantized decode #449 and
spike/openxla/on branchspike/openxla-export-449(the JAX ground truth: model, exported StableHLO, findings). - refactor: redraw ComputeBackend as an inference-session engine contract and move the MLX path behind it (byte-identical) #448 (the session contract the production backend integrates with later; out of scope here).
- MLIR / StableHLO: https://openxla.org/stablehlo ;
melior(Rust MLIR bindings): https://github.com/mlir-rs/melior ; IREE: https://iree.dev .
Activity
feat: Rust-native StableHLO emitter spike for Llama-3.2-1B (#451) (#455)
feat: emit Llama-3.2-1B prefill from the Rust StableHLO emitter (#451 …
inureyes commentedon Jun 27, 2026
Rust-native StableHLO emitter: done, recommended as the production authoring path
A dependency-free Rust crate emits the Llama-3.2-1B StableHLO as text; the existing
iree-compilelowers it and the IREE runtime runs it. No Python in the authoring path, nomelior, no MLIR/LLVM C++ build.dot_generalround-trips through IREE, exact versus numpy.decode_stepgreedy-decodes token-exact (48/48) versus HF temp-0. 20 op kinds, 145dot_general, zerocustom_call, zero f64.prefillemits too; the one new op wasstablehlo.gather, as the P1 findings predicted. Full Rust-emitted prefill plus decode is token-exact (48/48). The KV write usesdynamic_update_slice, so it stays GPU-portable.Recommendation for the ADR 0004 authoring-frontend decision: adopt the Rust text emitter as the production authoring path and keep the JAX reference as the per-architecture oracle (author and verify in JAX, emit from Rust, diff against the JAX
.mlirand tokens). The emitter wins on graph control (int4 dequant insertion andcustom_callrouting are local edits to oneBuilder::linearapplied to all matmuls) and on toolchain weight (cargo plus iree-compile only). Cost is roughly 1.5x to 2.5x the JAX per-architecture line count plus hand-spelled numerics, which the oracle workflow keeps from becoming a correctness risk.Findings:
spike/rust-emitter/FINDINGS.md.feat: wire OpenXLA/IREE execution into mlxcel-xla and the CLI (#449 P…
feat: emit the OpenXLA graphs from config.json at load (#449 M3 Stage…
inureyes commentedon Jul 21, 2026
Implemented and validated in merged PRs #455 and #457. The standalone Rust emitter covers both decode_step and bucketed prefill, was recorded as 48/48 token-exact against the HF reference through IREE, uses portable StableHLO without custom_call, and documents the production authoring-path recommendation. Current validation on main: cargo fmt/check/test for spike/rust-emitter pass, and fresh decode/prefill MLIR generation succeeds. Closing this stale tracking issue as completed.
feat: Rust-native StableHLO emitter spike for Llama-3.2-1B (lablup#451)…
feat: emit Llama-3.2-1B prefill from the Rust StableHLO emitter (labl…
feat: wire OpenXLA/IREE execution into mlxcel-xla and the CLI (lablup…
feat: emit the OpenXLA graphs from config.json at load (lablup#449 M3…