Skip to content

fix(gemma4): honor per-module MLP quant overrides - #690

Merged
inureyes merged 2 commits into
mainfrom
update/issue-683-gemma4-quant-overrides
Jul 8, 2026
Merged

inureyes merged 2 commits into
mainfrom
update/issue-683-gemma4-quant-overrides

Conversation

@inureyes

@inureyes inureyes commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

Honor Gemma 4 per-module quantization overrides so the #683 frontier variants generated by scripts/requantize_mlp.py can load with their declared MLP layouts instead of relying on ambiguous shape inference.

What changed

  • Preserve root config.quantization per-module override entries when promoting them into TextConfig, including DiffusionGemma's shared Gemma 4 text path.
  • Load Gemma 4 dense MLP gate/up/down projections with prefix-specific bits and group_size, which fixes group_size=32 4-bit variants and mixed down-proj-8-bit variants.
  • Route dense MLPs through the existing fused quantized helper only when gate/up/down share the same quant layout; mixed layouts fall back to projection-local UnifiedLinear::forward() calls so each projection uses its own parameters.
  • Add focused regression tests for root override preservation and the MLP shared-layout gate, plus a GB10 quality-frontier CSV for the shipped, uniform, down8, and gs32 Gemma 4 12B variants.

Frontier result

The C/D variants now load and run examples/perplexity.rs. On the README 512-token single-chunk quality gate, uniform 4-bit remained the best of this set (mean_nll=8.18222), while shipped mixed was 8.40327, down8 was 8.45547, and gs32 was 8.53010; the recorded data is in benchmarks/cuda_gb10_gemma4_frontier_683_2026-07-08.csv.

Test plan

  • cargo fmt --all -- --check
  • cargo test -p mlxcel --features cuda --lib gemma4_root_quantization_preserves_per_module_overrides
  • cargo test -p mlxcel --features cuda --lib dense_mlp_shared_quant_layout
  • cargo check -p mlxcel --features cuda --lib
  • RUST_LOG=mlxcel::quant=warn cargo run --features cuda --example perplexity -- /home/inureyes/models/gemma-4-12b-it-4bit-down8 README.md 32 1
  • RUST_LOG=mlxcel::quant=warn cargo run --features cuda --example perplexity -- /home/inureyes/models/gemma-4-12b-it-4bit-gs32 README.md 32 1
  • RUST_LOG=mlxcel::quant=warn cargo run --features cuda --example perplexity -- /home/inureyes/models/gemma-4-12b-it-4bit README.md 512 1
  • RUST_LOG=mlxcel::quant=warn cargo run --features cuda --example perplexity -- /home/inureyes/models/gemma-4-12b-it-4bit-uniform README.md 512 1
  • RUST_LOG=mlxcel::quant=warn cargo run --features cuda --example perplexity -- /home/inureyes/models/gemma-4-12b-it-4bit-down8 README.md 512 1
  • RUST_LOG=mlxcel::quant=warn cargo run --features cuda --example perplexity -- /home/inureyes/models/gemma-4-12b-it-4bit-gs32 README.md 512 1

Closes #683

@inureyes inureyes added status:review Under review type:performance Performance improvements priority:medium Medium priority area:inference Generation, sampling, decoding (incl. speculative, DRY) status:done Completed and removed status:review Under review labels Jul 8, 2026
…a4-quant-overrides

# Conflicts:
#	src/models/gemma4.rs
@inureyes
inureyes merged commit aff12a7 into main Jul 8, 2026
4 checks passed
@inureyes
inureyes deleted the update/issue-683-gemma4-quant-overrides branch July 8, 2026 11:48
inureyes added a commit that referenced this pull request Jul 8, 2026
## Summary

PR #690 added Gemma 4 per-module MLP quantization overrides through a flattened `overrides` map on `QuantizationArgs`, but a malformed override value (a non-object value, a wrong-typed/negative/zero `group_size`, or an unsupported `bits` width) silently fell back to the root defaults with no indication anything was wrong, which is a debugging hazard for quantization frontier work.

## What changed

