Skip to content

fix(models): stack per-expert NVFP4 linears of a Gemma 4 MoE export - #3198

Merged
inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma4-moe-per-expert-nvfp4
Nov 22, 2026
Merged

inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma4-moe-per-expert-nvfp4

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

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, so nvidia/Gemma-4-26B-A4B-NVFP4 and RedHatAI/gemma-4-26B-A4B-it-NVFP4 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.

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 fix

Cause

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_weights repacked each of them and stopped; nothing stacked them, and SwitchLinear's fallback for unstacked experts looks under switch_mlp, the name the other families use.

Change

  • stack_gemma4_per_expert_planes (new, private, src/models/sanitize.rs): for every …experts that has an expert 0, drains experts.{e}.{gate_proj, up_proj, down_proj} into experts.switch_glu.{proj} through drain_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_scale beside the stacked plane.
  • sanitize_gemma4_nvfp4_weights calls 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.

Checkpoint main, all four prompts This PR: capital of France / 2 + 2 / one sentence about the sea / (image) the two main colors
nvidia/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 1 Paris / 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) same error 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-lm nvfp4 export of Qwen3.5-35B-A3B.

Test plan

Run on this PR's head (tree 604061f70ca8; its parent is #3196's tree e3f9e430d94a) on a DGX Spark (GB10).

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings
  • cargo build --release --features cuda
  • The new test by name, gemma4_per_expert_planes_are_stacked_under_switch_glu (src/models/sanitize.rs): two layers of three repacked NVFP4 experts come out under experts.switch_glu, the tensor scales as the multiplier in expert order, each stacked plane loads through SwitchLinear, the dense MLP and the router of the layer are left alone, and a second pass over the result changes nothing: pass
  • cargo test --profile test-fast --features cuda --lib -- gemma4 sanitize switch_layers: 592 passed
  • Negative control, the stacking switched off: the new test fails
  • make 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 unmodified 363a0ba6b on this host: four mla::turbo_latent tests (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) and commands::serve::tests::serve_preflight_kv_mode_on_deepseek_v2_follows_the_absorption_decision. This PR touches none of that code
  • Self-hosted ci.yml jobs 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 refs
  • Validated with real checkpoints: the table above
  • Other exports of this model with the same layout (for example bg-digitalservices/Gemma-4-26B-A4B-it-NVFP4): not downloaded, not run
  • A gemma4_unified MoE export with numbered experts: none at hand. That route has its own sanitizer and is not changed
  • xla-compile: not run (no IREE on this host); no XLA file is touched
  • cargo deny check: not applicable (no manifest or lockfile change)
  • Metal and ROCm: not built or run here

Notes for reviewers

  • Keyed on the tensors. The pass looks for …experts.0.gate_proj.weight; mlx-lm checkpoints and the fused experts.gate_up_proj layout have no such key.
  • Where the scales go. After the repack each expert has a one-element global_scale; drain_numbered_expert_planes concatenates them into inv_global_scale, which SwitchLinear applies per selected expert. A plane without it would be refused by refuse_unstacked_expert_sidecars, as on main for Qwen3.5 before fix(models): keep per-expert NVFP4 tensor scales in the Qwen3.5 MoE stacking #3196.
  • The fused single-token GeGLU kernel reads affine planes only, so these NVFP4 experts take the SwitchGeGLU path. I did not measure speed.

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/: not needed (no option or documented behaviour changes)
  • Updated // Used by: ... comments: the new function carries one, and the helper's lists its new caller

…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.
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata platform:linux Linux (CUDA / packaging) specific labels Nov 22, 2026
# Conflicts:
#	src/models/switch_layers.rs
@inureyes

Copy link
Copy Markdown
Member

Hardening review

Reviewed against #3197 after #3196 merged.

  • Branch update: merged origin/main (with fix(models): keep per-expert NVFP4 tensor scales in the Qwen3.5 MoE stacking #3196 squashed) into the head (f24ad6f6) instead of rebasing, so the fork branch was not force-pushed. The resulting tree is identical to a rebase of ece47a05 alone onto main, and the diff against main is only this PR's change (src/models/sanitize.rs, plus the Used by line in switch_layers.rs). The one conflict was that doc line.
  • Correctness: stack_gemma4_per_expert_planes runs after the NVFP4 key normalization and repack in sanitize_gemma4_nvfp4_weights, which all three Gemma 4 call sites use (text-only load, the filtered text load, and load_gemma4_vlm), so the expert root it derives from ...experts.0.gate_proj.weight is already in the language_model.model. form the model reads. Checkpoints with stacked switch_glu planes or fused gate_up_proj have no such key and are untouched. fix(loading): repack NVFP4 exports on the Gemma 4 Unified route #3188 (Gemma 4 Unified NVFP4 repack, merged meanwhile) uses its own sanitizer and does not interact.
  • Security / performance: weight-map transform only; one stack per projection per layer.

No code fixes were needed.

Validation on Apple Silicon (Metal), on the merged tree:

  • cargo test --profile test-fast --lib -- gemma4 sanitize switch_layers --test-threads=1: 593 passed, 0 failed
  • cargo clippy -p mlxcel --lib --tests -- -D warnings: clean
  • cargo fmt --all -- --check: clean
  • CI run 45047489158: green

@inureyes
inureyes merged commit 81e723f into lablup:main Nov 22, 2026
32 checks passed
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
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 platform:linux Linux (CUDA / packaging) specific priority:medium Medium priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(models): Gemma 4 26B A4B NVFP4 MoE exports fail to load because per-expert linears are not stacked

2 participants