Skip to content

perf(moe): wire minimax to the fused decode-MoE kernel - #390

Merged
inureyes merged 2 commits into
mainfrom
feature/issue-304-minimax-fused-moe
Jun 21, 2026
Merged

inureyes merged 2 commits into
mainfrom
feature/issue-304-minimax-fused-moe

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Wire minimax's SparseMoeBlock::forward to the shared forward_fused_kernel path (#268 series, default-on since #285). For single-token decode with MLXCEL_FUSED_MOE enabled (default), the MoE block now dispatches the fused kernel; prefill and the disabled-env path fall through to experts.forward + moe_weighted_sum unchanged.

What changed

  • src/models/minimax.rs: replaced the single-path expert dispatch with the one-branch fused/fallback pattern matching qwen2_moe and dots1. The inputs to forward_fused_kernel (x_flat, topk_indices, norm_scores) are exactly what the fallback path passed to experts.forward and moe_weighted_sum; no routing logic was reordered or recomputed.
  • src/models/switch_layers.rs: promoted fused_moe_enabled_from from private to pub(crate). The function was already documented as a unit-testing seam; this makes it reachable from per-model test files without touching process-global env state.
  • src/models/minimax_tests.rs (new): config parsing tests (topology fields, quantization accessors including the gate 8-bit override, RoPE/head_dim) plus fused_dispatch_gate_conditions_for_minimax, which asserts the compound gate (n_tokens == 1 && fused_moe_enabled_from(...)) evaluates correctly for single-token+default-env (kernel attempted), single-token+disabled (kernel skipped), and prefill (kernel skipped).
  • src/models/mod.rs: registered minimax_tests under #[cfg(test)].
  • docs/benchmark_results/fused-moe-decode-kernel-design.md: updated "Models covered" count from ten to eleven, added minimax to the wired list with a note that runtime validation is hardware-blocked (see below).

Activation template

minimax experts are SwiGLU (SiLU), so the kernel runs with the default swiglu act=0 template. No GeGLU or relu2 variant is needed.

Routing inputs to the fused path

minimax uses sigmoid scoring with an e_score_correction_bias added only for expert selection (biased scores determine which experts to call; the selected scores then come from the original unbiased sigmoid output). The normalized unbiased scores (norm_scores) go to both moe_weighted_sum on the fallback path and forward_fused_kernel on the fused path. The kernel input triple (x_flat, topk_indices, norm_scores) is identical between the two paths; the fused branch only branches after routing is complete.

Verification

cargo check -p mlxcel --lib                   # clean
cargo test -p mlxcel --lib minimax            # 23 passed
cargo clippy -p mlxcel --lib --tests -- -D warnings  # clean
cargo fmt -p mlxcel                           # no changes

Runtime validation (hardware-blocked, deferred)

MiniMax-Text-01 is ~456B parameters; even a 4-bit quantized variant would require 230-260 GB of unified memory, which exceeds the 128 GB available on the test hardware. Greedy temp-0 throughput and output-parity measurements against the MLXCEL_FUSED_MOE=0 baseline are therefore deferred until a smaller MiniMax variant or a larger-memory machine becomes available. This matches the documented treatment of hunyuan-moe-a13b-bf16 in #294 (same does-not-fit-128 GB class). The dispatch wiring and code-path tests land independently of runtime validation.

Closes #304

