o
    î0 iÑ  ã                   @   s”   d dl Z d dlmZ d dlmZ ddlmZ erddlmZ ddl	m
Z
mZmZmZmZ dd	lmZmZ eƒ r;d dlZe e¡ZG d
d„ deƒZdS )é    N)ÚTYPE_CHECKING)Úversioné   )ÚHfQuantizeré   )ÚPreTrainedModel)Úis_auto_gptq_availableÚis_gptqmodel_availableÚis_optimum_availableÚis_torch_availableÚlogging)Ú
GPTQConfigÚQuantizationConfigMixinc                       s€   e Zd ZdZdZg d¢ZdZdef‡ fdd„Zdd	„ Z	ddd„Z
dd„ Zddd„Zddd„Zedefdd„ƒZddd„Z‡  ZS )ÚGptqHfQuantizerzå
    Quantizer of the GPTQ method - for GPTQ the quantizer support calibration of the model through
    `auto_gptq` or `gptqmodel` package. Quantization is done under the hood for users if they load a non-prequantized model.
    F)ÚoptimumÚ	auto_gptqÚ	gptqmodelNÚquantization_configc                    sD   t ƒ j|fi |¤Ž tƒ stdƒ‚ddlm} | | j ¡ ¡| _	d S )NúGLoading a GPTQ quantized model requires optimum (`pip install optimum`)r   )ÚGPTQQuantizer)
ÚsuperÚ__init__r
   ÚImportErrorZoptimum.gptqr   Ú	from_dictr   Zto_dict_optimumÚoptimum_quantizer)Úselfr   Úkwargsr   ©Ú	__class__© úr/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/transformers/quantizers/quantizer_gptq.pyr   -   s
   zGptqHfQuantizer.__init__c                 O   sî   t ƒ stdƒ‚tƒ rtƒ rt d¡ tƒ o"t tj	 d¡¡t d¡kp%tƒ }|s1t
j ¡ s1tdƒ‚tƒ s;tƒ s;tdƒ‚tƒ rPt tj	 d¡¡t d¡k rPtdƒ‚tƒ rst tj	 d	¡¡t d
¡k sot tj	 d¡¡t d¡k rutdƒ‚d S d S )Nr   z4Detected gptqmodel and auto-gptq, will use gptqmodelz	auto-gptqz0.4.2z2GPU is required to quantize or run quantize model.z|Loading a GPTQ quantized model requires gptqmodel (`pip install gptqmodel`) or auto-gptq (`pip install auto-gptq`) library. r   z‹You need a version of auto_gptq >= 0.4.2 to use GPTQ: `pip install --upgrade auto-gptq` or use gptqmodel by `pip install gptqmodel>=1.4.3`.r   z1.4.3r   ú1.23.99zJThe gptqmodel version should be >= 1.4.3, optimum version should >= 1.24.0)r
   r   r   r	   ÚloggerÚwarningr   ÚparseÚ	importlibÚmetadataÚtorchÚcudaZis_availableÚRuntimeError)r   Úargsr   Zgptq_supports_cpur   r   r    Úvalidate_environment6   s6   
þýÿÿÿüz$GptqHfQuantizer.validate_environmentÚdtypeútorch.dtypeÚreturnc                 C   s4   |d u rt j}t d¡ |S |t jkrt d¡ |S )NzLLoading the model in `torch.float16`. To overwrite it, set `dtype` manually.zLWe suggest you to set `dtype=torch.float16` for better efficiency with GPTQ.)r'   Zfloat16r"   Úinfo)r   r,   r   r   r    Úupdate_dtypeR   s   

þ
zGptqHfQuantizer.update_dtypec                 C   sB   |d u rdt  d¡i}tƒ s|ddt  d¡ifv r|ddik |S )NÚ Úcpur   )r'   Zdevicer	   )r   Z
device_mapr   r   r    Úupdate_device_mapZ   s
   z!GptqHfQuantizer.update_device_mapÚmodelr   c                 K   sb   |j jdkr
tdƒ‚| jr/t tj d¡¡t d¡kr#| j 	|¡}d S | jj	|fi |¤Ž}d S d S )NZ	input_idsz%We can only quantize pure text model.r   r!   )
r   Zmain_input_namer)   Úpre_quantizedr   r$   r%   r&   r   Zconvert_model©r   r4   r   r   r   r    Ú$_process_model_before_weight_loadingb   s   ûz4GptqHfQuantizer._process_model_before_weight_loadingc                 K   sV   | j r| j |¡}d S | jjd u r|j| j_| j || jj¡ t | j 	¡ ¡|j
_d S ©N)r5   r   Zpost_init_modelr   Z	tokenizerZname_or_pathZquantize_modelr   r   Úto_dictÚconfigr6   r   r   r    Ú#_process_model_after_weight_loadingm   s   
z3GptqHfQuantizer._process_model_after_weight_loadingc                 C   ó   dS ©NTr   )r   r   r   r    Úis_trainablew   s   zGptqHfQuantizer.is_trainablec                 C   r<   r=   r   )r   Zsafe_serializationr   r   r    Úis_serializable{   s   zGptqHfQuantizer.is_serializable)r,   r-   r.   r-   )r4   r   r8   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zrequires_calibrationZrequired_packagesr   r   r   r+   r0   r3   r7   r;   ÚpropertyÚboolr>   r?   Ú__classcell__r   r   r   r    r   #   s    	



r   )r%   Útypingr   Ú	packagingr   Úbaser   Zmodeling_utilsr   Úutilsr   r	   r
   r   r   Zutils.quantization_configr   r   r'   Z
get_loggerr@   r"   r   r   r   r   r    Ú<module>   s   
