Skip to content

fix(models): keep a Gemma MLP with per-tensor scales off the fused kernel - #3143

Merged
inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma3-modelopt-fused-mlp-global-scale
Nov 19, 2026
Merged

inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma3-modelopt-fused-mlp-global-scale

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

A ModelOpt NVFP4 Gemma 3 checkpoint generated noise: the fused Gemma MLP kernel has no argument for the per-tensor scale (QuantizedWeight::global_scale) such a checkpoint carries on every projection, so the MLP ran unscaled. fused_mlp_quantized_weights now declines a projection with a global scale, and the MLP runs projection by projection, where UnifiedLinear::forward applies it.

Related issues

Closes #3142.

Type of change

  • fix — bug fix

Cause

MLP::forward in src/models/gemma3.rs (also gemma.rs, gemma2.rs) calls compiled_gelu_approx_mlp_forward whenever fused_mlp_quantized_weights (mlxcel-core layers.rs) returns the three planes. That function filters layouts that differ between the projections (#2999), but not global_scale. ModelOpt NVFP4 stores the second-level scale weight_scale_2 per tensor; the loader keeps it as global_scale and UnifiedLinear::forward multiplies it in after the matmul. The fused kernel takes only weight, block scales and biases, so all three projections ran without it and the error compounds through every layer.

Change

  • fused_mlp_quantized_weights returns None when any of the three projections has a global_scale, beside the existing layout check. The callers already fall back to their per-projection forward on None. The doc comment says why.
  • No other path changes: a projection without a global scale (every mlx-lm quantized Gemma) takes the fused kernel as before.

Before / after

GB10 (sm_121, CUDA 13.0), release builds of fab141b1a (main, the base of this branch) and of this PR, generate -n 64 --temp 0, MLXCEL_SDPA_DETERMINISTIC=1.

Checkpoint Prompt main This PR
iromu/Gemma3-1B-tools-NVFP4 @ ec7bf1aee6d4 What is the capital of France? Answer in one word. Chor démontréne-secầnन्न اقدامات FA… Paris
What is 2 + 2? Answer with just the number. The question also былоaj trés EK EK DHOT … 4
Write one sentence about the sea. 了 then possible... } 有ાર很了 প্যাকে… The sea is a vast and powerful body of water teeming with life and mystery.
APMIC/ACE-gemma-3-4b-it-nvfp4 @ d8d6d7db9203 capital The three given non-zero twinnings have to be worked in a. … Paris
2 + 2 2. Set all the information a truly 2 sh, if 2.00, … 4
sea The sea of V's end, beginning to take, In the I.e. SoCCo … The sea is a big, blue ocean that has waves and salty water, and it's where many people and animals like to play and swim.

Gemma checkpoints without a global scale take the fused path as before and answer the first prompt (200 tokens) byte for byte as on main: gemma-3-270m-it-4bit, gemma-3-270m-it-8bit, gemma-3-1b-it-DQ (mixed bits), gemma-3-4b-it-4bit, gemma-2-2b-it-4bit.

Test plan

