
    bi                         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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d
Z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)BitsAndBytesConfigGGUFQuantizationConfigQuantizationConfigMixinQuantizationMethodQuantoConfigTorchAoConfig)QuantoQuantizer)TorchAoHfQuantizer)bitsandbytes_4bitbitsandbytes_8bitggufquantotorchaoc                       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        T/home/cdr/jupyterlab/.venv/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*   r/   kwargsr   r,   s        r-   from_configz"DiffusersAutoQuantizer.from_configP   s     )40"%--0C"D*77 -<<<"//''5::<<1, @/44678: 
 ,L9
-888r.   c                      | j                   |fi |}t        |dd       t        d| d      |j                  }| j	                  |      }|j                  |       | j                  |      S )Nr/   z)Did not found a `quantization_config` in z2. Make sure that the model is correctly quantized.)load_configgetattrr$   r/   r)   updater4   )r*   pretrained_model_name_or_pathr3   model_configr   r/   s         r-   from_pretrainedz&DiffusersAutoQuantizer.from_pretrainedi   s    &s'DOO<!6=E;<Y;Z  [M  N  $0#C#C !mm,DE""6*233r.   quantization_config_from_argsc                     |d}nd}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. )r1   r2   r)   warningswarn)r*   r/   r<   warning_msgs       r-   merge_quantization_configsz1DiffusersAutoQuantizer.merge_quantization_configsw   sQ     )4y 
 K)40"%--0C"D"MM+&""r.   N)__name__
__module____qualname____doc__classmethodr   r)   r   r   r4   r;   r2   r   rB    r.   r-   r   r   5   s    
 > > >( 9e4KT4Q.R 9 90 4 4 #"4)@#@A# (00G'H# #r.   r   )rF   r?   typingr   r   r   bitsandbytesr   r   r   r	   r/   r
   r   r   r   r   r   r   r   r   r   r(   r%   r   rH   r.   r-   <module>rK      sk   
  ( ( N   $ ' 32!  ,+"$  Z# Z#r.   