o
    
sh5                     @   s  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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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? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZG i de%de)de+d e7d!e!d"e?d#eAd$e5d%e/d&e9d'e;d(e-d)e1d*eEd+e'd,eGd-eCe3e#e=d.ZHi 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ee
ed.ZIeJeKZLG d/d0 d0ZMG d1d2 d2ZNd3eOfd4d5ZPd6eOfd7d8ZQd9d: ZRdS );    N)OptionalUnion   )
AutoConfig)logging)
AqlmConfigAutoRoundConfig	AwqConfigBitNetQuantConfigBitsAndBytesConfigCompressedTensorsConfig
EetqConfigFbgemmFp8ConfigFineGrainedFP8ConfigFPQuantConfig
GPTQConfigHiggsConfig	HqqConfigMxfp4ConfigQuantizationConfigMixinQuantizationMethodQuantoConfigQuarkConfig
SpQRConfigTorchAoConfig
VptqConfig   )HfQuantizer)AqlmHfQuantizer)AutoRoundQuantizer)AwqQuantizer)BitNetHfQuantizer)Bnb4BitHfQuantizer)Bnb8BitHfQuantizer)CompressedTensorsHfQuantizer)EetqHfQuantizer)FbgemmFp8HfQuantizer)FineGrainedFP8HfQuantizer)FPQuantHfQuantizer)GptqHfQuantizer)HiggsHfQuantizer)HqqHfQuantizer)Mxfp4HfQuantizer)QuantoHfQuantizer)QuarkHfQuantizer)SpQRHfQuantizer)TorchAoHfQuantizer)VptqHfQuantizerawqbitsandbytes_4bitbitsandbytes_8bitgptqaqlmquantoquarkfp_quanteetqhiggshqqzcompressed-tensors
fbgemm_fp8torchaobitnetvptqspqr)fp8z
auto-roundmxfp4c                   @   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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_8bitThe 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listAUTO_QUANTIZER_MAPPINGkeys	from_dict)clsrE   rF   suffix
target_cls rY   Z/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/transformers/quantizers/auto.pyrU   y   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.rY   )r   from_pretrainedgetattrrP   r[   rU   update)rV   pretrained_model_name_or_pathkwargsmodel_configrE   r[   rY   rY   rZ   r\      s   

z&AutoQuantizationConfig.from_pretrainedN)__name__
__module____qualname____doc__classmethoddictrU   r\   rY   rY   rY   rZ   rD   s   s    rD   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`.
    r[   c                 K   sx   t |tr
t|}|j}|tjkr|jr|d7 }n|d7 }|tvr0t	d| dt
t  t| }||fi |S )NrJ   rI   rL   rM   )
isinstancerg   rD   rU   rF   r   rO   rG   rS   rP   rR   rT   )rV   r[   r`   rF   rX   rY   rY   rZ   from_config   s   




zAutoHfQuantizer.from_configc                 K   s   t j|fi |}| |S )N)rD   r\   rj   )rV   r_   r`   r[   rY   rY   rZ   r\      s   
zAutoHfQuantizer.from_pretrainedquantization_config_from_argsc                 C   s   |durd}nd}t |trt |trt|}nt|}|dur9|jj|jjkr9td|jj d|jj dt |tt	tt
ttfrg|durg| }| D ]
\}}t||| qP|dt|  d7 }|dkrwt |tswt| |S t| |S )	z
        handles situations where both quantization_config from args and quantization_config from model config are present.
        NzYou 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The model is quantized with z but you are passing a z| config. Please make sure to pass the same quantization config class to `from_pretrained` with different loading attributes.z"However, loading attributes (e.g. z]) will be overwritten with the one you passed to `from_pretrained`. The rest will be ignored.)ri   rg   r   rU   rD   	__class__rb   rP   r   r	   r   r   r   get_loading_attributesitemssetattrrR   rT   warningswarnloggerinfo)rV   r[   rk   warning_msgloading_attr_dictattrvalrY   rY   rZ   merge_quantization_configs   s8   	




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r@td| d	tt	  d
 dS dS )NrF   rG   FrH   rI   rJ   rK   rL   rM   z~. Hence, we will skip the quantization. To remove the warning, you can delete the quantization_config attribute in config.jsonT)
rN   r   rO   rP   rQ   rs   warningrR   rS   rT   )rE   rF   rW   rY   rY   rZ   supports_quant_method   s    
z%AutoHfQuantizer.supports_quant_methodN)rb   rc   rd   re   rf   r   r   rg   rj   r\   r   ry   staticmethodr{   rY   rY   rY   rZ   rh      s    

4rh   methodc                        fdd}|S )z-Register a custom quantization configuration.c                    s6    t v rtd  dt| tstd| t  < | S )NzConfig '' already registeredz*Config must extend QuantizationConfigMixin)rQ   rP   
issubclassr   	TypeErrorrV   r}   rY   rZ   register_config_fn     
z8register_quantization_config.<locals>.register_config_fnrY   )r}   r   rY   r   rZ   register_quantization_config
     
r   namec                    r~   )zRegister a custom quantizer.c                    s6    t v rtd  dt| tstd| t  < | S )NzQuantizer 'r   z!Quantizer must extend HfQuantizer)rS   rP   r   r   r   r   rY   rZ   register_quantizer_fn  r   z1register_quantizer.<locals>.register_quantizer_fnrY   )r   r   rY   r   rZ   register_quantizer  r   r   c                 C   s   t | d}|rt| jsd}|s|d ur,|r t| j|| _n|| _tj| j|d}	nd }	|	d ur^|	j|||||d |	|}|	|}|		| } t
|	jdds^|	jj}
t
|
d|
|d< |	| ||fS )Nr[   F)pre_quantized)dtypefrom_tf	from_flax
device_mapweights_only
dequantizevaluequant)hasattrrh   r{   r[   ry   rj   validate_environmentupdate_dtypeupdate_device_mapupdate_tp_planr]   rF   )configr[   r   r   r   r   r   
user_agentr   hf_quantizerrF   rY   rY   rZ   get_hf_quantizer*  s:   



r   )Srq   typingr   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   r   r   r   baser   quantizer_aqlmr   quantizer_auto_roundr   quantizer_awqr    quantizer_bitnetr!   quantizer_bnb_4bitr"   quantizer_bnb_8bitr#   quantizer_compressed_tensorsr$   quantizer_eetqr%   quantizer_fbgemm_fp8r&   quantizer_finegrained_fp8r'   quantizer_fp_quantr(   quantizer_gptqr)   quantizer_higgsr*   quantizer_hqqr+   quantizer_mxfp4r,   quantizer_quantor-   quantizer_quarkr.   quantizer_spqrr/   quantizer_torchaor0   quantizer_vptqr1   rS   rQ   
get_loggerrb   rs   rD   rh   strr   r   r   rY   rY   rY   rZ   <module>   s   \	
	

)n