Skip to content

perf(granitemoehybrid): run the gated RMSNorm in the input dtype - #1723

Merged
inureyes merged 2 commits into
mainfrom
perf/granitemoehybrid-native-dtype-rmsnorm
Sep 9, 2026
Merged

inureyes merged 2 commits into
mainfrom
perf/granitemoehybrid-native-dtype-rmsnorm

Conversation

@inureyes

@inureyes inureyes commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

What changed

GraniteMoeHybridRMSNormGated::forward promoted y, gate and weight to float32, ran the gate and the norm there, and cast back. Every operand is already the input dtype, so the promotion buys nothing arithmetically and costs everywhere: the mixer widens the residual stream to f32 on each decode step, and each later matmul promotes its own weight to match. The forward now runs in the dtype it was given and returns it with no cast.

Throughput

Both hosts, before and after alternating, five runs per arm, mlxcel-bench-decode, binaries built from this branch and its merge-base. M1 Ultra at -n 200 --ignore-eos with Time Machine confirmed stopped; M5 Max at -n 128.

checkpoint host before (median) after (median) ratio before range after range
granite-4.0-h-350m-4bit M1 Ultra 272.29 293.88 1.079x 265.46-273.10 286.78-294.35
granite-4.0-h-350m-4bit M5 Max 438.67 519.19 1.184x 432.00-440.36 515.75-522.27
granite-4.0-h-tiny-4bit M1 Ultra 107.43 112.17 1.044x 105.51-107.78 109.95-112.74
granite-4.0-h-tiny-4bit M5 Max 200.09 209.27 1.046x 196.36-200.32 208.82-209.36

Every range is disjoint. The two hosts agree on direction, and on tiny they agree on size. The gap on 350m, 1.184x against 1.079x, is not explained. The obvious explanation was that #1718 already promotes M5's reductions so the local promotion is redundant there, but that reasoning does not hold: #1718 changed the bridge's reduction helpers and this code path runs MLX's fused fast_rms_norm. Recorded as unexplained rather than attributed.

Output

The arms are not byte-identical, which is expected once the arithmetic changes, so scripts/ab_output_equality.sh reports EQUAL on 350m and DIFFERS on tiny. Both tiny outputs are fluent and factually correct; greedy decoding diverges at one position and never reconverges, which is exactly the case byte-identity cannot judge.

The teacher-forced logit trace judges it. granite-4.0-h-tiny-4bit, tests/fixtures/wikitext2_excerpt.txt, gating on disagreement at decided positions (gap >= 2.0):

shape positions top-1 disagreement on decided positions worst rank perplexity
decode width 1, 512 context 400 19 (4.75%) 0 / 156 (0.00%) 3 +0.145%
prefill width 256 1024 51 (4.98%) 0 / 386 (0.00%) 3 +0.410%

Every disagreement falls in the undecided band: at decode width, 17 of 19 are at a top-two gap under 0.5, and the 2-to-inf bands are 0 of 156. The control, the baseline binary traced against itself at the same width, is 0 of 400, so the 4.75% is attributable to this change rather than to run-to-run jitter.

Safety

The comment justified the f32 promotion with a NaN in a half-precision x^2 sum on M5 Max. #1718 located that fault in the bridge's reduction helpers, where a bfloat16 max or sum returned NaN for a finite input on roughly one call in six, and fixed it by accumulating those reductions in f32.

That fix does not subsume this promotion. It routes max_all, min_all, sum_all, mean_all and their axis forms through reduce_in_f32 (mlx_cxx_bridge.cpp:840); fast_rms_norm (line 3675) calls mlx::core::fast::rms_norm directly and never reaches those wrappers. Two code paths that do not meet. So the safety of this removal rests on the guard, on the hardware where the fault reproduces, and the struct comment says so: if MLX changes that kernel the measurement has to be repeated rather than inferred.

tests/mamba2_hybrid_finite.rs with this arm applied: M5 Max 3 runs of 3, 3.23s / 3.25s / 3.28s, no panic under MLXCEL_REQUIRE_MODELS=1. M1 Ultra 3 runs of 3 at 7.96-8.03s. The elapsed times matter here: the same guard reports 2 passed ... finished in 0.00s on a tree where the checkpoints do not resolve, so a green run is not by itself evidence that it ran. #1722 is what makes it resolve on the M1 Ultra layout; the M5 Max tree resolves it already because its models/ symlink points at the store root.

