
    IgG                     f    d dl mZmZmZmZ  e       rddlZ ej                  e      Z	 	 	 	 ddZ	y)   )is_optimum_quanto_availableis_quanto_availableis_torch_availablelogging    Nc                    ddl m} t               rddlm}m}m}m}	m}
m	} n/t               r%t        j                  d       ddlm}m}m}m}	m}
m	} 
	d}d||d}|g }| j                         D ]  \  }}g j                  |       t!        fd|D              sA |       5  t#        |t$        j&                  j(                        r |j*                  |j,                  |j.                  du|j0                  j2                  ||j4                     ||j6                     	      | j8                  |<   | j8                  |   j;                  d
       d}nt#        |t$        j&                  j<                        r_|j6                  S |j>                  |j@                  |jB                  |j.                  du||j6                           | j8                  |<   d}ddd       tE        tG        |jI                                     dkD  rtK        ||||      \  }}jM                  d        | |fS # 1 sw Y   YxY w)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int8int4int2N)Nr   r   c              3   D   K   | ]  }|d j                        v   yw).N)join).0keycurrent_key_names     ]/var/www/html/answerous/venv/lib/python3.12/site-packages/transformers/integrations/quanto.py	<genexpr>z-replace_with_quanto_layers.<locals>.<genexpr>F   s      W3#((#344Ws    )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   r   loggerwarning_oncequanto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_s      `             r   r<   r<      s   2 ."$TTT		 D	
 	ML"E5%PIw>I%!#,,. %!f#!%W@VWW#% 1fehhoo6+2$*$6$6%+%8%8#[[4$mm11 )*=*E*E F$-.A.M.M$N,ENN4( NN4(77>(,%(:(:;*66B/9"33"JJ"55"KKt3(12E2Q2Q(R0t, -1)+1, tFOO%&'!+#=$7'=!1"3$ A  	R K%!L ###A1 1s   +D2H55H>	)NNNF)
utilsr   r   r   r   r/   
get_logger__name__r(   r<        r   <module>rI      s?    b a 			H	%
 O$rH   