
    
3j]                        S SK JrJr  S SKJr  SSKJrJrJrJ	r	J
r
JrJr  SSKJr  \(       a  SSKJr  \" 5       (       a  S SKr\" 5       (       a  S S	KJrJr  \
" 5       (       a  S
SKJr  \R,                  " \5      r " S S\5      rg)    )TYPE_CHECKINGAny)is_optimum_quanto_version   )	deprecateget_module_from_nameis_accelerate_availableis_accelerate_versionis_optimum_quanto_availableis_torch_availablelogging   )DiffusersQuantizer)
ModelMixinN)CustomDtypeset_module_tensor_to_device   )_replace_with_quanto_layersc            
       H  ^  \ rS rSrSrSrSrSS/rU 4S jrS r	S	S
SSS\
S\\
\4   4S jrS	S
SSS\
SS4S jrS\\
\\
-  4   S\\
\\
-  4   4S jrS"S jrS#S$S jjrS\\
   S\
S\\
   4S jr/ 4S	S
S\\
   4S jjrS r\S 5       r\S 5       r\S\4S  j5       rS!rU =r$ )%QuantoQuantizer!   z(
Diffusers Quantizer for Optimum Quanto
TFquanto
acceleratec                 (   > [         TU ]  " U40 UD6  g N)super__init__)selfquantization_configkwargs	__class__s      f/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/quantizers/quanto/quanto_quantizer.pyr   QuantoQuantizer.__init__*   s    ,77    c                 b   Sn[        SSU5        [        5       (       d  [        S5      e[        SS5      (       d  [        S5      e[	        5       (       d  [        S5      eUR                  S	S 5      n[        U[        5      (       a)  [        UR                  5       5      S
:  a  [        S5      eg g )NzHThe Quanto quantizer is deprecated and will be removed in version 1.0.0.r   z1.0.0zhLoading an optimum-quanto quantized model requires optimum-quanto library (`pip install optimum-quanto`)>=z0.2.6zLoading an optimum-quanto quantized model requires `optimum-quanto>=0.2.6`. Please upgrade your installation with `pip install --upgrade optimum-quantoz`Loading an optimum-quanto quantized model requires accelerate library (`pip install accelerate`)
device_mapr   zy`device_map` for multi-GPU inference or CPU/disk offload is currently not supported with Diffusers and the Quanto backend)r   r   ImportErrorr   r	   get
isinstancedictlenkeys
ValueError)r   argsr    deprecation_messager'   s        r"   validate_environment$QuantoQuantizer.validate_environment-   s    h#W.AB*,,z  )w77^ 
 '((r  ZZd3
j$''C
0A,BQ,F L  -G'r$   modelr   param_valueztorch.Tensor
param_name
state_dictc                    ^
 SSK JnJn  SSKJn  [        X5      u  m
n	U R                  (       a  [        U
4S jXx4 5       5      (       a  g[        T
U5      (       a  SU	;   a  T
R                  (       + $ g)Nr   )QModuleMixinQTensor)PackedTensorc              3   <   >#    U  H  n[        TU5      v   M     g 7fr   )r*   ).0tmodules     r"   	<genexpr>;QuantoQuantizer.check_if_quantized_param.<locals>.<genexpr>S   s     %]E\j&;&;E\s   TweightF)
optimum.quantor8   r9   optimum.quanto.tensor.packedr:   r   pre_quantizedanyr*   frozen)r   r3   r4   r5   r6   r    r8   r9   r:   tensor_namer>   s             @r"   check_if_quantized_param(QuantoQuantizer.check_if_quantized_paramF   s]     	9=25E#%]gE\%]"]"]--(k2I}}$$r$   target_deviceztorch.devicec                     UR                  S[        R                  5      n[        X5      u  pU R                  (       a  [        XU5        g[        XXBU5        UR                  5         SUR                  l	        g)zU
Create the quantized parameter by calling .freeze() after setting it to the module.
dtypeFN)
r)   torchfloat32r   rD   setattrr   freezerA   requires_grad)
r   r3   r4   r5   rJ   r/   r    rL   r>   rG   s
             r"   create_quantized_param&QuantoQuantizer.create_quantized_paramZ   sZ     

