
    bi	                     l    d dl mZ ddlmZmZ  ej
                  e      Z e       rd dlm	Z	 dde
fdZy)    N   )is_accelerate_availablelogging)init_empty_weightsmodules_to_not_convertc                 (  	
 ddl mm}m	m
mm dt        f	
fdfd | ||      } t        fd| j                         D              }|s,t        j                  | j                  j                   d       |r ||        | S )Nr   )QLinearfreezeqfloat8qint2qint4qint8dtypec                     d|    S )N)float8int8int4int2 )r   r   r   r   r   s    \/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/quantizers/quanto/utils.py_get_weight_typez5_replace_with_quanto_layers.<locals>._get_weight_type   s    !5%OPUVV    c                 :   t        | j                               }|s| S | j                         D ]  \  }} 	|||       ||v rt        |t        j
                        s0t               5   |j                  |j                  |j                  d u|j                  j                   |j                              }|| j                  |<   t        |      | j                  |   _        | j                  |   j!                  d       d d d         | S # 1 sw Y   xY w)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qlinearr	   r   _replace_layerss
          r   r0   z4_replace_with_quanto_layers.<locals>._replace_layers   s   ENN,-L!002 	?LD&F$79OP--&")),') 
?%$*$6$6%+%8%8#[[4$mm11 01D1R1R SG ,3ENN4(6:6lENN4(3NN4(77>
? 
?	?& 
? 
?s   )BDD	c              3   <   K   | ]  \  }}t        |        y w)N)r!   ).0_replaced_moduler	   s      r   	<genexpr>z._replace_with_quanto_layers.<locals>.<genexpr>.   s     qEWQJ@qs   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   r0   r   r   r   r   s         @@@@@@@r   _replace_with_quanto_layersr@      s    LLW W W4 E#68NOEq[`[n[n[pqq''( )C C	
 uLr   )F)torch.nnr"   utilsr   r   
get_loggerr=   r:   
accelerater   r   r@   r   r   r   <module>rE      s5     5 
		H	%-0TX 0r   