Ë
    (täiv  ã                   ó¬   — d Z ddl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é   )ÚBnB4BitDiffusersQuantizerÚBnB8BitDiffusersQuantizer)ÚGGUFQuantizer)ÚNVIDIAModelOptQuantizer)ÚBitsAndBytesConfigÚGGUFQuantizationConfigÚNVIDIAModelOptConfigÚQuantizationConfigMixinÚQuantizationMethodÚQuantoConfigÚTorchAoConfig)ÚQuantoQuantizer)ÚTorchAoHfQuantizer)Úbitsandbytes_4bitÚbitsandbytes_8bitÚggufÚquantoÚtorchaoÚmodeloptc                   óx   — e Zd ZdZedefd„«       Zedeez  fd„«       Zed„ «       Z	edeez  dedz  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Ú_8bitz‰The 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        úh/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/diffusers/quantizers/auto.pyr)   z DiffusersAutoQuantizer.from_dict>   sÜ   € à/×3Ñ3°NÀDÓIˆà#×'Ñ'¨¸Ô>ÐBZ×B^ÑB^Ð_mÐotÔBuØ 8× <Ñ <¸^ÈUÔ S‘WÐY`ˆFÜ-×<Ñ<¸vÑE‰LØÐ!Üð \óð ð Ô?×DÑDÓFÑFÜØ1°,°ð @ÜÔ/×4Ñ4Ó6Ó7Ð8ð:óð ô
 6°lÑ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_configS   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_pretrainedl   s‡   € à&�s—‘Ð'DÑOÈÑOˆÜ�<Ð!6¸Ó=ÐEÜØ;Ð<YÐ;Zð  [Mð  Nóð ð $0×#CÑ#CÐ Ø!Ÿm™mÐ,DÓEÐà×"Ñ" 6Ô*à�‰Ð2Ó3Ð3r.   Úquantization_config_from_argsNc                 óÊ   — |�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.
        zÑYou 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)   r
   Úcheck_model_patchingÚwarningsÚwarn)r*   r/   r<   Úwarning_msgs       r-   Úmerge_quantization_configsz1DiffusersAutoQuantizer.merge_quantization_configsz   si   € ð )Ð4ðyñ ð
 ˆKäÐ)¬4Ô0Ø"%§-¡-Ð0CÓ"DÐäÐ)Ô+?Ô@Ø×4Ñ4Ô6à˜"ÒÜ�M‰M˜+Ô&à"Ð"r.   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr2   r)   r   r4   r;   rC   © r.   r-   r   r   8   sˆ   „ ñð
 ð>°ò >ó ð>ð( ð9Ð.EÈÑ.Lò 9ó ð9ð0 ñ4ó ð4ð ð#à!Ð$;Ñ;ð#ð (?ÀÑ'Eò#ó ñ#r.   r   )rG   r@   Úbitsandbytesr   r   r   r   r   r   r/   r   r	   r
   r   r   r   r   r   r   r   r   r(   r%   r   rI   r.   r-   Ú<module>rK      ss   ðñó
 ç NÝ Ý -÷÷ ñ õ $Ý 'ð 3Ø2ØØØ!Ø'ñÐ ð ,Ø+Ø"ØØØ$ñ$Ð  ÷]#ò ]#r.   