7EMM225EF5'=W\]MMO*/FMM'r$   
max_memoryreturnc                 `    UR                  5        VVs0 s H
  u  p#X#S-  _M     nnnU$ s  snnf )Ng?)items)r   rT   keyvals       r"   adjust_max_memory!QuantoQuantizer.adjust_max_memoryp   s5    6@6F6F6HI6H(#c:o6H
I Js   *c                     [        SS5      (       aW  [        R                  [        R                  [        R
                  [        R                  S.nX R                  R                     nU$ )Nr&   z0.27.0)int8float8int4int2)	r
   rM   r]   r   FP8INT4INT2r   weights_dtype)r   target_dtypemappings      r"   adjust_target_dtype#QuantoQuantizer.adjust_target_dtypet   sR     x00

%//#((#((	G ##;#;#I#IJLr$   c                 V    Uc%  [         R                  S5        [        R                  nU$ )NzVYou did not specify `torch_dtype` in `from_pretrained`. Setting it to `torch.float32`.)loggerinforM   rN   )r   torch_dtypes     r"   update_torch_dtype"QuantoQuantizer.update_torch_dtype   s$    KKpq--Kr$   missing_keysprefixc                 \   SSK Jn  / nUR                  5        Hr  u  pg[        Xt5      (       d  M  U HU  nXh;   d  Xc SU 3;   d  M  UR	                  S5      (       a  M,  UR	                  S5      (       a  MD  UR                  U5        MW     Mt     U V	s/ s H  oU;  d  M
  U	PM     sn	$ s  sn	f )Nr   )r8   .z.weightz.bias)rB   r8   named_modulesr*   endswithappend)
r   r3   ro   rp   r8   not_missing_keysnamer>   missingks
             r"   update_missing_keys#QuantoQuantizer.update_missing_keys   s    /!//1LD&//+GDhay4I,I ' 0 0 ; ; ' 0 0 9 9(//8  , 2 (E<a4D+D<EEEs   	B) B)keep_in_fp32_modulesc                 \   U R                   R                  U l        [        U R                  [        5      (       d  U R                  /U l        U R                  R	                  U5        [        UU R                  U R                   U R                  S9nU R                   UR                  l         g )N)modules_to_not_convertr   rD   )r   r~   r*   listextendr   rD   config)r   r3   r'   r|   r    s        r"   $_process_model_before_weight_loading4QuantoQuantizer._process_model_before_weight_loading   s     '+&>&>&U&U#$55t<<+/+F+F*GD'##**+?@+#'#>#> $ 8 8,,	
 ,0+C+C(r$   c                     U$ r    )r   r3   r    s      r"   #_process_model_after_weight_loading3QuantoQuantizer._process_model_after_weight_loading   s    r$   c                     gNTr   r   s    r"   is_trainableQuantoQuantizer.is_trainable       r$   c                     gr   r   r   s    r"   is_serializableQuantoQuantizer.is_serializable   r   r$   c                     gr   r   r   s    r"   is_compileableQuantoQuantizer.is_compileable   r   r$   )r~   )re   torch.dtyperU   r   r   )rl   r   rU   r   )__name__
__module____qualname____firstlineno____doc__use_keep_in_fp32_modulesrequires_calibrationrequired_packagesr   r1   strr+   r   rH   rR   intrZ   rg   rm   r   rz   r   r   propertyr   r   boolr   __static_attributes____classcell__)r!   s   @r"   r   r   !   sU     $ !<082 $ 	
 cN(00 $0 	0
 &0,DcCi,@ T#sUXy.EY 
FtCy F# FRVWZR[ F( +-	DD #3i	D,       r$   r   )typingr   r   diffusers.utils.import_utilsr   utilsr   r   r	   r
   r   r   r   baser   models.modeling_utilsr   rM   accelerate.utilsr   r   r   
get_loggerr   rj   r   r   r$   r"   <module>r      sl    % B   & 3 I  2			H	%X( Xr$   