a
    h                     @   s6   d Z ddlmZ ddlmZ ddlmZ dddZdS )	z9VPTQ (Vector Post-Training Quantization) integration file    N)init_empty_weights)VQuantLinearFc                 C   sr  |r|ndg}|   D ]P\}}|du r,g }|| d|}|j}|j}	t|tjr2||vr2||	v sz|d |v r2|	|dp||d d}
t	  |j
}|j}t|||
d |
d |
d |
d |
d	 |
d
 |
d |
d |
d dd|jdud| j|< d}| j| d W d   n1 s(0    Y  tt| dkr^t|||||d\}}|d q| |fS )aw  
    Public method that recursively replaces the Linear layers of the given model with VPTQ quantized layers.
    `accelerate` is needed to use this method. Returns the converted model and a boolean that indicates if the
    conversion has been successful or not.

    Args:
        model (`torch.nn.Module`):
            The model to convert, can be any `torch.nn.Module` instance.
        quantization_config (`VptqConfig`):
            The quantization config object that contains the quantization parameters.
        modules_to_not_convert (`list[`str`]`, *optional*, defaults to `["lm_head"]`):
            Names of the modules to not convert in `VQuantLinear`. In practice we keep the `lm_head` in full precision
            for numerical stability reasons.
        current_key_name (`list`, *optional*):
            A list that contains the current key name. This is used for recursion and should not be passed by the user.
        has_been_replaced (`bool`, *optional*):
            A boolean that indicates if the conversion has been successful or not. This is used for recursion and
            should not be passed by the user.
    Zlm_headN.vector_lensnum_centroidsnum_res_centroids	group_num
group_sizeoutlier_sizeindices_as_floatenable_normenable_permTF)r   r   r   r	   r
   r   r   r   r   Zis_indice_packedZenable_proxy_errorbiasr   )quantization_configmodules_to_not_convertcurrent_key_namehas_been_replaced)Znamed_childrenappendjoinshared_layer_configconfig_for_layers
isinstancennZLineargetr   in_featuresout_featuresr   r   Z_modulesZrequires_grad_lenlistchildrenreplace_with_vptq_linearpop)modelr   r   r   r   namemoduleZ
layer_namer   r   Zlayer_paramsr   r   _ r&   Z/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/integrations/vptq.pyr       sd    



0
r    )NNNF)__doc__Ztorch.nnr   Z
accelerater   Zvptqr   r    r&   r&   r&   r'   <module>   s       