Skip to content

Quantizer API: checkpoint renames, skip-lists in checkpoint names, dequantized saves - #49774

Open
IlyasMoutawwakil wants to merge 29 commits into
mainfrom
fg-split/2-quantizer-api
Open

IlyasMoutawwakil wants to merge 29 commits into
mainfrom
fg-split/2-quantizer-api

Conversation

@IlyasMoutawwakil

@IlyasMoutawwakil IlyasMoutawwakil commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

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 conversion
    mapping. It replaces the legacy FP8 quantizer's update_weight_conversions override, which prepended a .scale
    rename 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_convert also covers the modules a checkpoint skip-list names under checkpoint names. A VLM's
    language_model.model.* loads as model.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.
  • Dequantized save: a model loaded with dequantize=True saved with the quantizer's conversions. A quantizer with
    dequantized_saves_unquantized=True now saves with the model's own conversions. update_weight_conversions marks
    them, 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=True now saves with the model's own keys.

Depends on. #49773.

How tested. It extends tests/quantization/test_quantizers_utils.py from #49771 with a Llava skip-list test. The new test in tests/quantization/finegrained_fp8/test_fp8.py checks that a
dequantized Mixtral saves the unquantized keys, so its fused-experts converter is reversed.
test_megamoe_takes_over_the_experts_ep_rule already 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

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/2-quantizer-api branch 2 times, most recently from 8ed1e7b to e00aaeb Compare October 20, 2026 13:33
@IlyasMoutawwakil
IlyasMoutawwakil removed this pull request from stack #49781 October 20, 2026 13:41
@IlyasMoutawwakil IlyasMoutawwakil changed the title Quantizer API: model-aware conversions, checkpoint renames, config readers and adapters, skip-list resolution, dequantized saves Quantizer API: checkpoint renames, skip-lists in checkpoint names, dequantized saves, a config adapt hook Oct 20, 2026
@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/2-quantizer-api branch from e00aaeb to baa62e2 Compare October 20, 2026 16:04
@IlyasMoutawwakil
IlyasMoutawwakil removed this pull request from stack #49788 October 20, 2026 16:25
@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/2-quantizer-api branch from baa62e2 to 2b29e37 Compare October 20, 2026 16:25

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments

Comment thread src/transformers/configuration_utils.py Outdated

return generation_params

def adapt_quantization_config(self, quantization_config):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like im missing something, why is this even needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved it to #49777, where DeepseekV4Config first overrides it (it is #49777, not #49778 as i said above). the dequantized save marking also moved out of conversion_mapping.py into the quantizer

Comment thread src/transformers/core_model_loading.py Outdated
# 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo this needs a more explicit naming => this is only for when the conversion is not gonna be reversed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no bias or other things?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/transformers/quantizers/base.py Outdated
Comment on lines +281 to +283
# 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))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm should this be converged into the one util?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread src/transformers/quantizers/base.py Outdated
Comment on lines +226 to +231
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()
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel like we should potentially make a set of conversions that we remove or similar no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/transformers/quantizers/auto.py Outdated
)
else:
config.quantization_config = quantization_config
config.quantization_config = config.adapt_quantization_config(quantization_config)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok its used herebut i still dont see the actual usage as it just returns the quant config directly no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is nice

Comment on lines +85 to +88
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.

@IlyasMoutawwakil IlyasMoutawwakil Oct 21, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or we can standardize on one rule here (save dequantized or requantize on save)

@IlyasMoutawwakil IlyasMoutawwakil changed the title Quantizer API: checkpoint renames, skip-lists in checkpoint names, dequantized saves, a config adapt hook Quantizer API: checkpoint renames, skip-lists in checkpoint names, dequantized saves Oct 21, 2026
IlyasMoutawwakil and others added 13 commits October 23, 2026 13:34
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>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@benthecarman

Copy link
Copy Markdown
Contributor

FWIW this also fixes saving a dequantized DeepSeek V4. On main (and 5.20.2), this fails in revert_weight_conversion with ValueError: Undefined Operation encountered! on the experts converter:

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 (ffn.experts.N.w1.weight, ...), and reloading gives the same weights.

…mapping' into fg-split/1-weight-loading

# Conflicts:
#	src/transformers/core_model_loading.py
#	tests/utils/test_core_model_loading.py
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: finegrained_fp8

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants