
    i^                         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)
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        |   |fi | y )N)super__init__)selfr   kwargs	__class__s      w/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_eetq.pyr   zEetqHfQuantizer.__init__*   s    ,77    c                    t               st        d      t               st        d      t        j                  j                         st        d      |j                  d      }|t        j                  d       y t        |t              r>t        |      dkD  rd|j                         v sd|j                         v rt        d	      y y )
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    #%hii&(noozz&&(PQQZZ-
I 
D):"u
0A0A0C'CvQ[QbQbQdGd h  He *r   returnc                 V    |t         j                  k7  rt        j                  d       |S )NzLWe suggest you to set `dtype=torch.float16` for better efficiency with EETQ.)r   float16r#   info)r   dtypes     r   update_dtypezEetqHfQuantizer.update_dtypeD   s     EMM!KKfgr   modelr   
param_namec                 l    ddl m} t        ||      \  }}t        ||      r| j                  s|dk(  ryyy)Nr   )
EetqLinearbiasFT)integrations.eetqr5   r   r%   pre_quantized)r   r2   r3   r   r5   moduletensor_names          r   param_needs_quantizationz(EetqHfQuantizer.param_needs_quantizationI   s9    225*Efj)!![F%:r   c                     ddl m} | j                  || j                  j                  |j
                        | _         ||| j                  | j                        }y )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_loadingU   sS    
 	<&*&E&E4++BBED_D_'
# )$*E*EUYUgUg
r   c                      yNT r   s    r   is_serializablezEetqHfQuantizer.is_serializabled   s    r   c                      yrD   rE   rF   s    r   is_trainablezEetqHfQuantizer.is_trainableg   s    r   c                     ddl m}  ||       S )Nr   )EetqQuantize)r7   rK   )r   rK   s     r   get_quantize_opsz EetqHfQuantizer.get_quantize_opsk   s    4D!!r   )r0   torch.dtyper,   rM   )r2   r   )__name__
__module____qualname____doc__requires_calibration__annotations__r   r+   r1   strboolr;   rB   rG   propertyrI   rL   __classcell__)r   s   @r   r   r   "   ss     !%%8.

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