
    6i
                         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       rd d	lZ ej                   e      Z G d
 de      Zy	)    )TYPE_CHECKING   )HfQuantizer   )PreTrainedModel)replace_with_spqr_linear)is_accelerate_availableis_spqr_availableis_torch_availablelogging)QuantizationConfigMixinNc                   \     e Zd ZdZdZdef fdZd Zd
dZ	 	 ddZ	e
d        Zd	 Z xZS )SpQRHfQuantizerzS
    Quantizer of the SpQR method. Enables the loading of prequantized models.
    Tquantization_configc                 &    t        |   |fi | y )N)super__init__)selfr   kwargs	__class__s      b/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_spqr.pyr   zSpQRHfQuantizer.__init__(   s    ,77    c                     t         j                  j                         st        d      t	               st        d      t               st        d      y )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   s      r   validate_environmentz$SpQRHfQuantizer.validate_environment+   sG    zz&&(MNN&(ghh "fgg #r   c                 B    |t         j                  k7  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   dtypes     r   update_dtypezSpQRHfQuantizer.update_dtype5   s%    EMM!v  r   c                     | j                  || j                  j                  |j                        | _        t	        || j                  | j                         y )N)r   modules_to_not_convert)get_modules_to_not_convertr   r'   _keep_in_fp32_modulesr   )r   modelr   s      r   $_process_model_before_weight_loadingz4SpQRHfQuantizer._process_model_before_weight_loading<   sO    
 '+&E&E4++BBED_D_'
# 	! $ 8 8#'#>#>	
r   c                      y)NF r   s    r   is_trainablezSpQRHfQuantizer.is_trainableJ   s    r   c                      y)NTr-   r.   s    r   is_serializablezSpQRHfQuantizer.is_serializableN   s    r   )r$   torch.dtypereturnr2   )r*   r   )__name__
__module____qualname____doc__requires_calibrationr   r   r    r%   r+   propertyr/   r1   __classcell__)r   s   @r   r   r   !   sN      8,C 8h
 
  r   r   )typingr   baser   modeling_utilsr   integrationsr   utilsr	   r
   r   r   utils.quantization_configr   r   
get_loggerr4   loggerr   r-   r   r   <module>rC      sJ    !  0 3 [ [ ? 			H	%.k .r   