
    Z j                        S SK Jr  S SKJr  S SKJrJr  \" 5       (       a
  SSKrSSKJ	r	  \R                  " \5      r " S S\5      r " S S	\R                  R                  5      r " S
 S\	R"                  5      rSS\\   S-  4S jjrg)   )ConversionOps)should_convert_module)is_torch_availablelogging    Nc            	       ~    \ rS rSrS r S	S\\\\R                     4   S\S-  S\\\R                  4   4S jjr
Srg)
EetqQuantize   c                     Xl         g Nhf_quantizer)selfr   s     o/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/integrations/eetq.py__init__EetqQuantize.__init__   s    (    N
input_dictfull_layer_namereturnc                 f   [        UR                  5       5      S   u  pEUS   nUR                  n[        R                  " U5      R                  5       R                  5       n[        R                  U[        R                  S5      u  pxUR                  U5      nUR                  U5      nX'U S3U0$ )Nr   F_scales)tupleitemsdevicetorcht
contiguouscpueetq_kernels_hubquant_weightsint8to)	r   r   r   kwargs_valuevalue_deviceint8_weightscaless	            r   convertEetqQuantize.convert   s     ))+,Q/a||ggen//1557.<<[%**V[\!nn\2<(0A.I6RRr   r   r   )__name__
__module____qualname____firstlineno__r   dictstrlistr   Tensorr*   __static_attributes__ r   r   r	   r	      s[    ) X\SsD$667SJMPT*S	c5<<	 S Sr   r	   c                   8    \ rS rSr\SS j5       r\S 5       rSrg)EetqLinearMMFunction/   Nc                 l    U R                  XX45        [        R                  XU5      nUb  XT-   nU$ UnU$ r   )save_for_backwardr    w8_a16_gemm)ctxxweightr)   biasoutputs         r   forwardEetqLinearMMFunction.forward0   sC     	a6!--a@"&"2 9?r   c                    U R                   u  p#pE[        R                  " UR                  S   5      R	                  UR
                  5      R	                  UR                  5      n[        R                  XcU5      nU R                  S   (       a?  UR                  S5      R                  UR                  SS5      5      R                  S5      nWS S S 4$ )Nr      )saved_tensorsr   eyeshaper#   r   dtyper    r;   needs_input_gradsqueezematmul	transpose	unsqueeze)r<   grad_outputinputr>   r)   r?   identity
grad_inputs           r   backwardEetqLinearMMFunction.backward8   s    &)&7&7#v99V\\!_-00?BB5;;O "--hG"$,,Q/66v7G7G17MNXXYZ[J4t++r   r5   r   )r,   r-   r.   r/   staticmethodrA   rR   r4   r5   r   r   r7   r7   /   s(      , ,r   r7   c                   J   ^  \ rS rSr\R
                  S4U 4S jjrS rSrU =r	$ )
EetqLinearG   Fc                   > [         TU ]  5         [        R                  " [        R
                  " X4US9SS9U l        [        R                  " [        R
                  " U[        R                  S95      U l        U(       a=  [        R                  " [        R
                  " U[        R                  S95      U l	        g S U l	        g )N)rH   F)requires_grad)
superr   nn	Parameterr   emptyr>   float16weight_scalesr?   )r   in_featuresout_featuresrH   r?   	__class__s        r   r   EetqLinear.__init__H   sy    ll5;;/JRW#Xhmn\\%++|EMM*Z[U[[,u}}%UVDIDIr   c                 p    [         R                  XR                  U R                  U R                  5      nU$ r   )r7   applyr>   r_   r?   )r   rO   r@   s      r   rA   EetqLinear.forwardQ   s,    %++E;;@R@RTXT]T]^r   )r?   r>   r_   )
r,   r-   r.   r/   r   r"   r   rA   r4   __classcell__)rb   s   @r   rV   rV   G   s    8=

  r   rV   modules_to_not_convertc                    SSK Jn  U" S5      qSnU(       a  0 OSS0nU R                  5        H  u  pg[	        Xa5      (       d  M  [
        R                  " S5         [        U[        R                  5      (       aB  [        UR                  UR                  4SUR                  SL0UD6nU R                  Xh5        S	nSSS5        M     U(       d  [        R!                  S
5        U $ ! , (       d  f       M  = f)a  
A helper function to replace all `torch.nn.Linear` modules by `EetqLinear` modules.

Parameters:
    model (`torch.nn.Module`):
        Input model or `torch.nn.Module` as the function is run recursively.
    modules_to_not_convert (`list[`str`]`, *optional*, defaults to `None`):
        Names of the modules to not convert in `EetqLinear`. In practice we keep the `lm_head` in full precision
        for numerical stability reasons.
rD   )
get_kernelz#kernels-community/quantization-eetqFrH   Nmetar?   TzYou are loading your model using eetq but no linear modules were found in your model. Please double check your model architecture, or submit an issue on github if you think this is a bug.)hub_kernelsrj   r    named_modulesr   r   r   
isinstancer[   LinearrV   r`   ra   r?   set_submoduleloggerwarning)	modelrh   pre_quantizedrj   has_been_replacedmodule_kwargsmodule_namemodule
new_modules	            r   replace_with_eetq_linearrz   V   s     ( ""GH'Bgt_M$224$[II\\&!&")),,'&&(;(;BH++UYBY]j
 ##K<$(! "!  5 	
 L "!s   A"C''
C6	)NF)core_model_loadingr   quantizers.quantizers_utilsr   utilsr   r   r   torch.nnr[   
get_loggerr,   rq   r	   autogradFunctionr7   ModulerV   r2   r1   rz   r5   r   r   <module>r      s|    / ? /  
		H	%S= S(,5>>22 ,0 %DI<L %r   