Repository navigation
Description
Summary
A Gemma 3 checkpoint quantized with NVIDIA ModelOpt (NVFP4, quant_method: modelopt) loads without error but generates noise. The Gemma 1, 2 and 3 MLPs run their three projections through one fused quantized kernel, and that kernel has no argument for the per-tensor scale ModelOpt stores beside the block scales (weight_scale_2, loaded as QuantizedWeight::global_scale). The MLP therefore runs unscaled, and the model emits garbage instead of an answer.
Background
Gemma 1/2/3 MLPs route gate, up and down projections through a single fused quantized kernel whenever the three planes share a layout. The non-fused UnifiedLinear::forward applies a global_scale after the matmul, but the fused path never receives it. fused_mlp_quantized_weights gates entry on group_size, bits and mode matching across the three planes, and does not consider global_scale at all, so a ModelOpt triplet that carries one still takes the fused path and loses it. Setting MLXCEL_COMPILED_QGELU_MLP=0 does not help: the wrapper's fallback takes the same unscaled planes.
The reporter's in-memory measurements show the output diverging exactly as the per-projection global_scale moves away from 1, while outputs are themselves of order 1e-3, so the differences are not rounding.
Proposed Solution
Keep a Gemma MLP whose quantized projections carry a global_scale off the fused kernel and route it through the per-projection path that applies the scale, or extend the fused kernel to apply the per-tensor scale. Either keeps affine and plain NVFP4 Gemma checkpoints on the fast fused path while making ModelOpt NVFP4 correct.
Implementation Notes
MLP::forwardtakes the fused path atsrc/models/gemma3.rs:470,src/models/gemma.rs:276andsrc/models/gemma2.rs:329, each guarded bymlxcel_core::layers::fused_mlp_quantized_weights.fused_mlp_quantized_weights(src/lib/mlxcel-core/src/layers.rs:74) compares onlygroup_size,bitsandmodeacross the threeQuantizedWeights; it never inspectsglobal_scale.QuantizedWeighthas aglobal_scale: Option<..>field thatUnifiedLinear::forwardapplies post-matmul.gemma3::ModelArgs::group_size()still falls back to 64 for a ModelOpt config, producing onequantized layout does not match declared metadatawarning per linear. Since fix(quant): load ModelOpt W4A16_NVFP4 checkpoints in families without a ModelOpt group size #3026 the mode is inferred from shapes so the planes still load as NVFP4; the dropped scale is the remaining cause of the noise, but the group-size fallback is worth tidying alongside.- Not affected: Gemma 4 has its own scaled fused MLP (perf(gemma4): fold NVFP4 global scales into fused MLP kernel #698), and the fused QKV path splits scale-carrying planes since fix: Close NVFP4 global-scale load gaps left after #2441 #2444.
Acceptance Criteria
iromu/Gemma3-1B-tools-NVFP4andAPMIC/ACE-gemma-3-4b-it-nvfp4load and answerParisfor the reproduction prompt.- Affine and plain NVFP4 Gemma 3 checkpoints keep their current fused path and output.
- A regression test covers a Gemma MLP whose gate/up/down projections carry non-unit per-tensor scales (the in-memory vs loaded-from-disk divergence in the report).
Original Suggestion
Title: fix(models): a ModelOpt NVFP4 Gemma 3 checkpoint loads and generates noise
Summary
A Gemma 3 checkpoint quantized with NVIDIA ModelOpt (NVFP4, quantization_config with quant_method: modelopt) loads without an error and generates noise. The Gemma 1, 2 and 3 MLPs run their three projections through one fused kernel, and that kernel has no argument for the per-tensor scale ModelOpt stores beside the block scales (weight_scale_2, loaded as QuantizedWeight::global_scale). The MLP runs unscaled.
Reproduction steps
hf download iromu/Gemma3-1B-tools-NVFP4 --revision ec7bf1aee6d438527ee2ee14f537c912273fedb9 \
--local-dir models/Gemma3-1B-tools-NVFP4
mlxcel generate -m models/Gemma3-1B-tools-NVFP4 -n 64 --temp 0 \
-p "What is the capital of France? Answer in one word."Expected behavior
Paris.
Actual behavior
Chor démontréne-secầnन्न اقدامات FAాడు επίσης j beeswax Nun “ Dzreg commun de lauta ricerchera leo fier serv …
The load itself takes the ModelOpt route: Repacking 182 ModelOpt NVFP4 weight groups (source mode nvfp4) to MLX native NVFP4 via direct triplet transcode. APMIC/ACE-gemma-3-4b-it-nvfp4 @ d8d6d7db9203 (4B, the same layout) does the same: The three given non-zero twinnings have to be worked in a. ….
mlxcel version
main @ fab141b
Install method
Built from source
Platform
Linux + CUDA (gb10)
Model checkpoint
iromu/Gemma3-1B-tools-NVFP4 @ ec7bf1aee6d4 (gemma3_text, ModelOpt 0.46.0, NVFP4, group size 16, lm_head excluded); APMIC/ACE-gemma-3-4b-it-nvfp4 @ d8d6d7db9203
Relevant logs
The MLP alone, on a synthetic NVFP4 MLP (64 wide, intermediate 128) whose three projections carry a global_scale, MLP::forward against the same three projections run one after the other (measured on an earlier base with the same fused path):
global_scale of gate / up / down |
in memory: largest difference | through load_text_weights from a ModelOpt triplet on disk |
|---|---|---|
| 1, 1, 1 | 0 | 4.8e-7 |
| 0.5, 1, 1 | 1.9e-4 | 3.58 |
| 1, 2, 1 | 3.7e-4 | 6.39 |
| 1, 1, 3.25 | 8.3e-4 | 14.37 |
| 0.5, 2, 3.25 | 8.0e-4 | 11.86 |
The in-memory outputs are themselves of the order of 1e-3, so those differences are not rounding.
Additional context
- Where.
MLP::forwardinsrc/models/gemma3.rs(andgemma.rs,gemma2.rs) takes the fused path whenevermlxcel_core::layers::fused_mlp_quantized_weightsreturns the three planes. That function checks that the layouts agree (fix(models): run a mixed-bit Gemma MLP projection by projection #2999) but notQuantizedWeight::global_scale;UnifiedLinear::forwardapplies the scale after the matmul, the fused kernel never sees it.MLXCEL_COMPILED_QGELU_MLP=0does not help: the wrapper's fallback takes the same unscaled planes. - Group size.
gemma3::ModelArgs::group_size()still falls back to 64 for a ModelOpt config, so these checkpoints print onequantized layout does not match declared metadatawarning per linear (104 for the 1B). Since fix(quant): load ModelOpt W4A16_NVFP4 checkpoints in families without a ModelOpt group size #3026 the mode is inferred from the shapes, so the planes load as NVFP4 anyway; the scale is the remaining cause of the noise. - Not affected. Gemma 4 has its own scaled fused MLP (perf(gemma4): fold NVFP4 global scales into fused MLP kernel #698). The fused QKV path splits scale-carrying planes since fix: Close NVFP4 global-scale load gaps left after #2441 #2444.
- Not measured. Metal and ROCm. A ModelOpt export of Gemma 1 or Gemma 2 (I found none): their MLPs share the fused path.
- A fix with tests is ready.
Pre-submission checks
- I searched existing issues and did not find a duplicate
- This is not a security vulnerability (those go to SECURITY.md instead)
Activity
[-]fix(models): a ModelOpt NVFP4 Gemma 3 checkpoint loads and generates noise[/-][+]fix(models): ModelOpt NVFP4 Gemma 3 generates noise because the fused MLP drops the per-tensor scale[/+]chore(release): prepare v0.9.0-beta.1
fix(layers): apply the fused-MLP scale guard to compiled_swiglu_mlp
fix(models): keep a Gemma MLP with per-tensor scales off the fused ke…
chore(release): prepare v0.9.0-beta.2
chore(release): prepare v0.9.0