Skip to content

fix: split quantized fused MoE experts (gate_up_proj/down_proj scales+biases) in gemma4_unified sanitize #155

Closed

Description

@inureyes

Tracking issue for the LOW-severity finding from the #151 / PR #153 review. The gemma4_unified weight sanitize splits fused MoE expert tensors (experts.gate_up_proj → switch_glu.gate_proj/up_proj) only for the non-quantized form, so a quantized MoE checkpoint's .scales/.biases companion tensors are not split and the experts fail to load.

Severity: LOW / dormant. No currently-available checkpoint triggers it — the reference mlx-community/gemma-4-12b-it-4bit is dense (no experts.* tensors). This will only bite a future Gemma 4 Unified checkpoint that (a) uses MoE blocks and (b) ships its fused experts in quantized form. Filing now so it is not lost.

Current behavior

src/loading/vlm_gemma_unified.rs (sanitize_gemma4_unified_weights, ~lines 59–81):

if key.ends_with("experts.gate_up_proj") {
    let prefix = key.trim_end_matches("gate_up_proj");
    // value: [num_experts, in, 2*ffn] → transpose [0,2,1] → [num_experts, 2*ffn, in]
    // → slice the doubled (2*ffn) axis in half
    let swapped = mlxcel_core::transpose_axes(&value, &[0, 2, 1]);
    // ...
    out.insert(format!("{prefix}switch_glu.gate_proj.weight"), gate);
    out.insert(format!("{prefix}switch_glu.up_proj.weight"),   up);
    continue;
}
if key.ends_with("experts.down_proj") {
    let prefix = key.trim_end_matches("down_proj");
    out.insert(format!("{prefix}switch_glu.down_proj.weight"), value);
    continue;
}

The match guards are key.ends_with("experts.gate_up_proj") and key.ends_with("experts.down_proj") — i.e. the bare (non-quantized) tensor key.

The defect

When the fused expert weights are quantized (e.g. 4-bit affine), a single logical tensor is persisted as a triplet:

non-quantized quantized
…experts.gate_up_proj …experts.gate_up_proj.weight (packed ints)
…experts.gate_up_proj.scales
…experts.gate_up_proj.biases (zero-points)

None of …gate_up_proj.weight / .scales / .biases end in the bare string experts.gate_up_proj, so the split never fires for a quantized MoE checkpoint. Consequences:

  • the three fused tensors fall through to prefix-normalization unchanged and land under …experts.gate_up_proj.{weight,scales,biases} instead of the expected split …switch_glu.{gate,up}_proj.{weight,scales,biases};
  • switch_glu construction cannot find its per-projection quantized parts → load fails (or, worse, silently mis-binds).

The same applies to experts.down_proj.{weight,scales,biases}, which need a rename (no split) to switch_glu.down_proj.{weight,scales,biases}.

The in-code comment (lines 59–62) already anticipates this — "quantized scales/biases follow the same split when present" — but the code does not implement it; the comment is currently aspirational.

Why this is dormant (LOW)

  • The reference checkpoint mlx-community/gemma-4-12b-it-4bit is dense: enable_moe_block is off and it has standard per-layer mlp.{gate,up,down}_proj (some at 8-bit), no experts.gate_up_proj tensors. The MoE-split branch is never entered, so nothing is mis-loaded today.
  • No quantized-MoE gemma4_unified checkpoint exists in the test set, so there is no current correctness or runtime impact. Text/image/audio paths are unaffected.

Root-cause detail / why the fix is tractable

  • The runtime is already quantization-aware for experts: switch_glu / the per-expert SwitchLinear consumes quantized gate_proj/up_proj/down_proj with .scales + .biases (see src/models/gemma4.rs expert forward, ~lines 407–429 and 502–517). So no runtime change is needed — only the sanitize-time split must emit the split quantized tensors.
  • The split is along the output (FFN) axis (the doubled 2*ffn dimension), not the contract/input axis. In MLX affine quantization the grouping is along the input dimension, so scales/biases are shaped per output-row × in/group_size. Splitting along the output axis therefore partitions weight, scales, and biases cleanly at the same half-boundary, with no group-boundary straddling and no need to dequantize/unpack. This is what makes a correct, lossless split feasible.

