
    sg                     x    d Z ddlmZmZmZ  e       rddlZ ej                  e      Zd Z	d Z
d Zd
dZdd	Zy)z2HQQ (Half-Quadratic Quantization) integration file   )is_hqq_availableis_torch_availablelogging    Nc                 B    | j                         D ]  \  }}||_         y N)named_modulesname)modelr
   modules      P/var/www/html/venv/lib/python3.12/site-packages/transformers/integrations/hqq.pyautoname_modulesr      s%    ++- f    c                     dj                  | j                  d      D cg c]  }|dvs|j                         r| c}      S c c}w )N.)r   layers)joinsplit	isnumeric)r
   ns     r   name_to_linear_tagr       s;    88

3l1Q>Q5Q\]\g\g\iQlmmls   	AAAc                     t               rddlm} t               }| j	                         D ]F  \  }}t        |t        j                  j                  f      s-|j                  t        |             H t        |      S )Nr   )	HQQLinear)r   hqq.core.quantizer   setr	   
isinstancetorchnnLinearaddr   list)r   r   linear_tagsr
   r   s        r   get_linear_tagsr#   %   sd    /%K++- 6ffuxx	:;OO.t456 r   c                 J   | j                         D ]  \  }}|g }|j                  |       t        |t        j                  j
                        rt        |j                        }||v rW||   R||   | j                  |   _	        t        |      | j                  |   _        | j                  |   j                  d       d}dD ]  }t        ||d         t        t        |j!                                     dkD  rt#        |||      \  }}|j%                  d        | |fS )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%   ,,. !f#!%fehhoo.+FKK8J \)
+78DZ8PENN4(56:6lENN4(3NN4(77> $ ' +T*+ tFOO%&'!+#:)"3$ A  	R ?!B ###r   c                   	 |g n|}t        |        t        |       	|j                  }|j                  }t	        t        	      t        |      z
  t        |      z
        	t        	fd|j                         D              r"	D ci c]  }|d }}|j                  |       n	D ci c]  }|| }}t        | ||      \  } }||j                  |d| j                  _        |st        j                  d       | S c c}w c c}w )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 neme 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 exepects it, this is referred to as patch_params
    Nc              3   &   K   | ]  }|v  
 y wr    ).0keyr"   s     r   	<genexpr>z)prepare_for_hqq_linear.<locals>.<genexpr>k   s     
=#3+
=s   r'   )r.   quant_methodskip_modulesz<No linear modules were found in your model for quantization.)r   r#   rB   r.   r!   r   anykeysupdater5   rA   configquantization_configloggerwarning)
r   rG   modules_to_not_convertr)   rB   r.   r?   r(   kr"   s
            @r   prepare_for_hqq_linearrL   U   s    $:#ARG] U "%(K '33L&33Ls;'#l*;;cBX>YYZK

=):):)<
==-89cT	99L) 2==A<==6L<M E %+88$(ELL$ UVL) : >s   
C?*
Dr   )NNF)__doc__utilsr   r   r   r   
get_logger__name__rH   r   r   r#   r5   rL   r=   r   r   <module>rQ      sH    5 A A 			H	%n
"$J,r   