
    bi                         d dl mZ ddlmZ er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dS )    )TYPE_CHECKING   )HfQuantizer   )PreTrainedModel)is_accelerate_availableis_kernels_availableis_torch_availablelogging)get_module_from_nameNc                   |     e Zd ZdZdZ fdZd ZddZd	d
dede	fdZ
	 	 ddZd Zede	fd            Zd Z xZS )EetqHfQuantizerz:
    8-bit quantization from EETQ quantization method
    Fc                 <     t                      j        |fi | d S )N)super__init__)selfquantization_configkwargs	__class__s      a/root/projects/butler/venv/lib/python3.11/site-packages/transformers/quantizers/quantizer_eetq.pyr   zEetqHfQuantizer.__init__(   s)    ,7777777    c                    t                      st          d          t                      st          d          t          j                                        st          d          |                    d          }|t          	                    d           d S t          |t                    rNt          |          dk    rd|                                v sd|                                v rt          d	          d S d S )
NzHLoading an EETQ quantized model requires kernels (`pip install kernels`)zNLoading an EETQ quantized model requires accelerate (`pip install accelerate`)z/No GPU found. A GPU is needed for quantization.
device_mapzYou have loaded an EETQ model on CPU and have a CUDA device available, make sure to set your model on a GPU device in order to run your model.r   cpudiskzYou are attempting to load an EETQ model with a device_map that contains a CPU or disk device. This is not supported. Please remove the CPU or disk device from the device_map.)r	   ImportErrorr   torchcudais_availableRuntimeErrorgetloggerwarning_once
isinstancedictlenvalues
ValueError)r   argsr   r   s       r   validate_environmentz$EetqHfQuantizer.validate_environment+   s   #%% 	jhiii&(( 	pnoooz&&(( 	RPQQQZZ--
I     
D)) 	:""u
0A0A0C0C'C'CvQ[QbQbQdQdGdGd h  	 	GdGdr   dtypetorch.dtypereturnc                 Z    |t           j        k    rt                              d           |S )NzLWe suggest you to set `dtype=torch.float16` for better efficiency with EETQ.)r   float16r"   info)r   r+   s     r   update_dtypezEetqHfQuantizer.update_dtypeB   s'    EM!!KKfgggr   modelr   
param_namec                 z    ddl m} t          ||          \  }}t          ||          r| j        s|dk    rdS dS dS )Nr   )
EetqLinearbiasFT)integrations.eetqr5   r   r$   pre_quantized)r   r2   r3   r   r5   moduletensor_names          r   param_needs_quantizationz(EetqHfQuantizer.param_needs_quantizationG   s^    22222225*EEfj)) 	! [F%:%:utur   c                     ddl m} |                     || j        j        |j                  | _         ||| j        | j                  }d S )Nr   )replace_with_eetq_linear)modules_to_not_convertr8   )integrationsr=   get_modules_to_not_convertr   r>   _keep_in_fp32_modulesr8   )r   r2   r   r=   s       r   $_process_model_before_weight_loadingz4EetqHfQuantizer._process_model_before_weight_loadingS   si    
 	<;;;;;&*&E&E4+BED_'
 '
# )($*EUYUg
 
 
r   c                     dS NT r   s    r   is_serializablezEetqHfQuantizer.is_serializableb   s    tr   c                     dS rD   rE   rF   s    r   is_trainablezEetqHfQuantizer.is_trainablee   s    tr   c                 $    ddl m}  ||           S )Nr   )EetqQuantize)r7   rK   )r   rK   s     r   get_quantize_opsz EetqHfQuantizer.get_quantize_opsi   s$    444444|D!!!r   )r+   r,   r-   r,   )r2   r   )__name__
__module____qualname____doc__requires_calibrationr   r*   r1   strboolr;   rB   rG   propertyrI   rL   __classcell__)r   s   @r   r   r   !   s          !8 8 8 8 8  .   

.? 
S 
_c 
 
 
 

 
 
 
 
   d    X" " " " " " "r   r   )typingr   baser   modeling_utilsr   utilsr   r	   r
   r   quantizers_utilsr   r   
get_loggerrM   r"   r   rE   r   r   <module>r\      s    !                  1000000 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 2 2 2 2 2 2  LLL 
	H	%	%K" K" K" K" K"k K" K" K" K" K"r   