
    g                     |    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dS )z2HQQ (Half-Quadratic Quantization) integration file   )is_hqq_availableis_torch_availablelogging    Nc                 H    |                                  D ]\  }}||_        d S N)named_modulesname)modelr
   modules      Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/integrations/hqq.pyautoname_modulesr      s4    ++--  f     c                 f    d                     d |                     d          D                       S )N.c                 B    g | ]}|d v|                                 |S ))r   layers)	isnumeric).0ns     r   
<listcomp>z&name_to_linear_tag.<locals>.<listcomp>!   s5    lll1Q>Q5Q5Q\]\g\g\i\i5QQ5Q5Q5Qr   )joinsplit)r
   s    r   name_to_linear_tagr       s-    88ll

3lllmmmr   c                    t                      rddlm} t                      }|                                 D ]H\  }}t          |t          j        j        |f          r"|	                    t          |                     It          |          S )Nr   )	HQQLinear)r   hqq.core.quantizer   setr	   
isinstancetorchnnLinearaddr   list)r   r   linear_tagsr
   r   s        r   get_linear_tagsr&   %   s     0//////%%K++-- 6 6ffux	:;; 	6OO.t44555r   c                    |                                  D ])\  }}|g }|                    |           t          |t          j        j                  rt          |j                  }||v r_||         W||         | j        |         _	        t          |          | j        |         _        | j        |                             d           d}dD ]}t          ||d            t          t          |                                                    dk    rt#          |||          \  }}|                    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   r8   r8   0   s[   ,,.. ! !f#!%%%feho.. 	++FK88J \))
+78DZ8PEN4(56:6llEN4(3N4(77>>> $ ' + +T****tFOO%%&&''!++#:)"3$ $ $ A  	R    ###r   Fc                 ,   |g n|}t          |            t          |           |j        }|j        t	          t                    t          |          z
  t          |          z
            t          fd                                D                       r"d D             }|                               nfdD             }t          | ||          \  } }|j
        |d| j        _        |st                              d           | S )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 V  	d S r    )r   keyr%   s     r   	<genexpr>z)prepare_for_hqq_linear.<locals>.<genexpr>k   s(      
=
=#3+
=
=
=
=
=
=r   c                     i | ]}|d S r   r@   )r   rA   s     r   
<dictcomp>z*prepare_for_hqq_linear.<locals>.<dictcomp>m   s    999cT999r   c                     i | ]}|S r@   r@   )r   kr1   s     r   rD   z*prepare_for_hqq_linear.<locals>.<dictcomp>q   s    ===A<===r   r*   )r1   quant_methodskip_modulesz<No linear modules were found in your model for quantization.)r   r&   rH   r1   r$   r   anykeysupdater8   rG   configquantization_configloggerwarning)r   rM   modules_to_not_convertr,   rH   r+   r%   r1   s         @@r   prepare_for_hqq_linearrQ   U   sQ    $:#ARRG] U "%((K '3L&3Ls;''#l*;*;;cBX>Y>YYZZK

=
=
=
=):):)<)<
=
=
=== >99[999L)))) >======6L<M     E %+8$( (EL$  WUVVVLr   r   )NNF)__doc__utilsr   r   r   r    
get_logger__name__rN   r   r   r&   r8   rQ   r@   r   r   <module>rV      s    5 4 A A A A A A A A A A  LLL		H	%	%  n n n
  "$ "$ "$ "$J, , , , , ,r   