o
    i
                     @   s   d dl mZ ddlmZ erddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ e r3d d	lZeeZG d
d deZd	S )    )TYPE_CHECKING   )HfQuantizer   )PreTrainedModel)replace_with_spqr_linear)is_accelerate_availableis_spqr_availableis_torch_availablelogging)QuantizationConfigMixinNc                       s^   e Zd ZdZdZdef fddZdd ZdddZ		dddZ	e
dd Zdd Z  ZS )SpQRHfQuantizerzS
    Quantizer of the SpQR method. Enables the loading of prequantized models.
    Tquantization_configc                    s   t  j|fi | d S )N)super__init__)selfr   kwargs	__class__ h/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/transformers/quantizers/quantizer_spqr.pyr   (   s   zSpQRHfQuantizer.__init__c                 O   s2   t j s	tdt stdt stdd S )Nz,GPU is required to run SpQR quantized model.zGUsing `spqr` quantization requires Accelerate: `pip install accelerate`zFUsing `spqr` quantization requires SpQR: `pip install spqr_quant[gpu]`)torchcudais_availableRuntimeErrorr   ImportErrorr	   )r   argsr   r   r   r   validate_environment+   s   
z$SpQRHfQuantizer.validate_environmentdtypetorch.dtypereturnc                 C   s   |t jkr	td|S )NzdYou cannot use any type other than torch.float16 for SpQR. Please set it totorch.float16 explicitly.)r   float16
ValueError)r   r   r   r   r   update_dtype5   s
   
zSpQRHfQuantizer.update_dtypemodelr   c                 K   s,   |  || jj|j| _t|| j| jd d S )N)r   modules_to_not_convert)get_modules_to_not_convertr   r%   _keep_in_fp32_modulesr   )r   r$   r   r   r   r   $_process_model_before_weight_loading<   s   
z4SpQRHfQuantizer._process_model_before_weight_loadingc                 C      dS )NFr   r   r   r   r   is_trainableJ   s   zSpQRHfQuantizer.is_trainablec                 C   r)   )NTr   r*   r   r   r   is_serializableN   s   zSpQRHfQuantizer.is_serializable)r   r   r    r   )r$   r   )__name__
__module____qualname____doc__requires_calibrationr   r   r   r#   r(   propertyr+   r,   __classcell__r   r   r   r   r   !   s    



r   )typingr   baser   modeling_utilsr   integrationsr   utilsr   r	   r
   r   utils.quantization_configr   r   
get_loggerr-   loggerr   r   r   r   r   <module>   s   
