Repository navigation
fix: validate external quant scheme and surface unclosed thinking - #605
Merged
Merged
Conversation
Serving an externally quantized 4-bit Gemma 4 (gemma-4-31B-it-qat-OptiQ-4bit) on macOS/MLX produced degenerate, repeating output at temperature 0 with every token trapped in reasoning_content and empty content. Two independent mlxcel paths combined: the quantized loader silently reconciled quantization parameters against tensor shapes with no validation, and the Gemma 4 thinking-channel filter emptied content whenever the primed thinking block never closed. Path A (quantization reconciliation): mlxcel-core now reconciles declared bits/group_size against the actual weight/scales shapes through one pure reconcile_quantization_layout helper, and the linear and embedding loaders warn at load time whenever reconciliation changes a value instead of silently overriding the config. The Gemma 4 loader (validate_quantization_scheme) rejects unsupported external schemes (OptiQ, AWQ, GPTQ, detected via a foreign quant_method or a non-affine mode) up front with an actionable message that names the format, rather than dequantizing them as affine and serving garbage. Correctly-declared uniform and legitimate mixed-precision checkpoints keep their exact effective parameters. Path B (unclosed thinking routing): the non-streaming chat path surfaces the primed-but-unclosed thinking condition through a shared primed_thinking_unclosed predicate and a structured warning, so a broken decode that never emits the close marker no longer masquerades as a clean, intentionally-empty response. Adds pure regression coverage for both the quantization-parameter reconciliation policy (synthetic tensor shapes) and the primed-but-unclosed thinking routing (synthetic decode streams).
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…blup#605) Serving an externally quantized 4-bit Gemma 4 (gemma-4-31B-it-qat-OptiQ-4bit) on macOS/MLX produced degenerate, repeating output at temperature 0 with every token trapped in reasoning_content and empty content. Two independent mlxcel paths combined: the quantized loader silently reconciled quantization parameters against tensor shapes with no validation, and the Gemma 4 thinking-channel filter emptied content whenever the primed thinking block never closed. Path A (quantization reconciliation): mlxcel-core now reconciles declared bits/group_size against the actual weight/scales shapes through one pure reconcile_quantization_layout helper, and the linear and embedding loaders warn at load time whenever reconciliation changes a value instead of silently overriding the config. The Gemma 4 loader (validate_quantization_scheme) rejects unsupported external schemes (OptiQ, AWQ, GPTQ, detected via a foreign quant_method or a non-affine mode) up front with an actionable message that names the format, rather than dequantizing them as affine and serving garbage. Correctly-declared uniform and legitimate mixed-precision checkpoints keep their exact effective parameters. Path B (unclosed thinking routing): the non-streaming chat path surfaces the primed-but-unclosed thinking condition through a shared primed_thinking_unclosed predicate and a structured warning, so a broken decode that never emits the close marker no longer masquerades as a clean, intentionally-empty response. Adds pure regression coverage for both the quantization-parameter reconciliation policy (synthetic tensor shapes) and the primed-but-unclosed thinking routing (synthetic decode streams).
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
Externally quantized 4-bit Gemma 4 (
gemma-4-31B-it-qat-OptiQ-4bit) decoded to degenerate, repeating output attemperature: 0with every token trapped inreasoning_contentand emptycontent. Two independent mlxcel paths combined to produce exactly that shape: the quantized loader silently reconciled quantization parameters against tensor shapes with no validation, and the Gemma 4 thinking-channel filter emptiedcontentwhenever the primed thinking block never closed. This change makes both paths fail loud instead of silent, and adds regression coverage for each.What changed
src/lib/mlxcel-core/src/layers.rs: added a pure, unit-testablereconcile_quantization_layout(plusReconciledQuant) that validates the declaredbits/group_sizeagainst the actualweight/scalesshapes. Both the linear and embedding quantized loaders now route through a shared logged wrapper that emits a load-timetracing::warn!when reconciliation changes a value (previously silent) and hard-errors on affine layouts that fit no valid bit width. Correctly-declared uniform checkpoints and legitimate per-layer mixed-precision exports keep their exact effective parameters (no behavior change beyond the added diagnostic).src/models/gemma4.rs: addedvalidate_quantization_scheme, which rejects unsupported external quantization formats (OptiQ / AWQ / GPTQ, detected via a foreignquant_methodor a non-affinemode, at the top level or nested undertext_config/quantization_config) before any weights load, with an actionable message that names the offending scheme. Wired into bothGemma4Model::loadandGemma4StageModel::load.src/server/routes/chat.rs: extracted the content-emptying decision into a sharedprimed_thinking_unclosedpredicate and emit a structuredtracing::warn!in the non-streaming path when a primed thinking channel never closes, so a broken or degenerate decode that never emits<channel|>/</think>no longer masquerades as a clean, intentionally-empty response.Notes
<|channel>/<channel|>are Gemma 4's deliberate thinking-channel markers, not leaked gpt-oss tokens, and are left intact.Test plan
cargo test -p mlxcel-core --features cuda --lib layers::tests::reconcile(9 new reconciliation tests)cargo test --features cuda --lib server::routes::chat(22 tests, incl. 4 newprimed_thinking_unclosedtests)cargo test --features cuda --lib models::gemma4::quant_scheme_tests(8 new scheme-validation tests)cargo clippy -p mlxcel-core --features cuda --lib --tests -- -D warningscargo clippy --features cuda --lib --tests -- -D warningsCloses #467