U
    diG                     @   s>   d dl mZ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_quanto_availableis_torch_availablelogging    NFc              
      s  ddl m} t r4ddlm}m}m}m}	m}
m	} n0t
 rdtd ddlm}m}m}m}	m}
m	} |||
|	d}d||d}|dkrg }|  D ]"\}} dkrg   | t fdd	|D s|  t|tjjr,||j|j|jdk	|jj||j ||j d
| j|< | j| d d}nJt|tjjrv|jdk	rv||j|j |j!|jdk	||j d| j|< d}W 5 Q R X t"t#|$ dkrt%||| |d\}} &d q| |fS )a[  
    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 successfull 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qint8zuImporting from quanto will be deprecated in v4.47. Please install optimum-quanto instead `pip install optimum-quanto`)float8int8Zint4Zint2N)Nr   r   c                 3   s   | ]}|d   kV  qdS ).N)join).0keycurrent_key_name D/tmp/pip-unpacked-wheel-bm_b0l5e/transformers/integrations/quanto.py	<genexpr>F   s     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   r   loggerZwarning_onceZquantoZnamed_childrenappendany
isinstancetorchnnZLinearr   r   r   Zweightr   r   r   Z_modulesZrequires_grad_Z	LayerNormZnormalized_shapeZ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,      sb    " 

r,   )NNNF)
utilsr   r   r   r   r'   Z
get_logger__name__r#   r,   r   r   r   r   <module>   s   
    