o
    Gi	                     @   sL   d dl mZ ddlmZmZ eeZe rd dlm	Z	 d	de
fddZdS )
    N   )is_accelerate_availablelogging)init_empty_weightsFmodules_to_not_convertc                    s   ddl m m}mmmm dtffdd fdd| ||} t fdd	| 	 D }|sCt
| jj d
 |rI||  | S )Nr   )QLinearfreezeqfloat8qint2qint4qint8dtypec                    s    d|  S )N)float8int8int4int2 )r   )r	   r
   r   r   r   U/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/quantizers/quanto/utils.py_get_weight_type   s   z5_replace_with_quanto_layers.<locals>._get_weight_typec              	      s   t |  }|s
| S |  D ]P\}}||| ||v rqt|tjr^t 0  |j|j|j	d u|j
j|jd}|| j|< t|| j| _| j| d W d    n1 sYw   Y  q| S )N)in_featuresout_featuresbiasr   weightsF)listchildrennamed_children
isinstancennLinearr   r   r   r   weightr   weights_dtype_modulestype
source_clsrequires_grad_)modelquantization_configr   has_childrennamemoduleqlinear)r   r   _replace_layersr   r   r+      s,   
z4_replace_with_quanto_layers.<locals>._replace_layersc                 3   s    | ]
\}}t | V  qd S )N)r   ).0_replaced_module)r   r   r   	<genexpr>.   s    z._replace_with_quanto_layers.<locals>.<genexpr>z does not appear to have any `nn.Linear` modules. Quantization will not be applied. Please check your model architecture, or submit an issue on Github if you think this is a bug. https://github.com/huggingface/diffusers/issues/new)optimum.quantor   r   r	   r
   r   r   stranynamed_modulesloggerwarning	__class____name__)r%   r&   r   pre_quantizedr   has_been_replacedr   )r   r   r+   r	   r
   r   r   r   _replace_with_quanto_layers   s    r:   )F)torch.nnr   utilsr   r   
get_loggerr7   r4   
accelerater   r   r:   r   r   r   r   <module>   s    
