Fix Qwen3_5 mixed-bit per-tensor quantization keys not remapped through sanitize#1311
Open
ivaniguarans wants to merge 1 commit into
Open
Fix Qwen3_5 mixed-bit per-tensor quantization keys not remapped through sanitize#1311ivaniguarans wants to merge 1 commit into
ivaniguarans wants to merge 1 commit into
Conversation
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.
Problem
Loading an
Qwen3_5ForConditionalGenerationcheckpoint with non-uniform per-tensorconfig["quantization"]overrides — notably Intel's AutoRound mixed-bit MLX builds — fails onmainwith:Reproduced against
Intel/Qwen3.6-27B-4.5b-mlx-AutoRound(459 per-tensor entries, 128 of which are real 6-bit overrides on top of a 4-bit global).Root cause
class_predicateinmlx_lm/utils.pylooks up paths in the post-sanitize namespace, butconfig["quantization"]keys are stored in the pre-sanitize namespace as they were written toconfig.json. For Qwen3.5_VL,Model.sanitizeinmlx_lm/models/qwen3_5.pyrewritesmodel.language_model.*→language_model.model.*(and dropsvision_tower.*/model.visual.*entries), but the quantization config is never remapped to match. Every per-tensor override misses the lookup, falls back to the global bit-width, and the tensor is allocated at the wrong size — failing the shape check on load.Fix
Add a per-model
sanitize_quantization(quant_config)method onQwen3_5ForConditionalGenerationthat mirrors the existingsanitize(weights)key transformation, and wire it via ahasattr-discovered opt-in call at the_quantizecall site inmlx_lm/utils.py— the same shape as the existingsanitizeinvocation a few lines above.Qwen3_5MoeForConditionalGenerationinherits the method through itsclass Model(Qwen3_5Model)subclassing, so MoE checkpoints pick up the prefix remap without a separate implementation (MoE-specific expert-key remaps such asexperts.gate_up_proj→switch_mlp.gate_projare not in scope of this change — no MoE mixed-bit checkpoint with per-tensor expert overrides has been reported yet).Models without the method are unaffected. The hook is scoped to the top-level
config["quantization"]branch; the legacyquantization_configpath (AWQ/GPTQ/mxfp4) carries flat global configs without per-tensor overrides and does not exercise this bug.Credit to @ivanfioravanti for the local-fix observation in #1214's comments.
Testing
TestQwen3_5SanitizeQuantizationintests/test_utils.py): one table-driven test covering all five key shapes the method handles (globals passthrough,vision_tower/model.visualdrops,model.language_model.*rewrite,language_model.*passthrough, bare-key prefix), plus a non-mutation test confirming the input dict is left intact.TestUtils.test_load_model_qwen3_5_with_unsanitized_per_tensor_quantization): mirrors the existingtest_load_model_gemma4_with_per_layer_projection_quantizationpattern. Builds a tiny synthetic Qwen3.5 model, hand-editsconfig["quantization"]with an un-sanitized per-tensor override key, saves and reloads viaload_model, and asserts the rewritten key appears inloaded_config["quantization"](the un-sanitized form is gone), then runs a forward pass as a smoke test thatload_weightsdoesn't raise on the rewritten config.Intel/Qwen3.6-27B-4.5b-mlx-AutoRound:ValueErroras quoted above.generate(prompt="The capital of France is", max_tokens=8)returns"Paris.\n\n<think>\nHere's a".python -m unittest discover tests/shows no regressions (12 pre-existing failures, all verified to fail identically onupstream/main).Fixes #1214.