- `QuantizationArgs::quant_params_for` in `src/models/gemma4.rs` now validates the override entry matched by the queried module `prefix` before applying it, reporting an actionable `tracing::warn!` naming the offending key for a non-object override value, a wrong-typed or negative `group_size`/`bits`, a zero group size, or an unsupported bit width (mlxcel only supports the affine `{2,3,4,5,6,8}` set), then falls back to the root defaults for that field exactly as before.
- Added two small validation helpers, `validate_override_group_size` and `validate_override_bits`, that produce the actionable reason strings used in the warnings.
- Metadata keys that legitimately live alongside per-module overrides in the same flattened map (`mode`, `quant_method`, `quant_algo`, `config_groups`) are untouched because they never coincide with a real module-prefix lookup, so they are never mistaken for malformed overrides.
- Added a `quant_override_diagnostics_tests` module in `src/models/gemma4.rs` covering valid full and partial overrides, absent overrides, a non-object override value, string-typed and negative/zero/unsupported-width numeric fields, sibling metadata keys, and a typo'd prefix that simply never matches.

## Test plan

- [x] `cargo check -p mlxcel --features cuda --lib`
- [x] `cargo test -p mlxcel --features cuda --lib models::gemma4::quant` (24 tests, all pass, including the 11 new diagnostics tests and the pre-existing `quant_scheme_tests`)
- [x] `cargo test -p mlxcel --features cuda --lib models::gemma4_tests` (9 pass, 8 ignored for serial MLX execution as before, including the existing `gemma4_root_quantization_preserves_per_module_overrides` regression test, confirming valid configs load unchanged)
- [x] `cargo clippy -p mlxcel --features cuda --lib --tests` (no warnings touching `gemma4.rs`; the crate has one pre-existing unrelated `redundant_closure` warning in `src/models/sanitize.rs` that predates this change)
- [x] `cargo fmt -p mlxcel -- --check`

Closes #691
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
## Summary

PR lablup#690 added Gemma 4 per-module MLP quantization overrides through a flattened `overrides` map on `QuantizationArgs`, but a malformed override value (a non-object value, a wrong-typed/negative/zero `group_size`, or an unsupported `bits` width) silently fell back to the root defaults with no indication anything was wrong, which is a debugging hazard for quantization frontier work.

## What changed

- `QuantizationArgs::quant_params_for` in `src/models/gemma4.rs` now validates the override entry matched by the queried module `prefix` before applying it, reporting an actionable `tracing::warn!` naming the offending key for a non-object override value, a wrong-typed or negative `group_size`/`bits`, a zero group size, or an unsupported bit width (mlxcel only supports the affine `{2,3,4,5,6,8}` set), then falls back to the root defaults for that field exactly as before.
- Added two small validation helpers, `validate_override_group_size` and `validate_override_bits`, that produce the actionable reason strings used in the warnings.
- Metadata keys that legitimately live alongside per-module overrides in the same flattened map (`mode`, `quant_method`, `quant_algo`, `config_groups`) are untouched because they never coincide with a real module-prefix lookup, so they are never mistaken for malformed overrides.
- Added a `quant_override_diagnostics_tests` module in `src/models/gemma4.rs` covering valid full and partial overrides, absent overrides, a non-object override value, string-typed and negative/zero/unsupported-width numeric fields, sibling metadata keys, and a typo'd prefix that simply never matches.

## Test plan

- [x] `cargo check -p mlxcel --features cuda --lib`
- [x] `cargo test -p mlxcel --features cuda --lib models::gemma4::quant` (24 tests, all pass, including the 11 new diagnostics tests and the pre-existing `quant_scheme_tests`)
- [x] `cargo test -p mlxcel --features cuda --lib models::gemma4_tests` (9 pass, 8 ignored for serial MLX execution as before, including the existing `gemma4_root_quantization_preserves_per_module_overrides` regression test, confirming valid configs load unchanged)
- [x] `cargo clippy -p mlxcel --features cuda --lib --tests` (no warnings touching `gemma4.rs`; the crate has one pre-existing unrelated `redundant_closure` warning in `src/models/sanitize.rs` that predates this change)
- [x] `cargo fmt -p mlxcel -- --check`

Closes lablup#691
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cuda/gemma4): decode is bound by 8-bit MLP weight bytes plus a near-saturated dual CPU/GPU pipeline; qmm kernels are healthy

1 participant