Run on this PR's commit (one commit on fab141b1a, tree 1b4acdf4ef84) on a DGX Spark (GB10).

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings
  • cargo build --release --features cuda
  • The three new tests by name: a_fused_mlp_takes_no_projection_with_a_tensor_scale (mlxcel-core: a scale on any one of the three projections declines the fused path, none takes it), a_gemma3_mlp_preserves_each_projection_global_scale (MLP::forward against the three projections run separately, five scale combinations) and a_disk_loaded_gemma3_mlp_preserves_modelopt_tensor_scales (the same through load_text_weights from ModelOpt triplets on disk): pass. The two MLP tests skip where the backend has no native NVFP4 kernel
  • cargo test --profile test-fast --features cuda --lib -- models::gemma models::sanitize modelopt nvfp4: 316 passed
  • Negative control, the scale check removed from fused_mlp_quantized_weights: all three new tests fail
  • make verify-test-cuda, on this commit together with two unrelated branches of mine (Qwen3.5 MoE loader, test-only paged_detach fixture): 15917 passed, 1 failed, 498 ignored. The one failure was a test of the Qwen3.5 branch (its reference, since fixed) and does not touch this change; the three tests above pass in that run
  • 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 tables above
  • 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

  • Speed. A ModelOpt Gemma MLP now runs three quantized matmuls instead of the fused kernel. Correctness first; a scaled fused kernel like the Gemma 4 one (perf(gemma4): fold NVFP4 global scales into fused MLP kernel #698) could follow.
  • Layout warnings remain. gemma3::ModelArgs::group_size() still falls back to 64 for a ModelOpt config, so these checkpoints log one quantized layout does not match declared metadata warning per linear (104 for the 1B, 136 for the 4B). Since fix(quant): load ModelOpt W4A16_NVFP4 checkpoints in families without a ModelOpt group size #3026 the mode comes from the shapes, so the planes load as NVFP4 and the answers above are right; giving Gemma the ModelOpt group-size accessor Qwen3 has would silence them, and is left out of this one-cause PR.
  • Gemma 1 and 2. They share fused_mlp_quantized_weights, so they get the same guard; I found no ModelOpt export of either to run.

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: unchanged callers (Gemma 1, 2, 3 MLPs)

…rnel

A ModelOpt NVFP4 Gemma 3 checkpoint carries a per-tensor scale
(weight_scale_2, loaded as QuantizedWeight::global_scale) on every
projection. The fused Gemma MLP kernel has no argument for it, so the three
projections ran unscaled and the model generated noise without an error.
fused_mlp_quantized_weights now declines a projection with a global scale and
the MLP runs projection by projection, where UnifiedLinear::forward applies
the scale.
@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 labels Nov 19, 2026
compiled_swiglu_mlp (SmolLM3, StableLM, Exaone4, Jamba) hands its three quantized projections to compiled_moe_expert_forward, which has no argument for a ModelOpt per-tensor global_scale and takes one layout for all three. It had the same silent bug as the Gemma MLP: a ModelOpt NVFP4 checkpoint would run unscaled, and a mixed-bit checkpoint would throw at the first forward. It now goes through fused_mlp_quantized_weights and returns None in both cases, so the callers take their per-projection forwards. Unscaled, same-layout projections keep the fused kernel.

Adds a_compiled_swiglu_mlp_declines_a_projection_with_a_tensor_scale; it fails with the guard removed. The Used-by notes now name the real callers.

Refs lablup#3142, lablup#3143
@inureyes

Copy link
Copy Markdown
Member

Hardening review

Reviewed against #3142. The guard in fused_mlp_quantized_weights is correct: it only rejects a triplet where some projection carries global_scale, so mlx-lm affine, plain NVFP4 and other unscaled Gemma 1/2/3 checkpoints keep the fused kernel. The check is three Option::is_none calls per MLP forward, so decode cost is unchanged. Gemma 4 already has its scaled fused MLP (#698), and Gemma3n's fused path only takes regular weights.

Same bug found elsewhere and fixed here (0f5e669): compiled_swiglu_mlp (SmolLM3, StableLM, Exaone4, Jamba) passed quantized projections straight to compiled_moe_expert_forward, which has no global_scale argument and takes one layout for all three. It now goes through fused_mlp_quantized_weights, so a ModelOpt per-tensor scale or a mixed layout falls back to the per-projection forward. New test a_compiled_swiglu_mlp_declines_a_projection_with_a_tensor_scale fails with the guard removed.

Noted, not changed: Llama3's opt-in fused causal prefill (env-gated) reads o_proj scales without global_scale, but it needs a fused QKV, which ModelOpt triplets with scales never produce since #2444. UnifiedLinear::dequantized_weight also ignores global_scale; its callers are diagnostics and embedding tables, not the MLP path.

Also merged origin/main.

Validation (Mac Studio M3 Ultra, Metal):

  • cargo test -p mlxcel-core --lib -- fused_mlp compiled_swiglu: 5 passed
  • cargo test -p mlxcel --lib -- models::gemma models::sanitize models::stablelm models::smollm3 models::exaone4 models::jamba modelopt: 318 passed
  • cargo fmt --all -- --check, cargo clippy -p mlxcel-core --lib --tests -- -D warnings, cargo clippy -p mlxcel --lib --tests -- -D warnings: clean

@inureyes
inureyes merged commit 9d05c3a into lablup:main Nov 19, 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 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): ModelOpt NVFP4 Gemma 3 generates noise because the fused MLP drops the per-tensor scale

2 participants