
    }-j^                         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d	S )    )TYPE_CHECKING   )HfQuantizer   )PreTrainedModel)
EetqConfig)is_accelerate_availableis_kernels_availableis_torch_availablelogging)get_module_from_nameNc                        e Zd ZU dZdZded<    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
    Fr   quantization_configc                 <     t                      j        |fi | d S )N)super__init__)selfr   kwargs	__class__s      f/var/www/html/banglarbhumi/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_dtypeD   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.eetqr6   r   r%   pre_quantized)r   r3   r4   r   r6   moduletensor_names          r   param_needs_quantizationz(EetqHfQuantizer.param_needs_quantizationI   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_convertr9   )integrationsr>   get_modules_to_not_convertr   r?   _keep_in_fp32_modulesr9   )r   r3   r   r>   s       r   $_process_model_before_weight_loadingz4EetqHfQuantizer._process_model_before_weight_loadingU   si    
 	<;;;;;&*&E&E4+BED_'
 '
# )($*EUYUg
 
 
r   c                     dS NT r   s    r   is_serializablezEetqHfQuantizer.is_serializabled   s    tr   c                     dS rE   rF   rG   s    r   is_trainablezEetqHfQuantizer.is_trainableg   s    tr   c                 $    ddl m}  ||           S )Nr   )EetqQuantize)r8   rL   )r   rL   s     r   get_quantize_opsz EetqHfQuantizer.get_quantize_opsk   s$    444444|D!!!r   )r,   r-   r.   r-   )r3   r   )__name__
__module____qualname____doc__requires_calibration__annotations__r   r+   r2   strboolr<   rC   rH   propertyrJ   rM   __classcell__)r   s   @r   r   r   "   s          !%%%%8 8 8 8 8  .   

.? 
S 
_c 
 
 
 

 
 
 
 
   d    X" " " " " " "r   r   )typingr   baser   modeling_utilsr   utils.quantization_configr   utilsr	   r
   r   r   quantizers_utilsr   r   
get_loggerrN   r#   r   rF   r   r   <module>r_      s    !                  7000000666666 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 2 2 2 2 2 2  LLL 
	H	%	%L" L" L" L" L"k L" L" L" L" L"r   