o
    ?"g                     @   s`   d Z ddlmZmZmZ e rddlZeeZdd Z	dd Z
d	d
 ZdddZdddZdS )z2HQQ (Half-Quadratic Quantization) integration file   )is_hqq_availableis_torch_availablelogging    Nc                 C   s   |   D ]\}}||_qd S N)named_modulesname)modelr   module r   _/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/transformers/integrations/hqq.pyautoname_modules   s   r   c                 C   s   d dd | dD S )N.c                 S   s    g | ]}|d vr|  s|qS ))r	   layers)	isnumeric).0nr   r   r   
<listcomp>!   s     z&name_to_linear_tag.<locals>.<listcomp>)joinsplit)r   r   r   r   name_to_linear_tag    s   r   c                 C   sR   t  r	ddlm} t }|  D ]\}}t|tjj|fr$|	t
| qt|S )Nr   )	HQQLinear)r   hqq.core.quantizer   setr   
isinstancetorchnnLinearaddr   list)r	   r   linear_tagsr   r
   r   r   r   get_linear_tags%   s   r!   c           	      C   s   |   D ]b\}}|d u rg }|| t|tjjrNt|j}||v rA|| d urA|| | j| _	t
|| j| _| j| d d}dD ]}t||d  qEtt| dkrat|||d\}}|d q| |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_r   r   r   r2   0   s,   


r2   Fc                    s   |du rg n|}t |  t|  |j}|jtt t| t|  t fdd D r>dd  D }| n	fdd D }t	| ||d\} }|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   s    | ]}| v V  qd S r   r   r   key)r    r   r   	<genexpr>k   s    z)prepare_for_hqq_linear.<locals>.<genexpr>c                 S   s   i | ]}|d qS r   r   r8   r   r   r   
<dictcomp>m       z*prepare_for_hqq_linear.<locals>.<dictcomp>c                    s   i | ]}| qS r   r   )r   k)r+   r   r   r;   q   r<   r$   )r+   quant_methodskip_modulesz<No linear modules were found in your model for quantization.)r   r!   r?   r+   r   r   anykeysupdater2   r>   configquantization_configloggerwarning)r	   rD   modules_to_not_convertr&   r?   r%   r   )r    r+   r   prepare_for_hqq_linearU   s(   	


rH   r   )NNF)__doc__utilsr   r   r   r   
get_logger__name__rE   r   r   r!   r2   rH   r   r   r   r   <module>   s   

%