Wire minimax's SparseMoeBlock to the shared forward_fused_kernel path
(#268 series, default-on since #285), following the one-branch pattern
from qwen2_moe / dots1. For single-token decode with MLXCEL_FUSED_MOE
enabled, the block now calls experts.forward_fused_kernel; for prefill
or when the env gate is off, it falls through to experts.forward +
moe_weighted_sum unchanged.

minimax experts are SwiGLU (SiLU), so the kernel runs with the default
swiglu act=0 template with no additional activation variant required.
Routing uses sigmoid with an e_score_correction_bias that biases expert
selection; the fused path receives the normalized unbiased scores, matching
exactly what moe_weighted_sum received on the non-fused path. The kernel
auto-falls-back for any unsupported quant config (non-affine, 3-bit, etc).

Also expose fused_moe_enabled_from as pub(crate) so that per-model test
files can assert the gating conditions without touching process-global env
state (the function is already documented as a testing seam).

Runtime validation is hardware-blocked: MiniMax-Text-01 at ~456B does not
fit 128 GB unified memory, so greedy temp-0 throughput and output-parity
measurements are deferred until a smaller variant or larger-memory machine
is available. The PR body documents this explicitly.

Changes:
- src/models/minimax.rs: add fused dispatch branch in SparseMoeBlock::forward
- src/models/switch_layers.rs: fused_moe_enabled_from fn -> pub(crate)
- src/models/minimax_tests.rs: new config + dispatch gate unit tests
- src/models/mod.rs: register minimax_tests module
- docs/benchmark_results/fused-moe-decode-kernel-design.md: update models covered
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:low Low priority area:models Model architectures, weights, loading, metadata labels Jun 21, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Wire minimax SparseMoeBlock::forward to the shared fused single-token decode-MoE kernel (#268 series, default-on), mirroring qwen2_moe/dots1/qwen3_moe.

Findings Addressed

None. No CRITICAL/HIGH/MEDIUM/LOW issues found in the independent review or in a supplementary automated review; no auto-fix was required and the working tree is unmodified.

Parity verification (the load-bearing check)

  • The fused and fallback branches receive the identical routing triple: the same x_flat, the same topk_indices (line 270), and the same norm_scores (line 280). Neither is recomputed, reordered, or renormalized inside the branch. e_score_correction_bias affects only top-k index selection (argpartition on neg_biased); the selected scores come from the unbiased sigmoid output, then normalized once. This matches mlx-lm minimax routing and the sibling pattern.
  • Shape: the fused branch reshapes the kernel output to [1, hidden_dim]; moe_weighted_sum returns [1, hidden_dim] for single-token decode (sum_axis(weighted, -2)). The two branches are drop-in interchangeable. hidden_dim is orig_shape[last], the correct dimension.
  • Gate: array_shape(&x_flat)[0] == 1 && fused_moe_enabled(), byte-identical to qwen2_moe/dots1/qwen3_moe. forward_fused_kernel returns None for any unsupported config (non-affine, gate/up not 4/8-bit, down not 4/6/8-bit, mismatched bits/group_size, Dff over the bound, non-single-token); the match { Some/None } falls back cleanly. minimax-m2 3-bit experts return None at the first bit check, so the documented hardware-blocked path stays on gather_qmm.
  • Fallback path is byte-for-byte unchanged from before the PR (experts.forward + moe_weighted_sum(&expert_out, &norm_scores, dtype)).

Verification

  • All stated requirements implemented (dispatch wired, fallback preserved, doc updated, hardware blocker documented not claimed)
  • No placeholder/mock code remaining
  • Integrated into project code flow (real SparseMoeBlock::forward, not orphaned)
  • Project conventions followed (one-branch pattern, pub(crate) test seam, no em dashes, no AI attribution, Closes #304)
  • Existing modules reused (forward_fused_kernel, moe_weighted_sum, fused_moe_enabled from switch_layers)
  • No unintended structural changes
  • Tests pass: cargo test -p mlxcel --lib minimax 23 passed; cargo clippy -p mlxcel --lib --tests -- -D warnings clean

Remaining Items

  • Greedy temp-0 throughput and output-parity vs the MLXCEL_FUSED_MOE=0 baseline (LOW): hardware-blocked, correctly deferred. MiniMax-Text-01 ~456B does not fit 128 GB unified memory. Documented in the PR, the doc, and the test module.

The bonus smoke loaded minimax-m2-3bit with the new dispatch wiring and
generated 16 coherent tokens at 17.6 tok/s on M1 Ultra. Because 3-bit quant
is not a supported fused-kernel input, forward_fused_kernel returned None and
decode fell back to gather_qmm cleanly (no crash, no OOM). This confirms the
wiring is non-breaking via the fallback path. Fused-path throughput and
output-parity validation remain blocked on a fitting 4-bit or 8-bit minimax
checkpoint.
@inureyes

Copy link
Copy Markdown
Member Author

PR Finalization Complete

Summary

  • Tests: existing gate-condition unit tests are sufficient (checkpoint-free); no new tests added per the task scope
  • Documentation: added a concise smoke-test note to the minimax entry in docs/benchmark_results/fused-moe-decode-kernel-design.md recording that minimax-m2-3bit runs coherently via the auto-fallback path on M1 Ultra, and that fused-path validation remains blocked on a fitting 4-bit or 8-bit checkpoint
  • Lint/Format: cargo fmt clean (no Rust files changed); cargo clippy -p mlxcel --lib --tests -- -D warnings passed with no warnings

All checks passing. Ready for merge.

@inureyes inureyes added status:done Completed and removed status:review Under review labels Jun 21, 2026
@inureyes
inureyes merged commit a882286 into main Jun 21, 2026
4 checks passed
@inureyes
inureyes deleted the feature/issue-304-minimax-fused-moe branch June 21, 2026 18:55
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
* perf(moe): wire minimax to the fused decode-MoE kernel

Wire minimax's SparseMoeBlock to the shared forward_fused_kernel path
(lablup#268 series, default-on since lablup#285), following the one-branch pattern
from qwen2_moe / dots1. For single-token decode with MLXCEL_FUSED_MOE
enabled, the block now calls experts.forward_fused_kernel; for prefill
or when the env gate is off, it falls through to experts.forward +
moe_weighted_sum unchanged.

minimax experts are SwiGLU (SiLU), so the kernel runs with the default
swiglu act=0 template with no additional activation variant required.
Routing uses sigmoid with an e_score_correction_bias that biases expert
selection; the fused path receives the normalized unbiased scores, matching
exactly what moe_weighted_sum received on the non-fused path. The kernel
auto-falls-back for any unsupported quant config (non-affine, 3-bit, etc).

Also expose fused_moe_enabled_from as pub(crate) so that per-model test
files can assert the gating conditions without touching process-global env
state (the function is already documented as a testing seam).

Runtime validation is hardware-blocked: MiniMax-Text-01 at ~456B does not
fit 128 GB unified memory, so greedy temp-0 throughput and output-parity
measurements are deferred until a smaller variant or larger-memory machine
is available. The PR body documents this explicitly.

Changes:
- src/models/minimax.rs: add fused dispatch branch in SparseMoeBlock::forward
- src/models/switch_layers.rs: fused_moe_enabled_from fn -> pub(crate)
- src/models/minimax_tests.rs: new config + dispatch gate unit tests
- src/models/mod.rs: register minimax_tests module
- docs/benchmark_results/fused-moe-decode-kernel-design.md: update models covered

* docs(moe): record minimax-m2-3bit auto-fallback smoke on M1 Ultra

The bonus smoke loaded minimax-m2-3bit with the new dispatch wiring and
generated 16 coherent tokens at 17.6 tok/s on M1 Ultra. Because 3-bit quant
is not a supported fused-kernel input, forward_fused_kernel returned None and
decode fell back to gather_qmm cleanly (no crash, no OOM). This confirms the
wiring is non-breaking via the fallback path. Fused-path throughput and
output-parity validation remain blocked on a fitting 4-bit or 8-bit minimax
checkpoint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:low Low priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(moe): wire minimax to the fused decode-MoE kernel

1 participant