Repository navigation
fix(models): run a mixed-bit Gemma MLP projection by projection - #2999
Merged
Merged
Conversation
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.
Member
|
Thanks @Jaeyeong-CHOI, this is a clean fix. Maintainer review on Apple Silicon (M3 Ultra, Metal):
Validation on Metal:
|
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 fixCause
MLP::forwardinsrc/models/gemma3.rs(andgemma.rs,gemma2.rs): when all three projections are quantized it calledcompiled_gelu_approx_mlp_forward(.., gate_qw.group_size, gate_qw.bits, &gate_qw.mode). The loader is right (UnifiedLinear::from_weightsinfers each projection's width from its shapes), so in layer 2 of the DQ checkpointup_projwas loaded as 4-bit and then run as 8-bit. MLX throws on the shapes, and the kernel crosses the bridge asUniquePtr<MlxArray>, notResult, so the exception ends the process.Change
fused_mlp_quantized_weights(new,mlxcel-corelayers.rs) returns the three quantized weights only when group size, bit width and mode are equal across gate, up and down. OtherwiseNone.Nonethey take the projection-by-projection forwards that were already there, which carry each projection's own layout.dense_mlp_shared_quant_layout).Before / after
GB10 (sm_121, CUDA 13.0),
generate -n 48 --temp 0.mainis the release build ofce6855bc8; the fix column is a test-fast build of this branch whose four runtime files are identical to this commit.mainmlx-community/gemma-3-1b-it-DQ@927113c169c1(4 bits, 81 modules at 8)[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=8Paris4Uniform 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, tree47cb025b1230) on a DGX Spark (GB10).cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features cuda -- -D warnings(the Linux spelling of the template's line)cargo build --release --features cudacargo 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 projectioncargo 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_layoutbuilds a Gemma 3 MLP quantized 8/4/8, 4/8/4 and 4/4/8 and comparesforwardwith the three projections run one after the other;a_uniform_mlp_keeps_the_fused_forwardchecks that 4/4/4 and 8/8/8 still fuseMLP::forwardput back as it is onmain, the mixed-bit test aborts with the originalquantized_matmulmessagemake 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 isnative_env_tests::the_cuda_cache_defaults_reach_mlx, which fails the same way on a cleanmain(test(cuda): make verify-test-cuda fails the cache-default test on every CUDA host since #2876 #2987)ci.ymljobs 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) andxla-compile(cargo check --features cuda,xla-iree --all-targets,cargo check --no-default-features --features xla-diagnostics --all-targets): all passmake verify-python-tooling(219 tests OK), license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refscargo deny check: not run and not applicable (no manifest, lockfile ordeny.tomlchange)Notes for reviewers
Checklist
docs/: not needed (no option, flag or documented behaviour changes)// Used by: ...comments: on the new function.envfiles committed