fix(utils): skip already-quantized layers in load_model._quantize predicate#1216
Open
adurham wants to merge 1 commit intoml-explore:mainfrom
Open
fix(utils): skip already-quantized layers in load_model._quantize predicate#1216adurham wants to merge 1 commit intoml-explore:mainfrom
adurham wants to merge 1 commit intoml-explore:mainfrom
Conversation
…dicate
Models that pre-quantize specific layers in their `__init__` (for
example DeepSeek V4's `DeepseekV4MoE` calling
`SwitchLinear.to_quantized(..., mode="mxfp4")` on its expert
projections so the experts have a non-default quantization mode) trip
`load_model._quantize`'s walker if those same layer paths also appear
in `config["quantization"]` as per-layer overrides. The predicate
returns the override dict for the path, so `nn.quantize` tries to
re-quantize the already-`Quantized*` module and raises:
ValueError: Unable to quantize model of type
<class 'mlx_lm.models.switch_layers.QuantizedSwitchLinear'>
The existing `not hasattr(m, "to_quantized")` clause already encodes
the "module is already quantized, skip" intent — moving it ahead of
the per-layer-override check makes that intent take effect even when
the override map covers a pre-quantized path. End state for
non-pre-quantized layers is unchanged.
Reproducible on `mlx-community/DeepSeek-V4-Flash-6bit` whose config
declares `model.layers.<i>.ffn.switch_mlp.{gate,up,down}_proj` overrides
at `mode="mxfp4", bits=4, group_size=32` — matching what the model
code pre-applies — and triggers the re-quantize attempt.
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
Models that pre-quantize specific layers in their
__init__(for example DeepSeek V4'sDeepseekV4MoEcallingSwitchLinear.to_quantized(..., mode=\"mxfp4\")on its expert projections so the experts have a non-default quantization mode) tripload_model._quantize's walker if those same layer paths also appear inconfig[\"quantization\"]as per-layer overrides. The predicate returns the override dict for the path, sonn.quantizetries to re-quantize the already-Quantized*module and raises:The existing
not hasattr(m, \"to_quantized\")clause already encodes the "module is already quantized, skip" intent — moving it ahead of the per-layer-override check makes that intent take effect even when the override map covers a pre-quantized path. End state for non-pre-quantized layers is unchanged.Repro
The 6bit checkpoint declares
model.layers.<i>.ffn.switch_mlp.{gate,up,down}_projoverrides atmode=\"mxfp4\", bits=4, group_size=32— matching what the model code pre-applies — and that's what triggers the re-quantize attempt.Diff
One-line move; ~8 lines counting the comment:
🤖 Generated with Claude Code