Repository navigation
perf(models): gate Mamba2 mixer eval boundary to M5 Max - #266
Merged
Merged
Conversation
The SSM/Mamba2 mixers (falcon_h1, granitemoehybrid, plamo2) forced an unconditional `mlxcel_core::eval(&result)` at the end of every mixer forward. It exists as an M5 Max (Metal GPU Family 4) workaround: the NAx GEMM kernels can fuse a lazy float32/float16 graph into NaN within a single Metal command buffer, so a materialization boundary is required there. But the eval ran on every Apple Silicon, and on non-M5 chips it is pure overhead, a per-layer GPU sync that blocks cross-layer decode pipelining. The effect was a 3-5x decode regression on the Mamba2 hybrids versus mlx-lm, while the dense / MoE / ShortConv paths (which build no such graph) stayed at parity. Gate the eval on `hardware::is_m5_neural_accelerator()` (`has_neural_accelerator && macos_supports_na`), per the CLAUDE.md "Apple Silicon precision" rule that M5-specific paths must check the neural-accelerator flags rather than fire unconditionally. Validated on M1 Ultra (128 GB, macOS 26.5.1), decode tok/s, best-of-3, temp-0 greedy with byte-identical output before and after: Falcon-H1-90M: 57 -> ~180 (mlx-lm 288) granite-4.0-h-tiny: 36 -> 92 (mlx-lm 110) plamo-2-1b: 76 -> 108 M5 Max behaviour is unchanged: the gate returns true there, so the eval boundary still fires. nemotron_h carries the same per-mixer eval plus a separate chunked-eval path and is left for a dedicated follow-up.
This was referenced Jun 13, 2026
inureyes
added a commit
that referenced
this pull request
Jun 13, 2026
Completes the M5-eval-gating sweep started in #266. nemotron_h's Mamba2 mixer ended its Rust `forward` with an unconditional `mlxcel_core::eval(&result)`, the same M5 Max (Metal GPU Family 4) NaN-avoidance workaround that #266 gated in falcon_h1 / granitemoehybrid / plamo2. It was the last ungated instance. Gate it on `hardware::is_m5_neural_accelerator()`. Scope note (important): unlike the other three hybrids, nemotron_h's single-token decode does NOT run the Rust mixer body. It dispatches to a fully-fused C++ path (`forward_fused` -> `fused_mamba2_forward`) that has no per-layer eval, so this eval was only ever on the prefill / fallback path. Measured on M1 Ultra (nemotron-h-30b-4bit, 24-token prompt): prefill 188 -> 197 tok/s, decode unchanged (55.1 -> 54.9, fused). Greedy temp-0 output coherent and unchanged. M5 Max behaviour unchanged (gate returns true there; prefill also keeps its separate `needs_chunked_eval` materialization). The real nemotron decode gap vs mlx-lm (55 vs 92 tok/s, 0.60x) is MoE-bound: block profiling shows the 23 MoE layers are ~60% of decode time, Mamba ~33% (fused), attention ~7%. That is the same MoE decode path that makes dots.llm1 lag, so it is tracked in #268, not here. Closes #267.
This was referenced Jun 13, 2026
perf(models): general MoE decode gap vs mlx-lm (dots.llm1 0.51x, qwen3_moe 0.69x, nemotron MoE)
#268
Closed
inureyes
added a commit
that referenced
this pull request
Jun 13, 2026
Consolidates the perf sweep and root-cause analysis for the mlxcel vs mlx-lm decode gap across the SSM hybrid (#266/#267, fixed), MoE (#268), and dense (#269) families measured on M1 Ultra. GPU profiling (mlxcel-gpu-profiling hooks) localizes the MoE gap: decode is ~95% GPU-bound (not FFI/dispatch), not at the memory wall (~16-20% bandwidth, GPU idle on small kernels), and mlxcel emits 773 AsType ops/token versus mlx-lm's 0. Tracing every candidate source (quantized wrappers, multiply_scalar, the bf16->f16 scale conversion, compiled activations, fallback norms) shows the dtypes are consistently f16 and the AsType are diffuse MLX-internal promotions that fuse at eval, the same class as the prior Gemma3n bf16 decode-gap investigation where removal measured only ~2.6%. Documents the ruled-out sources and the realistic fix paths (a fused decode-MoE kernel or an AsType reduction campaign) with a validation harness. Documentation only; the perf fix itself stays open under #268.
inureyes
added a commit
that referenced
this pull request
Jun 13, 2026
Consolidates the perf sweep and root-cause analysis for the mlxcel vs mlx-lm decode gap across the SSM hybrid (#266/#267, fixed), MoE (#268), and dense (#269) families measured on M1 Ultra. GPU profiling (mlxcel-gpu-profiling hooks) localizes the MoE gap: decode is ~95% GPU-bound (not FFI/dispatch), not at the memory wall (~16-20% bandwidth, GPU idle on small kernels), and mlxcel emits 773 AsType ops/token versus mlx-lm's 0. Tracing every candidate source (quantized wrappers, multiply_scalar, the bf16->f16 scale conversion, compiled activations, fallback norms) shows the dtypes are consistently f16 and the AsType are diffuse MLX-internal promotions that fuse at eval, the same class as the prior Gemma3n bf16 decode-gap investigation where removal measured only ~2.6%. Documents the ruled-out sources and the realistic fix paths (a fused decode-MoE kernel or an AsType reduction campaign) with a validation harness. Documentation only; the perf fix itself stays open under #268.
12 tasks
inureyes
added a commit
that referenced
this pull request
Sep 7, 2026
`falcon_h1` and `granitemoehybrid` ended every mixer forward with an `eval` on M5 Max, a NaN workaround for the lazy float32/float16 SSM graph fusing inside one Metal command buffer. #266 gated it to M5 after measuring the cost elsewhere, but the M5 branch itself was never measured. It is a per-layer GPU sync on every decode token, and on this host it was most of the decode time. Measured on M5 Max at the sweep condition (512-token prompt, 128 generated), decode tok/s against mlx-lm 0.31.3 on the same machine: falcon-h1-tiny-90m 160.7 -> 583.20 (31% -> 112% of mlx-lm) granite-4.0-h-350m 132.6 -> 482.50 (23% -> 84%) granite-4.0-h-tiny 76.5 -> 212.51 (33% -> 90%) Greedy output is byte-identical without the boundary, on a short prompt and on a 24-sentence prompt generating 256 tokens, and no NaN appears. An interval sweep was tried first and rejected: evaluating every K-th mixer recovers only part of the gain and never saturates (falcon-h1 at K=2/4/8/16 reaches 1.47x/2.11x/2.46x/2.69x of the 3.31x available), so a middle value keeps a cost without buying back anything measurable. `plamo2` keeps its gate: it carries the same code but measures 1.00x with the boundary removed, so changing it would be unmotivated. `nemotron_h` keeps its own: single-token decode returns early into `forward_fused` and never reaches that eval. `tests/mamba2_hybrid_decode_finite.rs` is what makes the removal safe to keep. It decodes 64 tokens on both families and fails on an empty generation, a collapsed single-token distribution, or replacement characters, which is how a NaN would surface. Closes #1685
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
The SSM/Mamba2 mixers (falcon_h1, granitemoehybrid, plamo2) forced an unconditional `mlxcel_core::eval(&result)` at the end of every mixer forward. It exists as an M5 Max (Metal GPU Family 4) workaround: the NAx GEMM kernels can fuse a lazy float32/float16 graph into NaN within a single Metal command buffer, so a materialization boundary is required there. But the eval ran on every Apple Silicon, and on non-M5 chips it is pure overhead, a per-layer GPU sync that blocks cross-layer decode pipelining. The effect was a 3-5x decode regression on the Mamba2 hybrids versus mlx-lm, while the dense / MoE / ShortConv paths (which build no such graph) stayed at parity. Gate the eval on `hardware::is_m5_neural_accelerator()` (`has_neural_accelerator && macos_supports_na`), per the CLAUDE.md "Apple Silicon precision" rule that M5-specific paths must check the neural-accelerator flags rather than fire unconditionally. Validated on M1 Ultra (128 GB, macOS 26.5.1), decode tok/s, best-of-3, temp-0 greedy with byte-identical output before and after: Falcon-H1-90M: 57 -> ~180 (mlx-lm 288) granite-4.0-h-tiny: 36 -> 92 (mlx-lm 110) plamo-2-1b: 76 -> 108 M5 Max behaviour is unchanged: the gate returns true there, so the eval boundary still fires. nemotron_h carries the same per-mixer eval plus a separate chunked-eval path and is left for a dedicated follow-up.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…#271) Completes the M5-eval-gating sweep started in lablup#266. nemotron_h's Mamba2 mixer ended its Rust `forward` with an unconditional `mlxcel_core::eval(&result)`, the same M5 Max (Metal GPU Family 4) NaN-avoidance workaround that lablup#266 gated in falcon_h1 / granitemoehybrid / plamo2. It was the last ungated instance. Gate it on `hardware::is_m5_neural_accelerator()`. Scope note (important): unlike the other three hybrids, nemotron_h's single-token decode does NOT run the Rust mixer body. It dispatches to a fully-fused C++ path (`forward_fused` -> `fused_mamba2_forward`) that has no per-layer eval, so this eval was only ever on the prefill / fallback path. Measured on M1 Ultra (nemotron-h-30b-4bit, 24-token prompt): prefill 188 -> 197 tok/s, decode unchanged (55.1 -> 54.9, fused). Greedy temp-0 output coherent and unchanged. M5 Max behaviour unchanged (gate returns true there; prefill also keeps its separate `needs_chunked_eval` materialization). The real nemotron decode gap vs mlx-lm (55 vs 92 tok/s, 0.60x) is MoE-bound: block profiling shows the 23 MoE layers are ~60% of decode time, Mamba ~33% (fused), attention ~7%. That is the same MoE decode path that makes dots.llm1 lag, so it is tracked in lablup#268, not here. Closes lablup#267.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
Consolidates the perf sweep and root-cause analysis for the mlxcel vs mlx-lm decode gap across the SSM hybrid (lablup#266/lablup#267, fixed), MoE (lablup#268), and dense (lablup#269) families measured on M1 Ultra. GPU profiling (mlxcel-gpu-profiling hooks) localizes the MoE gap: decode is ~95% GPU-bound (not FFI/dispatch), not at the memory wall (~16-20% bandwidth, GPU idle on small kernels), and mlxcel emits 773 AsType ops/token versus mlx-lm's 0. Tracing every candidate source (quantized wrappers, multiply_scalar, the bf16->f16 scale conversion, compiled activations, fallback norms) shows the dtypes are consistently f16 and the AsType are diffuse MLX-internal promotions that fuse at eval, the same class as the prior Gemma3n bf16 decode-gap investigation where removal measured only ~2.6%. Documents the ruled-out sources and the realistic fix paths (a fused decode-MoE kernel or an AsType reduction campaign) with a validation harness. Documentation only; the perf fix itself stays open under lablup#268.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
While benchmarking the newly ported model families against
mlx-lmon this M1 Ultra, the three Mamba2-SSM hybrids decoded 3-5x slower thanmlx-lmon identical checkpoints, while the dense / MoE / ShortConv families sat at parity. Root cause: every SSM mixer (falcon_h1,granitemoehybrid,plamo2) ended its forward with an unconditionalmlxcel_core::eval(&result).That eval is an M5 Max (Metal GPU Family 4) numerical workaround. The M5 Max NAx GEMM kernels can fuse a lazy float32/float16 graph into NaN inside a single Metal command buffer, so the SSM mixers force a materialization boundary to split the graph. The boundary is correct on M5 Max but ran on every Apple Silicon. On non-M5 chips it is a per-layer GPU sync that serializes decode and blocks cross-layer pipelining, with no numerical benefit (those chips have no NAx kernels and never hit the fusion bug). Transformer / MoE / ShortConv layers build no such graph, so they were unaffected, which is exactly the observed pattern.
This also violated the repo's own rule (CLAUDE.md, "Apple Silicon precision"): M5-specific code paths must gate on
hw.has_neural_accelerator && hw.macos_supports_na.Change
mlxcel-core/hardware.rs: addis_m5_neural_accelerator()(has_neural_accelerator && macos_supports_na), the documented gate for M5-Max-only numerical workarounds.get_hardware()isOnceLock-cached, so the per-layer call is free.falcon_h1.rs,granitemoehybrid.rs,plamo2.rs: wrap the per-mixereval(&result)inif mlxcel_core::hardware::is_m5_neural_accelerator(). M5 Max keeps the boundary (behaviour unchanged); every other chip skips it and lets the decode graph pipeline.Validation (M1 Ultra, 128 GB, macOS 26.5.1, MLX
a6ec712)Decode tok/s,
mlxcel-bench-decodevsmlx-lm0.31.3, raw prompt (no chat template), 100 tokens after a warmup pass, best-of-3 with cooldown, temp-0 greedy:falcon_h1)granitemoehybrid)plamo2)Greedy output is byte-identical before and after on every hybrid (e.g. plamo-2-1b still emits "...Paris is on the Île de la Cité"), confirming the eval was scheduling-only overhead here.
cargo fmtclean;cargo clippy -p mlxcel-core -- -D warningsclean.Scope / follow-ups
nemotron_hcarries the same per-mixer eval plus a separateneeds_chunked_eval/eval_intervalstrategy and a debug-timing path; gating it needs its own analysis and a 30B validation pass, so it is intentionally left out of this PR.dots.llm1decodes at ~0.51x of mlx-lm (a non-hybrid MoE / MLA path) and the denseapertus/seed_osssit at ~0.83x. Tracked as future perf work.