o
    Fôjž)  ã                   @   sv  d dl Z d dlmZ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mZmZmZmZmZmZmZmZmZmZmZ ddlmZ dd	lmZ dd
l m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? i de!“de%“de'“de1“de“de7“d e9“d!e+“d"e3“d#e5“d$e)“d%e-“d&e=“d'e#“d(e?“d)e;“d*e/“Z@i de“de“de“d!e“de“de
“de“d e“d#e“d$e“d%e“d"e“d&e“d'e“d(e“d)e“d*e“ZAe BeC¡ZDG d+d,„ d,ƒZEG d-d.„ d.ƒZFd/eGfd0d1„ZHd2eGfd3d4„ZIdS )5é    N)ÚDictÚOptionalÚUnioné   )Ú
AutoConfig)Úlogging)Ú
AqlmConfigÚ	AwqConfigÚBitNetConfigÚBitsAndBytesConfigÚCompressedTensorsConfigÚ
EetqConfigÚFbgemmFp8ConfigÚFineGrainedFP8ConfigÚ
GPTQConfigÚHiggsConfigÚ	HqqConfigÚQuantizationConfigMixinÚQuantizationMethodÚQuantoConfigÚQuarkConfigÚ
SpQRConfigÚTorchAoConfigÚ
VptqConfigé   )ÚHfQuantizer)ÚAqlmHfQuantizer)ÚAwqQuantizer)ÚBitNetHfQuantizer)ÚBnb4BitHfQuantizer)ÚBnb8BitHfQuantizer)ÚCompressedTensorsHfQuantizer)ÚEetqHfQuantizer)ÚFbgemmFp8HfQuantizer)ÚFineGrainedFP8HfQuantizer)ÚGptqHfQuantizer)ÚHiggsHfQuantizer)ÚHqqHfQuantizer)ÚQuantoHfQuantizer)ÚQuarkHfQuantizer)ÚSpQRHfQuantizer)ÚTorchAoHfQuantizer)ÚVptqHfQuantizerÚawqZbitsandbytes_4bitZbitsandbytes_8bitZgptqÚaqlmÚquantoÚquarkÚeetqÚhiggsÚhqqzcompressed-tensorsÚ
fbgemm_fp8ZtorchaoÚbitnetÚvptqÚspqrZfp8c                   @   s.   e Zd ZdZedefdd„ƒZedd„ ƒZdS )ÚAutoQuantizationConfigz·
    The Auto-HF quantization config class that takes care of automatically dispatching to the correct
    quantization config given a quantization config stored in a dictionary.
    Úquantization_config_dictc                 C   sŽ   |  dd ¡}|  dd¡s|  dd¡r"|  dd¡rdnd}tj| }n|d u r*tdƒ‚|t ¡ vr>td|› d	tt ¡ ƒ› �ƒ‚t| }| |¡S )
NÚquant_methodÚload_in_8bitFÚload_in_4bitÚ_4bitÚ_8bitú‰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)Úclsr9   r:   ÚsuffixÚ
target_cls© rM   úo/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/transformers/quantizers/auto.pyrI   m   s    ÿ
ÿÿ
z AutoQuantizationConfig.from_dictc                 K   sV   t j|fi |¤Ž}t|dd ƒd u rtd|› d�ƒ‚|j}|  |¡}|jdi |¤Ž |S )NÚquantization_configz)Did not found a `quantization_config` in z2. Make sure that the model is correctly quantized.rM   )r   Úfrom_pretrainedÚgetattrrD   rO   rI   Úupdate)rJ   Úpretrained_model_name_or_pathÚkwargsÚmodel_configr9   rO   rM   rM   rN   rP   ‚   s   
ÿ
z&AutoQuantizationConfig.from_pretrainedN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   rI   rP   rM   rM   rM   rN   r8   g   s    r8   c                   @   sd   e Zd ZdZedeeef fdd„ƒZedd„ ƒZ	edee
ef dee fdd	„ƒZed
d„ ƒZdS )ÚAutoHfQuantizerz–
     The Auto-HF quantizer class that takes care of automatically instantiating to the correct
    `HfQuantizer` given the `QuantizationConfig`.
    rO   c                 K   s|   t |tƒr
