Skip to content

Simplify the quantizer API changes - #49929

Open
SunMarc wants to merge 5 commits into
fg-split/2-quantizer-apifrom
fg-split/2-quantizer-api-simplify
Open

SunMarc wants to merge 5 commits into
fg-split/2-quantizer-apifrom
fg-split/2-quantizer-api-simplify

Conversation

@SunMarc

@SunMarc SunMarc commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do ?

This PR simplifies some of the changes in #49774

- Dequantized save: `remove_quantization_config` also resets `model._weight_conversions`, so a model
  loaded with `dequantize=True` (or dequantized later) saves with its own conversion mapping, like an
  unquantized model. This drops the `_unquantized_transform` slot on `WeightTransform`, the deep copies
  and the `dequantized_saves_unquantized` flag, and leaves core loading untouched.
- Skip-lists with checkpoint names: `get_converted_skip_modules` renames each module back to its
  checkpoint name and matches the skip-list as written, so regex entries keep working. It replaces
  `checkpoint_module_namer`.
- Shorter tests: the skip-list test builds a small Llava on the meta device, and the dequantized-save
  test mimics an FP8 checkpoint from the Hub (per-expert weights with their own scales), so it needs
  no patch and checks the saved names and values against the original weights.
@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.

- Dequantized save: the quantizer records which of the model's conversions each conversion it hands
  to the loader stands for. After a dequantized load (or `model.dequantize()`),
  `remove_quantization_config` swaps each conversion that fired for the model's own and drops the
  quantizer's, so the model saves like an unquantized model loaded from the same checkpoint layout.
  This drops the `_unquantized_transform` slot on `WeightTransform`, the deep copies and the
  `dequantized_saves_unquantized` flag, and leaves core loading untouched.
- Dequantize a checkpoint already in the model's layout: an FP8 checkpoint with fused experts
  (`experts.gate_up_proj` and `experts.gate_up_proj_scale_inv`) loaded its experts as raw fp8 values.
  The quantizer now adds a dequantize conversion for that layout, and `Fp8Dequantize` treats any
  `*_scale_inv` entry as a scale.
- Skip-lists with checkpoint names: `get_converted_skip_modules` renames each module back to its
  checkpoint name and matches the skip-list as written, so regex entries keep working. It replaces
  `checkpoint_module_namer`.
- Shorter tests: the skip-list test builds a small Llava on the meta device, and the dequantized-save
  test mimics an FP8 checkpoint from the Hub (experts per expert) and one in transformers' layout
  (experts fused), so it needs no patch and checks the saved names and values against the original
  weights.
@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: finegrained_fp8

Comment on lines +1095 to 1115
# Experts saved after quantizing on load have per-expert weights but their scales merged like the converter's
# target, e.g. `experts.gate_up_proj_scale_inv`. Split it into a scale per expert and weight: the converter
# merges the experts on dim 0 and concatenates the weights on dim 1.
input_dict = dict(input_dict)
weight_keys = [key for key in input_dict if "scale" not in key]
merged_keys = [
k for k in input_dict if k.endswith("_scale_inv") and k.removesuffix("_scale_inv") not in input_dict
]
for merged_key in merged_keys:
merged = input_dict[merged_key]
merged = merged[0] if isinstance(merged, list) else merged
# Only per-expert weights, one per expert of the merged scale. Other merges keep the scale unused
if not all(
isinstance(input_dict[key], list) and len(input_dict[key]) == len(merged) for key in weight_keys
):
continue
del input_dict[merged_key]
for key, scales in zip(weight_keys, merged.chunk(len(weight_keys), dim=1)):
input_dict.setdefault(f"{key}_scale_inv", list(scales))

# Generic chain path: dequantize every weight pattern that has a sibling scale.

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.

not the best way but this is to take care of a really special case where the hub checkpoint is in bf16 per expert and we quantize it but then we want to deq + save.

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 there is probably something structurally wrong with our API, this is the SplitModulelist which should be applied regarless of the input dict, on the weghts, on the scales etc

if self.pre_quantized and getattr(self.quantization_config, "dequantize", False):
self.remove_quantization_config(model)
conversions = getattr(model, "_weight_conversions", None)
if self.dequantized_saves_unquantized and conversions is not None:

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.

dequantized_saves_unquantized -> should always be true. We don't want to revert the dequantization once it happens. So I removed it

self.remove_quantization_config(model)
conversions = getattr(model, "_weight_conversions", None)
if self.dequantized_saves_unquantized and conversions is not None:
model_transforms = getattr(self, "_model_transforms", None)

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.

either we do that in the quantizer code with _model_transforms, or we do as you did in the core_modeling file

Comment on lines +283 to +284
# Also add the skipped modules under the names the conversion mapping gives them
modules_to_not_convert.extend(get_converted_skip_modules(model, skip_modules))

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.

i tried to clean a bit

from safetensors.torch import load_file

class FP8SavingTest(unittest.TestCase):
@parameterized.expand(["bf16_per_expert", "bf16_fused", "fp8_per_expert"])

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.

i think this is a bit more robust to test all cases


@parameterized.expand(["bf16_per_expert", "bf16_fused", "fp8_per_expert"])
@patch.object(FineGrainedFP8HfQuantizer, "validate_environment", lambda *args, **kwargs: None)
def test_dequantize_and_save(self, checkpoint):

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.

this is the test you had, feel free to update it if you think we are missing something

Comment on lines +663 to +664
def _quantize_to_fp8(path):
"""Quantize a checkpoint like the FP8 ones on the Hub: each attention and expert weight in fp8 with its scale."""

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.

we can't really do that anymore with the api, so i hardcoded this here to mimic the checkpoint but smaller

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.

3 participants