o
    §ôjà  ã                   @   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   ún/var/www/html/academy/tools/voices/clone/sv-venv/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)Z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
   Z
linear_tagÚattÚ_r   r   r   r1   0   s,   


ýr1   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   r6   r   r   r   Ú
<dictcomp>m   ó    z*prepare_for_hqq_linear.<locals>.<dictcomp>c                    s   i | ]}|ˆ “qS r   r   )r   Úk)r*   r   r   r9   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Úupdater1   r<   ÚconfigÚquantization_configÚloggerÚwarning)r	   rB   Úmodules_to_not_convertr%   r=   r$   r   )r    r*   r   Úprepare_for_hqq_linearU   s(   	
ÿ
ý
rF   r   )NNF)Ú__doc__Úutilsr   r   r   r   Ú
get_loggerÚ__name__rC   r   r   r!   r1   rF   r   r   r   r   Ú<module>   s   

%