Repository navigation
Conversation
- 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.
|
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.
…y' into fg-split/2-quantizer-api-simplify
|
[For maintainers] Suggested jobs to run (before merge) run-slow: finegrained_fp8 |
| # 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
either we do that in the quantizer code with _model_transforms, or we do as you did in the core_modeling file
| # 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)) |
| from safetensors.torch import load_file | ||
|
|
||
| class FP8SavingTest(unittest.TestCase): | ||
| @parameterized.expand(["bf16_per_expert", "bf16_fused", "fp8_per_expert"]) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
this is the test you had, feel free to update it if you think we are missing something
| 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.""" |
There was a problem hiding this comment.
we can't really do that anymore with the api, so i hardcoded this here to mimic the checkpoint but smaller
What does this PR do ?
This PR simplifies some of the changes in #49774