§
    ‚Štjü  ã                   ó  — d dl mZ d dlmZ d dlmZmZ  e¦   «         r
ddlZddlm	Z	  ej
        e¦  «        Z G d„ de¦  «        Z G d„ d	ej        j        ¦  «        Z G d
„ de	j        ¦  «        Zddee         dz  fd„ZdS )é   )ÚConversionOps)Úshould_convert_module)Úis_torch_availableÚloggingé    Nc            	       ór   — e Zd Zd„ Z	 ddeeeej                 f         dedz  deeej        f         fd„Z	dS )ÚEetqQuantizec                 ó   — || _         d S ©N)Úhf_quantizer)Úselfr   s     ú\/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/integrations/eetq.pyÚ__init__zEetqQuantize.__init__   s   € Ø(ˆÔÐÐó    NÚ
input_dictÚfull_layer_nameÚreturnc                 óœ  — t          |                     ¦   «         ¦  «        d         \  }}|d         }|j        }t          j        |¦  «                             ¦   «                              ¦   «         }t                               |t          j	        d¦  «        \  }}| 
                    |¦  «        }| 
                    |¦  «        }|||› d�|iS )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   ÚconvertzEetqQuantize.convert   s´   € õ ˜×)Ò)Ñ+Ô+Ñ,Ô,¨QÔ/‰ˆˆ5Ø�a”ˆà”|ˆÝ”g˜e‘n”n×/Ò/Ñ1Ô1×5Ò5Ñ7Ô7ˆÝ.×<Ò<¸[Í%Ì*ÐV[Ñ\Ô\Ñˆ�Và!—n’n \Ñ2Ô2ˆØ—’˜<Ñ(Ô(ˆà °Ð.IÐ.IÐ.IÈ6ÐRÐRr   r   )
Ú__name__Ú
__module__Ú__qualname__r   ÚdictÚstrÚlistr   ÚTensorr'   © r   r   r	   r	      s‡   € € € € € ð)ð )ð )ð X\ðSð SØ˜s D¨¬Ô$6Ð6Ô7ðSØJMÐPTÉ*ðSà	ˆc�5”<ÐÔ	 ðSð Sð Sð Sð Sð Sr   r	   c                   ó<   — e Zd Zedd„¦   «         Zed„ ¦   «         ZdS )ÚEetqLinearMMFunctionNc                 ó€   — |                       ||||¦  «         t                               |||¦  «        }|�||z   n|}|S r   )Úsave_for_backwardr   Úw8_a16_gemm)ÚctxÚxÚweightr&   ÚbiasÚoutputs         r   ÚforwardzEetqLinearMMFunction.forward0   sL   € ð 	×Ò˜a ¨°Ñ6Ô6Ð6Ý!×-Ò-¨a°¸Ñ@Ô@ˆØ"&Ð"2�˜$‘�¸ˆØˆr   c                 ó´  — | j         \  }}}}t          j        |j        d         ¦  «                             |j        ¦  «                             |j        ¦  «        }t                               |||¦  «        }| j	        d         rO| 
                    d¦  «                             |                     dd¦  «        ¦  «                             d¦  «        }|d d d fS )Nr   é   )Úsaved_tensorsr   ÚeyeÚshaper    r   Údtyper   r4   Úneeds_input_gradÚsqueezeÚmatmulÚ	transposeÚ	unsqueeze)r5   Úgrad_outputÚinputr7   r&   r8   ÚidentityÚ
