§
    Ý˜ºh  ã                   óf   — d dl mZmZmZ  e¦   «         rddlZ ej        e¦  «        Z	 	 	 	 dd„ZdS )é   )Úis_optimum_quanto_availableÚis_torch_availableÚloggingé    NFc                 óò  ‡— ddl m} t          ¦   «         rddlm}m}m}m}	m}
m	} |||
|	dœ}d||dœ}|€g }|  
                    ¦   «         D �]¨\  }}‰€g Š‰                     |¦  «         t          ˆfd„|D ¦   «         ¦  «        �s |¦   «         5  t          |t          j        j        ¦  «        rm ||j        |j        |j        du|j        j        ||j                 ||j                 ¬¦  «        | j        |<   | j        |                              d	¦  «         d
}nat          |t          j        j        ¦  «        rB|j        �; ||j        |j        |j        |j        du||j                 ¬¦  «        | j        |<   d
}ddd¦  «         n# 1 swxY w Y   t=          t?          |                      ¦   «         ¦  «        ¦  «        dk    rtC          |||‰|¬¦  «        \  }}‰ "                    d¦  «         �Œª| |fS )aZ  
    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 successful 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   óF   •K  — | ]}|d                       ‰¦  «        v V — ŒdS )ú.N)Újoin)Ú.0ÚkeyÚcurrent_key_names     €úh/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/transformers/integrations/quanto.pyú	<genexpr>z-replace_with_quanto_layers.<locals>.<genexpr>A   s5   øè è € ÐWÐW¸�3˜#Ÿ(š(Ð#3Ñ4Ô4Ð4ÐWÐWÐWÐWÐWÐWó    )Ú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Ú_s      `             r   r9   r9      s»  ø€ ð2 .Ð-Ð-Ð-Ð-Ð-å"Ñ$Ô$ð UØTÐTÐTÐTÐTÐTÐTÐTÐTÐTÐTÐTÐTÐTÐTÐTà"¨E¸5È%ÐPÐP€IØ w¸Ð>Ð>€IàÐ%Ø!#Ðà×,Ò,Ñ.Ô.ð %!ñ %!‰ˆˆfØÐ#Ø!ÐØ×Ò Ñ%Ô%Ð%åÐWÐWÐWÐWÐ@VÐWÑWÔWÑWÔWñ 	1Ø#Ð#Ñ%Ô%ð 1ð 1Ý˜f¥e¤h¤oÑ6Ô6ð 1Ø+2¨7Ø$*Ô$6Ø%+Ô%8Ø#œ[°Ð4Ø$œmÔ1Ø )Ð*=Ô*EÔ FØ$-Ð.AÔ.MÔ$Nð,ñ ,ô ,�E”N 4Ñ(ð ”N 4Ô(×7Ò7¸Ñ>Ô>Ð>Ø(,Ð%Ð%Ý ­¬Ô(:Ñ;Ô;ð 	1Ø*Ô6ÐBØ/9¨zØ"Ô3Ø"œJØ"Ô5Ø"œK¨tÐ3Ø(1Ð2EÔ2QÔ(Rð0ñ 0ô 0˜œ tÑ,ð -1Ð)ð+1ð 1ð 1ñ 1ô 1ð 1ð 1ð 1ð 1ð 1ð 1øøøð 1ð 1ð 1ð 1õ, �t�F—O’OÑ%Ô%Ñ&Ô&Ñ'Ô'¨!Ò+Ð+Ý#=ØØ$7Ø'=Ø!1Ø"3ð$ñ $ô $Ñ ˆAÐ ð 	×Ò˜RÑ Ô Ð Ñ ØÐ#Ð#Ð#s   ÂC.F
Æ
F	ÆF	)NNNF)	Úutilsr   r   r   r,   Ú
get_loggerÚ__name__Úloggerr9   © r   r   ú<module>rF      s†   ðð MÐ LÐ LÐ LÐ LÐ LÐ LÐ LÐ LÐ Lð ÐÑÔð Ø€L€L€Là	ˆÔ	˜HÑ	%Ô	%€ð
 ØØØðJ$ð J$ð J$ð J$ð J$ð J$r   