
    +h                        S SK JrJrJrJrJr  S SKJr  SSK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  \R0                  " \5      r " S S\5      rg)    )TYPE_CHECKINGAnyDictListUnion)is_optimum_quanto_version   )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            
       P  ^  \ 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   4   S\\
\\\
4   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/james-whalen/.local/lib/python3.13/site-packages/diffusers/quantizers/quanto/quanto_quantizer.pyr   QuantoQuantizer.__init__)   s    ,77    c                 D   [        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 )	NzhLoading 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   ImportErrorr   r   get
isinstancedictlenkeys
ValueError)r    argsr"   r)   s       r$   validate_environment$QuantoQuantizer.validate_environment,   s    *,,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>O   s     %]E\j&;&;E\s   TweightF)
optimum.quantor9   r:   optimum.quanto.tensor.packedr;   r
   pre_quantizedanyr,   frozen)r    r4   r5   r6   r7   r"   r9   r:   r;   tensor_namer?   s             @r$   check_if_quantized_param(QuantoQuantizer.check_if_quantized_paramB   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
   rE   setattrr   freezerB   requires_grad)
r    r4   r5   r6   rK   r1   r"   rM   r?   rH   s
             r$   create_quantized_param&QuantoQuantizer.create_quantized_paramV   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    rU   keyvals       r$   adjust_max_memory!QuantoQuantizer.adjust_max_memoryl   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   rN   r^   r   FP8INT4INT2r!   weights_dtype)r    target_dtypemappings      r$   adjust_target_dtype#QuantoQuantizer.adjust_target_dtypep   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inforN   rO   )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   )r9   .z.weightz.bias)rC   r9   named_modulesr,   endswithappend)
r    r4   rp   rq   r9   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!   rE   )r!   r   r,   listextendr   rE   config)r    r4   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    r4   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   )rf   torch.dtyperV   r   r   )rm   r   rV   r   )__name__
__module____qualname____firstlineno____doc__use_keep_in_fp32_modulesrequires_calibrationrequired_packagesr   r2   strr   r   rI   rS   r   intr[   rh   rn   r   r{   r   r   propertyr   r   boolr   __static_attributes____classcell__)r#   s   @r$   r   r       sd     $ !<08, $ 	
 cN(00 $0 	0
 &0,DeCHo1E,F 4PSUZ[^`c[cUdPdKe 
FtCy F# FRVWZR[ F( +-	DD #3i	D,       r&   r   )typingr   r   r   r   r   diffusers.utils.import_utilsr   utilsr
   r   r   r   r   r   baser   models.modeling_utilsr   rN   accelerate.utilsr   r   r   
get_loggerr   rk   r   r   r&   r$   <module>r      sj    8 8 B  & 3 I  2			H	%U( Ur&   