quantizer_quark.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. from typing import TYPE_CHECKING
  16. from .base import HfQuantizer
  17. if TYPE_CHECKING:
  18. from ..modeling_utils import PreTrainedModel
  19. from ..utils import is_quark_available, logging
  20. logger = logging.get_logger(__name__)
  21. CHECKPOINT_KEYS = {
  22. "weight_scale": "weight_quantizer.scale",
  23. "bias_scale": "bias_quantizer.scale",
  24. "input_scale": "input_quantizer.scale",
  25. "output_scale": "output_quantizer.scale",
  26. "weight_zero_point": "weight_quantizer.zero_point",
  27. "bias_zero_point": "bias_quantizer.zero_point",
  28. "input_zero_point": "input_quantizer.zero_point",
  29. "output_zero_point": "output_quantizer.zero_point",
  30. }
  31. class QuarkHfQuantizer(HfQuantizer):
  32. """
  33. Quark quantizer (https://quark.docs.amd.com/latest/).
  34. """
  35. requires_calibration = True # On-the-fly quantization with quark is not supported for now.
  36. required_packages = ["quark"]
  37. # Checkpoints are expected to be already quantized when loading a quark model. However, as some keys from
  38. # the checkpoint might mismatch the model parameters keys, we use the `create_quantized_param` method
  39. # to load the checkpoints, remapping the keys.
  40. requires_parameters_quantization = True
  41. def __init__(self, quantization_config, **kwargs):
  42. super().__init__(quantization_config, **kwargs)
  43. self.json_export_config = quantization_config.json_export_config
  44. def validate_environment(self, *args, **kwargs):
  45. if not is_quark_available():
  46. raise ImportError(
  47. "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."
  48. )
  49. def _process_model_before_weight_loading(self, model: "PreTrainedModel", **kwargs):
  50. from quark.torch.export.api import _map_to_quark
  51. _map_to_quark(
  52. model,
  53. self.quantization_config.quant_config,
  54. pack_method=self.json_export_config.pack_method,
  55. custom_mode=self.quantization_config.custom_mode,
  56. )
  57. return model
  58. def param_needs_quantization(self, model: "PreTrainedModel", param_name: str, **kwargs) -> bool:
  59. return True
  60. def create_quantized_param(self, model, param, param_name, param_device, **kwargs):
  61. from ..modeling_utils import _load_parameter_into_model
  62. postfix = param_name.split(".")[-1]
  63. if postfix in CHECKPOINT_KEYS:
  64. param_name = param_name.replace(postfix, CHECKPOINT_KEYS[postfix])
  65. _load_parameter_into_model(model, param_name, param.to(param_device))
  66. def _process_model_after_weight_loading(self, model: "PreTrainedModel", **kwargs):
  67. return model
  68. def is_serializable(self, safe_serialization=None):
  69. return False
  70. @property
  71. def is_trainable(self):
  72. return False