Skip to content

fix(models): run a mixed-bit Gemma MLP projection by projection - #2999

Merged
inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma-mixed-bit-mlp
Nov 14, 2026
Merged

inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma-mixed-bit-mlp

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

mlx-community/gemma-3-1b-it-DQ, a mixed-bit conversion of Gemma 3, ended the process at its first forward pass. The Gemma 1, 2 and 3 MLPs gave their fused quantized kernel one quantization layout for the gate, up and down projections, taken from the gate; in a mixed-bit checkpoint the three differ. The fused kernel is now used only when the three share one layout.

Related issues

Closes #2998.

Type of change

  • fix — bug fix

Cause

MLP::forward in src/models/gemma3.rs (and gemma.rs, gemma2.rs): when all three projections are quantized it called compiled_gelu_approx_mlp_forward(.., gate_qw.group_size, gate_qw.bits, &gate_qw.mode). The loader is right (UnifiedLinear::from_weights infers each projection's width from its shapes), so in layer 2 of the DQ checkpoint up_proj was loaded as 4-bit and then run as 8-bit. MLX throws on the shapes, and the kernel crosses the bridge as UniquePtr<MlxArray>, not Result, so the exception ends the process.

Change

  • fused_mlp_quantized_weights (new, mlxcel-core layers.rs) returns the three quantized weights only when group size, bit width and mode are equal across gate, up and down. Otherwise None.
  • The Gemma 1, 2 and 3 MLPs call it instead of checking only that all three are quantized. On None they take the projection-by-projection forwards that were already there, which carry each projection's own layout.
  • Gemma 4 guards its fused MLP the same way (dense_mlp_shared_quant_layout).

Before / after

GB10 (sm_121, CUDA 13.0), generate -n 48 --temp 0. main is the release build of ce6855bc8; the fix column is a test-fast build of this branch whose four runtime files are identical to this commit.

Checkpoint Prompt main This PR
mlx-community/gemma-3-1b-it-DQ @ 927113c169c1 (4 bits, 81 modules at 8) What is the capital of France? Answer in one word. abort: [quantized_matmul] The shapes of the weight and scales are incompatible … w.shape() == (6912,144) and scales.shape() == (6912,18) with group_size=64 and bits=8 Paris
same What is 2 + 2? Answer with just the number. (same abort) 4

Uniform checkpoints, which keep the fused kernel, answer the first prompt as on main: gemma-3-270m-it-4bit, gemma-3-270m-it-8bit, gemma-2-2b-it-4bit, gemma-1.1-2b-it-4bit.

Test plan

Run on this PR's source (one commit on ce6855bc8, tree 47cb025b1230) on a DGX Spark (GB10).

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings (the Linux spelling of the template's line)
  • cargo build --release --features cuda
  • cargo test -p mlxcel-core --profile test-fast --features cuda --lib a_fused_mlp_needs_one_quantization_layout: passes. It covers mismatched bits in each position, a different group size, a different mode and an unquantized projection
  • cargo test --profile test-fast --features cuda --lib models::gemma -- --test-threads=1: 190 passed, 26 ignored. a_mixed_bit_mlp_runs_each_projection_on_its_own_layout builds a Gemma 3 MLP quantized 8/4/8, 4/8/4 and 4/4/8 and compares forward with the three projections run one after the other; a_uniform_mlp_keeps_the_fused_forward checks that 4/4/4 and 8/8/8 still fuse
  • Negative control: with the Gemma 3 MLP::forward put back as it is on main, the mixed-bit test aborts with the original quantized_matmul message
  • make verify-test-cuda (cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 186 of 187 test binaries pass (15489 tests passed, 1 failed, 496 ignored), the new tests among them. The one failure is native_env_tests::the_cuda_cache_defaults_reach_mlx, which fails the same way on a clean main (test(cuda): make verify-test-cuda fails the cache-default test on every CUDA host since #2876 #2987)
  • The self-hosted ci.yml jobs a pull request from a fork does not get, run by hand: clippy (cargo clippy -p mlxcel --lib --tests -- -D warnings, default features), cpu-link (cargo test -p mlxcel-core --profile test-fast --lib --no-run) and xla-compile (cargo check --features cuda,xla-iree --all-targets, cargo check --no-default-features --features xla-diagnostics --all-targets): all pass
  • make verify-python-tooling (219 tests OK), license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refs
  • Validated with real checkpoints: the table above
  • cargo deny check: not run and not applicable (no manifest, lockfile or deny.toml change)
  • Metal and ROCm: not built or run here

Notes for reviewers

  • The table's fix column comes from a test-fast build, not from the release build of the test plan; the four runtime files are the same blobs. The release build was built and gated, and the prompts were not repeated on it.
  • A mixed-bit MLP is not fused: three quantized matmuls and the activation instead of one compiled graph, for the layers that mix widths (8 of 26 in the DQ checkpoint). Uniform layers and uniform checkpoints are unchanged.
  • Gemma 1 and Gemma 2 share the code and the fix; I have no mixed-bit checkpoint of either, so they are covered by the helper test only.
  • Implemented and validated with AI assistance (Claude) driving the GB10 host. Every number above comes from the logs of those runs.

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/: not needed (no option, flag or documented behaviour changes)
  • Updated // Used by: ... comments: on the new function
  • No secrets, credentials, or .env files committed

Jaeyeong-CHOI and others added 2 commits November 12, 2026 14:16
The Gemma 1, 2 and 3 MLPs hand their gate, up and down projections to one
fused quantized kernel, which takes a single group size, bit width and mode
for the three. They passed the gate's. In a mixed-bit checkpoint the three
need not agree: layer 2 of `mlx-community/gemma-3-1b-it-DQ` has gate and
down at 8 bits and up at 4. MLX then throws on the up projection's shapes,
and the exception crosses the bridge and ends the process.

`fused_mlp_quantized_weights` returns the three weights only when they share
one layout; otherwise the MLP takes its projection-by-projection forwards,
which carry each projection's own layout. Gemma 4 already guards its fused
MLP the same way.
@inureyes

Copy link
Copy Markdown
Member

Thanks @Jaeyeong-CHOI, this is a clean fix. Maintainer review on Apple Silicon (M3 Ultra, Metal):

  • Purpose fit: every caller of the single-layout fused quantized MLP in Gemma 1, 2 and 3 now goes through fused_mlp_quantized_weights; Gemma 4 already had its own guard, and no other family passes the gate's layout for all three projections.
  • Security and performance: no new inputs. Uniform checkpoints keep the fused kernel; only mixed-layout layers take the three-matmul path.
  • Merged main into the branch (no conflicts). No code changes were needed.

Validation on Metal:

  • mlx-community/gemma-3-1b-it-DQ on main: exit 134, [quantized_matmul] The shapes of the weight and scales are incompatible ... w.shape() == (6912,144) and scales.shape() == (6912,18) with group_size=64 and bits=8 (same abort as on GB10)
  • Same checkpoint on this branch: Paris and 4 for the two prompts in the PR body
  • cargo test -p mlxcel-core --lib a_fused_mlp_needs_one_quantization_layout: passed
  • cargo test --lib models::gemma3: 56 passed; models::gemma2: 2 passed
  • cargo clippy --lib --tests -- -D warnings: clean

@inureyes
inureyes merged commit 224be49 into lablup:main Nov 14, 2026
33 checks passed
@inureyes inureyes added the status:done Completed label Nov 14, 2026
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:models Model architectures, weights, loading, metadata modeltype:text Text-only language model arch:dense Dense transformer decoder labels Nov 30, 2026
@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

arch:dense Dense transformer decoder area:models Model architectures, weights, loading, metadata modeltype:text Text-only language model priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(models): run a mixed-bit Gemma MLP projection by projection

2 participants