quantizer_compressed_tensors.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 ..utils import is_compressed_tensors_available, is_torch_available, logging
  15. from ..utils.quantization_config import QuantizationConfigMixin
  16. from .base import HfQuantizer
  17. if is_torch_available():
  18. import torch
  19. logger = logging.get_logger(__name__)
  20. class CompressedTensorsHfQuantizer(HfQuantizer):
  21. """
  22. Quantizer for the compressed_tensors package. Loads and restores models to
  23. quantized state with compressed_tensors
  24. """
  25. requires_calibration = True
  26. required_packages = ["compressed_tensors"]
  27. def __init__(self, quantization_config: QuantizationConfigMixin, **kwargs):
  28. super().__init__(quantization_config, **kwargs)
  29. from compressed_tensors.compressors import ModelCompressor
  30. self.compressor = ModelCompressor.from_compression_config(quantization_config)
  31. def validate_environment(self, *args, **kwargs):
  32. if not is_compressed_tensors_available():
  33. raise ImportError(
  34. "Using `compressed_tensors` quantized models requires the compressed-tensors library: "
  35. "`pip install compressed-tensors`"
  36. )
  37. if not is_torch_available():
  38. # torch already should be installed as part of compressed tensors
  39. raise ImportError("torch is required for using compressed-tensors quantization")
  40. def update_torch_dtype(self, torch_dtype: "torch.dtype") -> "torch.dtype":
  41. if torch_dtype is None:
  42. logger.info("Loading model using torch.float16 for compressed-tensors quantization")
  43. torch_dtype = torch.float16
  44. elif torch_dtype != torch.float16:
  45. logger.info(
  46. "We suggest you to set `torch_dtype=torch.float16` for better efficiency with compressed_tensors."
  47. )
  48. return torch_dtype
  49. def _process_model_before_weight_loading(self, model, **kwargs):
  50. from compressed_tensors.quantization import apply_quantization_config
  51. ct_quantization_config = self.compressor.quantization_config
  52. apply_quantization_config(model, ct_quantization_config, run_compressed=True)
  53. def _process_model_after_weight_loading(self, model, **kwargs):
  54. pass
  55. @property
  56. def is_trainable(self):
  57. return False
  58. def is_serializable(self, safe_serialization=None):
  59. return False