Also on M5 Max with the arm applied: granite-4.0-h-350m-4bit, granite-4.0-h-tiny-4bit and granite-4.0-3b-vision-4bit all answer "The capital of France is Paris.", and all three recall COBALT-3317 correctly from a roughly 3000-token context. An overflow in the norm would surface at that length.

Note on the commit history

The first commit's body ends "The M5 Max half is required before merge", written when this was a draft. That half is now in the tables above. Its comment also called the promotion "a second, local copy of the same defense" as #1718, which reads as the opposite of what the paragraph above establishes; the follow-up commit corrects it. Both were left unamended rather than force-pushed.

`GraniteMoeHybridRMSNormGated::forward` promoted the whole computation to float32 and cast back at the end. The promotion escapes through the mixer: the residual stream widens to f32 and each later matmul promotes its own weight to match, on every decode step.

Measured on M1 Ultra with `mlxcel-bench-decode`, before and after alternating, five runs each at `-n 200 --ignore-eos`, Time Machine confirmed stopped. granite-4.0-h-350m-4bit 272.29 to 293.88 tok/s (1.079x, ranges 265.46-273.10 and 286.78-294.35, disjoint). granite-4.0-h-tiny-4bit 107.43 to 112.17 tok/s (1.044x, ranges 105.51-107.78 and 109.95-112.74, disjoint).

Output changes, and the teacher-forced logit trace says where. At decode width (1 position, 512 tokens of context, 400 positions) top-1 disagreement is 19/400 = 4.75% overall and 0/156 on decided positions; at prefill width 256 over 1024 positions it is 51/1024 = 4.98% overall and 0/386 decided. Every disagreement sits in the undecided band, worst rank 3, perplexity +0.145% and +0.410%. The control, the baseline binary against itself at the same width, is 0/400.

The float32 promotion was a local defense against a NaN in a half-precision `x^2` sum on M5 Max. #1718 found that fault in the bridge's reduction helpers and fixed it there, but it did not touch MLX's fused `fast_rms_norm`, so this removal is not covered by that fix and rests on the guard instead. `tests/mamba2_hybrid_finite.rs` passes 3 runs of 3 with the arm applied, on M1 Ultra, where the fault does not reproduce. The M5 Max half is required before merge.
…1718

The comment introduced with this change called the float32 promotion "a second, local copy of the same defense" as #1718, which reads as "that fix covers this site, so removing this is safe by inference". It does not, and the PR body says the opposite.

Checked in the bridge: #1718 routes `max_all`, `min_all`, `sum_all`, `mean_all` and their axis forms through `reduce_in_f32` (`mlx_cxx_bridge.cpp:840`), while `fast_rms_norm` (line 3675) calls `mlx::core::fast::rms_norm` directly and never reaches those wrappers. Two code paths that do not meet.

What actually covers the removal is `tests/mamba2_hybrid_finite.rs`, 250 forwards per checkpoint, 3 runs of 3 on both hosts with the promotion removed, plus a 3000-token needle recall on M5 Max. The comment now says that, and says the measurement has to be repeated rather than inferred if MLX changes the kernel. Comment only.
inureyes added a commit that referenced this pull request Sep 9, 2026
…de (#1724)

Three families carry the same local float32 promotion against the M5 Max half-precision `x^2` NaN. #1723 removes it from `granitemoehybrid.rs` for 1.079x. The other two were measured as well, and both answers are "leave it", so nothing in the code moved and the finding had nowhere to live. Written where the next person will look, which is the struct itself, not an issue.

`nemotron_h.rs`: measured on M1 Ultra with nvidia-nemotron-3-nano-30b-a3b-4bit, five runs per arm, 96.51 against 97.31 tok/s. Ranges are disjoint but the gain is 1.008x, and 0.8% does not buy giving up a NaN defense whose coverage after #1718 is not established, since that fix changed the bridge's reduction helpers and not MLX's fused `fast_rms_norm`.

`falcon_h1.rs`: there is nothing to measure rather than nothing measured. `mamba_rms_norm` defaults to false and the only Falcon-H1 checkpoint in the store spells it `false`, so the gated-norm `forward` is never reached on any checkpoint available to test with.
@inureyes
inureyes merged commit 587d6ac into main Sep 9, 2026
13 checks passed
@inureyes
inureyes deleted the perf/granitemoehybrid-native-dtype-rmsnorm branch October 6, 2026 12:12
@inureyes inureyes added this to the 0.7 milestone Nov 30, 2026
@inureyes inureyes added priority:medium Medium priority status:done Completed type:performance Performance improvements labels Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant