a
    h                     @   s:   d dl mZmZmZ e r"ddlZeeZdddZdS )   )is_optimum_quanto_availableis_torch_availablelogging    NFc              
      s  ddl m} t r2ddlm}m}m}m}	m}
m	} |||
|	d}d||d}|du rXg }| 
 D ]6\}} du rvg   | t fdd|D sb|  t|tjjr||j|j|jdu|jj||j ||j d	| j|< | j| d
 d}nJt|tjjrB|jdurB||j|j|j|jdu||j d| j|< d}W d   n1 sX0    Y  tt|  dkrt!||| |d\}} "d q`| |fS )aZ  
    Public method that recursively replaces the Linear layers of the given model with Quanto quantized layers.
    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 (`AqlmConfig`, defaults to `None`):
            The quantization config object that contains the quantization parameters.
        modules_to_not_convert (`list`, *optional*, defaults to `None`):
            A list of modules to not convert. If a module name is in the list (e.g. `lm_head`), it will not be
            converted.
        current_key_name (`list`, *optional*, defaults to `None`):
            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*, defaults to `None`):
            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.
    r   )init_empty_weights)
QLayerNormQLinearqfloat8qint2qint4qint8)float8int8Zint4Zint2N)Nr   r   c                 3   s   | ]}|d   v V  qdS ).N)join).0keycurrent_key_name \/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/integrations/quanto.py	<genexpr>A       z-replace_with_quanto_layers.<locals>.<genexpr>)in_featuresout_featuresbiasdtypeweightsactivationsFT)r   )quantization_configmodules_to_not_convertr   has_been_replaced)#Z
accelerater   r   Zoptimum.quantor   r   r	   r
   r   r   Znamed_childrenappendany
isinstancetorchnnZLinearr   r   r   weightr   r   r   Z_modulesZrequires_grad_Z	LayerNormZnormalized_shapeepsZelementwise_affinelenlistchildrenreplace_with_quanto_layerspop)modelr   r    r   r!   r   r   r   r	   r
   r   r   Z	w_mappingZ	a_mappingnamemodule_r   r   r   r-      sX     
$
r-   )NNNF)	utilsr   r   r   r&   Z
get_logger__name__loggerr-   r   r   r   r   <module>   s   
    