Skip to content

fix: validate external quant scheme and surface unclosed thinking - #605

Merged
inureyes merged 1 commit into
mainfrom
fix/issue-467-optiq-quant-and-thinking
Jul 2, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/issue-467-optiq-quant-and-thinking

Conversation

@inureyes

@inureyes inureyes commented Jul 2, 2026

Copy link
Copy Markdown
Member

Summary

Externally quantized 4-bit Gemma 4 (gemma-4-31B-it-qat-OptiQ-4bit) decoded to degenerate, repeating output at temperature: 0 with every token trapped in reasoning_content and empty content. 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 emptied content whenever 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-testable reconcile_quantization_layout (plus ReconciledQuant) that validates the declared bits / group_size against the actual weight / scales shapes. Both the linear and embedding quantized loaders now route through a shared logged wrapper that emits a load-time tracing::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: added validate_quantization_scheme, which rejects unsupported external quantization formats (OptiQ / AWQ / GPTQ, detected via a foreign quant_method or a non-affine mode, at the top level or nested under text_config / quantization_config) before any weights load, with an actionable message that names the offending scheme. Wired into both Gemma4Model::load and Gemma4StageModel::load.
  • src/server/routes/chat.rs: extracted the content-emptying decision into a shared primed_thinking_unclosed predicate and emit a structured tracing::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.
  • The OptiQ model is macOS / MLX specific and cannot run on this Linux / CUDA host, so verification is by unit / regression tests over synthetic tensor shapes and synthetic decode streams, per the acceptance criteria.

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 new primed_thinking_unclosed tests)
  • 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 warnings
  • cargo clippy --features cuda --lib --tests -- -D warnings

Closes #467

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).
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:models Model architectures, weights, loading, metadata area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review status:done Completed and removed status:review Under review labels Jul 2, 2026
@inureyes
inureyes merged commit abcda45 into main Jul 2, 2026
4 checks passed
@inureyes
inureyes deleted the fix/issue-467-optiq-quant-and-thinking branch July 2, 2026 03:04
@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
…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata 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: externally quantized 4-bit Gemma 4 (OptiQ) decodes to degenerate output that is trapped entirely in reasoning_content

1 participant