Skip to content

feat: evaluate a Rust-native StableHLO emitter as the compiler-family authoring path (spike) #451

Closed

Description

@inureyes

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 .mlir text with a parameterized emitter that loops over layers), then feed it to iree-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 branch spike/openxla-export-449: model_jax.py (the reference forward, KV layout, RoPE llama3 scaling, mask, GQA), the exported decode_step.stablehlo.mlir / prefill.stablehlo.mlir, check_correctness.py, run_spike.py, and FINDINGS.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_step StableHLO 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/ (suggested spike/rust-emitter/), no effect on mlxcel default crates or builds.

Constraints

References

Activity

  1. added
    status:ready
    type:enhancement
    priority:medium
    area:architecture
    on Jun 26, 2026
  2. inureyes commented on Jun 27, 2026

    @inureyes
    MemberAuthor

    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-compile lowers it and the IREE runtime runs it. No Python in the authoring path, no melior, no MLIR/LLVM C++ build.

    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 .mlir and tokens). The emitter wins on graph control (int4 dequant insertion and custom_call routing are local edits to one Builder::linear applied 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.

  3. added
    status:done
    and removed
    status:ready
    on Jul 21, 2026
  4. inureyes commented on Jul 21, 2026

    @inureyes
    MemberAuthor

    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.

  5. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:architecturepriority:mediumstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions