Repository navigation
fix(models): stack per-expert NVFP4 linears of a Gemma 4 MoE export - #3198
Merged
inureyes merged 3 commits intoNov 22, 2026
Merged
Conversation
…tacking
An NVFP4 export from NVIDIA ModelOpt or compressed-tensors stores each expert
as its own linear, and the repack leaves that expert's tensor scale beside its
plane. The Qwen3.5 and Qwen3-Next stacking passes took weight / scales / biases
only, so the scales stayed behind and the expert loader refused the plane
("... was left unstacked"). Both passes now go through one helper,
drain_numbered_expert_planes, which stacks every expert plane leaf, the scale
included, as the DeepSeek-V3, Mistral 4 and PhiMoE loops already do.
NVIDIA ModelOpt and compressed-tensors write a Gemma 4 MoE layer's experts as
one linear each (experts.{e}.{gate_proj,up_proj,down_proj}); the model loads
them stacked under experts.switch_glu, so such an export failed with Missing
weight: ...experts.switch_glu.gate_proj. The Gemma 4 NVFP4 sanitizer now
stacks them after the repack, each expert's tensor scale with its plane.
# Conflicts: # src/models/switch_layers.rs
Member
Hardening reviewReviewed against #3197 after #3196 merged.
No code fixes were needed. Validation on Apple Silicon (Metal), on the merged tree:
|
12 of 19 tasks
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
The NVFP4 exports of Gemma 4 26B A4B store a layer's experts as one linear each, and the Gemma 4 loader only read them stacked under
experts.switch_glu, sonvidia/Gemma-4-26B-A4B-NVFP4andRedHatAI/gemma-4-26B-A4B-it-NVFP4failed withMissing weight: …experts.switch_glu.gate_proj. The Gemma 4 NVFP4 sanitizer now stacks them after the repack, each expert's tensor scale with its plane.Stacked on #3196. This branch is #3196's commit plus one; only the last commit (
ece47a058) is new. It uses the helper that PR adds. I will rebase once #3196 is merged.Related issues
Closes #3197. Builds on #3195 / #3196.
Type of change
fix— bug fixCause
mlx-lm stores a Gemma 4 MoE layer's experts as
…experts.switch_glu.{gate_proj, up_proj, down_proj}, one[num_experts, …]tensor per leaf. NVIDIA ModelOpt and llm-compressor write…experts.{e}.{proj}as 128 separate linears.sanitize_gemma4_nvfp4_weightsrepacked each of them and stopped; nothing stacked them, andSwitchLinear's fallback for unstacked experts looks underswitch_mlp, the name the other families use.Change
stack_gemma4_per_expert_planes(new, private,src/models/sanitize.rs): for every…expertsthat has an expert 0, drainsexperts.{e}.{gate_proj, up_proj, down_proj}intoexperts.switch_glu.{proj}throughdrain_numbered_expert_planes(fix(models): keep per-expert NVFP4 tensor scales in the Qwen3.5 MoE stacking #3196), so each expert's tensor scale lands in the[num_experts]inv_global_scalebeside the stacked plane.sanitize_gemma4_nvfp4_weightscalls it after the repack. The text-only and the VLM Gemma 4 routes both go through that function (the real checkpoints below took the VLM route; the text-only route is covered by the unit test only). A checkpoint without numbered experts is untouched.Before / after
GB10 (sm_121, CUDA 13.0), release builds of
363a0ba6b(main) and of this PR,generate -n 200 --temp 0,MLXCEL_SDPA_DETERMINISTIC=1. The image is a 448 x 448 red square with a blue square in the middle.main, all four promptsnvidia/Gemma-4-26B-A4B-NVFP4@a19cfe00b(ModelOpt, 11520 expert linears)Error: Failed to load Gemma4 text model: Missing weight: language_model.model.layers.0.experts.switch_glu.gate_proj, exit 1Paris/4/The vast, rhythmic expanse of the sea holds a mysterious depth that connects every shore on Earth./The two main colors are red and blue.RedHatAI/gemma-4-26B-A4B-it-NVFP4@5557756b8(compressed-tensors, 11725 linears)Paris/4/The vast, rhythmic expanse of the sea stretches toward the horizon, whispering ancient secrets with every crashing wave./The two main colors in the image are red and blue.Load: 60 s and 17.7 GB resident for the nvidia export, 64 s and 15.5 GB for the RedHatAI one. Neither logs a layout warning.
Checkpoints that load on #3196 answer byte for byte the same on this PR (13 runs): two text prompts and an image prompt on lmstudio-community/gemma-4-26B-A4B-it-MLX-4bit @
79290e197(the same model with its experts already stacked), axteam/gemma-4-E2B-it-NVFP4 and mlx-community/gemma-4-12B-it-4bit; a text prompt on gemma-3-4b-it-4bit, mgoin/Qwen3-0.6B-NVFP4, iromu/Gemma3-1B-tools-NVFP4 and an mlx-lmnvfp4export of Qwen3.5-35B-A3B.Test plan
Run on this PR's head (tree
604061f70ca8; its parent is #3196's treee3f9e430d94a) on a DGX Spark (GB10).cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features cuda -- -D warningscargo build --release --features cudagemma4_per_expert_planes_are_stacked_under_switch_glu(src/models/sanitize.rs): two layers of three repacked NVFP4 experts come out underexperts.switch_glu, the tensor scales as the multiplier in expert order, each stacked plane loads throughSwitchLinear, the dense MLP and the router of the layer are left alone, and a second pass over the result changes nothing: passcargo test --profile test-fast --features cuda --lib -- gemma4 sanitize switch_layers: 592 passedmake verify-test-cuda(cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 16091 passed, 5 failed, 500 ignored. The five are the ones that fail on unmodified363a0ba6bon this host: fourmla::turbo_latenttests (fix(cuda): reductions with a differing output dtype return garbage (sum over bool, any/all over non-bool) #3184, fixed by fix(cuda): write reductions through the output dtype #3185) andcommands::serve::tests::serve_preflight_kv_mode_on_deepseek_v2_follows_the_absorption_decision. This PR touches none of that codeci.ymljobs a fork PR does not get, by hand:clippy(cargo clippy -p mlxcel --lib --tests -- -D warnings, default features); license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refsbg-digitalservices/Gemma-4-26B-A4B-it-NVFP4): not downloaded, not rungemma4_unifiedMoE export with numbered experts: none at hand. That route has its own sanitizer and is not changedxla-compile: not run (no IREE on this host); no XLA file is touchedcargo deny check: not applicable (no manifest or lockfile change)Notes for reviewers
…experts.0.gate_proj.weight; mlx-lm checkpoints and the fusedexperts.gate_up_projlayout have no such key.global_scale;drain_numbered_expert_planesconcatenates them intoinv_global_scale, whichSwitchLinearapplies per selected expert. A plane without it would be refused byrefuse_unstacked_expert_sidecars, as onmainfor Qwen3.5 before fix(models): keep per-expert NVFP4 tensor scales in the Qwen3.5 MoE stacking #3196.SwitchGeGLUpath. I did not measure speed.Checklist
docs/: not needed (no option or documented behaviour changes)// Used by: ...comments: the new function carries one, and the helper's lists its new caller