|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 Advanced Micro Devices, Inc. and The HuggingFace Inc. team. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from typing import TYPE_CHECKING, Any, Dict |
| 17 | + |
| 18 | +from ..file_utils import is_torch_available |
| 19 | +from .base import HfQuantizer |
| 20 | + |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from ..modeling_utils import PreTrainedModel |
| 24 | + |
| 25 | + if is_torch_available(): |
| 26 | + import torch |
| 27 | + |
| 28 | +from ..utils import is_accelerate_available, is_quark_available, logging |
| 29 | + |
| 30 | + |
| 31 | +if is_accelerate_available(): |
| 32 | + from accelerate.utils import set_module_tensor_to_device |
| 33 | + |
| 34 | +logger = logging.get_logger(__name__) |
| 35 | + |
| 36 | + |
| 37 | +CHECKPOINT_KEYS = { |
| 38 | + "weight_scale": "weight_quantizer.scale", |
| 39 | + "bias_scale": "bias_quantizer.scale", |
| 40 | + "input_scale": "input_quantizer.scale", |
| 41 | + "output_scale": "output_quantizer.scale", |
| 42 | + "weight_zero_point": "weight_quantizer.zero_point", |
| 43 | + "bias_zero_point": "bias_quantizer.zero_point", |
| 44 | + "input_zero_point": "input_quantizer.zero_point", |
| 45 | + "output_zero_point": "output_quantizer.zero_point", |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +class QuarkHfQuantizer(HfQuantizer): |
| 50 | + """ |
| 51 | + Quark quantizer (https://quark.docs.amd.com/latest/). |
| 52 | + """ |
| 53 | + |
| 54 | + requires_calibration = True # On-the-fly quantization with quark is not supported for now. |
| 55 | + required_packages = ["quark"] |
| 56 | + |
| 57 | + # Checkpoints are expected to be already quantized when loading a quark model. However, as some keys from |
| 58 | + # the checkpoint might mismatch the model parameters keys, we use the `create_quantized_param` method |
| 59 | + # to load the checkpoints, remapping the keys. |
| 60 | + requires_parameters_quantization = True |
| 61 | + |
| 62 | + def __init__(self, quantization_config, **kwargs): |
| 63 | + super().__init__(quantization_config, **kwargs) |
| 64 | + |
| 65 | + self.json_export_config = quantization_config.json_export_config |
| 66 | + |
| 67 | + def validate_environment(self, *args, **kwargs): |
| 68 | + if not is_quark_available(): |
| 69 | + raise ImportError( |
| 70 | + "Loading a Quark quantized model requires the `quark` library but it was not found in the environment. Please refer to https://quark.docs.amd.com/latest/install.html." |
| 71 | + ) |
| 72 | + |
| 73 | + def _process_model_before_weight_loading(self, model: "PreTrainedModel", **kwargs): |
| 74 | + from quark.torch.export.api import _map_to_quark |
| 75 | + |
| 76 | + _map_to_quark( |
| 77 | + model, |
| 78 | + self.quantization_config.quant_config, |
| 79 | + pack_method=self.json_export_config.pack_method, |
| 80 | + custom_mode=self.quantization_config.custom_mode, |
| 81 | + ) |
| 82 | + |
| 83 | + return model |
| 84 | + |
| 85 | + def check_quantized_param( |
| 86 | + self, |
| 87 | + model: "PreTrainedModel", |
| 88 | + param_value: "torch.Tensor", |
| 89 | + param_name: str, |
| 90 | + state_dict: Dict[str, Any], |
| 91 | + **kwargs, |
| 92 | + ) -> bool: |
| 93 | + return True |
| 94 | + |
| 95 | + def create_quantized_param( |
| 96 | + self, model, param, param_name, param_device, state_dict, unexpected_keys |
| 97 | + ) -> "torch.nn.Parameter": |
| 98 | + postfix = param_name.split(".")[-1] |
| 99 | + |
| 100 | + if postfix in CHECKPOINT_KEYS: |
| 101 | + param_name = param_name.replace(postfix, CHECKPOINT_KEYS[postfix]) |
| 102 | + |
| 103 | + set_module_tensor_to_device(model, param_name, param_device, value=param) |
| 104 | + |
| 105 | + def _process_model_after_weight_loading(self, model: "PreTrainedModel", **kwargs): |
| 106 | + return model |
| 107 | + |
| 108 | + def is_serializable(self, safe_serialization=None): |
| 109 | + return False |
| 110 | + |
| 111 | + @property |
| 112 | + def is_trainable(self): |
| 113 | + return False |
0 commit comments