
    i                         d Z ddlZddlmZmZmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZmZmZmZmZmZmZ dd	lmZ dd
lmZ eee
eeedZeeeeeedZ G d d      Zy)z
Adapted from
https://github.com/huggingface/transformers/blob/c409cd81777fb27aadc043ed3d8339dbc020fb3b/src/transformers/quantizers/auto.py
    N)DictOptionalUnion   )BnB4BitDiffusersQuantizerBnB8BitDiffusersQuantizer)GGUFQuantizer)NVIDIAModelOptQuantizer)BitsAndBytesConfigGGUFQuantizationConfigNVIDIAModelOptConfigQuantizationConfigMixinQuantizationMethodQuantoConfigTorchAoConfig)QuantoQuantizer)TorchAoHfQuantizer)bitsandbytes_4bitbitsandbytes_8bitggufquantotorchaomodeloptc                       e Zd ZdZedefd       Zedeeef   fd       Z	ed        Z
edeeef   dee   fd       Zy	)
DiffusersAutoQuantizerz
     The auto diffusers quantizer class that takes care of automatically instantiating to the correct
    `DiffusersQuantizer` given the `QuantizationConfig`.
    quantization_config_dictc           	         |j                  dd       }|j                  dd      s|j                  dd      r*|j                  dd      rdnd}t        j                  |z   }n|t        d      |t        j                         vr,t        d| d	t        t        j                                      t        |   }|j                  |      S )
Nquant_methodload_in_8bitFload_in_4bit_4bit_8bitzThe model's quantization config from the arguments has no `quant_method` attribute. Make sure that the model has been correctly quantizedUnknown quantization type, got  - supported types are: )	getr   BITS_AND_BYTES
ValueError AUTO_QUANTIZATION_CONFIG_MAPPINGkeyslistAUTO_QUANTIZER_MAPPING	from_dict)clsr   r   suffix
target_clss        c/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/quantizers/auto.pyr,   z DiffusersAutoQuantizer.from_dict?   s    /33NDI#''>BZB^B^_motBu 8 < <^U SWY`F-<<vEL! \  ?DDFF1, @/44678: 
 6lC
##$<==    quantization_configc           	      X   t        |t              r| j                  |      }|j                  }|t        j
                  k(  r|j                  r|dz  }n|dz  }|t        j                         vr,t        d| dt        t        j                                      t        |   } ||fi |S )Nr"   r!   r#   r$   )
isinstancedictr,   r   r   r&   r   r+   r)   r'   r*   )r-   r2   kwargsr   r/   s        r0   from_configz"DiffusersAutoQuantizer.from_configT   s     )40"%--0C"D*77 -<<<"//''5::<<1, @/44678: 
 ,L9
-888r1   c                      | j                   |fi |}t        |dd       t        d| d      |j                  }| j	                  |      }|j                  |       | j                  |      S )Nr2   z)Did not found a `quantization_config` in z2. Make sure that the model is correctly quantized.)load_configgetattrr'   r2   r,   updater7   )r-   pretrained_model_name_or_pathr6   model_configr   r2   s         r0   from_pretrainedz&DiffusersAutoQuantizer.from_pretrainedm   s    &s'DOO<!6=E;<Y;Z  [M  N  $0#C#C !mm,DE""6*233r1   quantization_config_from_argsc                     |d}nd}t        |t              r| j                  |      }t        |t              r|j	                          |dk7  rt        j                  |       |S )z
        handles situations where both quantization_config from args and quantization_config from model config are
        present.
        zYou passed `quantization_config` or equivalent parameters to `from_pretrained` but the model you're loading already has a `quantization_config` attribute. The `quantization_config` from the model will be used. )r4   r5   r,   r   check_model_patchingwarningswarn)r-   r2   r?   warning_msgs       r0   merge_quantization_configsz1DiffusersAutoQuantizer.merge_quantization_configs{   si     )4y 
 K)40"%--0C"D)+?@446"MM+&""r1   N)__name__
__module____qualname____doc__classmethodr   r,   r   r   r7   r>   r5   r   rF    r1   r0   r   r   9   s    
 > > >( 9e4KT4Q.R 9 90 4 4 #"4)@#@A# (00G'H# #r1   r   )rJ   rC   typingr   r   r   bitsandbytesr   r   r   r	   r   r
   r2   r   r   r   r   r   r   r   r   r   r   r   r+   r(   r   rL   r1   r0   <module>rO      sy   
  ( ( N  -   $ ' 32!'  ,+"$$  ]# ]#r1   