Repository navigation
Quantizer API: checkpoint renames, skip-lists in checkpoint names, dequantized saves - #49774
IlyasMoutawwakil wants to merge 29 commits into
Conversation
f06ffe2 to
14dbad6
Compare
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
8ed1e7b to
e00aaeb
Compare
e00aaeb to
baa62e2
Compare
baa62e2 to
2b29e37
Compare
|
|
||
| return generation_params | ||
|
|
||
| def adapt_quantization_config(self, quantization_config): |
There was a problem hiding this comment.
I feel like im missing something, why is this even needed?
There was a problem hiding this comment.
nothing overrides it in this pr, first user is DeepseekV4Config in #49778: its checkpoints say fp8 and mark the experts with expert_dtype="fp4" on the model config, so it returns a config with mxfp4 experts. added that to the docstring
| # Flag to notice if the Transform was used | ||
| self._was_used = False | ||
| # The model's own transform this one derives from. Only set when a dequantized model saves unquantized. | ||
| self._origin: WeightTransform | None = None |
There was a problem hiding this comment.
imo this needs a more explicit naming => this is only for when the conversion is not gonna be reversed?
There was a problem hiding this comment.
renamed to _unquantized_transform, it's the model's own transform the quantizer copy came from, only the dequantized save reads it
| """Return a function mapping a module name of `model` to its name in the checkpoint. | ||
|
|
||
| It applies the model's renames in reverse, as the loader does. Renames match keys, and some anchor on a parameter | ||
| name such as `^embed.weight`. So the namer renames the module's `weight` key. |
There was a problem hiding this comment.
the suffix is only there so renames anchored on a param name like ^embed.weight match. a module rename moves all its params the same way so bias gives the same name. said it in the docstring
| # The skip-list uses checkpoint names, so also add the modules under their model names | ||
| modules_to_not_convert.extend(skip_modules) | ||
| modules_to_not_convert.extend(modules_skipped_in_checkpoint(model, skip_modules)) |
There was a problem hiding this comment.
hmm should this be converged into the one util?
There was a problem hiding this comment.
yep merged, inlined it into get_modules_to_not_convert since it was the only caller. checkpoint_module_namer stays separate because the fine-grained plan uses it too (#49777)
| if self.dequantized_saves_unquantized and getattr(model, "_weight_conversions", None) is not None: | ||
| # Keep the model transforms that fired, directly or through a quantizer copy | ||
| fired = (conversion._origin for conversion in model._weight_conversions) | ||
| model._weight_conversions = list( | ||
| {id(origin): origin for origin in fired if origin is not None}.values() | ||
| ) |
There was a problem hiding this comment.
hmm not sure whether _origin should live within core model loading no? This is very quant specific so not sure if we should mess with things there
There was a problem hiding this comment.
feel like we should potentially make a set of conversions that we remove or similar no?
There was a problem hiding this comment.
thought about keeping a list on the quantizer instead but the loader only keeps the transforms that fired, so a plain list would reverse transforms that never matched on save. the per-transform link is what maps the fired ones back
| ) | ||
| else: | ||
| config.quantization_config = quantization_config | ||
| config.quantization_config = config.adapt_quantization_config(quantization_config) |
There was a problem hiding this comment.
ok its used herebut i still dont see the actual usage as it just returns the quant config directly no?
There was a problem hiding this comment.
yes the default just returns it as is, only models whose checkpoints describe part of their quantization elsewhere override it (deepseek v4 in #49778)
| return {"dequantize": Fp8Dequantize} | ||
|
|
||
| def update_weight_conversions(self, weight_conversions, model): | ||
| def get_checkpoint_renamings(self, model=None): |
e74b6e8 to
c15c3b7
Compare
458a02a to
b050c04
Compare
| dequantized_saves_unquantized (`bool`): | ||
| Whether a model loaded with `dequantize=True` saves like an unquantized model. The save then uses only | ||
| the model's own conversions, not the quantizer's. It is off by default, so the other quantizers keep | ||
| their current save. |
There was a problem hiding this comment.
or we can standardize on one rule here (save dequantized or requantize on save)
No other key under the MoE block starts with `e_score_correction_bias`, so the alternative matches without it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- register_quantization_config_reader: AutoQuantizationConfig.from_dict consults a reader for checkpoints another tool wrote before the class mapping (empty on main; the block-scaled formats register theirs). - PreTrainedConfig.adapt_quantization_config: called once when the quantizer is built, so a model whose checkpoints describe part of their quantization elsewhere (DeepSeek-V4's expert_dtype) adapts it without auto.py knowing. - QuantizationConfigMixin.loading_attributes_lender: a config names which passed config may lend it loading attributes, by default its own class. - The AutoRound special case in merge_quantization_configs becomes CHECKPOINT_READING_CONFIG_TYPES. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd dequantized saves - HfQuantizer.get_weight_conversions(model): converters may depend on the modules that were built. - HfQuantizer.get_checkpoint_renamings(model): renames of the checkpoint's own keys, applied before the model's conversion mapping (a producer's naming). - get_modules_to_not_convert also takes the modules a checkpoint skip-list names under the checkpoint's names (checkpoint_module_namer); the legacy FP8 quantizer's own forward-renaming pass goes. - Core dequantized save: each transform the quantizer derives from a model transform remembers it (_origin); a quantizer with dequantized_saves_unquantized=True saves a dequantized load through the model's own conversions that fired. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tized save The legacy FP8 quantizer's `.scale` rename moves onto `get_checkpoint_renamings`. It also opts into `dequantized_saves_unquantized`, so a dequantized load saves like an unquantized model. Only such a load copies the model's conversions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`register_quantization_config_reader` has no user, so it goes with its test. `get_hf_quantizer` builds the checkpoint's config before `adapt_quantization_config` sees it. Add a test that the hook runs on both paths and rewrite the hook docstrings plainly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rewrite the comments, docstrings and docs prose this PR adds in short, plain sentences. Drop comments that restate the code and history or benchmark narration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the PRs that use them Main has no config class that lends its loading attributes, no producer reader and no get_weight_conversions that reads the model, so the class check, the AutoRound special case and the old signature stay as on main. The machinery PR adds loading_attributes_lender and get_weight_conversions(model); the FP8 PR adds the reader registry. Build the legacy config directly in the dequantized-save test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`ty` types `nn.Module` attribute reads as tensors or modules and its writes as the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… core loading update_weight_conversions already receives the model's conversions, so conversion_mapping.py keeps main's code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es it Its first user is DeepseekV4Config in the FP8 PR. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6c12f9d to
3473b2a
Compare
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
FWIW this also fixes saving a dequantized DeepSeek V4. On main (and 5.20.2), this fails in from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("tiny-random/deepseek-v4") # on CPU, so dequantize=True
model.save_pretrained("out")With this branch it saves BF16 experts under the checkpoint keys ( |
…nto fg-split/fix-minimax-m3-vl-save-mapping
…mapping' into fg-split/1-weight-loading # Conflicts: # src/transformers/core_model_loading.py # tests/utils/test_core_model_loading.py
…fg-split/2-quantizer-api
|
[For maintainers] Suggested jobs to run (before merge) run-slow: finegrained_fp8 |
What. Hooks only, no format code. Each says why it exists and who uses it first.
HfQuantizer.get_checkpoint_renamings(): renames of the checkpoint keys, applied before the model's conversionmapping. It replaces the legacy FP8 quantizer's
update_weight_conversionsoverride, which prepended a.scalerename for DeepSeek-V4-Flash. The block-scaled formats use it for their producers' scale names (FP8 on the fine-grained machinery (
Fp8Config) #49777).get_modules_to_not_convertalso covers the modules a checkpoint skip-list names under checkpoint names. A VLM'slanguage_model.model.*loads asmodel.language_model.*, so its skip-list missed those modules before.It reuses
checkpoint_module_namer, which the fine-grained plan also uses to read checkpoint names (FP8 on the fine-grained machinery (Fp8Config) #49777).The legacy FP8 quantizer's own renaming pass goes.
dequantize=Truesaved with the quantizer's conversions. A quantizer withdequantized_saves_unquantized=Truenow saves with the model's own conversions.update_weight_conversionsmarksthem, so core loading has no quantization branch. The legacy FP8 quantizer opts in here, the block-scaled formats
in FP8 on the fine-grained machinery (
Fp8Config) #49777. It is off by default so the other quantizers keep their save.Behaviour change. A legacy FP8 checkpoint loaded with
dequantize=Truenow saves with the model's own keys.Depends on. #49773.
How tested. It extends
tests/quantization/test_quantizers_utils.pyfrom #49771 with a Llava skip-list test. The new test intests/quantization/finegrained_fp8/test_fp8.pychecks that adequantized Mixtral saves the unquantized keys, so its fused-experts converter is reversed.
test_megamoe_takes_over_the_experts_ep_rulealready fails on main; #49775 fixes it.The comments and docstrings that say why each change exists are there for readability. Feel free to ask for any of them to go if it's self-evident.
🤖 Generated with Claude Code