Repository navigation
perf(granitemoehybrid): run the gated RMSNorm in the input dtype - #1723
Merged
Merged
Conversation
`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.
inureyes
marked this pull request as ready for review
September 9, 2026 09:01
…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.
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.
What changed
GraniteMoeHybridRMSNormGated::forwardpromotedy,gateandweightto 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-eoswith Time Machine confirmed stopped; M5 Max at-n 128.Every range is disjoint. The two hosts agree on direction, and on
tinythey agree on size. The gap on350m, 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 fusedfast_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.shreports 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):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^2sum on M5 Max. #1718 located that fault in the bridge's reduction helpers, where a bfloat16maxorsumreturned 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_alland their axis forms throughreduce_in_f32(mlx_cxx_bridge.cpp:840);fast_rms_norm(line 3675) callsmlx::core::fast::rms_normdirectly 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.rswith this arm applied: M5 Max 3 runs of 3, 3.23s / 3.25s / 3.28s, no panic underMLXCEL_REQUIRE_MODELS=1. M1 Ultra 3 runs of 3 at 7.96-8.03s. The elapsed times matter here: the same guard reports2 passed ... finished in 0.00son 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 itsmodels/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-4bitandgranite-4.0-3b-vision-4bitall answer "The capital of France is Paris.", and all three recallCOBALT-3317correctly 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.