o
    h)                     @   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awqbitsandbytes_4bitbitsandbytes_8bitgptqaqlmquantoquarkeetqhiggshqqzcompressed-tensors
fbgemm_fp8torchaobitnetvptqspqr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_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keyslistAUTO_QUANTIZER_MAPPING	from_dict)clsr>   r?   suffix
target_cls rR   p/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/quantizers/auto.pyrN   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.rR   )r   from_pretrainedgetattrrI   rT   rN   update)rO   pretrained_model_name_or_pathkwargsmodel_configr>   rT   rR   rR   rS   rU      s   

z&AutoQuantizationConfig.from_pretrainedN)__name__
__module____qualname____doc__classmethodr   rN   rU   rR   rR   rR   rS   r=   g   s    r=   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`.
    rT   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 )NrC   rB   rE   rF   )
isinstancedictr=   rN   r?   r   rH   r@   rM   rK   rI   rL   )rO   rT   rY   r?   rQ   rR   rR   rS   from_config   s   




zAutoHfQuantizer.from_configc                 K   s   t j|fi |}| |S )N)r=   rU   rc   )rO   rX   rY   rT   rR   rR   rS   rU      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.
        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"However, loading attributes (e.g. z]) will be overwritten with the one you passed to `from_pretrained`. The rest will be ignored.)ra   rb   r=   rN   r   r	   r   r   get_loading_attributesitemssetattrrL   rK   warningswarn)rO   rT   rd   warning_msgloading_attr_dictattrvalrR   rR   rS   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@   FrA   rB   rC   rD   rE   rF   z~. Hence, we will skip the quantization. To remove the warning, you can delete the quantization_config attribute in config.jsonT)
rG   r   rH   rI   rJ   rK   loggerwarningrL   rM   )r>   r?   rP   rR   rR   rS   supports_quant_method   s    
z%AutoHfQuantizer.supports_quant_methodN)r[   r\   r]   r^   r_   r   r   r   rc   rU   rb   r   ro   staticmethodrr   rR   rR   rR   rS   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)rJ   rI   
issubclassr   rO   rt   rR   rS   register_config_fn      
z8register_quantization_config.<locals>.register_config_fnrR   )rt   r{   rR   rz   rS   register_quantization_config      
r}   namec                    ru   )zRegister a custom quantizer.c                    rv   )NzQuantizer 'rw   z!Quantizer must extend HfQuantizer)rM   rI   rx   r   ry   r   rR   rS   register_quantizer_fn   r|   z1register_quantizer.<locals>.register_quantizer_fnrR   )r   r   rR   r   rS   register_quantizer   r~   r   )Jri   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   quantizer_aqlmr   quantizer_awqr   quantizer_bitnetr   quantizer_bnb_4bitr   quantizer_bnb_8bitr    quantizer_compressed_tensorsr!   quantizer_eetqr"   quantizer_fbgemm_fp8r#   quantizer_finegrained_fp8r$   quantizer_gptqr%   quantizer_higgsr&   quantizer_hqqr'   quantizer_quantor(   quantizer_quarkr)   quantizer_spqrr*   quantizer_torchaor+   quantizer_vptqr,   rM   rJ   
get_loggerr[   rp   r=   r`   strr}   r   rR   rR   rR   rS   <module>   s   P	
	

)]