grad_inputs           r   ÚbackwardzEetqLinearMMFunction.backward8   sÁ   € à&)Ô&7Ñ#ˆˆv�v˜tÝ”9˜Vœ\¨!œ_Ñ-Ô-×0Ò0°´Ñ?Ô?×BÒBÀ5Ä;ÑOÔOˆõ "×-Ò-¨h¸ÀÑGÔGˆàÔ Ô"ð 	\à$×,Ò,¨QÑ/Ô/×6Ò6°v×7GÒ7GÈÈ1Ñ7MÔ7MÑNÔN×XÒXÐYZÑ[Ô[ˆJà˜4  tÐ+Ð+r   r   )r(   r)   r*   Ústaticmethodr:   rJ   r/   r   r   r1   r1   /   sM   € € € € € Øðð ð ñ „\ðð ð,ð ,ñ „\ð,ð ,ð ,r   r1   c                   ó4   ‡ — e Zd Zej        dfˆ fd„	Zd„ Zˆ xZS )Ú
EetqLinearFc                 óœ  •— t          ¦   «                              ¦   «          t          j        t	          j        ||f|¬¦  «        d¬¦  «        | _        t          j        t	          j        |t          j        ¬¦  «        ¦  «        | _        |r9t          j        t	          j        |t          j        ¬¦  «        ¦  «        | _	        d S d | _	        d S )N)r@   F)Úrequires_grad)
Úsuperr   ÚnnÚ	Parameterr   Úemptyr7   Úfloat16Úweight_scalesr8   )r   Úin_featuresÚout_featuresr@   r8   Ú	__class__s        €r   r   zEetqLinear.__init__H   s¡   ø€ Ý‰Œ×ÒÑÔÐÝ”l¥5¤;°¸\Ð/JÐRWÐ#XÑ#XÔ#XÐhmÐnÑnÔnˆŒÝœ\­%¬+°|ÍEÌMÐ*ZÑ*ZÔ*ZÑ[Ô[ˆÔØð 	Ýœ¥U¤[°,ÅuÄ}Ð%UÑ%UÔ%UÑVÔVˆDŒIˆIˆIàˆDŒIˆIˆIr   c                 ó^   — t                                || j        | j        | j        ¦  «        }|S r   )r1   Úapplyr7   rU   r8   )r   rG   r9   s      r   r:   zEetqLinear.forwardQ   s)   € Ý%×+Ò+¨E°4´;ÀÔ@RÐTXÔT]Ñ^Ô^ˆØˆr   )r(   r)   r*   r   r   r   r:   Ú__classcell__)rX   s   @r   rM   rM   G   sS   ø€ € € € € Ø8=¼
Èð ð ð ð ð ð ðð ð ð ð ð ð r   rM   FÚmodules_to_not_convertc                 óÌ  — ddl m}  |dd¬¦  «        ad}|ri nddi}|                      ¦   «         D ]•\  }}t	          ||¦  «        sŒt          j        d¦  «        5  t          |t          j	        ¦  «        r9t          |j        |j        fd	|j        dui|¤Ž}|                      ||¦  «         d
}ddd¦  «         n# 1 swxY w Y   Œ–|st                               d¦  «         | S )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.
    r<   )Ú
get_kernelz#kernels-community/quantization-eetq)ÚversionFr@   NÚmetar8   Tz»You 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_kernelsr^   r   Únamed_modulesr   r   r   Ú
isinstancerQ   ÚLinearrM   rV   rW   r8   Úset_submoduleÚloggerÚwarning)	Úmodelr\   Úpre_quantizedr^   Úhas_been_replacedÚmodule_kwargsÚmodule_nameÚmoduleÚ
new_modules	            r   Úreplace_with_eetq_linearro   V   sx  € ð (Ð'Ð'Ð'Ð'Ð'ð "�zÐ"GÐQRÐSÑSÔSÐàÐà'Ð<�B�B¨g°t¨_€MØ$×2Ò2Ñ4Ô4ð 	)ð 	)Ñˆ�VÝ$ [Ð2HÑIÔIð 	ØÝŒ\˜&Ñ!Ô!ð 	)ð 	)Ý˜&¥"¤)Ñ,Ô,ð )Ý'ØÔ&¨Ô(;ðð ØBHÄ+ÐUYÐBYðØ]jðð �
ð ×#Ò# K°Ñ<Ô<Ð<Ø$(Ð!ð	)ð 	)ð 	)ñ 	)ô 	)ð 	)ð 	)ð 	)ð 	)ð 	)ð 	)øøøð 	)ð 	)ð 	)ð 	)øð ð 
Ý�Šðñ	
ô 	
ð 	
ð €Ls   ÁAB<Â<C 	ÃC 	)NF)Úcore_model_loadingr   Úquantizers.quantizers_utilsr   Úutilsr   r   r   Útorch.nnrQ   Ú
get_loggerr(   rf   r	   ÚautogradÚFunctionr1   ÚModulerM   r-   r,   ro   r/   r   r   ú<module>rx      sE  ðð /Ð .Ð .Ð .Ð .Ð .Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /ð ÐÑÔð Ø€L€L€LØÐÐÐÐÐð 
ˆÔ	˜HÑ	%Ô	%€ðSð Sð Sð Sð S�=ñ Sô Sð Sð(,ð ,ð ,ð ,ð ,˜5œ>Ô2ñ ,ô ,ð ,ð0ð ð ð ð �”ñ ô ð ð%ð %¸DÀ¼IÈÑ<Lð %ð %ð %ð %ð %ð %r   