o
    h                     @   sB   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 rddlm}m}m}m}	m}
m	} |||
|	d}d||d}|du r,g }| 
 D ]\}} du r:g   | t fdd|D s| W t|tjjrz||j|j|jdu|jj||j ||j d	| j|< | j| d
 d}n#t|tjjr|jdur||j|j|j|jdu||j d| j|< d}W d   n1 sw   Y  tt|  dkrt!||| |d\}} "d q0| |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qint8)float8int8int4int2N)Nr   r   c                 3   s    | ]
}|d   v V  qdS ).N)join).0keycurrent_key_name t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/integrations/quanto.py	<genexpr>A   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)#
accelerater   r   optimum.quantor   r   r	   r
   r   r   named_childrenappendany
isinstancetorchnnLinearr   r   r   weightr   r   r   _modulesrequires_grad_	LayerNormnormalized_shapeepselementwise_affinelenlistchildrenreplace_with_quanto_layerspop)modelr    r!   r   r"   r   r   r   r	   r
   r   r   	w_mapping	a_mappingnamemodule_r   r   r   r7      s\    


r7   )NNNF)	utilsr   r   r   r*   
get_logger__name__loggerr7   r   r   r   r   <module>   s   
