o
    0 i5                     @   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)VptqHfQuantizerZawqZbitsandbytes_4bitZbitsandbytes_8bitZgptqZaqlmZquantoZquarkZfp_quantZeetqZhiggsZhqqzcompressed-tensorsZ
fbgemm_fp8ZtorchaoZbitnetZvptqZspqr)Zfp8z
auto-roundZ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)clsr3   r4   suffix
target_cls rG   h/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/transformers/quantizers/auto.pyrC   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.rG   )r   from_pretrainedgetattrr>   rI   rC   update)rD   pretrained_model_name_or_pathkwargsZmodel_configr3   rI   rG   rG   rH   rJ      s   

z&AutoQuantizationConfig.from_pretrainedN)__name__
__module____qualname____doc__classmethoddictrC   rJ   rG   rG   rG   rH   r2   s   s    r2   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`.
    rI   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 )Nr8   r7   r:   r;   )
isinstancerT   r2   rC   r4   r   r=   r5   rA   r>   r@   rB   )rD   rI   rN   r4   rF   rG   rG   rH   from_config   s   




zAutoHfQuantizer.from_configc                 K   s   t j|fi |}| |S )N)r2   rJ   rW   )rD   rM   rN   rI   rG   rG   rH   rJ      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.)rV   rT   r   rC   r2   	__class__rO   r>   r   r	   r   r   r   Zget_loading_attributesitemssetattrr@   rB   warningswarnloggerinfo)rD   rI   rX   Zwarning_msgZloading_attr_dictattrvalrG   rG   rH   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 )Nr4   r5   Fr6   r7   r8   r9   r:   r;   z~. Hence, we will skip the quantization. To remove the warning, you can delete the quantization_config attribute in config.jsonT)
r<   r   r=   r>   r?   r_   warningr@   rA   rB   )r3   r4   rE   rG   rG   rH   supports_quant_method   s    
z%AutoHfQuantizer.supports_quant_methodN)rO   rP   rQ   rR   rS   r   r   rT   rW   rJ   r   rc   staticmethodre   rG   rG   rG   rH   rU      s    

4rU   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)r?   r>   
issubclassr   	TypeErrorrD   rg   rG   rH   register_config_fn     
z8register_quantization_config.<locals>.register_config_fnrG   )rg   rn   rG   rm   rH   register_quantization_config
     
rp   namec                    rh   )zRegister a custom quantizer.c                    s6    t v rtd  dt| tstd| t  < | S )NzQuantizer 'ri   z!Quantizer must extend HfQuantizer)rA   r>   rj   r   rl   rr   rG   rH   register_quantizer_fn  ro   z1register_quantizer.<locals>.register_quantizer_fnrG   )rr   rt   rG   rs   rH   register_quantizer  rq   ru   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rc|	j|||||d |	|}|	|}|		| } |	
| } t|	jddsc|	jj}
t|
d|
|d< |	| ||fS )NrI   F)pre_quantized)dtypefrom_tf	from_flax
device_mapweights_onlyZ
dequantizevalueZquant)hasattrrU   re   rI   rc   rW   Zvalidate_environmentZupdate_dtypeZupdate_device_mapZupdate_tp_planZupdate_ep_planrK   r4   )configrI   rw   rx   ry   rz   r{   
user_agentrv   Zhf_quantizerr4   rG   rG   rH   get_hf_quantizer*  s<   




r   )Sr]   typingr   r   Zmodels.auto.configuration_autor   utilsr   Zutils.quantization_configr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   baser   Zquantizer_aqlmr   Zquantizer_auto_roundr   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_fp_quantr(   Zquantizer_gptqr)   Zquantizer_higgsr*   Zquantizer_hqqr+   Zquantizer_mxfp4r,   Zquantizer_quantor-   Zquantizer_quarkr.   Zquantizer_spqrr/   Zquantizer_torchaor0   Zquantizer_vptqr1   rA   r?   Z
get_loggerrO   r_   r2   rU   strrp   ru   r   rG   rG   rG   rH   <module>   s   \	
	

)n