t |¡}|j}|tjkr|jr|d7 }n|d7 }|t 	¡ vr2t
d|› dtt 	¡ ƒ› �ƒ‚t| }||fi |¤ŽS )Nr>   r=   r@   rA   )Ú
isinstanceÚdictr8   rI   r:   r   rC   r;   rH   rF   rD   rG   )rJ   rO   rT   r:   rL   rM   rM   rN   Úfrom_config–   s   




ÿÿzAutoHfQuantizer.from_configc                 K   s   t j|fi |¤Ž}|  |¡S )N)r8   rP   r^   )rJ   rS   rT   rO   rM   rM   rN   rP   ¯   s   
zAutoHfQuantizer.from_pretrainedÚquantization_config_from_argsc                 C   s”   |durd}nd}t |tƒrt |¡}t |ttttfƒr?|dur?| ¡ }| 	¡ D ]
\}}t
|||ƒ q(|dt| ¡ ƒ› d�7 }|dkrHt |¡ |S )z„
        handles situations where both quantization_config from args and quantization_config from model config are present.
        NzÑ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.Ú z"However, loading attributes (e.g. z]) will be overwritten with the one you passed to `from_pretrained`. The rest will be ignored.)r\   r]   r8   rI   r   r	   r   r   Zget_loading_attributesÚitemsÚsetattrrG   rF   ÚwarningsÚwarn)rJ   rO   r_   Úwarning_msgZloading_attr_dictÚattrÚvalrM   rM   rN   Úmerge_quantization_configs´   s    	ÿ

ÿ
z*AutoHfQuantizer.merge_quantization_configsc                 C   sˆ   |   dd ¡}|   dd¡s|   dd¡r"|   dd¡rdnd}tj| }n|d u r*tdƒ‚|t ¡ vrBt d|› d	tt	 ¡ ƒ› d
�¡ dS dS )Nr:   r;   Fr<   r=   r>   r?   r@   rA   z~. Hence, we will skip the quantization. To remove the warning, you can delete the quantization_config attribute in config.jsonT)
rB   r   rC   rD   rE   rF   ÚloggerÚwarningrG   rH   )r9   r:   rK   rM   rM   rN   Úsupports_quant_methodØ   s    ÿ
ÿÿz%AutoHfQuantizer.supports_quant_methodN)rV   rW   rX   rY   rZ   r   r   r   r^   rP   r]   r   rh   Ústaticmethodrk   rM   rM   rM   rN   r[   �   s    

þý#r[   Úmethodc                    ó   ‡ fdd„}|S )z-Register a custom quantization configuration.c                    ó6   ˆ t v rtdˆ › d�ƒ‚t| tƒstdƒ‚| t ˆ < | S )NzConfig 'ú' already registeredz*Config must extend QuantizationConfigMixin)rE   rD   Ú
issubclassr   ©rJ   ©rm   rM   rN   Úregister_config_fnð   ó   
z8register_quantization_config.<locals>.register_config_fnrM   )rm   rt   rM   rs   rN   Úregister_quantization_configí   ó   
rv   Únamec                    rn   )zRegister a custom quantizer.c                    ro   )NzQuantizer 'rp   z!Quantizer must extend HfQuantizer)rH   rD   rq   r   rr   ©rx   rM   rN   Úregister_quantizer_fn   ru   z1register_quantizer.<locals>.register_quantizer_fnrM   )rx   rz   rM   ry   rN   Úregister_quantizerý   rw   r{   )Jrc   Útypingr   r   r   Úmodels.auto.configuration_autor   Úutilsr   Úutils.quantization_configr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Úbaser   Zquantizer_aqlmr   Zquantizer_awqr   Zquantizer_bitnetr   Zquantizer_bnb_4bitr   Zquantizer_bnb_8bitr    Zquantizer_compressed_tensorsr!   Zquantizer_eetqr"   Zquantizer_fbgemm_fp8r#   Zquantizer_finegrained_fp8r$   Zquantizer_gptqr%   Zquantizer_higgsr&   Zquantizer_hqqr'   Zquantizer_quantor(   Zquantizer_quarkr)   Zquantizer_spqrr*   Zquantizer_torchaor+   Zquantizer_vptqr,   rH   rE   Ú
get_loggerrV   ri   r8   r[   Ústrrv   r{   rM   rM   rM   rN   Ú<module>   sÄ   Pÿþýüûúùø	÷
öõôóòñðïÿþýüûúùø	÷
öõôóòñðï
)]