
    Z j^                         S SK Jr  SSKJr  \(       a  SSKJr  SSKJr  SSKJ	r	J
r
JrJr  SSKJr  \" 5       (       a  S S	Kr\R                   " \5      r " S
 S\5      rg	)    )TYPE_CHECKING   )HfQuantizer   )PreTrainedModel)
EetqConfig)is_accelerate_availableis_kernels_availableis_torch_availablelogging)get_module_from_nameNc                      ^  \ rS rSr% SrSrS\S'   U 4S jrS rSS	 jr	S
SS\
S\4S jr  SS jrS r\S\4S j5       rS rSrU =r$ )EetqHfQuantizer"   z2
8-bit quantization from EETQ quantization method
Fr   quantization_configc                 (   > [         TU ]  " U40 UD6  g )N)super__init__)selfr   kwargs	__class__s      w/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/quantizers/quantizer_eetq.pyr   EetqHfQuantizer.__init__*   s    ,77    c                    [        5       (       d  [        S5      e[        5       (       d  [        S5      e[        R                  R                  5       (       d  [        S5      eUR                  S5      nUc  [        R                  S5        g [        U[        5      (       aC  [        U5      S:  a  SUR                  5       ;   d  SUR                  5       ;   a  [        S	5      eg g )
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_environment$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                 X    U[         R                  :w  a  [        R                  S5        U$ )NzLWe suggest you to set `dtype=torch.float16` for better efficiency with EETQ.)r    float16r%   info)r   dtypes     r   update_dtypeEetqHfQuantizer.update_dtypeD   s     EMM!KKfgr   modelr   
param_namec                 |    SSK Jn  [        X5      u  pV[        XT5      (       a  U R                  (       d  US:X  a  ggg)Nr   )
EetqLinearbiasFT)integrations.eetqr9   r   r'   pre_quantized)r   r6   r7   r   r9   moduletensor_names          r   param_needs_quantization(EetqHfQuantizer.param_needs_quantizationI   s6    225Ef))!![F%:r   c                     SSK Jn  U R                  XR                  R                  UR
                  5      U l        U" XR                  U R                  S9ng )Nr   )replace_with_eetq_linear)modules_to_not_convertr<   )integrationsrB   get_modules_to_not_convertr   rC   _keep_in_fp32_modulesr<   )r   r6   r   rB   s       r   $_process_model_before_weight_loading4EetqHfQuantizer._process_model_before_weight_loadingU   sO    
 	<&*&E&E++BBED_D_'
# )*E*EUYUgUg
r   c                     gNT r   s    r   is_serializableEetqHfQuantizer.is_serializabled   s    r   c                     grJ   rK   rL   s    r   is_trainableEetqHfQuantizer.is_trainableg   s    r   c                     SSK Jn  U" U 5      $ )Nr   )EetqQuantize)r;   rS   )r   rS   s     r   get_quantize_ops EetqHfQuantizer.get_quantize_opsk   s    4D!!r   )rC   )r3   torch.dtyper/   rV   )r6   r   )__name__
__module____qualname____firstlineno____doc__requires_calibration__annotations__r   r-   r4   strboolr?   rG   rM   propertyrP   rT   __static_attributes____classcell__)r   s   @r   r   r   "   sx     !%%8.

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