
    eTh                         S r SSKJrJrJr  \" 5       (       a  SSKr\R                  " \5      rS r	S r
S rS
S jrSS	 jrg)z2HQQ (Half-Quadratic Quantization) integration file   )is_hqq_availableis_torch_availablelogging    Nc                 B    U R                  5        H  u  pXl        M     g N)named_modulesname)modelr
   modules      U/var/www/auris/envauris/lib/python3.13/site-packages/transformers/integrations/hqq.pyautoname_modulesr      s    ++- .    c                     SR                  U R                  S5       Vs/ s H#  oS;  d  M
  UR                  5       (       a  M!  UPM%     sn5      $ s  snf )N.)r   layers)joinsplit	isnumeric)r
   ns     r   name_to_linear_tagr       s?    88

3l1>Q5QQ\]\g\g\iQlmmls   	AAAc                    [        5       (       a  SSKJn  [        5       nU R	                  5        HL  u  p4[        U[        R                  R                  W45      (       d  M2  UR                  [        U5      5        MN     [        U5      $ )Nr   )	HQQLinear)r   hqq.core.quantizer   setr	   
isinstancetorchnnLinearaddr   list)r   r   linear_tagsr
   r   s        r   get_linear_tagsr#   %   sc    /%K++-fuxx	:;;OO.t45 . r   c                 R   U R                  5        GH  u  pEUc  / nUR                  U5        [        U[        R                  R
                  5      (       a  [        UR                  5      nXa;   aV  X   bQ  X   U R                  U   l	        [        U5      U R                  U   l        U R                  U   R                  S5        SnS H  n[        XWS 5        M     [        [        UR!                  5       5      5      S:  a  [#        UUUS9u  pUR%                  S5        GM     X4$ )NFT)W_qmetar   patch_paramshas_been_replaced)named_childrenappendr   r   r   r   r   r
   _modulesquant_configtype
source_clsrequires_grad_setattrlenr!   children_prepare_for_hqq_linearpop)	r   r(   r)   current_key_namer
   r   
linear_tagatt_s	            r   r5   r5   0   s   ,,.#!%fehhoo..+FKK8J )+78D8PENN4(56:6lENN4(3NN4(77> $ 'T* ' tFOO%&'!+#:)"3$ A 	R ? /B ##r   c                 "  ^ Uc  / OUn[        U 5        [        U 5      mUR                  nUR                  n[	        [        T5      [        U5      -
  [        U5      -
  5      m[        U4S jUR                  5        5       5      (       a'  [        R                  T5      nUR                  U5        O[        R                  TU5      n[        XUS9u  pUUR                  US.U R                  l        U(       d  [        R!                  S5        U $ )a  
Prepares nn.Linear layers for HQQ quantization.
Since each layer type can have separate quantization parameters, we need to do the following:
1- tag each module with its name via autoname_modules()
2- Extract linear_tags (e.g. ['self_attn.q_proj', ...])
3- Map quantization parameters as a dictionary linear_tag -> quant_params as HQQLinear expects it, this is referred to as patch_params
c              3   ,   >#    U  H	  oT;   v   M     g 7fr    ).0keyr"   s     r   	<genexpr>)prepare_for_hqq_linear.<locals>.<genexpr>k   s     
=)<#+)<s   r'   )r.   quant_methodskip_modulesz<No linear modules were found in your model for quantization.)r   r#   rC   r.   r!   r   anykeysdictfromkeysupdater5   rB   configquantization_configloggerwarning)r   rJ   modules_to_not_convertr)   rC   r.   r(   r"   s          @r   prepare_for_hqq_linearrN   U   s     $:#ARG] U "%(K '33L&33Ls;'#l*;;cBX>YYZK

=):):)<
===}}[1L) }}[,?6<M E %+88$(ELL$ UVLr   r   )NNF)__doc__utilsr   r   r   r   
get_logger__name__rK   r   r   r#   r5   rN   r=   r   r   <module>rS      sI    5 A A 			H	%n
"$J,r   