Proposed fix

In sanitize_gemma4_unified_weights:

  1. Generalize the matchers to also catch the quantized triplet, e.g. match a key whose component path is …experts.gate_up_proj with an optional .weight / .scales / .biases suffix.
  2. For gate_up_proj: split each present tensor (weight, scales, biases) along the doubled-FFN (output) axis into gate (first half) and up (second half), emitting:
    • …switch_glu.gate_proj.{weight,scales,biases}
    • …switch_glu.up_proj.{weight,scales,biases}
      Preserve the existing transpose/half-split for .weight; apply the matching half-split to .scales/.biases on the same axis.
  3. For down_proj: rename the whole triplet …experts.down_proj.{weight,scales,biases} → …switch_glu.down_proj.{weight,scales,biases} (no split).
  4. Keep the existing non-quantized (.weight-only / bare-key) path working unchanged.

Key risk / unknown to resolve during implementation

No real quantized-MoE gemma4_unified checkpoint is available to confirm the on-disk axis orientation and 4-bit packing layout of the fused gate_up_proj (whether the packed .weight and its scales/biases align with the assumed [num_experts, in, 2*ffn] → transpose → output-axis-split). The implementer must validate the split orientation against a real checkpoint when one appears, or via a synthetic dequantize-equivalence test (below). Do not ship an unvalidated split that merely compiles.

Test plan

  • Add a synthetic quantized fused-MoE fixture to src/loading/vlm_gemma_unified_tests.rs (mirroring the existing non-quantized split test at ~lines 96–110): construct experts.gate_up_proj.{weight,scales,biases} and experts.down_proj.{weight,scales,biases} with known shapes.
  • Assert sanitize produces exactly switch_glu.{gate,up,down}_proj.{weight,scales,biases} with correct shapes, and that the bare/fused keys are gone.
  • Numerical gate: assert dequantize(split(tensor)) == split(dequantize(tensor)) along the output axis (i.e. the split is lossless and correctly aligned), so the fix is verified without needing a real checkpoint.
  • Keep the existing non-quantized split test green (no regression).

Acceptance criteria

  • sanitize_gemma4_unified_weights splits both non-quantized and quantized fused MoE experts, emitting the full switch_glu.{gate,up,down}_proj.{weight[,scales,biases]} set.
  • A synthetic quantized-MoE fixture loads through sanitize with correct shapes and passes the dequantize-equivalence test.
  • The existing dense reference model (gemma-4-12b-it-4bit) and the non-quantized MoE path are unaffected (no regression).
  • The aspirational comment in vlm_gemma_unified.rs is replaced with an accurate description of the implemented behavior.
  • When a real quantized-MoE gemma4_unified checkpoint becomes available, it loads and generates correctly (note in the PR if validated against one; otherwise state that validation relied on the synthetic round-trip).

Touchpoints

Area File Note
Sanitize split src/loading/vlm_gemma_unified.rs generalize gate_up_proj/down_proj matchers to the quantized triplet; split scales/biases on the output axis
Tests src/loading/vlm_gemma_unified_tests.rs add quantized fixture + dequantize-equivalence gate
Runtime (reference only, no change) src/models/gemma4.rs quant-aware switch_glu expert forward already consumes split .scales/.biases

Context: discovered during the #151 / PR #153 implementation review; the PR body documents it as a deferred follow-up.

Activity

  1. added
    type:bug
    area:models
    priority:low
    status:ready
    status:in-progress
    status:review
    status:done
    and removed
    status:ready
    status:in-progress
    status:review
    on Jun 4, 2026
  2. added this to the 0.1 milestone on Jun 5, 2026
  3. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelspriority:lowstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions