quantizer_auto_round.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2024 The HuggingFace Inc. team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import TYPE_CHECKING
  15. from .base import HfQuantizer
  16. if TYPE_CHECKING:
  17. from ..modeling_utils import PreTrainedModel
  18. from ..utils import is_auto_round_available, is_torch_available, logging
  19. from ..utils.quantization_config import QuantizationConfigMixin
  20. if is_torch_available():
  21. import torch
  22. logger = logging.get_logger(__name__)
  23. class AutoRoundQuantizer(HfQuantizer):
  24. """
  25. Quantizer of the AutoRound method. (https://huggingface.co/papers/2309.05516)
  26. """
  27. # AutoRound requires data calibration - we support only inference
  28. requires_calibration = True
  29. required_packages = ["auto_round"]
  30. def __init__(self, quantization_config: QuantizationConfigMixin, **kwargs):
  31. super().__init__(quantization_config, **kwargs)
  32. def validate_environment(self, *args, **kwargs):
  33. self.device_map = kwargs.get("device_map")
  34. if not is_auto_round_available():
  35. raise ImportError(
  36. "Loading an AutoRound quantized model requires auto-round library (`pip install 'auto-round>=0.5'`)"
  37. )
  38. def update_dtype(self, dtype: "torch.dtype") -> "torch.dtype":
  39. if dtype is None:
  40. dtype = torch.bfloat16
  41. logger.info("Loading the model in `torch.bfloat16`. To overwrite it, set `dtype` manually.")
  42. return dtype
  43. def _process_model_before_weight_loading(self, model: "PreTrainedModel", **kwargs):
  44. if model.__class__.main_input_name != "input_ids":
  45. logger.warning("AutoRound offers only limited support for models that are not strictly text-based.")
  46. from auto_round.inference.convert_model import convert_hf_model, infer_target_device
  47. if self.pre_quantized:
  48. target_device = infer_target_device(self.device_map)
  49. model, used_backends = convert_hf_model(model, target_device)
  50. self.used_backends = used_backends
  51. def _process_model_after_weight_loading(self, model: "PreTrainedModel", **kwargs):
  52. if self.pre_quantized:
  53. from auto_round.inference.convert_model import post_init
  54. post_init(model, self.used_backends)
  55. else:
  56. raise ValueError("AutoRound only sports pre-quantized models.")
  57. @property
  58. def is_trainable(self) -> bool:
  59. return False
  60. def is_serializable(self, safe_serialization=None):
  61. ## for gptq/awq models, the quantization config will be changed
  62. return True