Skip to content

feat(xla): keep the quantized MoE expert bank packed on device - #3588

Merged
inureyes merged 3 commits into
mainfrom
feat/issue-3585-packed-moe-experts
Nov 30, 2026
Merged

inureyes merged 3 commits into
mainfrom
feat/issue-3585-packed-moe-experts

Conversation

@inureyes

@inureyes inureyes commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Closes #3585. Refs #3350.

An MLX affine-quantized MoE checkpoint used to dequantize its whole expert bank to f32 at load, so Nemotron-3-Nano-30B-A3B 4-bit needed 117.6 GiB and could not load on GB10. The bank now stays packed (ui32 plus f16/bf16 scales and biases) and is dequantized in the graph; the 30B loads in 23.8 GB.

Design

  • Gate: Config::packed_experts, resolved once by both IREE loaders and read by the emitter and weights::weight_specs, so each expert projection's spec becomes the packed, scales, biases triple in place. MLXCEL_XLA_QUANT_EXPERTS=dequant restores the f32 bank, whose graphs stay byte-identical to an unquantized checkpoint's (unit test). A checkpoint whose expert tensors are not U32 with uniform f16/bf16 metadata warns and dequantizes at load.
  • Default on for cuda, hip, local-task; never on metal (bug(xla): packed int8 (MLXCEL_XLA_QUANT=packed) prefill invoke faults on metal-spirv #613). Default-on because the large quantized MoE checkpoints (this 30B, Qwen3-30B-A3B, Mixtral, Phi-3.5-MoE) cannot load with the f32 bank on GB10. Any quantized MoE that fit before now takes the packed path too, and may be slower there (the 4-bit Nemotron-H fixture decodes at 800 vs 1034 tok/s B=1); dequant is the opt-out. hip stays unvalidated until the ROCm host reports.
  • Decode (single, uniform batch, ragged) gathers only the top-k selected experts. Prefill keeps the masked-dense combine in expert chunks (MLXCEL_XLA_MOE_PREFILL_CHUNK, 512 MiB budget) because a gather prefill at capacity 256 would dequantize about 61 GB per layer. No graph uses a scatter.
  • Routing fix that also changes the path-off graph: the grouped sigmoid router's first-index argmax used a signed i32 minimum reduction, which this IREE CUDA build pads with -1 once a row reaches 32 lanes (minimal repro: [1,w] rows 5,6,... reduce to 5 at w=16 and -1 at w=32/64/128). On main every Nemotron-H router with 32 or more experts picked expert -1 on CUDA. It now reduces the non-negative reversed rank with maximum. No registered golden contained the old form.

Validation (GB10, CUDA)

  • 4-bit Jamba and Nemotron-H MoE fixtures (new spike/openxla/packed_moe_fixture.py, HF fp32 oracle over the exact dequantized weights): structural, token-exact and serve gates pass with the bank packed and with dequant, on CUDA and local-task, and with MLXCEL_XLA_MOE_PREFILL_CHUNK=3 (multi-chunk prefill). jamba-moe-tiny and Jamba-3B unchanged.
  • Nemotron-3-Nano-30B-A3B 4-bit, against mlxcel's MLX eager model on CUDA (mlxcel XLA vs mlxcel MLX on a 4-bit checkpoint, not an independent reference; EOS ends the oracle at 16 tokens): serve reference-exact PASS. Token-exact MISMATCH at index 5: the oracle's bf16 logits tie exactly (3403 vs 34315, margin 0.0), XLA picks 34315 by 0.10. B=1 decode 3.54 tok/s (MLX 7.40), held back by the dequant not fusing into the matvec (track(upstream-iree): fused dequant->matmul for the packed int8 bandwidth win #574). B=4 0.96 tok/s is dominated by prefill: about 17.5 s at capacity 256, because the masked-dense form runs all 128 experts over every static position. Zero scatter in all production modules.
  • Lib tests, clippy -D warnings (--features iree) and fmt pass. Details: spike/openxla/FINDINGS_ssm.md.

An MLX affine-quantized MoE checkpoint used to dequantize its whole expert bank to f32 at load, so Nemotron-3-Nano-30B-A3B 4-bit needed 117.6 GiB (111 GiB of experts) and could not load on GB10. The bank now stays packed (u32 weights plus f16 or bf16 scales and biases) and is dequantized in the graph.

- `Config::packed_experts` is set once by both IREE loaders (`iree_packed_experts::resolve_packed_experts`) and read by the emitter and `weights::weight_specs`, so each expert projection's spec becomes the packed, scales, biases triple in place. On by default except on metal (#613); `MLXCEL_XLA_QUANT_EXPERTS=dequant` restores the f32 bank, whose graphs stay byte-identical to an unquantized checkpoint's.
- Decode graphs (single, uniform batch, ragged) gather only the top-k selected experts and dequantize those rows. Prefill runs the masked-dense combine in expert chunks sized by `MLXCEL_XLA_MOE_PREFILL_CHUNK` (512 MiB transient budget). No graph uses a scatter.
- Routing moves to `moe_routing.rs` and returns the per-pick expert ids.
- The grouped sigmoid router's first-index argmax now reduces the non-negative reversed rank with a maximum. The pinned IREE CUDA compiler pads a signed i32 minimum reduction with -1 once a row reaches 32 lanes, so on main every Nemotron-H router with 32 or more experts picked expert -1 on CUDA (a dropped pick on the dense path, an out-of-bounds gather here). This changes the emitted sigmoid-router graph for every config; no registered golden used it.

New fixtures `qwen2-moe-packed-tiny` and `nemotron-h-packed-tiny` freeze the packed graphs, and `spike/openxla/packed_moe_fixture.py` builds 4-bit Jamba and Nemotron-H MoE checkpoints with an exact-dequant HF oracle.

Refs #3585
Adds the #3585 section to the SSM findings: the 4-bit Jamba and Nemotron-H MoE fixtures pass every gate on CUDA with the bank packed and dequantized, Nemotron-3-Nano-30B-A3B 4-bit loads in 23.8 GB and passes the serve gate, its token gate against mlxcel's MLX oracle diverges at index 5 on an exact bf16 tie in the oracle, no production module contains a scatter, and decode throughput is bound by the unfused dequant-then-matmul.

Refs #3585
@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:architecture Architecture and code structure changes area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 30, 2026
Records the 30B CLI run with the default request-sized capacity, attributes the prefill time to the masked-dense form over all experts and every static position rather than to the missing fusion, corrects the per-prefill figure to about 17.5 s, and adds the multi-chunk prefill fixture gate.

Refs #3585
@inureyes

Copy link
Copy Markdown
Member Author

gfx1151 (ROCm/HIP) validation at 9f4576f8

Host: AMD Ryzen AI Max+ 395 (Radeon 8060S, gfx1151), HIP 7.15.26333 (ROCm 7.15), IREE compiler 3.12.0rc20260721 @ dc9601f, oracle venv torch 2.12.1+cpu / transformers 5.12.1. Each run used a fresh chmod-700 MLXCEL_CACHE_DIR, under the ROCm GPU guard lock. No push.

Rust gates: cargo test -p mlxcel-xla --lib 594 passed; --release --features iree 624 passed; moe_packed 8 passed; validation::tests 10 passed.

Fixtures: both regenerated here and identical to the CUDA host: prompt [4,17,5,29,11,8], jamba ref [15,9,13,14,28,30,12,25,5,28,27,13], nemotron_h ref [11,27,7,30,10,30,27,8,7,30,27,7].

validate_arch (--device hip --batch 4 --requests 8 --maxcap 24; B=1 is the sequential baseline, B=4 the batched serve gate):

Model Mode structural token-exact serve ref-exact B=1 tok/s B=4 tok/s
jamba-moe-q4 packed (default) PASS PASS PASS 2347.2 2281.7
jamba-moe-q4 QUANT_EXPERTS=dequant PASS PASS PASS 2481.7 2392.8
jamba-moe-q4 MOE_PREFILL_CHUNK=3 FAIL (see below) PASS PASS 2318.0 2699.1
nemotron_h-moe-q4 packed (default) PASS PASS PASS 2687.6 3017.8
nemotron_h-moe-q4 QUANT_EXPERTS=dequant PASS PASS PASS 3199.7 2748.5
nemotron_h-moe-q4 MOE_PREFILL_CHUNK=3 FAIL (see below) PASS PASS 2801.4 2672.6
jamba-moe-tiny (f32) regression PASS PASS PASS 2731.4 2540.8
AI21-Jamba-Reasoning-3B regression PASS PASS PASS 6.93 5.19

I checked that the modes actually diverge. The packed cache MLIR carries ui32 expert parameters (61 for jamba, 37 for nemotron_h), and the dequant MLIR has none. Under chunk=3, the jamba prefill has 45 dot_general against 37 with the default chunk (4 experts, so 2 chunks instead of 1), and decode is byte-identical. Jamba 3B matches my #3448 run (6.92 / 5.16 tok/s).

Chunk=3 structural failure: the byte-exact pre-gate inherits MLXCEL_XLA_MOE_PREFILL_CHUNK. With the env set, registered_fixtures_are_byte_exact and emit_all_reproduces_registered_goldens fail on qwen2-moe-packed-tiny/prefill.mlir, because the goldens are emitted with the default chunk. MLXCEL_XLA_QUANT_EXPERTS=dequant does not leak this way. This is a test-isolation gap, not a numeric divergence: rerunning with --skip-structural passes both execution gates (rows above). It could be fixed by having the structural tests clear the env, or by having validate_arch unset it for the pre-gate.

Scatter: packed nemotron_h rerun with an iree-compile wrapper adding --iree-hal-dump-executable-sources-to: 196 dumped executable sources (decode_step and prefill), 0 iree_linalg_ext.scatter. Same gate results. Caveat: modules with the same name (for example decode and decode_ragged_logits_b4) write into one directory, so a later compile can overwrite an earlier file of the same name.

Logs: acceptance/pr3588/9f4576f8c1f1b807a00d4c2065b063e40c07a8d0/ on the ROCm host (va-*.log, rust-*.log, summary.tsv, scatter-count.txt).

@inureyes
inureyes merged commit abd81b0 into main Nov 30, 2026
28 of 36 checks passed
@inureyes
inureyes deleted the feat/issue-3585-packed-moe-experts branch November 30, 2026 12:16
inureyes added a commit that referenced this pull request Nov 30, 2026
#3588 added `MLXCEL_DUMP_PACKED_EXPERTS` and `MLXCEL_MOE_DISPATCH` to the opt-in `#[ignore]` MoE probe dump test in `mlxcel-xla/src/emitter/mod.rs` without listing them, so the `env var docs` job and the Python tooling test `test_repository_passes` fail on main (abd81b0) and on every PR CI run merged against it. Both are read only by that fixture-authoring test, so they join the existing group with `MLXCEL_DUMP_OUT` and `MLXCEL_MOE_N` instead of the user-facing page.

`check_env_var_docs.py` and its unit tests pass locally.
@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:medium Medium priority status:review Under review type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(xla): packed 4-bit MoE expert bank with selected-expert compute

1 participant