| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- # Copyright 2024 The HuggingFace Inc. team. All rights reserved.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- from typing import TYPE_CHECKING
- from .base import HfQuantizer
- if TYPE_CHECKING:
- from ..modeling_utils import PreTrainedModel
- from ..utils.quantization_config import VptqConfig
- from ..utils import is_accelerate_available, is_torch_available, is_vptq_available, logging
- from ..utils.quantization_config import QuantizationConfigMixin
- if is_torch_available():
- import torch
- logger = logging.get_logger(__name__)
- class VptqHfQuantizer(HfQuantizer):
- """
- Quantizer of the VPTQ method. Enables the loading of prequantized models.
- """
- requires_calibration = True
- quantization_config: "VptqConfig"
- def __init__(self, quantization_config: QuantizationConfigMixin, **kwargs):
- super().__init__(quantization_config, **kwargs)
- def validate_environment(self, *args, **kwargs):
- if not is_accelerate_available():
- raise ImportError("Using `vptq` quantization requires Accelerate: `pip install accelerate`")
- if not is_vptq_available():
- raise ImportError("Using `vptq` quantization requires VPTQ>=0.0.4: `pip install -U vptq`")
- if not torch.cuda.is_available():
- raise RuntimeError("GPU is required to run VTPQ quantized model.")
- def _process_model_before_weight_loading(
- self,
- model: "PreTrainedModel",
- **kwargs,
- ):
- from ..integrations import replace_with_vptq_linear
- self.modules_to_not_convert = self.get_modules_to_not_convert(
- model, self.quantization_config.modules_to_not_convert, model._keep_in_fp32_modules
- )
- replace_with_vptq_linear(
- model,
- quantization_config=self.quantization_config,
- modules_to_not_convert=self.modules_to_not_convert,
- )
- @property
- def is_trainable(self) -> bool:
- return False
- def is_serializable(self):
- return True
|