o
    h                     @   s>   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   sf  |sdgn|}|   D ]\}}|du rg }|| d|}|j}|j}	t|tjr||vr||	v s9|d |v r|	|dpF||d d}
t	 B |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 sw   Y  tt| dkrt|||||d\}}|d q| |fS )ax  
    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 successfull 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.
    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   is_indice_packedenable_proxy_errorbiasr   )quantization_configmodules_to_not_convertcurrent_key_namehas_been_replaced)named_childrenappendjoinshared_layer_configconfig_for_layers
isinstancennLineargetr   in_featuresout_featuresr   r   _modulesrequires_grad_lenlistchildrenreplace_with_vptq_linearpop)modelr   r   r   r   namemodule
layer_namer   r   layer_paramsr    r!   _ r/   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/integrations/vptq.pyr'      s^   



r'   )NNNF)__doc__torch.nnr   
accelerater   vptqr   r'   r/   r/   r/   r0   <module>   s   